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
impl PityDelivery
Sourcepub fn new(
counter: PityCounter,
stream: u64,
) -> Result<PityDelivery, PityDeliveryError>
pub fn new( counter: PityCounter, stream: u64, ) -> Result<PityDelivery, PityDeliveryError>
Move an existing counter into a delivery instance. Stream zero is invalid.
Sourcepub fn offer(
&mut self,
attempt: PityAttempt,
) -> Result<PityOfferOutcome, PityDeliveryError>
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.
Sourcepub fn pending(&self) -> Option<PityOffer>
pub fn pending(&self) -> Option<PityOffer>
Read the sole outstanding offer for delivery/retry. Does not reroll.
Sourcepub fn settle(
&mut self,
id: PityOfferId,
destination_accepted: bool,
) -> Result<PitySettlement, PityDeliveryError>
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.