Add endpoints and game utilities
This commit is contained in:
parent
56f2195962
commit
6a174e4d36
7 changed files with 213 additions and 1 deletions
33
src/endpoints/guess_letter.ts
Normal file
33
src/endpoints/guess_letter.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { database } from "$/main";
|
||||
import { addLetter } from "$/utils/game";
|
||||
import { ServerRoute } from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
|
||||
const route: ServerRoute = {
|
||||
method: `POST`, path: `/{channel}/guess`,
|
||||
options: {
|
||||
validate: {
|
||||
params: Joi.object({
|
||||
channel: Joi.string().alphanum(),
|
||||
}),
|
||||
payload: Joi.object({
|
||||
guess: Joi.string().length(1),
|
||||
}),
|
||||
},
|
||||
},
|
||||
async handler(request) {
|
||||
// @ts-ignore
|
||||
const guess = request.payload.guess.toUpperCase();
|
||||
const { channel } = request.params;
|
||||
|
||||
let data = await database.getChannel(channel);
|
||||
console.log(data)
|
||||
if (data.solution.includes(guess)) {
|
||||
data.current = addLetter(data.key, data.current, guess);
|
||||
} else {
|
||||
data.incorrect++;
|
||||
};
|
||||
return `${data.current} (incorrect: ${data.incorrect}/6)`;
|
||||
},
|
||||
};
|
||||
export default route;
|
||||
33
src/endpoints/new_game.ts
Normal file
33
src/endpoints/new_game.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { database } from "$/main";
|
||||
import { anonymizePhrase, convertToKey, spacePhrase } from "$/utils/game";
|
||||
import { ServerRoute } from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
|
||||
const route: ServerRoute = {
|
||||
method: `POST`, path: `/{channel}/game`,
|
||||
options: {
|
||||
validate: {
|
||||
params: Joi.object({
|
||||
channel: Joi.string().alphanum(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
async handler(request) {
|
||||
const { channel } = request.params;
|
||||
|
||||
let data = await database.getChannel(channel);
|
||||
|
||||
// TODO: Get the proper phrase
|
||||
let phrase = "Hello world";
|
||||
|
||||
let spaced = spacePhrase(phrase.toUpperCase());
|
||||
let anonymized = anonymizePhrase(spaced);
|
||||
data.current = anonymized;
|
||||
data.solution = spaced;
|
||||
data.incorrect = 0;
|
||||
data.key = convertToKey(spaced);
|
||||
|
||||
return `${data.current} (incorrect: ${data.incorrect}/6)`;
|
||||
},
|
||||
};
|
||||
export default route;
|
||||
54
src/endpoints/nightbot.ts
Normal file
54
src/endpoints/nightbot.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { nightbotCustomHeadersSchema } from "$/schemas/nightbot";
|
||||
import { ServerRoute } from "@hapi/hapi";
|
||||
import boom from "@hapi/boom";
|
||||
import Joi from "joi";
|
||||
|
||||
const subcommands = {
|
||||
"start": {
|
||||
method: `POST`,
|
||||
modOnly: true,
|
||||
},
|
||||
"show": {
|
||||
method: `GET`,
|
||||
modOnly: false,
|
||||
},
|
||||
};
|
||||
|
||||
const route: ServerRoute = {
|
||||
method: `GET`, path: `/nightbot`,
|
||||
options: {
|
||||
validate: {
|
||||
query: Joi.object({
|
||||
args: Joi.string().allow("").required(),
|
||||
msg: Joi.string(),
|
||||
}),
|
||||
headers: nightbotCustomHeadersSchema.unknown(),
|
||||
},
|
||||
},
|
||||
async handler(request) {
|
||||
let args = (<string>request.query.args)
|
||||
.split(` `)
|
||||
.filter(x => x.length > 0);
|
||||
|
||||
if (args.length < 1) {
|
||||
return `Not enough arguments`;
|
||||
};
|
||||
|
||||
let channelData = new URLSearchParams(request.headers["nightbot-channel"]);
|
||||
let channel = channelData.get(`name`);
|
||||
let userData = new URLSearchParams(request.headers["nightbot-user"]);
|
||||
let user = userData.get(`name`);
|
||||
|
||||
if (!channel) { throw boom.badData(`Missing channel name`) };
|
||||
if (!user) { throw boom.badData(`Missing user name`) };
|
||||
|
||||
// User is guessing a letter
|
||||
if (args[0].length == 1) {
|
||||
return (await request.server.inject({
|
||||
method: `POST`, url: `/${channel}/guess`,
|
||||
payload: { guess: args[0] }
|
||||
})).payload;
|
||||
};
|
||||
},
|
||||
};
|
||||
export default route;
|
||||
20
src/endpoints/setup_channel.ts
Normal file
20
src/endpoints/setup_channel.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { database } from "$/main";
|
||||
import { ServerRoute } from "@hapi/hapi";
|
||||
import Joi from "joi";
|
||||
|
||||
const route: ServerRoute = {
|
||||
method: `POST`, path: `/channels`,
|
||||
options: {
|
||||
validate: {
|
||||
payload: Joi.object({
|
||||
channel: Joi.string().alphanum(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
async handler(request, h) {
|
||||
// @ts-ignore
|
||||
await database.createChannel(request.payload.channel);
|
||||
return h.close;
|
||||
},
|
||||
};
|
||||
export default route;
|
||||
Loading…
Add table
Add a link
Reference in a new issue