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

Commit

Permalink
🗃️ Store battle count when votes comes in
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Aug 4, 2023
1 parent f3bc341 commit 0205b43
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/db/votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod tests {
#[ignore]
async fn get_all_by_account_id_ok() -> Result {
let manager = Db::open_unittests().await?.votes().await?;
let mut vote = Vote::new(1, TankId(42), Rating::Like);
let mut vote = Vote::new(1, TankId(42), 1, Rating::Like);
vote.timestamp = vote.timestamp.duration_round(Duration::seconds(1))?;
manager.insert(&vote).await?;

Expand Down Expand Up @@ -116,7 +116,7 @@ mod tests {
#[ignore]
async fn delete_vote_ok() -> Result {
let manager = Db::open_unittests().await?.votes().await?;
let vote = Vote::new(1, TankId(42), Rating::Like);
let vote = Vote::new(1, TankId(42), 1, Rating::Like);
manager.insert(&vote).await?;
manager.delete(vote.id.account_id, vote.id.tank_id).await?;
assert!(
Expand Down
11 changes: 10 additions & 1 deletion src/models/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ pub struct Vote {
deserialize_with = "Rating::deserialize"
)]
pub rating: Rating,

#[serde(rename = "nb")]
pub n_battles: u32,
}

impl Vote {
pub fn new(account_id: impl Into<AccountId>, tank_id: TankId, rating: Rating) -> Self {
pub fn new(
account_id: impl Into<AccountId>,
tank_id: TankId,
n_battles: u32,
rating: Rating,
) -> Self {
Self {
id: VoteId {
account_id: account_id.into(),
tank_id,
},
n_battles,
rating,
timestamp: Utc::now(),
}
Expand Down
7 changes: 3 additions & 4 deletions src/web/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl AppState {
.get_vehicles_stats(account_id)
.await?
.into_iter()
.filter(VehicleStats::is_played)
.filter(|stats| is_known_tank_id(stats.tank_id))
.filter(|stats| stats.inner.n_battles != 0 && is_known_tank_id(stats.tank_id))
.sorted_unstable_by(|lhs, rhs| rhs.last_battle_time.cmp(&lhs.last_battle_time))
.map(|stats| (stats.tank_id, stats))
.collect();
Expand All @@ -93,12 +92,12 @@ impl AppState {
}

#[instrument(skip_all, fields(account_id = %account_id, tank_id = %tank_id))]
pub async fn owns_vehicle(&self, account_id: AccountId, tank_id: TankId) -> Result<bool> {
pub async fn get_battle_count(&self, account_id: AccountId, tank_id: TankId) -> Result<u32> {
Ok(self
.get_vehicles_stats(account_id)
.await?
.get(&tank_id)
.is_some_and(VehicleStats::is_played))
.map_or(0, |stats| stats.inner.n_battles))
}

#[instrument(skip_all, fields(account_id = %account_id))]
Expand Down
5 changes: 3 additions & 2 deletions src/web/views/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,16 @@ async fn rate_vehicle(
if params.account_id != user.account_id {
return Err(WebError::Forbidden(ForbiddenReason::NonOwner));
}
if !state.owns_vehicle(user.account_id, params.tank_id).await? {
if state.get_battle_count(user.account_id, params.tank_id).await? == 0 {
return Err(WebError::ImATeapot);
}

info!(?rating);
if let Some(rating) = rating {
let n_battles = state.get_battle_count(user.account_id, params.tank_id).await?;
state
.vote_manager
.insert(&Vote::new(user.account_id, params.tank_id, rating))
.insert(&Vote::new(user.account_id, params.tank_id, n_battles, rating))
.await?;
} else {
state.vote_manager.delete(user.account_id, params.tank_id).await?;
Expand Down
4 changes: 0 additions & 4 deletions src/wg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ pub struct VehicleStats {
}

impl VehicleStats {
pub const fn is_played(&self) -> bool {
self.inner.n_battles != 0
}

/// Deserialize last battle time and take care of missing timestamps in the response.
#[inline]
fn deserialize_last_battle_time<'de, D: Deserializer<'de>>(
Expand Down

0 comments on commit 0205b43

Please sign in to comment.