overlord_event_system/async_handler/
gifts.rs1use crate::{
2 async_handler::handler::OverlordAsyncEventHandler, event::OverlordEvent,
3 game_config_helpers::GameConfigLookup, state::OverlordState,
4};
5
6use essences::{
7 currency::{CurrencyConsumer, CurrencySource},
8 gift::{Gift, GiftType},
9};
10
11use event_system::system::EventHandleResult;
12use uuid::Uuid;
13
14impl OverlordAsyncEventHandler {
15 pub fn handle_send_gift(
16 &self,
17 receiver_id: Uuid,
18 config_gift_id: Uuid,
19 mut state: OverlordState,
20 ) -> EventHandleResult<OverlordEvent, OverlordState> {
21 let game_config = self.game_config.get();
22
23 let Ok(config_gift) = game_config.require_gift_template(config_gift_id) else {
24 tracing::error!(
25 "Tried finding gift with id = {}, but didn't find it in config",
26 config_gift_id
27 );
28 return EventHandleResult::fail(state);
29 };
30
31 let Some(currency_event) =
32 Self::currency_decrease(&state, &config_gift.price, CurrencyConsumer::GiftSend)
33 else {
34 return EventHandleResult::fail(state);
35 };
36
37 let events = vec![currency_event];
38
39 if config_gift.gift_type == GiftType::VassalGift {
40 match state
41 .character_state
42 .vassals
43 .iter_mut()
44 .find(|vassal| vassal.character_id == receiver_id)
45 {
46 Some(vassal) => {
47 vassal.loyalty += config_gift.loyalty;
48 }
49 None => {
50 tracing::error!(
51 "Tried sending vassal_gift to character_id = {}, but he is not in state.vassals",
52 receiver_id
53 );
54 return EventHandleResult::fail(state);
55 }
56 }
57 };
58
59 EventHandleResult::ok_events(state, events)
60 }
61
62 pub fn handle_new_gift(
63 &self,
64 new_gift: Gift,
65 mut state: OverlordState,
66 ) -> EventHandleResult<OverlordEvent, OverlordState> {
67 state.incoming_gifts.push(new_gift.clone());
68 if new_gift.loyalty > 0 {
69 let Some(suzerain) = state.character_state.suzerain.as_mut() else {
70 tracing::error!(
71 "Got gift with id = {}, with loyalty, but didn't find suzerain in state",
72 new_gift.id
73 );
74 return EventHandleResult::fail(state);
75 };
76 suzerain.loyalty += new_gift.loyalty;
77 };
78 EventHandleResult::ok(state)
79 }
80
81 pub fn handle_accept_gift(
82 &self,
83 gift_id: Uuid,
84 mut state: OverlordState,
85 ) -> EventHandleResult<OverlordEvent, OverlordState> {
86 let mut events = vec![];
87
88 if let Some(pos) = state
89 .incoming_gifts
90 .iter_mut()
91 .position(|x| x.id == gift_id)
92 {
93 let reward = state.incoming_gifts[pos].reward.clone();
94
95 events.push(Self::currency_increase(&reward, CurrencySource::GiftClaim));
96
97 state.incoming_gifts.remove(pos);
98 } else {
99 tracing::error!(
100 "Tried claiming gift with id = {}, but didn't find it in state",
101 gift_id
102 );
103 return EventHandleResult::fail(state);
104 };
105
106 EventHandleResult::ok_events(state, events)
107 }
108}