0
0
Fork 0

Allow multiple word lists

This commit is contained in:
Oliver-Akins 2023-01-19 12:47:22 -06:00
parent 1764eb9c53
commit 0d1a7e12f8
2 changed files with 23 additions and 4 deletions

View file

@ -1,6 +1,8 @@
import { database } from "$/main";
import { anonymizePhrase, convertToKey, spacePhrase } from "$/utils/game"; import { anonymizePhrase, convertToKey, spacePhrase } from "$/utils/game";
import { config, database } from "$/main";
import { ServerRoute } from "@hapi/hapi"; import { ServerRoute } from "@hapi/hapi";
import { readFileSync } from "fs";
import boom from "@hapi/boom";
import Joi from "joi"; import Joi from "joi";
const route: ServerRoute = { const route: ServerRoute = {
@ -10,15 +12,24 @@ const route: ServerRoute = {
params: Joi.object({ params: Joi.object({
channel: Joi.string().alphanum(), channel: Joi.string().alphanum(),
}), }),
query: Joi.object({
word_list: Joi.string(),
}),
}, },
}, },
async handler(request) { async handler(request) {
const { channel } = request.params; const { channel } = request.params;
const { word_list } = request.query;
let data = await database.getChannel(channel); let data = await database.getChannel(channel);
// TODO: Get the proper phrase
let phrase = "Hello world"; if (config.game.files[word_list] == null) {
throw boom.notAcceptable(`Invalid word list`);
};
let phrases = readFileSync(config.game.files[word_list] as string, `utf-8`).split(`\n`);
let phrase = phrases[Math.floor(Math.random() * phrases.length)].trim();
let spaced = spacePhrase(phrase.toUpperCase()); let spaced = spacePhrase(phrase.toUpperCase());
let anonymized = anonymizePhrase(spaced); let anonymized = anonymizePhrase(spaced);
@ -27,7 +38,7 @@ const route: ServerRoute = {
data.incorrect = 0; data.incorrect = 0;
data.key = convertToKey(spaced); data.key = convertToKey(spaced);
return `${data.current} (incorrect: ${data.incorrect}/6)`; return `${data.current} (incorrect: ${data.incorrect}/${config.game.max_incorrect})`;
}, },
}; };
export default route; export default route;

View file

@ -1,6 +1,13 @@
import Joi from "joi"; import Joi from "joi";
export const gameOptionsSchema = Joi.object({
files: Joi.object().required().min(1).unknown(true),
max_incorrect: Joi.number().min(1).default(6),
})
.meta({ className: `gameOptions` })
.description(`The game-specific options`);
export const serverOptionsSchema = Joi.object({ export const serverOptionsSchema = Joi.object({
port: Joi port: Joi
.number() .number()
@ -25,6 +32,7 @@ export const databaseOptionsSchema = Joi.object({
export const configSchema = Joi.object({ export const configSchema = Joi.object({
server: serverOptionsSchema.required(), server: serverOptionsSchema.required(),
database: databaseOptionsSchema.required(), database: databaseOptionsSchema.required(),
game: gameOptionsSchema.required(),
}) })
.meta({ className: `config` }) .meta({ className: `config` })
.description(`The configuration format for the server`); .description(`The configuration format for the server`);