From c256623a122fcf2bef72335181384a460207fdab Mon Sep 17 00:00:00 2001 From: Morteza Ziyae Date: Mon, 8 Jul 2019 21:45:34 +0430 Subject: [PATCH] hotfix getPositionAfterMove() throws error --- package.json | 2 +- src/functions/move-helpers.ts | 35 +++++++++++++++++++---------------- src/functions/path-helpers.ts | 2 +- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index bb9670d..14e4924 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ludo", - "version": "v1.2.1", + "version": "v1.2.2", "private": true, "scripts": { "start": "yarn serve", diff --git a/src/functions/move-helpers.ts b/src/functions/move-helpers.ts index 508ce46..85240cf 100644 --- a/src/functions/move-helpers.ts +++ b/src/functions/move-helpers.ts @@ -167,34 +167,37 @@ function _getInGameActions(diceInfo: DiceInfo, player: Player): MoveAction[] { playerMarblesInGame.forEach((marble: Marble) => { const marblePosition: PositionInBoard = getPositionOfMarble(marble); const finalStepPosition: PositionInBoard = getPositionOfStep(store.getters["steps/finalStep"]); - const toPosition = getPositionAfterMove({ from: marblePosition, player, amount: diceInfo.value }); + const distanceToFinal: number = getDistance(marblePosition, finalStepPosition, player); + const isOutOfPath: boolean = diceInfo.value <= distanceToFinal; + + if (!isOutOfPath) { + return; + } + + const toPosition: PositionInBoard = getPositionAfterMove({ from: marblePosition, player, amount: diceInfo.value }); // prevent move to filled step const playerMarblesAtToPosition = store.getters["marbles/listPlayerMarblesByPosition"]( player, toPosition ); - const toPositionIsFilled = playerMarblesAtToPosition.length > 0; if (toPositionIsFilled) { return; } - const distance: number = getDistance(marblePosition, finalStepPosition, player); - // console.log("distance", distance); - if (diceInfo.value <= distance) { - const action: MoveAction = { + const action: MoveAction = { + from: marblePosition, + to: getPositionAfterMove({ from: marblePosition, - to: getPositionAfterMove({ - from: marblePosition, - amount: diceInfo.value, - player - }), - type: MoveType.IN_GAME, - marble - }; - availableActions.push(action); - } + amount: diceInfo.value, + player + }), + type: MoveType.IN_GAME, + marble + }; + availableActions.push(action); + }); return availableActions; } diff --git a/src/functions/path-helpers.ts b/src/functions/path-helpers.ts index d38f1d7..9b0f139 100644 --- a/src/functions/path-helpers.ts +++ b/src/functions/path-helpers.ts @@ -23,7 +23,7 @@ export function getPositionAfterMove({ const positionIndex = playerPath.findIndex((step: StepPlace) => { return step[StepPlaceProps.ROW] === from.row && step[StepPlaceProps.COLUMN] === from.column; }); - + if (playerPath.length >= positionIndex + amount) { const step = playerPath[positionIndex + amount]; return getPositionOfStep(step);