1 const mariadb = require('mariadb');
8 const pool = mariadb.createPool(dsn);
10 async function getMaxUpdatedState(session_id) {
13 conn = await pool.getConnection();
14 return await conn.query(`
17 UNIX_TIMESTAMP(max_updated_at) AS last_updated
18 FROM go.state g JOIN (
19 SELECT MAX(updated_at) AS max_updated_at
23 ON x.max_updated_at = g.updated_at
32 async function deleteSession(session_id) {
35 conn = await pool.getConnection();
36 return await conn.query(
37 "DELETE FROM go.state WHERE session_id = ?",
47 async function initBoard() {
50 conn = await pool.getConnection();
51 return await conn.query(`
52 CREATE TABLE IF NOT EXISTS
54 session_id INT UNSIGNED,
57 state TINYINT UNSIGNED,
58 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
59 ON UPDATE CURRENT_TIMESTAMP,
60 PRIMARY KEY(session_id, x, y)
70 async function addMove(session_id, pos_x, pos_y, state) {
73 conn = await pool.getConnection();
74 return await conn.query(`
75 INSERT INTO go.state (session_id, x, y, state)
77 ON DUPLICATE KEY UPDATE
78 state = VALUES(state);
79 `, [session_id, pos_x, pos_y, state]);
87 async function getBoardState(session_id) {
90 conn = await pool.getConnection();
91 return await conn.query(`
92 SELECT x, y, state from go.state where session_id = ?
101 exports.getMaxUpdatedState = getMaxUpdatedState;
102 exports.deleteSession = deleteSession;
103 exports.initBoard = initBoard;
104 exports.addMove = addMove;
105 exports.getBoardState = getBoardState;