Refactor playCard to options-based API instead of overloaded positional args
This commit is contained in:
parent
7e46b190f1
commit
886b744db8
2 changed files with 18 additions and 24 deletions
25
src/state.js
25
src/state.js
|
|
@ -113,28 +113,9 @@ export function drawCards(state, playerIndexOrCount, count) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function playCard(
|
export function playCard(state, handIndex, opts = {}) {
|
||||||
state,
|
const playerIndex = opts.playerIndex ?? 0;
|
||||||
playerIndexOrHandIndex,
|
const enemyIndex = opts.targetIndex ?? 0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
const player = state.players[playerIndex];
|
const player = state.players[playerIndex];
|
||||||
const cardId = player.hand[handIndex];
|
const cardId = player.hand[handIndex];
|
||||||
|
|
|
||||||
|
|
@ -175,7 +175,7 @@ describe("playCard - indexed player and enemy", () => {
|
||||||
let state = createCombatState(["ironclad", "ironclad"], ["jaw_worm"]);
|
let state = createCombatState(["ironclad", "ironclad"], ["jaw_worm"]);
|
||||||
state = drawCards(state, 1, 5);
|
state = drawCards(state, 1, 5);
|
||||||
const cardIndex = state.players[1].hand.indexOf("strike_r");
|
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].energy).toBe(2);
|
||||||
expect(next.players[1].hand).toHaveLength(4);
|
expect(next.players[1].hand).toHaveLength(4);
|
||||||
// player 0 unaffected
|
// player 0 unaffected
|
||||||
|
|
@ -191,9 +191,22 @@ describe("playCard - indexed player and enemy", () => {
|
||||||
players: [state.players[0], zeroEnergy],
|
players: [state.players[0], zeroEnergy],
|
||||||
};
|
};
|
||||||
const cardIndex = state.players[1].hand.indexOf("strike_r");
|
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();
|
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", () => {
|
describe("endTurn - indexed player", () => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue