pocket_relay_database/interfaces/
galaxy_at_war.rs

1use crate::{
2    entities::{galaxy_at_war, players},
3    DbResult, GalaxyAtWar,
4};
5use chrono::Local;
6use sea_orm::{
7    ActiveModelTrait,
8    ActiveValue::{NotSet, Set},
9    DatabaseConnection, IntoActiveModel, ModelTrait,
10};
11use std::cmp;
12
13impl GalaxyAtWar {
14    /// The minimum value for galaxy at war entries
15    const MIN_VALUE: u16 = 5000;
16    /// The maximum value for galaxy at war entries
17    const MAX_VALUE: u16 = 10099;
18
19    /// Finds or creates a new galaxy at war entry for the provided
20    /// player. If one exists then the provided decay value will be
21    /// applied to it.
22    ///
23    /// `db`     The database connection
24    /// `player` The player to search for galaxy at war models for
25    /// `decay`  The decay value
26    pub async fn find_or_create(
27        db: &DatabaseConnection,
28        player: &players::Model,
29        decay: f32,
30    ) -> DbResult<Self> {
31        let existing = player.find_related(galaxy_at_war::Entity).one(db).await?;
32        if let Some(value) = existing {
33            return value.apply_decay(db, decay).await;
34        }
35
36        let current_time = Local::now().naive_local();
37        let model = galaxy_at_war::ActiveModel {
38            id: NotSet,
39            player_id: Set(player.id),
40            last_modified: Set(current_time),
41            group_a: Set(Self::MIN_VALUE),
42            group_b: Set(Self::MIN_VALUE),
43            group_c: Set(Self::MIN_VALUE),
44            group_d: Set(Self::MIN_VALUE),
45            group_e: Set(Self::MIN_VALUE),
46        };
47
48        model.insert(db).await
49    }
50
51    /// Increases the group values stored on the provided
52    /// galaxy at war models by the values provided.
53    ///
54    /// `db`     The database connection
55    /// `value`  The galaxy at war model to increase
56    /// `values` The values to increase each group by
57    pub async fn increase(
58        self,
59        db: &DatabaseConnection,
60        values: (u16, u16, u16, u16, u16),
61    ) -> DbResult<galaxy_at_war::Model> {
62        let new_a = self.group_a + values.0;
63        let new_b = self.group_b + values.1;
64        let new_c = self.group_c + values.2;
65        let new_d = self.group_d + values.3;
66        let new_e = self.group_e + values.4;
67
68        let mut gaw_data = self.into_active_model();
69        gaw_data.group_a = Set(cmp::min(new_a, Self::MAX_VALUE));
70        gaw_data.group_b = Set(cmp::min(new_b, Self::MAX_VALUE));
71        gaw_data.group_c = Set(cmp::min(new_c, Self::MAX_VALUE));
72        gaw_data.group_d = Set(cmp::min(new_d, Self::MAX_VALUE));
73        gaw_data.group_e = Set(cmp::min(new_e, Self::MAX_VALUE));
74        gaw_data.update(db).await
75    }
76
77    /// Applies the provided galaxy at war decay value to the provided
78    /// galaxy at war model decreasing the values by the number of days
79    /// that have passed.
80    ///
81    /// `db`    The database connection
82    /// `value` The galaxy at war model to decay
83    /// `decay` The decay value
84    async fn apply_decay(self, db: &DatabaseConnection, decay: f32) -> DbResult<Self> {
85        // Skip decaying if decay is non existent
86        if decay <= 0.0 {
87            return Ok(self);
88        }
89
90        let current_time = Local::now().naive_local();
91        let days_passed = (current_time - self.last_modified).num_days() as f32;
92        let decay_value = (decay * days_passed * 100.0) as u16;
93
94        // Apply decay while keeping minimum
95        let a = cmp::max(self.group_a - decay_value, Self::MIN_VALUE);
96        let b = cmp::max(self.group_b - decay_value, Self::MIN_VALUE);
97        let c = cmp::max(self.group_c - decay_value, Self::MIN_VALUE);
98        let d = cmp::max(self.group_d - decay_value, Self::MIN_VALUE);
99        let e = cmp::max(self.group_e - decay_value, Self::MIN_VALUE);
100
101        // Update stored copy
102        let mut value = self.into_active_model();
103        value.group_a = Set(a);
104        value.group_b = Set(b);
105        value.group_c = Set(c);
106        value.group_d = Set(d);
107        value.group_e = Set(e);
108
109        value.update(db).await
110    }
111}