use std::marker::PhantomData;
use crate::mechanics::{EventEmitter, Mechanic, ParallelSafe};
use super::policies::{CapacityPolicy, CostPolicy, StackingPolicy};
use super::strategies::{AlwaysStack, FixedSlotCapacity, NoCost};
use super::types::*;
pub struct InventoryMechanic<
C: CapacityPolicy = FixedSlotCapacity,
S: StackingPolicy = AlwaysStack,
K: CostPolicy = NoCost,
> {
_marker: PhantomData<(C, S, K)>,
}
impl<C, S, K> Mechanic for InventoryMechanic<C, S, K>
where
C: CapacityPolicy,
S: StackingPolicy,
K: CostPolicy,
{
type Config = InventoryConfig;
type State = InventoryState;
type Input = InventoryInput;
type Event = InventoryEvent;
type Execution = ParallelSafe;
fn step(
config: &Self::Config,
state: &mut Self::State,
input: Self::Input,
emitter: &mut impl EventEmitter<Self::Event>,
) {
match input.operation {
InventoryOperation::Add {
stack,
weight_per_item,
} => {
match C::can_add(state, &stack, weight_per_item, config) {
Ok(()) => {
S::add_to_inventory(state, stack, weight_per_item);
emitter.emit(InventoryEvent::ItemAdded { stack });
}
Err(reason) => {
emitter.emit(InventoryEvent::OperationRejected {
operation: input.operation,
reason,
});
match reason {
RejectionReason::InsufficientSlots => {
if let Some(max_slots) = config.max_slots {
emitter.emit(InventoryEvent::CapacityReached {
occupied_slots: state.occupied_slots,
max_slots,
});
}
}
RejectionReason::WeightLimitExceeded => {
if let Some(max_weight) = config.max_weight {
emitter.emit(InventoryEvent::WeightLimitReached {
current_weight: state.total_weight,
max_weight,
});
}
}
_ => {}
}
}
}
}
InventoryOperation::Remove { stack } => {
match C::can_remove(state, &stack) {
Ok(()) => {
S::remove_from_inventory(state, stack);
emitter.emit(InventoryEvent::ItemRemoved { stack });
}
Err(reason) => {
emitter.emit(InventoryEvent::OperationRejected {
operation: input.operation,
reason,
});
}
}
}
InventoryOperation::Transfer { stack } => {
match C::can_remove(state, &stack) {
Ok(()) => {
}
Err(reason) => {
emitter.emit(InventoryEvent::OperationRejected {
operation: input.operation,
reason,
});
}
}
}
}
if input.elapsed_time > 0 {
let cost = K::calculate_cost(state, config, input.elapsed_time);
if cost > 0.0 {
emitter.emit(InventoryEvent::HoldingCostApplied { cost });
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mechanics::inventory::strategies::*;
struct TestEmitter {
events: Vec<InventoryEvent>,
}
impl TestEmitter {
fn new() -> Self {
Self { events: Vec::new() }
}
}
impl EventEmitter<InventoryEvent> for TestEmitter {
fn emit(&mut self, event: InventoryEvent) {
self.events.push(event);
}
}
type BasicInventory = InventoryMechanic<FixedSlotCapacity, AlwaysStack, NoCost>;
#[test]
fn test_add_item_success() {
let config = InventoryConfig::default();
let mut state = InventoryState::new();
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Add {
stack: ItemStack::new(1, 10),
weight_per_item: 1.0,
},
elapsed_time: 0,
};
BasicInventory::step(&config, &mut state, input, &mut emitter);
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 10);
assert_eq!(emitter.events.len(), 1);
assert!(matches!(
emitter.events[0],
InventoryEvent::ItemAdded { .. }
));
}
#[test]
fn test_add_item_stacking() {
let config = InventoryConfig::default();
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 5));
state.occupied_slots = 1;
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Add {
stack: ItemStack::new(1, 3),
weight_per_item: 1.0,
},
elapsed_time: 0,
};
BasicInventory::step(&config, &mut state, input, &mut emitter);
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 8); }
#[test]
fn test_add_item_capacity_exceeded() {
let config = InventoryConfig {
max_slots: Some(1),
..Default::default()
};
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 10));
state.occupied_slots = 1;
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Add {
stack: ItemStack::new(2, 5), weight_per_item: 1.0,
},
elapsed_time: 0,
};
BasicInventory::step(&config, &mut state, input, &mut emitter);
assert_eq!(state.stacks.len(), 1);
assert!(emitter
.events
.iter()
.any(|e| matches!(&e, InventoryEvent::OperationRejected { .. })));
}
#[test]
fn test_remove_item_success() {
let config = InventoryConfig::default();
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 10));
state.occupied_slots = 1;
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Remove {
stack: ItemStack::new(1, 5),
},
elapsed_time: 0,
};
BasicInventory::step(&config, &mut state, input, &mut emitter);
assert_eq!(state.stacks.len(), 1);
assert_eq!(state.stacks[0].quantity, 5); assert!(matches!(
emitter.events[0],
InventoryEvent::ItemRemoved { .. }
));
}
#[test]
fn test_remove_item_not_found() {
let config = InventoryConfig::default();
let mut state = InventoryState::new();
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Remove {
stack: ItemStack::new(99, 1),
},
elapsed_time: 0,
};
BasicInventory::step(&config, &mut state, input, &mut emitter);
assert!(matches!(
emitter.events[0],
InventoryEvent::OperationRejected { .. }
));
}
#[test]
fn test_holding_cost() {
type CostlyInventory = InventoryMechanic<FixedSlotCapacity, AlwaysStack, SlotBasedCost>;
let config = InventoryConfig {
holding_cost_per_slot: 10.0,
..Default::default()
};
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 5));
state.occupied_slots = 1;
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Add {
stack: ItemStack::new(2, 3),
weight_per_item: 1.0,
},
elapsed_time: 5,
};
CostlyInventory::step(&config, &mut state, input, &mut emitter);
let cost_event = emitter
.events
.iter()
.find(|e| matches!(e, InventoryEvent::HoldingCostApplied { .. }));
assert!(cost_event.is_some());
}
#[test]
fn test_weight_based_capacity() {
type WeightInventory = InventoryMechanic<WeightBasedCapacity, AlwaysStack, NoCost>;
let config = InventoryConfig {
max_weight: Some(100.0),
..Default::default()
};
let mut state = InventoryState::new();
state.total_weight = 90.0;
let mut emitter = TestEmitter::new();
let input = InventoryInput {
operation: InventoryOperation::Add {
stack: ItemStack::new(1, 10),
weight_per_item: 2.0, },
elapsed_time: 0,
};
WeightInventory::step(&config, &mut state, input, &mut emitter);
assert!(emitter
.events
.iter()
.any(|e| matches!(&e, InventoryEvent::OperationRejected { .. })));
}
}