Skip to content

Commit

Permalink
Fix sorting events on the client (#47)
Browse files Browse the repository at this point in the history
(k1 < k2) should have been (k1 < k2 ? -1 : 1), but Array.sort() has been
stable for a long time. We don't compare keys on the server side, so
just remove this on the client side as well.

Also, change tiebreak in the leaderboard as suggested in[1]. Instead of
comparing user IDs, which are arbitrary, compare the times that they
found the last set.

Fixes #46

[1] ekzhang/setwithfriends#153
  • Loading branch information
eltoder authored Nov 30, 2024
1 parent 84bb130 commit 2b8ec71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/pages/GamePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,16 @@ function GamePage({ match }) {
const spectating = !game.users || !(user.id in game.users);
const maxNumHints = gameMode === "ultraset" ? 4 : 3;

const { current, scores, history, boardSize } = computeState(
const { current, scores, lastEvents, history, boardSize } = computeState(
gameData,
gameMode
);

const leaderboard = Object.keys(game.users).sort((u1, u2) => {
const s1 = scores[u1] || 0;
const s2 = scores[u2] || 0;
if (s1 !== s2) return s2 - s1;
return u1 < u2 ? -1 : 1;
});
const leaderboard = Object.keys(game.users).sort(
(u1, u2) =>
(scores[u2] || 0) - (scores[u1] || 0) ||
(lastEvents[u1] || 0) - (lastEvents[u2] || 0)
);

function handleSet(cards) {
const event =
Expand Down
22 changes: 15 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ function removeCards(internalGameState, cards) {
}

function processValidEvent(internalGameState, event, cards) {
const { scores, history } = internalGameState;
const { scores, lastEvents, history } = internalGameState;
scores[event.user] = (scores[event.user] || 0) + 1;
lastEvents[event.user] = event.time;
history.push(event);
removeCards(internalGameState, cards);
}
Expand Down Expand Up @@ -300,21 +301,22 @@ export function computeState(gameData, gameMode = "normal") {
const used = {}; // set of cards that have been taken
const history = []; // list of valid events in time order
const current = gameData.deck.slice(); // remaining cards in the game
const lastEvents = {}; // time of the last event for each user
const internalGameState = {
used,
current,
scores,
history,
lastEvents,
// Initial deck split
boardSize: splitDeck(current, gameMode, 12, [])[0].length,
};

if (gameData.events) {
const events = Object.entries(gameData.events)
.sort(([k1, e1], [k2, e2]) => {
return e1.time !== e2.time ? e1.time - e2.time : k1 < k2;
})
.map(([_k, e]) => e);
// Array.sort() is guaranteed to be stable in since around 2018
const events = Object.values(gameData.events).sort(
(e1, e2) => e1.time - e2.time
);
const processFn =
gameMode === "normal"
? processEventNormal
Expand All @@ -328,7 +330,13 @@ export function computeState(gameData, gameMode = "normal") {
}
}

return { current, scores, history, boardSize: internalGameState.boardSize };
return {
current,
scores,
history,
lastEvents,
boardSize: internalGameState.boardSize,
};
}

export function formatTime(t, hideSubsecond) {
Expand Down

0 comments on commit 2b8ec71

Please sign in to comment.