summaryrefslogtreecommitdiff
path: root/database/games.js
diff options
context:
space:
mode:
authorJulien Dessaux2022-11-22 22:55:36 +0100
committerJulien Dessaux2022-11-22 22:55:36 +0100
commit038c877aed55cffdb4401a4c7e7b15b113798145 (patch)
treee2902779e76ba8bd60b63be0e512a3c112530d68 /database/games.js
parentAdded basic games handling (diff)
downloadjeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.tar.gz
jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.tar.bz2
jeux-de-mots-038c877aed55cffdb4401a4c7e7b15b113798145.zip
Implemented game creation
Diffstat (limited to '')
-rw-r--r--database/games.js25
1 files changed, 14 insertions, 11 deletions
diff --git a/database/games.js b/database/games.js
index b378eb2..dae3ce6 100644
--- a/database/games.js
+++ b/database/games.js
@@ -1,21 +1,14 @@
import db from "./db.js";
-const createGameStatement = db.prepare("INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);");
const getGameStatement = db.prepare("SELECT * from games where id = ?;");
const listGamesStatement = db.prepare("SELECT * from games where player1 = ?1 OR player2 = ?1 ORDER BY last_move_at;");
-
-export function createGame(player1, player2, data) {
- try {
- return createGameStatement.run(player1, player2, data).lastInsertRowId;
- } catch {
- return null;
- }
-}
+const newGameStatement = db.prepare("INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);");
export function getGame(id) {
try {
return getGameStatement.get(id);
- } catch {
+ } catch (err) {
+ console.log(err);
return null;
}
}
@@ -23,7 +16,17 @@ export function getGame(id) {
export function listGames(userId) {
try {
return listGamesStatement.all({ 1: userId });
- } catch {
+ } catch (err) {
+ console.log(err);
return [];
}
}
+
+export function newGame(player1, player2, data) {
+ try {
+ return newGameStatement.run(player1, player2, JSON.stringify(data)).lastInsertRowid;
+ } catch (err) {
+ console.log(err);
+ return null;
+ }
+}