2020-01-21 07:19:22 +00:00
|
|
|
const knex = require('../db');
|
|
|
|
|
|
|
|
const findGameRecord = async (gameId) => {
|
|
|
|
return await knex('move')
|
2020-05-12 01:18:42 +00:00
|
|
|
.where({ 'game': gameId, 'game_record': true })
|
|
|
|
.select('player', 'point_x', 'point_y', 'number')
|
|
|
|
.orderBy('number')
|
|
|
|
.then(record => record.map(({player, point_x, point_y}) => ({player, pos: { x: point_x, y: point_y } }) ))
|
|
|
|
// .then(res => res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }) => {
|
|
|
|
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 {
|
|
|
|
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
|
2020-01-21 07:19:22 +00:00
|
|
|
}
|