From df2c6c95bdf71d8d355f06de1faef38084463e50 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Tue, 26 May 2020 11:56:07 +0200 Subject: [PATCH] Beginning stats calculation --- package-lock.json | 6 + package.json | 1 + src/controllers/statsController.ts | 185 ++++++++++++++++++++++++----- 3 files changed, 161 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8c7af84..9de231a 100755 --- a/package-lock.json +++ b/package-lock.json @@ -9004,6 +9004,12 @@ "integrity": "sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=", "dev": true }, + "prettier": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", + "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", + "dev": true + }, "pretty-hrtime": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", diff --git a/package.json b/package.json index fe399a2..1e93b31 100755 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "concurrently": "^5.2.0", "gulp": "^3.9.1", "nodemon": "^2.0.4", + "prettier": "^2.0.5", "semantic-ui": "^2.4.2", "ts-loader": "^7.0.5", "tslint": "^6.1.1", diff --git a/src/controllers/statsController.ts b/src/controllers/statsController.ts index 5d39750..97dfa93 100755 --- a/src/controllers/statsController.ts +++ b/src/controllers/statsController.ts @@ -1,44 +1,167 @@ -import passport from "passport"; -import DbUser, {IDbUser, IDbUserDoc} from "../models/dbUser"; -import {RequestHandler} from "express"; -import SavedGame from "../models/savedGame"; -import Player, {IPlayer} from "../models/player"; +import DbUser, { IDbUser } from "../models/dbUser"; +import { RequestHandler } from "express"; +import Player, { IPlayer } from "../models/player"; + +const DEFAULT_RULESET = "DEFAULT_RULESET"; export interface GameSubmission { - players: {id: string, nick: string}[], - results: GameResults[]; + ruleset: string; + players: { id: string; nick: string }[]; + results: GameResults[]; } interface GameResults { - playerId: string; - blocks: any; + playerId: string; + blocks: Record; } +type BlockName = "top" | "bottom"; + +interface Block { + cells: Record; +} + +type CellName = + | "aces" + | "twos" + | "threes" + | "fours" + | "fives" + | "sixes" + | "three_kind" + | "four_kind" + | "full_house" + | "sml_straight" + | "lg_straight" + | "yahtzee" + | "chance"; + +interface StandardCell { + value: CellValue; +} + +type CellValue = number | boolean | "cellFlagStrike"; + export const listGames: RequestHandler = async (req, res) => { - const user = (req.user as IDbUser); - const dbUser = await DbUser.findById(user.id, {"savedGames._id": 1, "savedGames.results": 1, "savedGames.createdAt": 1}); - if (dbUser) { - res.json({games: dbUser.savedGames}); - } - else { - res.sendStatus(404); - } + const user = req.user as IDbUser; + const dbUser = await DbUser.findById(user.id, { + "savedGames._id": 1, + "savedGames.results": 1, + "savedGames.createdAt": 1, + }); + if (dbUser) { + res.json({ games: dbUser.savedGames }); + } else { + res.sendStatus(404); + } }; export const saveGame: RequestHandler = async (req, res) => { - const user = (req.user as IDbUser); - const submission = (req.body as GameSubmission); - for (let player of submission.players) { - if (player.id === player.nick) { - const newGuest = await user.addGuest(player.nick); - player.id = newGuest.id; - const playerResult = submission.results.find(result => result.playerId === player.nick); - playerResult!.playerId = player.id; - } - } - const newGame = await user.addGame(submission); - res.send({message: "Game submitted successfully!", newGame: newGame}); + const user = req.user as IDbUser; + const submission = req.body as GameSubmission; + await addNewGuestsFromSubmissionAndFillInIds(submission, user); + const newGame = await user.addGame(submission); + if (submission.ruleset === DEFAULT_RULESET) { + processStandardStatistics(submission.results, user); + } + console.log(JSON.stringify(req.body)); + res.send({ message: "Game submitted successfully!", newGame: newGame }); }; -const processStatistics = (results: GameResults, account: IDbUser) => { -}; \ No newline at end of file +async function addNewGuestsFromSubmissionAndFillInIds( + submission: GameSubmission, + user: IDbUser +): Promise { + for (const playerInParticipantList of submission.players) { + if (playerInParticipantList.id === playerInParticipantList.nick) { + const newGuest = await user.addGuest(playerInParticipantList.nick); + const gameResultsFromNewGuest = submission.results.find( + (result) => result.playerId === playerInParticipantList.nick + ); + gameResultsFromNewGuest!.playerId = newGuest.id; + playerInParticipantList.id = newGuest.id; + } + } +} + +function processStandardStatistics(results: GameResults[], account: IDbUser) { + let runnerUp: IPlayer; + const drawnPlayers: IPlayer[] = []; + const scoredResults: { pid: string; score: number }[] = []; + for (const result of results) { + Player.updatePlayerStats(result.playerId, result); + scoredResults.push({pid: result.playerId, score: calculateStandardScore(result)}); + } + const winner = Math.max(...scoredResults.map(result => result.score)); + //winner.incrementWin(); + //runnerUp.incrementRunnerUp(); + //drawnPlayers.forEach(player => player.incrementDraw(); +} + +function calculateStandardScore(result: GameResults): number { + const top = result.blocks.top.cells; + const bottom = result.blocks.bottom.cells; + return ( + cellScore(top.aces) + + cellScore(top.twos) * 2 + + cellScore(top.threes) * 3 + + cellScore(top.fours) * 4 + + cellScore(top.fives) * 5 + + cellScore(top.sixes) * 6 + + cellScore(bottom.three_kind) + + cellScore(bottom.four_kind) + + cellScore(bottom.full_house) * 25 + + cellScore(bottom.sml_straight) * 30 + + cellScore(bottom.lg_straight) * 40 + + cellScore(bottom.yahtzee) * 50 + + cellScore(bottom.chance) + ); +} + +function cellScore(cell: StandardCell) { + if (cell.value === "cellFlagStrike" || cell.value === false) { + return 0; + } else if (cell.value === true) { + return 1; + } + return cell.value; +} + +const example = { + players: [ + { + id: "5ecbf33a9d246114c0c9d9bb", + nick: "Ledda", + }, + ], + results: [ + { + playerId: "5ecbf33a9d246114c0c9d9bb", + blocks: [ + { + id: "top", + cells: [ + { id: "aces", value: 1 }, + { id: "twos", value: 1 }, + { id: "threes", value: 1 }, + { id: "fours", value: 1 }, + { id: "fives", value: 1 }, + { id: "sixes", value: 1 }, + ], + }, + { + id: "bottom", + cells: [ + { id: "three_kind", value: "cellFlagStrike" }, + { id: "four_kind", value: "cellFlagStrike" }, + { id: "full_house", value: true }, + { id: "sml_straight", value: true }, + { id: "lg_straight", value: true }, + { id: "yahtzee", value: 1 }, + { id: "chance", value: "cellFlagStrike" }, + ], + }, + ], + }, + ], +};