add placement boolean to Move migration, add placement and prior_move selects to move query
This commit is contained in:
parent
dd0289439c
commit
5eca128110
2 changed files with 45 additions and 33 deletions
|
@ -1,17 +1,18 @@
|
||||||
const players = ['white', 'black']
|
const players = ["white", "black"];
|
||||||
|
|
||||||
exports.up = knex => {
|
exports.up = (knex) => {
|
||||||
return knex.schema.createTable("move", table => {
|
return knex.schema.createTable("move", (table) => {
|
||||||
table.increments('id').primary();
|
table.increments("id").primary();
|
||||||
table.enu('player', players).notNullable();
|
table.enu("player", players).notNullable();
|
||||||
table.integer('point_x').notNullable();
|
table.integer("point_x").notNullable();
|
||||||
table.integer('point_y').notNullable();
|
table.integer("point_y").notNullable();
|
||||||
table.integer('number').notNullable();
|
table.integer("number").notNullable();
|
||||||
table.boolean('game_record').notNullable().default(true);
|
table.boolean("game_record").notNullable().default(true);
|
||||||
|
table.boolean("placement").notNullable().default(false);
|
||||||
|
|
||||||
table.integer('game').references('id').inTable('game').notNullable();
|
table.integer("game").references("id").inTable("game").notNullable();
|
||||||
table.integer('prior_move').references('id').inTable('move');
|
table.integer("prior_move").references("id").inTable("move");
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.down = knex => knex.schema.dropTableIfExists("move");
|
exports.down = (knex) => knex.schema.dropTableIfExists("move");
|
||||||
|
|
|
@ -1,36 +1,47 @@
|
||||||
const knex = require('../db');
|
const knex = require("../db");
|
||||||
|
|
||||||
const findGameRecord = async (gameId) => {
|
const findGameRecord = async (gameId) => {
|
||||||
return await knex('move')
|
return await knex("move")
|
||||||
.where({ 'game': gameId, 'game_record': true })
|
.where({ game: gameId, game_record: true })
|
||||||
.select('player', 'point_x', 'point_y', 'number')
|
.select("player", "point_x", "point_y", "number", "prior_move", "placement")
|
||||||
.orderBy('number')
|
.orderBy("number")
|
||||||
.then(record => record.map(({player, point_x, point_y}) => ({player, pos: { x: point_x, y: point_y } }) ))
|
.then((record) =>
|
||||||
|
record.map(({ player, point_x, point_y }) => ({
|
||||||
|
player,
|
||||||
|
pos: { x: point_x, y: point_y },
|
||||||
|
}))
|
||||||
|
);
|
||||||
// .then(res => res)
|
// .then(res => res)
|
||||||
}
|
};
|
||||||
|
|
||||||
// id: 1, player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: 1, prior_move: null
|
// 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 addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => {
|
||||||
|
// ! priorMove must be FK not move number
|
||||||
const number = priorMove + 1;
|
const number = priorMove + 1;
|
||||||
let result;
|
let result;
|
||||||
try {
|
try {
|
||||||
result = await knex('move')
|
result = await knex("move")
|
||||||
.returning('*')
|
.returning("*")
|
||||||
.insert({ game: gameId, player, point_x: x, point_y: y, number, game_record: gameRecord, prior_move: priorMove })
|
.insert({
|
||||||
.then(res => res);
|
game: gameId,
|
||||||
}
|
player,
|
||||||
catch (e) {
|
point_x: x,
|
||||||
result = e
|
point_y: y,
|
||||||
}
|
number,
|
||||||
finally {
|
game_record: gameRecord,
|
||||||
|
prior_move: priorMove,
|
||||||
|
})
|
||||||
|
.then((res) => res);
|
||||||
|
} catch (e) {
|
||||||
|
result = e;
|
||||||
|
} finally {
|
||||||
console.log(result);
|
console.log(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
findGameRecord,
|
findGameRecord,
|
||||||
addMove
|
addMove,
|
||||||
}
|
};
|
||||||
|
|
Loading…
Reference in a new issue