node-go/packages/server/data/queries/game.js

98 lines
1.7 KiB
JavaScript
Raw Normal View History

const knex = require("../db");
2020-01-21 07:05:55 +00:00
const gameDetailSelect = [
"game.id",
"application",
"application_version",
"board_size",
"komi",
"handicap",
"open",
"win_type",
"player_black",
"player_black_rank",
"player_white",
"player_white_rank",
"captures_black",
"captures_white",
"score",
"win_type",
"description",
"event",
"round",
"name",
"room",
];
const timeSettingSelect = [
"main_time",
"time_period",
"period_length",
"overtime",
"overtime_period",
"overtime_length",
];
const gameOverviewSelect = [
"id",
"board_size",
"komi",
"handicap",
"open",
"win_type",
"player_black",
"player_black_rank",
"player_white",
"player_white_rank",
];
const findGameById = async function (gameId) {
const selection = gameDetailSelect.concat(timeSettingSelect);
const game = await knex
.from("game")
.select(selection)
.where({ "game.id": gameId })
.leftJoin("time_setting", function () {
this.on("time_setting.id", "=", "game.time_setting");
});
return game[0];
};
2020-01-21 07:05:55 +00:00
const findGameByRoom = async (roomId) => {
const games = await knex("game")
.where({ room: roomId })
.select(gameOverviewSelect);
return games;
};
2020-01-21 07:05:55 +00:00
const insertGame = async (game) => {};
2020-01-21 07:05:55 +00:00
const endGame = async ({ id, winType, score, bCaptures, wCaptures }) => {
try {
const game = await knex
.from("game")
.returning(gameDetailSelect)
.where({ id: id })
.update({
win_type: winType,
score: score,
captures_black: bCaptures,
captures_white: wCaptures,
open: false,
});
return game;
} catch (e) {
return e;
}
};
2020-01-21 07:05:55 +00:00
module.exports = {
findGameById,
findGameByRoom,
insertGame,
endGame,
};