use crate::context::ResourceContext;
use async_trait::async_trait;
use super::types::{EntityId, ItemId};
#[async_trait]
pub trait InventoryHook: Send + Sync {
async fn validate_add_item(
&self,
_entity_id: &EntityId,
_item_id: &ItemId,
_quantity: u32,
_resources: &ResourceContext,
) -> Result<(), String> {
Ok(())
}
async fn on_item_added(
&self,
_entity_id: &EntityId,
_item_id: &ItemId,
_quantity: u32,
_resources: &mut ResourceContext,
) {
}
async fn on_item_removed(
&self,
_entity_id: &EntityId,
_item_id: &ItemId,
_quantity: u32,
_resources: &mut ResourceContext,
) {
}
async fn on_item_used(
&self,
_entity_id: &EntityId,
_item_id: &ItemId,
_resources: &mut ResourceContext,
) -> Result<(), String> {
Ok(())
}
async fn validate_transfer(
&self,
_from_entity: &EntityId,
_to_entity: &EntityId,
_item_id: &ItemId,
_quantity: u32,
_resources: &ResourceContext,
) -> Result<(), String> {
Ok(())
}
async fn on_item_transferred(
&self,
_from_entity: &EntityId,
_to_entity: &EntityId,
_item_id: &ItemId,
_quantity: u32,
_resources: &mut ResourceContext,
) {
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct DefaultInventoryHook;
#[async_trait]
impl InventoryHook for DefaultInventoryHook {
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_default_hook_does_nothing() {
let hook = DefaultInventoryHook;
let entity_id = "player_1".to_string();
let item_id = "sword".to_string();
let resources = ResourceContext::new();
let result = hook
.validate_add_item(&entity_id, &item_id, 1, &resources)
.await;
assert!(result.is_ok());
let mut resources = ResourceContext::new();
hook.on_item_added(&entity_id, &item_id, 1, &mut resources)
.await;
hook.on_item_removed(&entity_id, &item_id, 1, &mut resources)
.await;
let result = hook
.on_item_used(&entity_id, &item_id, &mut resources)
.await;
assert!(result.is_ok());
}
}