Add run state module with createRunState and rewards deck builder
This commit is contained in:
parent
28e54d502f
commit
11e12ee906
2 changed files with 79 additions and 0 deletions
28
src/run.js
Normal file
28
src/run.js
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { getAllCards, getStarterDeck } from "./cards.js";
|
||||||
|
import { shuffle } from "./state.js";
|
||||||
|
|
||||||
|
export function createRunState(character) {
|
||||||
|
return {
|
||||||
|
character,
|
||||||
|
hp: 11,
|
||||||
|
maxHp: 11,
|
||||||
|
deck: [...getStarterDeck(character)],
|
||||||
|
cardRewardsDeck: buildRewardsDeck(character),
|
||||||
|
potions: [],
|
||||||
|
combatCount: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildRewardsDeck(character) {
|
||||||
|
const ids = [];
|
||||||
|
for (const card of getAllCards()) {
|
||||||
|
if (
|
||||||
|
card.character === character &&
|
||||||
|
(card.rarity === "common" || card.rarity === "uncommon") &&
|
||||||
|
!card.id.endsWith("+")
|
||||||
|
) {
|
||||||
|
ids.push(card.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return shuffle(ids);
|
||||||
|
}
|
||||||
51
src/run.test.js
Normal file
51
src/run.test.js
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
import { beforeAll, describe, expect, test } from "bun:test";
|
||||||
|
import { getAllCards, initCards } from "./cards.js";
|
||||||
|
import { initEnemies } from "./enemies.js";
|
||||||
|
import { createRunState } from "./run.js";
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await Promise.all([initCards(), initEnemies()]);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createRunState", () => {
|
||||||
|
test("initializes ironclad with correct defaults", () => {
|
||||||
|
const run = createRunState("ironclad");
|
||||||
|
expect(run.character).toBe("ironclad");
|
||||||
|
expect(run.hp).toBe(11);
|
||||||
|
expect(run.maxHp).toBe(11);
|
||||||
|
expect(run.deck).toEqual([
|
||||||
|
"strike_r",
|
||||||
|
"strike_r",
|
||||||
|
"strike_r",
|
||||||
|
"strike_r",
|
||||||
|
"strike_r",
|
||||||
|
"defend_r",
|
||||||
|
"defend_r",
|
||||||
|
"defend_r",
|
||||||
|
"defend_r",
|
||||||
|
"bash",
|
||||||
|
]);
|
||||||
|
expect(run.combatCount).toBe(0);
|
||||||
|
expect(run.potions).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("rewards deck has more than 20 cards, excludes starters and rares and upgraded", () => {
|
||||||
|
const run = createRunState("ironclad");
|
||||||
|
expect(run.cardRewardsDeck.length).toBeGreaterThan(20);
|
||||||
|
for (const cardId of run.cardRewardsDeck) {
|
||||||
|
const card = getAllCards().find((c) => c.id === cardId);
|
||||||
|
expect(card).toBeDefined();
|
||||||
|
expect(card.rarity).not.toBe("starter");
|
||||||
|
expect(card.rarity).not.toBe("rare");
|
||||||
|
expect(cardId.endsWith("+")).toBe(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("rewards deck is shuffled", () => {
|
||||||
|
const run = createRunState("ironclad");
|
||||||
|
const sorted = [...run.cardRewardsDeck].sort();
|
||||||
|
// at least one card should be out of sorted order
|
||||||
|
const allSorted = run.cardRewardsDeck.every((id, i) => id === sorted[i]);
|
||||||
|
expect(allSorted).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue