Created
September 22, 2023 21:15
-
-
Save thawkin3/022307c9169c9965fdc148988093f562 to your computer and use it in GitHub Desktop.
Ping Pong Ranking App - Leaderboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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