add prior move to move pipeline: FE socket message -> move record

This commit is contained in:
sorrelbri 2020-07-10 15:54:19 -04:00
parent 0f3e7942ef
commit e95315053c
4 changed files with 33 additions and 24 deletions

View file

@ -48,6 +48,9 @@ const Point = (props) => {
};
return dispatch(action);
}
const priorMove = meta.gameRecord.length
? meta.gameRecord[meta.gameRecord.length - 1].id
: null;
const action = {
type: "SOCKET",
message: "MAKE_MOVE",
@ -56,7 +59,7 @@ const Point = (props) => {
game,
room: game.room,
board: {},
move: { player: turn, pos: { x: posX, y: posY } },
move: { player: turn, pos: { x: posX, y: posY }, priorMove },
},
};
dispatch(action);

View file

@ -3,12 +3,22 @@ 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", "prior_move", "placement")
.select(
"player",
"point_x",
"point_y",
"number",
"prior_move",
"placement",
"id"
)
.orderBy("number")
.then((record) =>
record.map(({ player, point_x, point_y }) => ({
record.map(({ player, point_x, point_y, id, prior_move }) => ({
player,
pos: { x: point_x, y: point_y },
id,
prior: prior_move,
}))
);
// .then(res => res)
@ -32,11 +42,10 @@ const addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => {
game_record: gameRecord,
prior_move: priorMove,
})
.then((res) => res);
.then((res) => res[0]);
} catch (e) {
result = e;
} finally {
console.log(result);
return result;
}
};

View file

@ -262,7 +262,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
return { ...this, success: false };
},
makeMove: function ({ player, pos: { x, y } }) {
makeMove: function ({ player, pos: { x, y }, id }) {
if (this.pass > 1) {
return { ...this, success: false };
}
@ -286,7 +286,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
const point = game.boardState[`${x}-${y}`];
game.pass = 0;
// allows for recording of prior move on game record
game.addToRecord(game.move);
game.addToRecord({ player, pos: { x, y }, id });
if (game.kos.length) helper.clearKo.call(game);
point.makeMove(game);
game.turn *= -1;

View file

@ -7,22 +7,28 @@ const GameService = ({ moveQueries, gameQueries }) => {
};
const gamesInProgress = {};
const storeMove = (gameId) => async ({ player, pos: { x, y } }) => {
let move = { player, pos: { x, y } };
const storeMove = (gameId) => async ({
player,
pos: { x, y },
priorMove,
}) => {
let move = { player, pos: { x, y }, priorMove };
try {
if (moveQueries) {
const { id } = await moveQueries.addMove({
const moveDbResult = await moveQueries.addMove({
gameId,
player,
x,
y,
gameRecord: true,
priorMove: null,
priorMove,
});
move.id = id;
move.id = moveDbResult.id;
move.prior = moveDbResult.prior_move;
move.success = true;
}
} catch (e) {
console.log("Exception ------------");
console.log(e);
move.success = false;
} finally {
@ -55,23 +61,14 @@ const GameService = ({ moveQueries, gameQueries }) => {
}
}
gamesInProgress[id] = await gamesInProgress[id].checkMove(move);
gamesInProgress[id] = gamesInProgress[id].makeMove(move);
if (gamesInProgress[id].success === false)
return { message: "illegal move" };
try {
if (moveQueries) {
const priorMove = gamesInProgress[id].gameRecord.length;
const moveInsert = {
gameId: id,
player: move.player,
x: move.pos.x,
y: move.pos.y,
gameRecord: true,
priorMove,
};
let moveDbResult;
moveDbResult = await moveQueries.addMove(moveInsert);
// todo change prior move
move = await storeMove(id)(move);
}
gamesInProgress[id] = gamesInProgress[id].makeMove(move);
} catch {
gamesInProgress[id].returnToMove(-1);
} finally {