0
0
Fork 0

Begin working on support for solving the puzzle entirely

This commit is contained in:
Oliver-Akins 2023-02-20 19:57:58 -07:00
parent 29e7e2a297
commit ca92d9131a

View file

@ -1,5 +1,6 @@
import { addLetter, spacePhrase } from "$/utils/game";
import { channelSchema } from "$/schemas/general";
import { config, database } from "$/main"; import { config, database } from "$/main";
import { addLetter } from "$/utils/game";
import { ServerRoute } from "@hapi/hapi"; import { ServerRoute } from "@hapi/hapi";
import Joi from "joi"; import Joi from "joi";
@ -8,36 +9,58 @@ const route: ServerRoute = {
options: { options: {
validate: { validate: {
params: Joi.object({ params: Joi.object({
channel: Joi.string().alphanum(), channel: channelSchema,
}), }),
payload: Joi.object({ payload: Joi.object({
type: Joi.string().valid(`letter`, `solve`),
guess: Joi.string().length(1), guess: Joi.string().length(1),
}), }),
}, },
}, },
async handler(request) { async handler(request) {
// @ts-ignore // @ts-ignore
const guess = request.payload.guess.toUpperCase(); let { guess, type } = request.payload;
guess = guess.toUpperCase();
const { channel } = request.params; const { channel } = request.params;
let data = await database.getChannel(channel); let data = await database.getChannel(channel);
if (data.key[guess] != null) { let won = false;
data.current = addLetter(data.key, data.current, guess);
/* // The user is guessing a single letter
The player(s) won the game, so we want to tell the overlay(s) and if (type === `letter`) {
then respond to the request. if (data.key[guess] != null) {
*/ data.current = addLetter(data.key, data.current, guess);
if (data.current == data.solution) { won = data.current == data.solution;
request.server.app.io.to(channel).emit(`finish`, {
win: true,
solution: data.solution,
});
return `Congrats! You won. Merry Chatmanmas! Answer: ${data.current}`;
}; };
} else { }
data.incorrect++;
// The user is guessing the entire solution
else if (type === `solve`) {
guess = spacePhrase(guess).replace(/[^a-z█] /gi, ``);
let solved = data.solution.replace(/[^a-z█] /gi, ``);
if (guess === solved) {
won = true;
} else {
data.incorrect += config.game.incorrect_solve_penalty;
};
};
/*
The player(s) won the game, so we want to tell the overlay(s) and
then respond to the request.
*/
if (won) {
request.server.app.io.to(channel).emit(`finish`, {
win: true,
solution: data.solution,
});
// NOTE: This might cause reference issues with the return
await database.resetChannel(channel);
return `Congrats! You won. Merry Chatmanmas! Answer: ${data.solution}`;
}; };
@ -50,10 +73,20 @@ const route: ServerRoute = {
win: false, win: false,
solution: data.solution, solution: data.solution,
}); });
// NOTE: This might cause reference issues with the return
await database.resetChannel(channel);
return `Oop, you ded. Answer: ${data.solution}`; return `Oop, you ded. Answer: ${data.solution}`;
}; };
// Update all the overlays for the channel
// clamp the incorrect value to prevent weird behaviours in overlays
data.incorrect = Math.min(data.incorrect, config.game.max_incorrect);
/*
Update all the overlays for the channel and tell the user the new game
state.
*/
request.server.app.io.to(channel).emit(`update`, { request.server.app.io.to(channel).emit(`update`, {
current: data.current, current: data.current,
incorrect: { incorrect: {