Created
September 22, 2023 21:14
-
-
Save thawkin3/5f8e4372c9143717f9b952482b7370fa to your computer and use it in GitHub Desktop.
Ping Pong Ranking App - Record a New Match
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 = { | |
playerOneID: string; | |
playerTwoID: string; | |
winnerID: string; | |
}; | |
export async function handler(inputs: Inputs) { | |
const { playerOneID, playerTwoID, winnerID } = inputs; | |
if (!playerOneID || !playerTwoID || !winnerID) { | |
return "Error: Please fill out all input fields."; | |
} | |
if (playerOneID === playerTwoID) { | |
return "Error: PlayerOne and PlayerTwo must have different IDs."; | |
} | |
if (winnerID !== playerOneID && winnerID !== playerTwoID) { | |
return "Error: Winner ID must match either PlayerOne ID or PlayerTwo ID"; | |
} | |
const matchID = Date.now().toString(); | |
const matchInfo = { | |
matchID, | |
winnerID, | |
loserID: winnerID === playerOneID ? playerTwoID : playerOneID, | |
}; | |
try { | |
await Zipper.storage.set(matchID, matchInfo); | |
return `Thanks for recording your match. Player ${winnerID} is the winner!`; | |
} catch (e) { | |
return `Error: Information was not written to the database. Please try again later.`; | |
} | |
} | |
export const config: Zipper.HandlerConfig = { | |
description: { | |
title: "Record New Ping Pong Match", | |
subtitle: "Enter who played and who won", | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment