0
0
Fork 0

Add finalize querystring for incrementing win streak and resetting vote counts

This commit is contained in:
Oliver-Akins 2021-08-30 16:57:24 -06:00
parent 32a4062ea0
commit 08084b0ab9

View file

@ -1,11 +1,20 @@
import { Request, ResponseToolkit } from "@hapi/hapi";
import { Request, ResponseToolkit, ServerRoute } from "@hapi/hapi";
import { config, db } from "@/main";
import Joi from "joi";
export default {
const route: ServerRoute = {
method: `GET`, path: `/{guild_id}/bracket/winners`,
options: {
validate: {
query: Joi.object({
finalize: Joi.boolean().optional().default(false),
}),
},
},
async handler(request: Request, h: ResponseToolkit) {
let gID = request.params.guild_id;
let data = db[gID].bracket;
let { finalize } = request.query;
let winners: quote[] = [];
let highest = -1;
@ -13,6 +22,11 @@ export default {
// Run through all of the quotes to find the most voted for ones
for (var quote of data.quotes) {
if (finalize) {
quote.win_streak++;
quote.votes = 0;
};
// New maximum, remove all previous winners
if (quote.votes > highest) {
winners = [ quote ];
@ -31,4 +45,5 @@ export default {
eliminate_all: count > Math.floor(config.guilds[gID].quote_max / 2),
}).code(200);
},
}
};
export default route;