use crate::mechanics::inventory::policies::CapacityPolicy;
use crate::mechanics::inventory::types::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixedSlotCapacity;
impl CapacityPolicy for FixedSlotCapacity {
fn can_add(
state: &InventoryState,
stack: &ItemStack,
_weight_per_item: Weight,
config: &InventoryConfig,
) -> Result<(), RejectionReason> {
let Some(max_slots) = config.max_slots else {
return Ok(()); };
if state.find_stack(stack.item_id).is_some() {
return Ok(());
}
if state.occupied_slots >= max_slots {
Err(RejectionReason::InsufficientSlots)
} else {
Ok(())
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WeightBasedCapacity;
impl CapacityPolicy for WeightBasedCapacity {
fn can_add(
state: &InventoryState,
stack: &ItemStack,
weight_per_item: Weight,
config: &InventoryConfig,
) -> Result<(), RejectionReason> {
let Some(max_weight) = config.max_weight else {
return Ok(()); };
let added_weight = weight_per_item * stack.quantity as Weight;
let new_total_weight = state.total_weight + added_weight;
if new_total_weight > max_weight {
Err(RejectionReason::WeightLimitExceeded)
} else {
Ok(())
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnlimitedCapacity;
impl CapacityPolicy for UnlimitedCapacity {
fn can_add(
_state: &InventoryState,
_stack: &ItemStack,
_weight_per_item: Weight,
_config: &InventoryConfig,
) -> Result<(), RejectionReason> {
Ok(()) }
}
#[cfg(test)]
mod tests {
use super::*;
fn default_config() -> InventoryConfig {
InventoryConfig {
max_slots: Some(10),
max_weight: Some(100.0),
max_stack_size: Some(99),
holding_cost_per_slot: 0.0,
holding_cost_per_weight: 0.0,
}
}
#[test]
fn test_fixed_slot_can_add_to_existing_stack() {
let config = default_config();
let mut state = InventoryState::new();
state.stacks.push(ItemStack::new(1, 5));
state.occupied_slots = 1;
let result = FixedSlotCapacity::can_add(&state, &ItemStack::new(1, 10), 0.0, &config);
assert!(result.is_ok());
}
#[test]
fn test_fixed_slot_can_add_new_item() {
let config = default_config();
let mut state = InventoryState::new();
state.occupied_slots = 5;
let result = FixedSlotCapacity::can_add(&state, &ItemStack::new(1, 1), 0.0, &config);
assert!(result.is_ok());
}
#[test]
fn test_fixed_slot_full_inventory() {
let config = default_config();
let mut state = InventoryState::new();
state.occupied_slots = 10;
let result = FixedSlotCapacity::can_add(&state, &ItemStack::new(99, 1), 0.0, &config);
assert_eq!(result, Err(RejectionReason::InsufficientSlots));
}
#[test]
fn test_fixed_slot_unlimited() {
let config = InventoryConfig {
max_slots: None,
..default_config()
};
let mut state = InventoryState::new();
state.occupied_slots = 9999;
let result = FixedSlotCapacity::can_add(&state, &ItemStack::new(1, 1), 0.0, &config);
assert!(result.is_ok());
}
#[test]
fn test_weight_based_under_limit() {
let config = default_config();
let mut state = InventoryState::new();
state.total_weight = 50.0;
let result = WeightBasedCapacity::can_add(&state, &ItemStack::new(1, 10), 4.0, &config);
assert!(result.is_ok());
}
#[test]
fn test_weight_based_over_limit() {
let config = default_config();
let mut state = InventoryState::new();
state.total_weight = 90.0;
let result = WeightBasedCapacity::can_add(&state, &ItemStack::new(1, 10), 2.0, &config);
assert_eq!(result, Err(RejectionReason::WeightLimitExceeded));
}
#[test]
fn test_weight_based_unlimited() {
let config = InventoryConfig {
max_weight: None,
..default_config()
};
let mut state = InventoryState::new();
state.total_weight = 9999.0;
let result = WeightBasedCapacity::can_add(&state, &ItemStack::new(1, 100), 100.0, &config);
assert!(result.is_ok());
}
#[test]
fn test_unlimited_capacity_always_succeeds() {
let config = default_config();
let state = InventoryState::new();
let result = UnlimitedCapacity::can_add(&state, &ItemStack::new(1, 999999), 999.0, &config);
assert!(result.is_ok());
}
}