diff --git a/packages/play-node-go/src/components/GameUI/Point/Point.js b/packages/play-node-go/src/components/GameUI/Point/Point.js index 7b2aa0e..eb17a43 100644 --- a/packages/play-node-go/src/components/GameUI/Point/Point.js +++ b/packages/play-node-go/src/components/GameUI/Point/Point.js @@ -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); diff --git a/packages/server/data/queries/move.js b/packages/server/data/queries/move.js index a8c79eb..f08f9e8 100644 --- a/packages/server/data/queries/move.js +++ b/packages/server/data/queries/move.js @@ -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; } }; diff --git a/packages/server/services/Game.js b/packages/server/services/Game.js index 20dcecc..7731972 100644 --- a/packages/server/services/Game.js +++ b/packages/server/services/Game.js @@ -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; diff --git a/packages/server/services/gameServices.js b/packages/server/services/gameServices.js index f94ed1e..fe7660d 100644 --- a/packages/server/services/gameServices.js +++ b/packages/server/services/gameServices.js @@ -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 {