Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Created September 22, 2023 21:14
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/5f8e4372c9143717f9b952482b7370fa to your computer and use it in GitHub Desktop.
Save thawkin3/5f8e4372c9143717f9b952482b7370fa to your computer and use it in GitHub Desktop.
Ping Pong Ranking App - Record a New Match
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