diff options
author | Julien Dessaux | 2022-12-31 18:13:35 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-12-31 18:13:35 +0100 |
commit | 3121f9f60886b291103bfb10b26803196b7e4e6b (patch) | |
tree | 5cc4f3566b3c4a3c29700fbf861f1a5795985b37 | |
parent | Fixed game creation (diff) | |
download | jeux-de-mots-3121f9f60886b291103bfb10b26803196b7e4e6b.tar.gz jeux-de-mots-3121f9f60886b291103bfb10b26803196b7e4e6b.tar.bz2 jeux-de-mots-3121f9f60886b291103bfb10b26803196b7e4e6b.zip |
Refactored game creation
Diffstat (limited to '')
-rw-r--r-- | controllers/games/root.js | 8 | ||||
-rw-r--r-- | 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<count; i++) { - let n = Math.floor(Math.random() * bag.remaining); - let j = 0; - for (;;) { - if (bag.letters[allLetters[j]].count === 0) { - j++; - } else if (bag.letters[allLetters[j]].count < n) { - n -= bag.letters[allLetters[j]].count; - j++; - } else { - n = 0; - break; + pick(count) { + if (count > this.remaining) { + count = this.remaining; + } + let ret = []; + for (let i=0; i<count; i++) { + let n = Math.floor(Math.random() * this.remaining); + let j = 0; + for (;;) { + if (this.letters[allLetters[j]].count === 0) { + j++; + } else if (this.letters[allLetters[j]].count < n) { + n -= this.letters[allLetters[j]].count; + j++; + } else { + n = 0; + break; + } } + this.letters[allLetters[j]].count--; + this.remaining--; + ret.push(allLetters[j]); } - bag.letters[allLetters[j]].count--; - bag.remaining--; - ret.push(allLetters[j]); + return ret; } - return ret; } + +const allLetters = Object.keys(new Bag().letters); |