From 038c877aed55cffdb4401a4c7e7b15b113798145 Mon Sep 17 00:00:00 2001
From: Julien Dessaux
Date: Tue, 22 Nov 2022 22:55:36 +0100
Subject: Implemented game creation
---
controllers/games/gameId.js | 21 +++++++++-----
controllers/games/root.js | 69 +++++++++++++++++++++++++++++++++++++++------
database/000_init.sql | 1 +
database/001_games.sql | 1 +
database/games.js | 25 ++++++++--------
database/users.js | 18 ++++++++++--
routes/games.js | 5 +++-
routes/root.js | 10 +------
utils/board.js | 63 +++++++++++++++++++++++++++++++++++++++++
utils/checks.js | 15 ++++++++++
views/game.ejs | 6 ++--
views/games.ejs | 26 +++++++++++++++--
12 files changed, 215 insertions(+), 45 deletions(-)
create mode 100644 utils/board.js
create mode 100644 utils/checks.js
diff --git a/controllers/games/gameId.js b/controllers/games/gameId.js
index c87a857..d3363fd 100644
--- a/controllers/games/gameId.js
+++ b/controllers/games/gameId.js
@@ -1,17 +1,24 @@
import { getGame } from "../../database/games.js";
-function makePageData(user, cwdata) {
+function makePageData(user, game) {
return {
title: "Jouer",
user: user,
- CWDATA: cwdata,
+ data: game,
};
}
export function gameId_get(req, res) {
- const game = getGame(req.params.gameId);
- // TODO redirect if null
- let cwdata = game; // TODO reformat this object
- console.log(cwdata);
- return res.render("game", makePageData(req.session.user, cwdata));
+ const game = JSON.parse(getGame(req.params.gameId).data);
+ if (game) {
+ if (game.player1.id === req.session.user.id) {
+ game.letters = game.player1.letters;
+ delete game.player2.letters;
+ } else {
+ game.letters = game.player2.letters;
+ delete game.player1.letters;
+ }
+ return res.render("game", makePageData(req.session.user, game));
+ }
+ return res.redirect("/games");
}
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
-player one name: 0
-player two name: 0
+<%- data.player1.username %>: <%- data.player1.score %>
+<%- data.player2.username %>: <%- data.player2.score %>