diff options
author | Julien Dessaux | 2022-11-22 22:55:36 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-11-22 22:55:36 +0100 |
commit | 038c877aed55cffdb4401a4c7e7b15b113798145 (patch) | |
tree | e2902779e76ba8bd60b63be0e512a3c112530d68 /controllers/games/root.js | |
parent | Added basic games handling (diff) | |
download | jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.tar.gz jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.tar.bz2 jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.zip |
Implemented game creation
Diffstat (limited to 'controllers/games/root.js')
-rw-r--r-- | controllers/games/root.js | 69 |
1 files changed, 61 insertions, 8 deletions
diff --git a/controllers/games/root.js b/controllers/games/root.js index ece7b74..7406815 100644 --- a/controllers/games/root.js +++ b/controllers/games/root.js @@ -1,13 +1,66 @@ -import { listGames } from "../../database/games.js"; +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"; + +function makePageData(user) { + return { + title: "Parties", + user: user, + games: listGames(user.id), + formdata: { + name: "", + username: "", + }, + errors: {}, + }; +} export function root_get(req, res) { - const data = { - title: "Liste des parties", - user: req.session.user, - games: listGames(req.session.user.id), + let page = makePageData(req.session.user); + for (let i=0; i<page.games.length; i++) { + page.games[i].data = JSON.parse(page.games[i].data); + } + return res.render("games", page); +} + +function makeNewGameData(name, player1, player2) { + let bag = makeLettersBag(); + return { + board: emptyBoard, + name: name, + player1: { + id: player1.id, + username: player1.username, + score: 0, + letters: pickLetters(bag, 7), + }, + player2: { + id: player2.id, + username: player2.username, + score: 0, + letters: pickLetters(bag, 7), + }, }; - for (let i=0; i<data.games.length; i++) { - data.games[i].data = JSON.parse(data.games[i].data); +} + +export function root_post(req, res) { + let page = makePageData(req.session.user); + page.formdata = req.body; + page.errors = validationResult(req).mapped(); + if (Object.keys(page.errors).length === 0) { + const player2 = getUserByUsername(page.formdata.username); + if (player2) { + const gameId = newGame(req.session.user.id, player2.id, makeNewGameData(page.formdata.name, req.session.user, player2)); + if (gameId) { + return res.redirect(302, `/games/${gameId}`); + } else { + page.errors.mismatch = "Erreur du serveur: la création de partie a échoué"; + } + } else { + page.errors.username = { msg: "L'identifiant n'existe pas." }; + } } - return res.render("games", data); + return res.render("games", page); } |