summaryrefslogtreecommitdiff
path: root/database/games.js
blob: dae3ce6f1b83489fb88ba7eb4b222edec1549bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import db from "./db.js";

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;");
const newGameStatement = db.prepare("INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);");

export function getGame(id) {
	try {
		return getGameStatement.get(id);
	} catch (err) {
		console.log(err);
		return null;
	}
}

export function listGames(userId) {
	try {
		return listGamesStatement.all({ 1: userId });
	} 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;
	}
}