Skip to content

Commit

Permalink
Fix local score calculation for localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jggoebel committed May 31, 2024
1 parent f2723ef commit 33e8488
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/app/services/score.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class ScoreService {
private findLocalScores(leaderboard: Leaderboard, newScore: Score) {
// Create a deep copy of the scores and add the new score for sorting and placement
const scores = [...leaderboard.scores];
scores.push(newScore);

// Sort the scores array by score in descending order
scores.sort((a, b) => b.score - a.score);
Expand All @@ -155,12 +156,21 @@ export class ScoreService {

// Get two scores above the new score (if available)
if (placement >= 10) {
localScores = localScores.concat(
scores.slice((Math.max(10, placement - 2), placement - 1)),
);
localScores = localScores.concat(
scores.slice(placement + 1, Math.min(scores.length, placement + 1)),
);
for (
let i = placement - 1;
i >= 0 && i >= placement - 2 && i >= 10;
i--
) {
localScores.push(scores[i]);
}

for (
let i = placement + 1;
i < scores.length && localScores.length < 4;
i++
) {
localScores.push(scores[i]);
}
}

// Return the updated leaderboard object with localScores and placement
Expand Down
2 changes: 1 addition & 1 deletion src/app/terminals/bashbrawl/bashbrawlterminal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class BashbrawlterminalComponent implements OnInit, AfterViewInit {
private TERMINAL_WHITESPACE_DELAY = 2;

// Game related
private DEFAULT_GAME_TIME = 60;
private DEFAULT_GAME_TIME = 3;
private gameLanguage: string;
private gameRunning = false;
private commandsEntered: string[] = [];
Expand Down

0 comments on commit 33e8488

Please sign in to comment.