Skip to content

Commit

Permalink
fix local highscore
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed May 31, 2024
1 parent 986669c commit 6df074e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,25 +397,27 @@ impl LoadedBoard {
log::debug!("Reloading local scores with a new score: {:?}", score);
let mut highscores: Vec<SavedScore> =
preferences::load(crate::HIGHSCORES_STORAGE).unwrap_or_default();

let mut save = false;
if let Some(highscore) = highscores
self.local_high = if let Some(highscore) = highscores
.iter_mut()
.find(|s| s.meta.category == self.category)
.find(|s| s.level == self.level && s.meta.category == self.category)
{
if let Some(score) = score {
if score.score > highscore.score && score.meta.category == highscore.meta.category {
*highscore = score.clone();
save = true;
}
}
self.local_high = Some(highscore.clone());
Some(highscore.clone())
} else if let Some(score) = score {
highscores.push(score.clone());
save = true;
self.local_high = Some(score.clone());
Some(score.clone())
} else {
self.local_high = None;
}
None
};

if save {
preferences::save(crate::HIGHSCORES_STORAGE, &highscores);
}
Expand Down
8 changes: 5 additions & 3 deletions src/render/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,11 @@ impl UiRender {

self.draw_quad(leaderboard.separator.position, theme.light, framebuffer);

self.draw_text(&leaderboard.highscore.rank, framebuffer);
self.draw_text(&leaderboard.highscore.player, framebuffer);
self.draw_text(&leaderboard.highscore.score, framebuffer);
if leaderboard.highscore.state.visible {
self.draw_text(&leaderboard.highscore.rank, framebuffer);
self.draw_text(&leaderboard.highscore.player, framebuffer);
self.draw_text(&leaderboard.highscore.score, framebuffer);
}
}

pub fn draw_toggle_button(
Expand Down

0 comments on commit 6df074e

Please sign in to comment.