Struct PityDelivery

Source
pub struct PityDelivery { /* private fields */ }
Expand description

Allocation-free delivery with exactly one pending offer and no history list.

Accepted misses commit immediately. Reward offers freeze the counter until accepted delivery; refusal preserves both counter and offer. While pending, new accepted attempts return Pending (even natural successes); ineligible and deferred observations remain harmless. Retry via Self::pending and Self::settle, never by admitting the gameplay event or rolling again. There is no individual cancellation: Self::reset explicitly abandons the old run’s debt. Payload storage and its cancellation remain caller-owned.

This type deliberately implements neither Copy nor Clone. The host must allocate a distinct nonzero stream namespace for every independently routed instance/lifetime, including replacements. Reusing a namespace in a new instance can alias old tokens. Copying a legacy counter creates no unique identity. IDs are local correlation only, with no save/wire/replay encoding.

Sequences 1 through u64::MAX are issued once per reward offer. After the last offer, all further accepted admissions fail closed with IdExhausted, even after reset; pending settlement still works. Reset never reuses a sequence.

Runnable first-party recipe: a confirmed, deduplicated projectile hit admits a reward into an existing bounded inventory. No reward feedback precedes destination acceptance, and capacity retry consumes no additional RNG.

use ferravex_play::gameplay::{
    inventory::{Inventory, Stack},
    loot::{RewardRng, RewardTable},
    pity::{PityAttempt, PityCounter, PityDelivery, PityOfferOutcome, PitySettlement},
    projectile::{HitResult, ProjectileBudget},
};
fn main() {
    let mut hits = ProjectileBudget::<1>::new(0, 0).unwrap();
    let mut bag = Inventory::<1>::new().unwrap();
    bag.add(Stack { item: 99, amount: 1, limit: 1 }).unwrap();
    let mut pity = PityDelivery::new(PityCounter::new(1).unwrap(), 7).unwrap();
    let mut rng = RewardRng::new(42);
    let table = RewardTable::new([1, 0]).unwrap();
    let mut feedback = 0;
    // The authoritative host has confirmed collision with target 101.
    assert_eq!(hits.target(101), HitResult::Stop);
    let natural_success = table.roll(&mut rng) == 1; // One caller-owned roll.
    let PityOfferOutcome::Reward(offer) = pity.offer(
        PityAttempt::Accepted { natural_success }).unwrap() else { panic!("limit one") };
    let reward = Stack { item: 5, amount: 1, limit: 1 }; // Retain with offer.
    let inserted = bag.add(reward).is_ok();
    assert!(!inserted);
    assert_eq!(pity.settle(offer.id(), inserted).unwrap(), PitySettlement::Pending(offer));
    assert_eq!(feedback, 0);
    assert_eq!(hits.target(101), HitResult::Ignored); // No second admission/roll.
    bag.remove(99, 1).unwrap(); // An actual inventory edit frees capacity.
    if let Some(retry) = pity.pending() {
        let inserted = bag.add(reward).is_ok();
        if let PitySettlement::Delivered(_) = pity.settle(retry.id(), inserted).unwrap() {
            feedback += 1; // Publish only this accepted reward event.
        }
    }
    assert!(pity.pending().is_none());
    assert_eq!((bag.count(5), feedback), (1, 1));
}

Orbit migration handoff: hit_block already rolls once on destruction before its drops.len() < 8 guard. Keep that roll and guard in that order; full capacity is Deferred, without counter advancement or a new owed reward. At available capacity, admit the same natural boolean, insert the capsule, then settle and publish reward feedback. Retain any unexpectedly refused offer/payload for retry before admitting another reward attempt. Do not replace the guard with accepted offers while full without an explicit rules version change: that would create new debt and change the old sequence.

Implementations§

Source§

impl PityDelivery

Source

pub fn new( counter: PityCounter, stream: u64, ) -> Result<PityDelivery, PityDeliveryError>

Move an existing counter into a delivery instance. Stream zero is invalid.

Source

pub fn offer( &mut self, attempt: PityAttempt, ) -> Result<PityOfferOutcome, PityDeliveryError>

Admit once per accepted gameplay event, using its already-consumed roll. No state changes on errors, ineligibility or deferral. See the type-level recipe for delivery and the stream provenance/overflow contract.

Source

pub fn pending(&self) -> Option<PityOffer>

Read the sole outstanding offer for delivery/retry. Does not reroll.

Source

pub fn settle( &mut self, id: PityOfferId, destination_accepted: bool, ) -> Result<PitySettlement, PityDeliveryError>

Record the destination’s actual insertion result for the current offer. false must mean no delivery happened; true must mean exactly one delivery happened. This method does not call or roll back external code.

The host must validate pending() before attempting insertion, keep exclusive ownership through insertion and settlement, and settle before publishing feedback. Stale, duplicate and foreign-stream tokens return StaleOffer without changing state. This rejects duplicate settlement, not a repeated external insertion performed before validation. Panic, crash recovery, dishonest results and other external effects are the caller’s responsibility; there is no durable exactly-once guarantee.

Source

pub fn misses(&self) -> u16

Committed misses only; an outstanding reward has not reset them yet.

Source

pub fn reset(&mut self) -> Option<PityOffer>

Begin a fresh drought and explicitly abandon/return the old pending offer. Old IDs become stale. Stream, sequence and exhaustion are retained. The host must discard the associated payload and suppress its feedback.

Trait Implementations§

Source§

impl Debug for PityDelivery

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.