add placement boolean to Move migration, add placement and prior_move selects to move query

This commit is contained in:
sorrelbri 2020-07-01 10:54:32 -07:00
parent dd0289439c
commit 5eca128110
2 changed files with 45 additions and 33 deletions

View file

@ -1,17 +1,18 @@
const players = ['white', 'black']
const players = ["white", "black"];
exports.up = knex => {
return knex.schema.createTable("move", table => {
table.increments('id').primary();
table.enu('player', players).notNullable();
table.integer('point_x').notNullable();
table.integer('point_y').notNullable();
table.integer('number').notNullable();
table.boolean('game_record').notNullable().default(true);
exports.up = (knex) => {
return knex.schema.createTable("move", (table) => {
table.increments("id").primary();
table.enu("player", players).notNullable();
table.integer("point_x").notNullable();
table.integer("point_y").notNullable();
table.integer("number").notNullable();
table.boolean("game_record").notNullable().default(true);
table.boolean("placement").notNullable().default(false);
table.integer('game').references('id').inTable('game').notNullable();
table.integer('prior_move').references('id').inTable('move');
table.integer("game").references("id").inTable("game").notNullable();
table.integer("prior_move").references("id").inTable("move");
});
};
exports.down = knex => knex.schema.dropTableIfExists("move");
exports.down = (knex) => knex.schema.dropTableIfExists("move");

View file

@ -1,36 +1,47 @@
const knex = require('../db');
const knex = require("../db");
const findGameRecord = async (gameId) => {
return await knex('move')
.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 } }) ))
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 },
}))
);
// .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 }) => {
// ! priorMove must be FK not move number
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 {
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;
}
}
};
module.exports = {
findGameRecord,
addMove
}
addMove,
};