Skip to content

Commit ae6c4fe

Browse files
committed
Seperated gaw decay compute from gaw lookup
1 parent 098c7d4 commit ae6c4fe

File tree

3 files changed

+59
-66
lines changed

3 files changed

+59
-66
lines changed

src/database/entities/galaxy_at_war.rs

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use sea_orm::prelude::*;
77
use sea_orm::{
88
ActiveModelTrait,
99
ActiveValue::{NotSet, Set},
10-
DatabaseConnection, IntoActiveModel,
10+
DatabaseConnection,
1111
};
1212
use serde::Serialize;
13-
use std::cmp;
13+
use std::future::Future;
1414

1515
/// Structure for a galaxy at war model stored in the database
1616
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize)]
@@ -49,29 +49,18 @@ impl Model {
4949
/// The maximum value for galaxy at war entries
5050
const MAX_VALUE: u16 = 10099;
5151

52-
/// Finds or creates a new galaxy at war entry for the provided
53-
/// player. If one exists then the provided decay value will be
54-
/// applied to it.
55-
///
56-
/// `db` The database connection
57-
/// `player` The player to search for galaxy at war models for
58-
/// `decay` The decay value
59-
pub async fn find_or_create(
60-
db: &DatabaseConnection,
61-
player_id: PlayerID,
62-
decay: f32,
63-
) -> DbResult<Self> {
52+
pub async fn get(db: &DatabaseConnection, player_id: PlayerID) -> DbResult<Self> {
6453
let existing = Entity::find()
6554
.filter(Column::PlayerId.eq(player_id))
6655
.one(db)
6756
.await?;
6857

6958
if let Some(value) = existing {
70-
return value.apply_decay(db, decay).await;
59+
return Ok(value);
7160
}
7261

7362
let current_time = Local::now().naive_local();
74-
let model = ActiveModel {
63+
ActiveModel {
7564
id: NotSet,
7665
player_id: Set(player_id),
7766
last_modified: Set(current_time),
@@ -80,45 +69,62 @@ impl Model {
8069
group_c: Set(Self::MIN_VALUE),
8170
group_d: Set(Self::MIN_VALUE),
8271
group_e: Set(Self::MIN_VALUE),
83-
};
72+
}
73+
.insert(db)
74+
.await
75+
}
8476

85-
model.insert(db).await
77+
/// Increases the stored group values increasing them by the `values`
78+
/// provided for each respective group
79+
pub fn add(
80+
self,
81+
db: &DatabaseConnection,
82+
values: [u16; 5],
83+
) -> impl Future<Output = DbResult<Self>> + '_ {
84+
self.transform(db, |a, b| a.saturating_add(b).min(Model::MAX_VALUE), values)
8685
}
8786

88-
/// Increases the group values stored on the provided
89-
/// galaxy at war models by the values provided.
90-
///
91-
/// `db` The database connection
92-
/// `value` The galaxy at war model to increase
93-
/// `values` The values to increase each group by
94-
pub async fn increase(
87+
/// Decrease the stored group values decreasuing them by the `values`
88+
/// provided for each respective group
89+
pub fn sub(
9590
self,
9691
db: &DatabaseConnection,
97-
values: (u16, u16, u16, u16, u16),
98-
) -> DbResult<Self> {
99-
let new_a = self.group_a + values.0;
100-
let new_b = self.group_b + values.1;
101-
let new_c = self.group_c + values.2;
102-
let new_d = self.group_d + values.3;
103-
let new_e = self.group_e + values.4;
92+
values: [u16; 5],
93+
) -> impl Future<Output = DbResult<Self>> + '_ {
94+
self.transform(db, |a, b| a.saturating_sub(b).max(Model::MIN_VALUE), values)
95+
}
10496

105-
let mut gaw_data = self.into_active_model();
106-
gaw_data.group_a = Set(cmp::min(new_a, Self::MAX_VALUE));
107-
gaw_data.group_b = Set(cmp::min(new_b, Self::MAX_VALUE));
108-
gaw_data.group_c = Set(cmp::min(new_c, Self::MAX_VALUE));
109-
gaw_data.group_d = Set(cmp::min(new_d, Self::MAX_VALUE));
110-
gaw_data.group_e = Set(cmp::min(new_e, Self::MAX_VALUE));
111-
gaw_data.update(db).await
97+
/// Transforms the underlying group values using the provided action
98+
/// function which is given the current value as the first argument
99+
/// and the respective value from `values` as the second argument
100+
#[inline]
101+
pub async fn transform<F>(
102+
self,
103+
db: &DatabaseConnection,
104+
action: F,
105+
values: [u16; 5],
106+
) -> DbResult<Self>
107+
where
108+
F: Fn(u16, u16) -> u16,
109+
{
110+
let current_time = Local::now().naive_local();
111+
ActiveModel {
112+
id: Set(self.id),
113+
player_id: Set(self.player_id),
114+
last_modified: Set(current_time),
115+
group_a: Set(action(self.group_a, values[0])),
116+
group_b: Set(action(self.group_b, values[1])),
117+
group_c: Set(action(self.group_c, values[2])),
118+
group_d: Set(action(self.group_d, values[3])),
119+
group_e: Set(action(self.group_e, values[4])),
120+
}
121+
.update(db)
122+
.await
112123
}
113124

114-
/// Applies the provided galaxy at war decay value to the provided
115-
/// galaxy at war model decreasing the values by the number of days
116-
/// that have passed.
117-
///
118-
/// `db` The database connection
119-
/// `value` The galaxy at war model to decay
120-
/// `decay` The decay value
121-
async fn apply_decay(self, db: &DatabaseConnection, decay: f32) -> DbResult<Self> {
125+
/// Applies the daily decay progress to the group values calculating the
126+
/// decay amount from the number of days passed
127+
pub async fn apply_decay(self, db: &DatabaseConnection, decay: f32) -> DbResult<Self> {
122128
// Skip decaying if decay is non existent
123129
if decay <= 0.0 {
124130
return Ok(self);
@@ -128,21 +134,6 @@ impl Model {
128134
let days_passed = (current_time - self.last_modified).num_days() as f32;
129135
let decay_value = (decay * days_passed * 100.0) as u16;
130136

131-
// Apply decay while keeping minimum
132-
let a = cmp::max(self.group_a - decay_value, Self::MIN_VALUE);
133-
let b = cmp::max(self.group_b - decay_value, Self::MIN_VALUE);
134-
let c = cmp::max(self.group_c - decay_value, Self::MIN_VALUE);
135-
let d = cmp::max(self.group_d - decay_value, Self::MIN_VALUE);
136-
let e = cmp::max(self.group_e - decay_value, Self::MIN_VALUE);
137-
138-
// Update stored copy
139-
let mut value = self.into_active_model();
140-
value.group_a = Set(a);
141-
value.group_b = Set(b);
142-
value.group_c = Set(c);
143-
value.group_d = Set(d);
144-
value.group_e = Set(e);
145-
146-
value.update(db).await
137+
self.sub(db, [decay_value; 5]).await
147138
}
148139
}

src/routes/gaw.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub async fn increase_ratings(
141141
) -> Result<Xml, GAWError> {
142142
let (gaw_data, promotions) = get_player_gaw_data(&db, sessions, &id, &config).await?;
143143
let gaw_data = gaw_data
144-
.increase(&db, (query.a, query.b, query.c, query.d, query.e))
144+
.add(&db, [query.a, query.b, query.c, query.d, query.e])
145145
.await?;
146146
Ok(ratings_response(gaw_data, promotions))
147147
}
@@ -166,9 +166,11 @@ async fn get_player_gaw_data(
166166
.ok_or(GAWError::InvalidToken)?;
167167

168168
let (gaw_data, promotions) = try_join!(
169-
GalaxyAtWar::find_or_create(db, player.id, config.galaxy_at_war.decay),
169+
GalaxyAtWar::get(db, player.id),
170170
get_promotions(db, &player, config)
171171
)?;
172+
let gaw_data = gaw_data.apply_decay(db, config.galaxy_at_war.decay).await?;
173+
172174
Ok((gaw_data, promotions))
173175
}
174176

src/routes/players.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ pub async fn get_player_gaw(
542542
Extension(db): Extension<DatabaseConnection>,
543543
) -> PlayersRes<GalaxyAtWar> {
544544
let player = find_player(&db, player_id).await?;
545-
let galax_at_war = GalaxyAtWar::find_or_create(&db, player.id, 0.0).await?;
545+
let galax_at_war = GalaxyAtWar::get(&db, player.id).await?;
546546
Ok(Json(galax_at_war))
547547
}
548548

0 commit comments

Comments
 (0)