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

View file

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

View file

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

View file

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