Skip to content

Commit

Permalink
Merge pull request #84 from varoonp123/fix/persistTotalShotsHit
Browse files Browse the repository at this point in the history
BUG FIX: persist total shots hit to compute accuracy across all games. It was accidentally getting dropped from the db
  • Loading branch information
varoonp123 authored Aug 17, 2023
2 parents 4f1ff20 + 9b4b1a4 commit d2de3ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
22 changes: 16 additions & 6 deletions crates/thetawave_storage/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,18 @@ mod test {
MobsKilledBy1PlayerCacheT::from([(EnemyMobType::Drone, N_DRONES)]),
);
}
fn set_n_games_lost_by_player_in_user_stats_cache<const N_GAMES_LOST: usize>(
fn set_user_stats_for_completed_games<
const N_GAMES_LOST: usize,
const TOTAL_SHOTS_HIT: usize,
const TOTAL_SHOTS_FIRED: usize,
>(
mut historical_user_stats: ResMut<UserStatsByPlayerForCompletedGamesCache>,
) {
(**historical_user_stats).insert(
DEFAULT_USER_ID,
UserStat {
total_shots_fired: 0,
total_shots_hit: 0,
total_shots_fired: TOTAL_SHOTS_FIRED,
total_shots_hit: TOTAL_SHOTS_HIT,
total_games_lost: N_GAMES_LOST,
},
);
Expand All @@ -204,6 +208,8 @@ mod test {
fn _test_can_flush_caches_to_db() {
const N_DRONES_KILLED: usize = 15;
const N_GAMES_PLAYED: usize = 2;
const TOTAL_SHOTS_HIT: usize = 10;
const TOTAL_SHOTS_FIRED: usize = 15;

let mob_kills_after_1_game =
MobKillsByPlayerForCompletedGames::from(MobsKilledByPlayerCacheT::from([(
Expand All @@ -216,7 +222,11 @@ mod test {
OnEnter(AppStates::Game),
(
set_n_drones_killed_for_p1_in_completed_games_cache::<N_DRONES_KILLED>,
set_n_games_lost_by_player_in_user_stats_cache::<N_GAMES_PLAYED>,
set_user_stats_for_completed_games::<
N_GAMES_PLAYED,
TOTAL_SHOTS_HIT,
TOTAL_SHOTS_FIRED,
>,
),
)
.add_systems(OnEnter(AppStates::Game), set_game_over_state)
Expand All @@ -229,8 +239,8 @@ mod test {
assert_eq!(
get_user_stats(DEFAULT_USER_ID).unwrap(),
UserStat {
total_shots_fired: 0,
total_shots_hit: 0,
total_shots_fired: TOTAL_SHOTS_FIRED,
total_shots_hit: TOTAL_SHOTS_HIT,
total_games_lost: N_GAMES_PLAYED,
}
);
Expand Down
9 changes: 5 additions & 4 deletions crates/thetawave_storage/src/user_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub(super) fn set_user_stats_for_user_id(
) -> Result<(), OurDBError> {
let stmt_raw = format!(
"
INSERT OR REPLACE INTO {USERSTAT} (userId, totalShotsFired, totalGamesLost)
VALUES (?1, ?2, ?3)
ON CONFLICT DO UPDATE SET totalShotsFired=?2, totalGamesLost=?3"
INSERT OR REPLACE INTO {USERSTAT} (userId, totalShotsFired, totalGamesLost, totalShotsHit)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT DO UPDATE SET totalShotsFired=?2, totalGamesLost=?3, totalShotsHit=?4"
);
let conn = get_db()?;
info!(
Expand All @@ -23,7 +23,8 @@ pub(super) fn set_user_stats_for_user_id(
conn.prepare(&stmt_raw)?.execute(params![
user_id,
user_stats.total_shots_fired,
user_stats.total_games_lost
user_stats.total_games_lost,
user_stats.total_shots_hit,
])?;
Ok(())
}
Expand Down

0 comments on commit d2de3ec

Please sign in to comment.