Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sorting events on the client #47

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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