diff --git a/src/state.js b/src/state.js index e194fe9..cac35f3 100644 --- a/src/state.js +++ b/src/state.js @@ -113,28 +113,9 @@ export function drawCards(state, playerIndexOrCount, count) { }; } -export function playCard( - state, - playerIndexOrHandIndex, - handIndexOrTarget, - targetIndex, -) { - // playCard(state, handIndex) — old form, targets enemy[0] - // playCard(state, playerIndex, handIndex, targetIndex) — new form - let playerIndex, handIndex, enemyIndex; - if (handIndexOrTarget === undefined) { - playerIndex = 0; - handIndex = playerIndexOrHandIndex; - enemyIndex = 0; - } else if (targetIndex === undefined) { - playerIndex = 0; - handIndex = playerIndexOrHandIndex; - enemyIndex = handIndexOrTarget; - } else { - playerIndex = playerIndexOrHandIndex; - handIndex = handIndexOrTarget; - enemyIndex = targetIndex; - } +export function playCard(state, handIndex, opts = {}) { + const playerIndex = opts.playerIndex ?? 0; + const enemyIndex = opts.targetIndex ?? 0; const player = state.players[playerIndex]; const cardId = player.hand[handIndex]; diff --git a/src/state.test.js b/src/state.test.js index fcbb4b8..41ad2e2 100644 --- a/src/state.test.js +++ b/src/state.test.js @@ -175,7 +175,7 @@ describe("playCard - indexed player and enemy", () => { let state = createCombatState(["ironclad", "ironclad"], ["jaw_worm"]); state = drawCards(state, 1, 5); const cardIndex = state.players[1].hand.indexOf("strike_r"); - const next = playCard(state, 1, cardIndex, 0); + const next = playCard(state, cardIndex, { playerIndex: 1, targetIndex: 0 }); expect(next.players[1].energy).toBe(2); expect(next.players[1].hand).toHaveLength(4); // player 0 unaffected @@ -191,9 +191,22 @@ describe("playCard - indexed player and enemy", () => { players: [state.players[0], zeroEnergy], }; const cardIndex = state.players[1].hand.indexOf("strike_r"); - const result = playCard(state, 1, cardIndex, 0); + const result = playCard(state, cardIndex, { + playerIndex: 1, + targetIndex: 0, + }); expect(result).toBeNull(); }); + + test("playCard with targetIndex: 1 targets the second enemy", () => { + let state = createCombatState(["ironclad"], ["jaw_worm", "jaw_worm"]); + state = drawCards(state, 0, 5); + const cardIndex = state.players[0].hand.indexOf("strike_r"); + const enemy1HpBefore = state.enemies[1].hp; + const next = playCard(state, cardIndex, { targetIndex: 1 }); + expect(next.enemies[1].hp).toBeLessThan(enemy1HpBefore); + expect(next.enemies[0].hp).toBe(state.enemies[0].hp); + }); }); describe("endTurn - indexed player", () => {