Created
September 22, 2023 21:16
-
-
Save thawkin3/9a7d3bba991755b5b83bb4f31095d659 to your computer and use it in GitHub Desktop.
Ping Pong Ranking App - Player History
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 Inputs = { | |
playerID: string; | |
}; | |
type Match = { | |
matchID: string; | |
winnerID: string; | |
loserID: string; | |
}; | |
type Matches = { | |
[key: string]: Match; | |
}; | |
type FormattedMatch = { | |
matchID: string; | |
opponent: string; | |
result: "Won" | "Lost"; | |
}; | |
export async function handler({ playerID }: Inputs) { | |
const allMatches: Matches = await Zipper.storage.getAll(); | |
const matchesArray: Match[] = Object.values(allMatches); | |
const playerMatches = matchesArray.filter((match: Match) => { | |
return playerID === match.winnerID || playerID === match.loserID; | |
}); | |
const formattedPlayerMatches = playerMatches.map((match: Match) => { | |
const formattedMatch: FormattedMatch = { | |
matchID: match.matchID, | |
opponent: playerID === match.winnerID ? match.loserID : match.winnerID, | |
result: playerID === match.winnerID ? "Won" : "Lost", | |
}; | |
return formattedMatch; | |
}); | |
return formattedPlayerMatches; | |
} | |
export const config: Zipper.HandlerConfig = { | |
description: { | |
title: "Player History", | |
subtitle: "See match history for the selected player", | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment