fish_lib/game/services/
encounter_service.rs

1use crate::config::ConfigInterface;
2use crate::game::errors::resource::GameResourceError;
3use crate::game::errors::GameResult;
4use crate::game::systems::encounter_system::{EncounterSystem, EncounterWeather};
5use crate::game::systems::weather_system::weather::Weather;
6use chrono::DateTime;
7use chrono_tz::Tz;
8use std::sync::Arc;
9
10pub trait EncounterServiceInterface: Send + Sync {
11    fn roll_encounter(
12        &self,
13        time: DateTime<Tz>,
14        weather: Weather,
15        location_id: i32,
16    ) -> GameResult<i32>;
17}
18
19pub struct EncounterService {
20    system: EncounterSystem,
21}
22
23impl EncounterService {
24    pub fn new(config: Arc<dyn ConfigInterface>) -> Self {
25        let system = EncounterSystem::new(config.species(), config.settings().rarity_exponent);
26
27        Self { system }
28    }
29}
30
31impl EncounterServiceInterface for EncounterService {
32    fn roll_encounter(
33        &self,
34        time: DateTime<Tz>,
35        weather: Weather,
36        location_id: i32,
37    ) -> GameResult<i32> {
38        let encounter_weather = if weather.is_raining {
39            EncounterWeather::Rain
40        } else {
41            EncounterWeather::Any
42        };
43
44        self.system
45            .roll_encounter(time, encounter_weather, location_id)
46            .ok_or_else(|| GameResourceError::no_available_encounters().into())
47    }
48}