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

48 lines
1.1 KiB
JavaScript
Raw Normal View History

const knex = require("../db");
2020-01-21 07:19:22 +00:00
const findGameRecord = async (gameId) => {
return await knex("move")
.where({ game: gameId, game_record: true })
.select("player", "point_x", "point_y", "number", "prior_move", "placement")
.orderBy("number")
.then((record) =>
record.map(({ player, point_x, point_y }) => ({
player,
pos: { x: point_x, y: point_y },
}))
);
2020-05-12 01:18:42 +00:00
// .then(res => res)
};
2020-05-12 01:18:42 +00:00
// id: 1, player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: 1, prior_move: null
const addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => {
// ! priorMove must be FK not move number
2020-05-12 01:18:42 +00:00
const number = priorMove + 1;
let result;
try {
result = await knex("move")
.returning("*")
.insert({
game: gameId,
player,
point_x: x,
point_y: y,
number,
game_record: gameRecord,
prior_move: priorMove,
})
.then((res) => res);
} catch (e) {
result = e;
} finally {
2020-05-12 01:18:42 +00:00
console.log(result);
return result;
}
};
2020-01-21 07:19:22 +00:00
module.exports = {
2020-05-12 01:18:42 +00:00
findGameRecord,
addMove,
};