From 35d36aa06bb8bac1945ad6a4c41cc61c082a4083 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 3 Sep 2023 11:46:51 -0600 Subject: [PATCH] Add an endpoint to end a game prematurely --- src/endpoints/end_game.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/endpoints/end_game.ts diff --git a/src/endpoints/end_game.ts b/src/endpoints/end_game.ts new file mode 100644 index 0000000..0ae78c1 --- /dev/null +++ b/src/endpoints/end_game.ts @@ -0,0 +1,31 @@ +import { channelSchema } from "$/schemas/general"; +import { ServerRoute } from "@hapi/hapi"; +import { database, log } from "$/main"; +import Joi from "joi"; + +const route: ServerRoute = { + method: `DELETE`, path: `/{channel}/game`, + options: { + validate: { + params: Joi.object({ + channel: channelSchema, + }), + }, + }, + async handler(request) { + const { channel } = request.params; + log.info(`Ending game for channel: ${channel}`); + + await database.resetChannel(channel); + + // Tell the overlay(s) to end the game + request.server.app.io.to(channel).emit(`finish`, { + active: false, + win: null, + solution: null, + }); + + return `Reset the game, start a new game to keep playing`; + }, +}; +export default route;