@@ -7,10 +7,10 @@ use sea_orm::prelude::*;
7
7
use sea_orm:: {
8
8
ActiveModelTrait ,
9
9
ActiveValue :: { NotSet , Set } ,
10
- DatabaseConnection , IntoActiveModel ,
10
+ DatabaseConnection ,
11
11
} ;
12
12
use serde:: Serialize ;
13
- use std:: cmp ;
13
+ use std:: future :: Future ;
14
14
15
15
/// Structure for a galaxy at war model stored in the database
16
16
#[ derive( Clone , Debug , PartialEq , Eq , DeriveEntityModel , Serialize ) ]
@@ -49,29 +49,18 @@ impl Model {
49
49
/// The maximum value for galaxy at war entries
50
50
const MAX_VALUE : u16 = 10099 ;
51
51
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 > {
64
53
let existing = Entity :: find ( )
65
54
. filter ( Column :: PlayerId . eq ( player_id) )
66
55
. one ( db)
67
56
. await ?;
68
57
69
58
if let Some ( value) = existing {
70
- return value . apply_decay ( db , decay ) . await ;
59
+ return Ok ( value ) ;
71
60
}
72
61
73
62
let current_time = Local :: now ( ) . naive_local ( ) ;
74
- let model = ActiveModel {
63
+ ActiveModel {
75
64
id : NotSet ,
76
65
player_id : Set ( player_id) ,
77
66
last_modified : Set ( current_time) ,
@@ -80,45 +69,62 @@ impl Model {
80
69
group_c : Set ( Self :: MIN_VALUE ) ,
81
70
group_d : Set ( Self :: MIN_VALUE ) ,
82
71
group_e : Set ( Self :: MIN_VALUE ) ,
83
- } ;
72
+ }
73
+ . insert ( db)
74
+ . await
75
+ }
84
76
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)
86
85
}
87
86
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 (
95
90
self ,
96
91
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
+ }
104
96
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
112
123
}
113
124
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 > {
122
128
// Skip decaying if decay is non existent
123
129
if decay <= 0.0 {
124
130
return Ok ( self ) ;
@@ -128,21 +134,6 @@ impl Model {
128
134
let days_passed = ( current_time - self . last_modified ) . num_days ( ) as f32 ;
129
135
let decay_value = ( decay * days_passed * 100.0 ) as u16 ;
130
136
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
147
138
}
148
139
}
0 commit comments