2020-06-20 23:25:09 +00:00
|
|
|
const knex = require("../db");
|
2020-01-21 07:05:55 +00:00
|
|
|
|
2020-01-24 08:07:40 +00:00
|
|
|
const gameDetailSelect = [
|
2020-06-20 23:25:09 +00:00
|
|
|
"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",
|
|
|
|
];
|
2020-01-24 08:07:40 +00:00
|
|
|
|
|
|
|
const timeSettingSelect = [
|
2020-06-20 23:25:09 +00:00
|
|
|
"main_time",
|
|
|
|
"time_period",
|
|
|
|
"period_length",
|
|
|
|
"overtime",
|
|
|
|
"overtime_period",
|
|
|
|
"overtime_length",
|
|
|
|
];
|
2020-01-24 08:07:40 +00:00
|
|
|
|
2020-01-23 22:46:49 +00:00
|
|
|
const gameOverviewSelect = [
|
2020-06-20 23:25:09 +00:00
|
|
|
"id",
|
|
|
|
"board_size",
|
|
|
|
"komi",
|
|
|
|
"handicap",
|
|
|
|
"open",
|
|
|
|
"win_type",
|
|
|
|
"player_black",
|
|
|
|
"player_black_rank",
|
|
|
|
"player_white",
|
|
|
|
"player_white_rank",
|
|
|
|
];
|
2020-01-23 22:46:49 +00:00
|
|
|
|
2020-01-24 08:07:40 +00:00
|
|
|
const findGameById = async function (gameId) {
|
|
|
|
const selection = gameDetailSelect.concat(timeSettingSelect);
|
2020-01-24 00:14:38 +00:00
|
|
|
|
2020-01-24 08:07:40 +00:00
|
|
|
const game = await knex
|
2020-06-20 23:25:09 +00:00
|
|
|
.from("game")
|
|
|
|
.select(selection)
|
|
|
|
.where({ "game.id": gameId })
|
|
|
|
.leftJoin("time_setting", function () {
|
|
|
|
this.on("time_setting.id", "=", "game.time_setting");
|
|
|
|
});
|
|
|
|
|
2020-01-24 05:25:08 +00:00
|
|
|
return game[0];
|
2020-06-20 23:25:09 +00:00
|
|
|
};
|
2020-01-21 07:05:55 +00:00
|
|
|
|
|
|
|
const findGameByRoom = async (roomId) => {
|
2020-06-20 23:25:09 +00:00
|
|
|
const games = await knex("game")
|
|
|
|
.where({ room: roomId })
|
|
|
|
.select(gameOverviewSelect);
|
|
|
|
|
2020-01-24 00:14:38 +00:00
|
|
|
return games;
|
2020-06-20 23:25:09 +00:00
|
|
|
};
|
2020-01-21 07:05:55 +00:00
|
|
|
|
2020-06-20 23:25:09 +00:00
|
|
|
const insertGame = async (game) => {};
|
2020-01-21 07:05:55 +00:00
|
|
|
|
2020-06-20 23:25:09 +00:00
|
|
|
const endGame = async ({ id }) => {
|
|
|
|
const game = await knex(game).where({ id: id }).update(
|
|
|
|
{
|
|
|
|
win_type: winType,
|
|
|
|
score: score,
|
|
|
|
captures_black: capturesBlack,
|
|
|
|
captures_white: capturesWhite,
|
|
|
|
}
|
|
|
|
// ["id"]
|
|
|
|
);
|
|
|
|
return game;
|
|
|
|
};
|
2020-01-21 07:05:55 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
findGameById,
|
|
|
|
findGameByRoom,
|
2020-06-20 23:25:09 +00:00
|
|
|
insertGame,
|
|
|
|
endGame,
|
|
|
|
};
|