1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use crate::{
entities::{player_data, players, PlayerData},
DbResult, Player, PlayerRole,
};
use sea_orm::{
ActiveModelTrait,
ActiveValue::{NotSet, Set},
ColumnTrait, DatabaseConnection, DeleteResult, EntityTrait, InsertResult, IntoActiveModel,
ModelTrait, PaginatorTrait, QueryFilter, QueryOrder,
};
use std::{future::Future, iter::Iterator, pin::Pin};
type DbFuture<'a, T> = Pin<Box<dyn Future<Output = DbResult<T>> + Send + 'a>>;
impl Player {
/// Takes all the player models using a cursor starting at the offset row
/// and finding the count number of values will check the count + 1 rows
/// in order to determine if there are more entires to come.
///
/// `db` The database connection
/// `offset` The number of rows to skip
/// `count` The number of rows to collect
pub async fn all(
db: &DatabaseConnection,
page: u64,
count: u64,
) -> DbResult<(Vec<Self>, bool)> {
let paginate = players::Entity::find()
.order_by_asc(players::Column::Id)
.paginate(db, count);
let total_pages = paginate.num_pages().await?;
let is_more = page < total_pages;
let values = paginate.fetch_page(page).await?;
Ok((values, is_more))
}
/// Creates a new player with the proivded details and inserts
/// it into the database
///
/// `db` The database instance
/// `email` The player account email
/// `display_name` The player display name
/// `password` The hashed player password
/// `origin` Whether the account is an origin account
pub fn create(
db: &DatabaseConnection,
email: String,
display_name: String,
password: Option<String>,
) -> DbFuture<Self> {
let active_model = players::ActiveModel {
email: Set(email),
display_name: Set(display_name),
password: Set(password),
..Default::default()
};
active_model.insert(db)
}
/// Deletes the provided player
///
/// `db` The database connection
pub fn delete(self, db: &DatabaseConnection) -> DbFuture<DeleteResult> {
// Delete player itself
let model = self.into_active_model();
model.delete(db)
}
/// Retrieves all the player data for the desired player
///
/// `id` The ID of the player
/// `db` The database connection
pub fn all_data(
id: u32,
db: &DatabaseConnection,
) -> impl Future<Output = DbResult<Vec<PlayerData>>> + Send + '_ {
player_data::Entity::find()
.filter(player_data::Column::PlayerId.eq(id))
.all(db)
}
/// Sets the key value data for the provided player. If the data exists then
/// the value is updated otherwise the data will be created. The new data is
/// returned.
///
/// `id` The ID of the player
/// `db` The database connection
/// `key` The data key
/// `value` The data value
pub async fn set_data(
id: u32,
db: &DatabaseConnection,
key: String,
value: String,
) -> DbResult<PlayerData> {
let existing = player_data::Entity::find()
.filter(
player_data::Column::PlayerId
.eq(id)
.and(player_data::Column::Key.eq(&key as &str)),
)
.one(db)
.await?;
if let Some(player_data) = existing {
let mut model = player_data.into_active_model();
model.key = Set(key);
model.value = Set(value);
model.update(db).await
} else {
player_data::ActiveModel {
player_id: Set(id),
key: Set(key),
value: Set(value),
..Default::default()
}
.insert(db)
.await
}
}
/// Bulk inserts a collection of player data for the provided player. Will not handle
/// conflicts so this should only be done on a freshly create player where data doesnt
/// already exist
///
/// `db` The database connection
/// `data` Iterator of the data keys and values
pub fn bulk_insert_data<'a>(
&self,
db: &'a DatabaseConnection,
data: impl Iterator<Item = (String, String)>,
) -> impl Future<Output = DbResult<InsertResult<player_data::ActiveModel>>> + Send + 'a {
// Transform the provided key values into active models
let models_iter = data.map(|(key, value)| player_data::ActiveModel {
id: NotSet,
player_id: Set(self.id),
key: Set(key),
value: Set(value),
});
// Insert all the models
player_data::Entity::insert_many(models_iter).exec(db)
}
/// Deletes the player data with the provided key for the
/// current player
///
/// `db` The database connection
/// `key` The data key
pub fn delete_data<'a>(
&self,
db: &'a DatabaseConnection,
key: &str,
) -> impl Future<Output = DbResult<DeleteResult>> + Send + 'a {
player_data::Entity::delete_many()
.belongs_to(self)
.filter(player_data::Column::Key.eq(key))
.exec(db)
}
/// Gets the player data with the provided key for the
/// current player
///
/// `db` The database connection
/// `key` The data key
pub fn get_data<'a>(
&self,
db: &'a DatabaseConnection,
key: &str,
) -> impl Future<Output = DbResult<Option<PlayerData>>> + Send + 'a {
self.find_related(player_data::Entity)
.filter(player_data::Column::Key.eq(key))
.one(db)
}
/// Gets all the player class data for the current player
///
/// `db` The database connection
pub fn get_classes<'a>(
&self,
db: &'a DatabaseConnection,
) -> impl Future<Output = DbResult<Vec<PlayerData>>> + Send + 'a {
self.find_related(player_data::Entity)
.filter(player_data::Column::Key.starts_with("class"))
.all(db)
}
/// Gets all the player character data for the current player
///
/// `db` The database connection
pub fn get_characters<'a>(
&self,
db: &'a DatabaseConnection,
) -> impl Future<Output = DbResult<Vec<PlayerData>>> + Send + 'a {
self.find_related(player_data::Entity)
.filter(player_data::Column::Key.starts_with("char"))
.all(db)
}
/// Parses the challenge points value which is the second
/// item in the completion list.
///
/// `db` The database connection
pub async fn get_challenge_points(&self, db: &DatabaseConnection) -> Option<u32> {
let list = self.get_data(db, "Completion").await.ok()??.value;
let part = list.split(',').nth(1)?;
let value: u32 = part.parse().ok()?;
Some(value)
}
/// Attempts to find a player with the provided ID will return none
/// if there was no players with that ID
///
/// `db` The database instance
/// `id` The ID of the player to find
pub fn by_id(
db: &DatabaseConnection,
id: u32,
) -> impl Future<Output = DbResult<Option<Player>>> + Send + '_ {
players::Entity::find_by_id(id).one(db)
}
/// Attempts to find a player with the provided email.
///
/// `db` The database connection
/// `email` The email address to search for
pub fn by_email<'a>(
db: &'a DatabaseConnection,
email: &str,
) -> impl Future<Output = DbResult<Option<Player>>> + Send + 'a {
players::Entity::find()
.filter(players::Column::Email.eq(email))
.one(db)
}
/// Determines whether the current player has permission to
/// make actions on behalf of the other player. This can
/// occur when they are both the same player or the role of
/// self is greater than the other role
///
/// `other` The player to check for permission over
pub fn has_permission_over(&self, other: &Self) -> bool {
self.id == other.id || self.role > other.role
}
/// Updates the password for the provided player returning
/// a future resolving to the new player with its updated
/// password value
///
/// `db` The database connection
/// `password` The new hashed password
pub fn set_password(self, db: &DatabaseConnection, password: String) -> DbFuture<'_, Player> {
let mut model = self.into_active_model();
model.password = Set(Some(password));
model.update(db)
}
/// Sets the role of the provided player
///
/// `db` The database connection
/// `role` The new role for the player
pub fn set_role(self, db: &DatabaseConnection, role: PlayerRole) -> DbFuture<'_, Player> {
let mut model = self.into_active_model();
model.role = Set(role);
model.update(db)
}
/// Updates the basic details of the provided player if
/// they are provided
///
/// `db` The database connection
/// `username` Optional new username for the player
/// `email` Optional new email for the player
pub fn set_details(
self,
db: &DatabaseConnection,
username: Option<String>,
email: Option<String>,
) -> DbFuture<'_, Player> {
let mut model = self.into_active_model();
if let Some(username) = username {
model.display_name = Set(username);
}
if let Some(email) = email {
model.email = Set(email)
}
model.update(db)
}
}