Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Created September 22, 2023 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thawkin3/022307c9169c9965fdc148988093f562 to your computer and use it in GitHub Desktop.
Save thawkin3/022307c9169c9965fdc148988093f562 to your computer and use it in GitHub Desktop.
Ping Pong Ranking App - Leaderboard
type PlayerRecord = {
playerID: string;
losses: number;
wins: number;
};
type PlayerRecords = {
[key: string]: PlayerRecord;
};
type Match = {
matchID: string;
winnerID: string;
loserID: string;
};
type Matches = {
[key: string]: Match;
};
export async function handler() {
const allMatches: Matches = await Zipper.storage.getAll();
const matchesArray: Match[] = Object.values(allMatches);
const players: PlayerRecords = {};
matchesArray.forEach((match: Match) => {
const { loserID, winnerID } = match;
if (players[loserID]) {
players[loserID].losses++;
} else {
players[loserID] = {
playerID: loserID,
losses: 0,
wins: 0,
};
}
if (players[winnerID]) {
players[winnerID].wins++;
} else {
players[winnerID] = {
playerID: winnerID,
losses: 0,
wins: 0,
};
}
});
return Object.values(players);
}
export const config: Zipper.HandlerConfig = {
run: true,
description: {
title: "Leaderboard",
subtitle: "See player rankings for all recorded matches",
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment