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