From 3121f9f60886b291103bfb10b26803196b7e4e6b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 31 Dec 2022 18:13:35 +0100 Subject: Refactored game creation --- controllers/games/root.js | 8 +++---- utils/board.js | 59 +++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/controllers/games/root.js b/controllers/games/root.js index 7406815..0483f55 100644 --- a/controllers/games/root.js +++ b/controllers/games/root.js @@ -2,7 +2,7 @@ import { validationResult } from "express-validator"; import { getUserByUsername } from "../../database/users.js"; import { listGames, newGame } from "../../database/games.js"; -import { emptyBoard, makeLettersBag, pickLetters, } from "../../utils/board.js"; +import { emptyBoard, Bag } from "../../utils/board.js"; function makePageData(user) { return { @@ -26,7 +26,7 @@ export function root_get(req, res) { } function makeNewGameData(name, player1, player2) { - let bag = makeLettersBag(); + let bag = new Bag(); return { board: emptyBoard, name: name, @@ -34,13 +34,13 @@ function makeNewGameData(name, player1, player2) { id: player1.id, username: player1.username, score: 0, - letters: pickLetters(bag, 7), + letters: bag.pick(7), }, player2: { id: player2.id, username: player2.username, score: 0, - letters: pickLetters(bag, 7), + letters: bag.pick(7), }, }; } diff --git a/utils/board.js b/utils/board.js index 72c7951..f9a3b9d 100644 --- a/utils/board.js +++ b/utils/board.js @@ -18,9 +18,9 @@ export const emptyBoard = [ export const letters_total = 102; -export function makeLettersBag () { - return { - letters : { +export class Bag { + constructor() { + this.letters = { JOKER:{count:2, points:0 }, E:{count:15, points:1}, A:{count:9, points:1}, I:{count:9, points:1}, N:{count:6, points:1}, O:{count:6, points:1}, R:{count:6, points:1}, S:{count:6, points:1}, T:{count:6, points:1}, U:{count:6, points:1}, L:{count:5, points:1}, D:{count:3, points:2}, G:{count:2, points:2}, M:{count:3, points:2}, @@ -28,35 +28,34 @@ export function makeLettersBag () { F:{count:2, points:4}, H:{count:2, points:4}, V:{count:2, points:4}, J:{count:1, points:8}, Q:{count:1, points:8}, K:{count:1, points:10}, W:{count:1, points:10}, X:{count:1, points:10}, Y:{count:1, points:10}, Z:{count:1, points:10}, - }, - remaining: letters_total, - }; -} - -const allLetters = Object.keys(makeLettersBag().letters); - -export function pickLetters(bag, count) { - if (count > bag.remaining) { - count = bag.remaining; + }; + this.remaining = letters_total; } - let ret = []; - for (let i=0; i this.remaining) { + count = this.remaining; + } + let ret = []; + for (let i=0; i