overlord_event_system/async_handler/
bundles.rs

1use crate::{
2    async_handler::handler::OverlordAsyncEventHandler, bundles::bundle_raw_step_to_element,
3    event::OverlordEvent, game_config_helpers::GameConfigLookup, state::OverlordState,
4};
5
6use essences::{
7    bundles::{BundleClaimMode, BundleId, BundleStep, BundleStepGeneric},
8    currency::CurrencySource,
9};
10use event_system::{event::EventPluginized, system::EventHandleResult};
11
12impl OverlordAsyncEventHandler {
13    pub fn handle_add_bundle_group(
14        &self,
15        bundle_ids: &[BundleId],
16        source: CurrencySource,
17        mut state: OverlordState,
18    ) -> EventHandleResult<OverlordEvent, OverlordState> {
19        let game_config = self.game_config.get();
20
21        if state.character_state.bundle_step_generic.is_empty() {
22            for &bundle_id in bundle_ids {
23                let Ok(bundle) = game_config.require_bundle(bundle_id) else {
24                    tracing::error!("Couldn't find bundle with id: {bundle_id} in config");
25                    return EventHandleResult::fail(state);
26                };
27
28                let step = match bundle.claim_mode {
29                    BundleClaimMode::Sequential => {
30                        let Some(raw_item) = bundle.steps.first() else {
31                            tracing::error!(
32                                "Failed to get element with index 0 in bundle {}",
33                                bundle_id
34                            );
35                            return EventHandleResult::fail(state);
36                        };
37                        let element = bundle_raw_step_to_element(
38                            raw_item,
39                            &state.character_state,
40                            &self.script_runner,
41                            &game_config,
42                        );
43                        BundleStepGeneric::Sequential(BundleStep {
44                            bundle_id,
45                            element,
46                            has_pop_up: raw_item.has_pop_up,
47                            source,
48                        })
49                    }
50                    BundleClaimMode::AllAtOnce => {
51                        let elements: Vec<_> = bundle
52                            .steps
53                            .iter()
54                            .map(|raw_step| {
55                                bundle_raw_step_to_element(
56                                    raw_step,
57                                    &state.character_state,
58                                    &self.script_runner,
59                                    &game_config,
60                                )
61                            })
62                            .collect();
63
64                        if elements.is_empty() {
65                            tracing::error!("Got empty bundle for id {bundle_id}");
66                            return EventHandleResult::fail(state);
67                        }
68
69                        let has_pop_up = bundle.steps.iter().any(|s| s.has_pop_up);
70
71                        BundleStepGeneric::AllAtOnce {
72                            bundle_id,
73                            elements,
74                            has_pop_up,
75                            source,
76                        }
77                    }
78                };
79
80                state.character_state.bundle_step_generic.push(step);
81            }
82        }
83
84        let mut events = vec![];
85        if state
86            .character_state
87            .bundle_step_generic
88            .first()
89            .is_some_and(|step| !step.has_pop_up())
90        {
91            events.push(EventPluginized::now(
92                OverlordEvent::ClaimBundleStepGeneric {},
93            ));
94        }
95
96        EventHandleResult::ok_events(state, events)
97    }
98}