use super::types::*;
use async_trait::async_trait;
#[async_trait]
pub trait SynthesisHook: Send + Sync {
async fn consume_ingredients(
&self,
_entity_id: &EntityId,
_ingredients: &[(IngredientType, u32)],
) -> Result<(), SynthesisError> {
Ok(())
}
async fn apply_synthesis_result(&self, _entity_id: &EntityId, _result: &SynthesisResult) {
}
async fn refund_ingredients(
&self,
_entity_id: &EntityId,
_ingredients: &[(IngredientType, u32)],
) {
}
async fn get_skill_modifier(&self, _entity_id: &EntityId, _recipe_id: &RecipeId) -> f32 {
0.0
}
async fn generate_byproduct(&self, _entity_id: &EntityId, _recipe_id: &RecipeId) {
}
async fn on_synthesis_started(&self, _entity_id: &EntityId, _recipe_id: &RecipeId) {
}
async fn on_synthesis_success(
&self,
_entity_id: &EntityId,
_recipe_id: &RecipeId,
_quality: f32,
) {
}
async fn on_synthesis_failure(&self, _entity_id: &EntityId, _recipe_id: &RecipeId) {
}
async fn on_recipe_discovered(&self, _entity_id: &EntityId, _recipe_id: &RecipeId) {
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DefaultSynthesisHook;
#[async_trait]
impl SynthesisHook for DefaultSynthesisHook {}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_default_hook_consume() {
let hook = DefaultSynthesisHook;
let result = hook
.consume_ingredients(
&"player1".to_string(),
&[(
IngredientType::Item {
item_id: "iron".to_string(),
},
3,
)],
)
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_default_hook_apply_result() {
let hook = DefaultSynthesisHook;
let result = SynthesisResult {
result_type: ResultType::Item {
item_id: "sword".to_string(),
},
quantity: 1,
quality_range: (0.8, 1.2),
};
hook.apply_synthesis_result(&"player1".to_string(), &result)
.await;
}
#[tokio::test]
async fn test_default_hook_get_skill_modifier() {
let hook = DefaultSynthesisHook;
let modifier = hook
.get_skill_modifier(&"player1".to_string(), &"sword".to_string())
.await;
assert_eq!(modifier, 0.0);
}
#[tokio::test]
async fn test_default_hook_events() {
let hook = DefaultSynthesisHook;
hook.on_synthesis_started(&"player1".to_string(), &"sword".to_string())
.await;
hook.on_synthesis_success(&"player1".to_string(), &"sword".to_string(), 0.9)
.await;
hook.on_synthesis_failure(&"player1".to_string(), &"sword".to_string())
.await;
hook.on_recipe_discovered(&"player1".to_string(), &"sword".to_string())
.await;
hook.generate_byproduct(&"player1".to_string(), &"sword".to_string())
.await;
}
}