Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 0205b43

Browse files
committed
🗃️ Store battle count when votes comes in
1 parent f3bc341 commit 0205b43

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

src/db/votes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mod tests {
8080
#[ignore]
8181
async fn get_all_by_account_id_ok() -> Result {
8282
let manager = Db::open_unittests().await?.votes().await?;
83-
let mut vote = Vote::new(1, TankId(42), Rating::Like);
83+
let mut vote = Vote::new(1, TankId(42), 1, Rating::Like);
8484
vote.timestamp = vote.timestamp.duration_round(Duration::seconds(1))?;
8585
manager.insert(&vote).await?;
8686

@@ -116,7 +116,7 @@ mod tests {
116116
#[ignore]
117117
async fn delete_vote_ok() -> Result {
118118
let manager = Db::open_unittests().await?.votes().await?;
119-
let vote = Vote::new(1, TankId(42), Rating::Like);
119+
let vote = Vote::new(1, TankId(42), 1, Rating::Like);
120120
manager.insert(&vote).await?;
121121
manager.delete(vote.id.account_id, vote.id.tank_id).await?;
122122
assert!(

src/models/vote.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,24 @@ pub struct Vote {
3333
deserialize_with = "Rating::deserialize"
3434
)]
3535
pub rating: Rating,
36+
37+
#[serde(rename = "nb")]
38+
pub n_battles: u32,
3639
}
3740

3841
impl Vote {
39-
pub fn new(account_id: impl Into<AccountId>, tank_id: TankId, rating: Rating) -> Self {
42+
pub fn new(
43+
account_id: impl Into<AccountId>,
44+
tank_id: TankId,
45+
n_battles: u32,
46+
rating: Rating,
47+
) -> Self {
4048
Self {
4149
id: VoteId {
4250
account_id: account_id.into(),
4351
tank_id,
4452
},
53+
n_battles,
4554
rating,
4655
timestamp: Utc::now(),
4756
}

src/web/state.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ impl AppState {
8080
.get_vehicles_stats(account_id)
8181
.await?
8282
.into_iter()
83-
.filter(VehicleStats::is_played)
84-
.filter(|stats| is_known_tank_id(stats.tank_id))
83+
.filter(|stats| stats.inner.n_battles != 0 && is_known_tank_id(stats.tank_id))
8584
.sorted_unstable_by(|lhs, rhs| rhs.last_battle_time.cmp(&lhs.last_battle_time))
8685
.map(|stats| (stats.tank_id, stats))
8786
.collect();
@@ -93,12 +92,12 @@ impl AppState {
9392
}
9493

9594
#[instrument(skip_all, fields(account_id = %account_id, tank_id = %tank_id))]
96-
pub async fn owns_vehicle(&self, account_id: AccountId, tank_id: TankId) -> Result<bool> {
95+
pub async fn get_battle_count(&self, account_id: AccountId, tank_id: TankId) -> Result<u32> {
9796
Ok(self
9897
.get_vehicles_stats(account_id)
9998
.await?
10099
.get(&tank_id)
101-
.is_some_and(VehicleStats::is_played))
100+
.map_or(0, |stats| stats.inner.n_battles))
102101
}
103102

104103
#[instrument(skip_all, fields(account_id = %account_id))]

src/web/views/profile.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ async fn rate_vehicle(
127127
if params.account_id != user.account_id {
128128
return Err(WebError::Forbidden(ForbiddenReason::NonOwner));
129129
}
130-
if !state.owns_vehicle(user.account_id, params.tank_id).await? {
130+
if state.get_battle_count(user.account_id, params.tank_id).await? == 0 {
131131
return Err(WebError::ImATeapot);
132132
}
133133

134134
info!(?rating);
135135
if let Some(rating) = rating {
136+
let n_battles = state.get_battle_count(user.account_id, params.tank_id).await?;
136137
state
137138
.vote_manager
138-
.insert(&Vote::new(user.account_id, params.tank_id, rating))
139+
.insert(&Vote::new(user.account_id, params.tank_id, n_battles, rating))
139140
.await?;
140141
} else {
141142
state.vote_manager.delete(user.account_id, params.tank_id).await?;

src/wg.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,6 @@ pub struct VehicleStats {
197197
}
198198

199199
impl VehicleStats {
200-
pub const fn is_played(&self) -> bool {
201-
self.inner.n_battles != 0
202-
}
203-
204200
/// Deserialize last battle time and take care of missing timestamps in the response.
205201
#[inline]
206202
fn deserialize_last_battle_time<'de, D: Deserializer<'de>>(

0 commit comments

Comments
 (0)