diff --git a/src/cards.js b/src/cards.js index 0eb7456..fa92cb1 100644 --- a/src/cards.js +++ b/src/cards.js @@ -1,6 +1,12 @@ -import starterIronclad from "../data/starter-ironclad.json"; +let cardDb = {}; -const cardDb = { ...starterIronclad }; +export async function initCards() { + const url = new URL("../data/starter-ironclad.json", import.meta.url); + const res = await fetch(url); + if (!res.ok) throw new Error(`failed to load card data: ${res.status}`); + const starterIronclad = await res.json(); + cardDb = { ...starterIronclad }; +} export function getCard(id) { return cardDb[id]; diff --git a/src/cards.test.js b/src/cards.test.js index 00ada4c..b845db5 100644 --- a/src/cards.test.js +++ b/src/cards.test.js @@ -1,5 +1,9 @@ -import { describe, expect, test } from "bun:test"; -import { getCard, getStarterDeck } from "./cards.js"; +import { beforeAll, describe, expect, test } from "bun:test"; +import { getCard, getStarterDeck, initCards } from "./cards.js"; + +beforeAll(async () => { + await initCards(); +}); describe("cards", () => { test("getCard returns card by id", () => { diff --git a/src/combat.test.js b/src/combat.test.js index 9086441..0a72e4f 100644 --- a/src/combat.test.js +++ b/src/combat.test.js @@ -1,7 +1,13 @@ -import { describe, expect, test } from "bun:test"; +import { beforeAll, describe, expect, test } from "bun:test"; +import { initCards } from "./cards.js"; import { checkCombatEnd, resolveEnemyTurn, startTurn } from "./combat.js"; +import { initEnemies } from "./enemies.js"; import { createCombatState } from "./state.js"; +beforeAll(async () => { + await Promise.all([initCards(), initEnemies()]); +}); + describe("startTurn", () => { test("resets energy and block, draws 5 cards", () => { let state = createCombatState("ironclad", "jaw_worm"); diff --git a/src/effects.test.js b/src/effects.test.js index 4ed72d3..6612a9e 100644 --- a/src/effects.test.js +++ b/src/effects.test.js @@ -1,7 +1,13 @@ -import { describe, expect, test } from "bun:test"; +import { beforeAll, describe, expect, test } from "bun:test"; +import { initCards } from "./cards.js"; import { calculateHitDamage, resolveEffects } from "./effects.js"; +import { initEnemies } from "./enemies.js"; import { createCombatState } from "./state.js"; +beforeAll(async () => { + await Promise.all([initCards(), initEnemies()]); +}); + function makeState(overrides = {}) { const base = createCombatState("ironclad", "jaw_worm"); return { diff --git a/src/enemies.js b/src/enemies.js index ce17e03..47afb0c 100644 --- a/src/enemies.js +++ b/src/enemies.js @@ -1,4 +1,11 @@ -import enemyDb from "../data/enemies.json"; +let enemyDb = {}; + +export async function initEnemies() { + const url = new URL("../data/enemies.json", import.meta.url); + const res = await fetch(url); + if (!res.ok) throw new Error(`failed to load enemy data: ${res.status}`); + enemyDb = await res.json(); +} export function getEnemy(id) { return enemyDb[id]; diff --git a/src/enemies.test.js b/src/enemies.test.js index 4b869df..0037bc1 100644 --- a/src/enemies.test.js +++ b/src/enemies.test.js @@ -1,5 +1,9 @@ -import { describe, expect, test } from "bun:test"; -import { getEnemy, resolveEnemyAction } from "./enemies.js"; +import { beforeAll, describe, expect, test } from "bun:test"; +import { getEnemy, initEnemies, resolveEnemyAction } from "./enemies.js"; + +beforeAll(async () => { + await initEnemies(); +}); describe("getEnemy", () => { test("returns enemy by id", () => { diff --git a/src/main.js b/src/main.js index ac0d03a..91dddcc 100644 --- a/src/main.js +++ b/src/main.js @@ -1,11 +1,13 @@ -import { getCard } from "./cards.js"; +import { getCard, initCards } from "./cards.js"; import { checkCombatEnd, resolveEnemyTurn, startTurn } from "./combat.js"; +import { initEnemies } from "./enemies.js"; import { render } from "./render.js"; import { createCombatState, endTurn, playCard } from "./state.js"; let state = null; -function init() { +async function init() { + await Promise.all([initCards(), initEnemies()]); state = createCombatState("ironclad", "jaw_worm"); state = startTurn(state); render(state); diff --git a/src/state.test.js b/src/state.test.js index 5708f20..7db5742 100644 --- a/src/state.test.js +++ b/src/state.test.js @@ -1,6 +1,12 @@ -import { describe, expect, test } from "bun:test"; +import { beforeAll, describe, expect, test } from "bun:test"; +import { initCards } from "./cards.js"; +import { initEnemies } from "./enemies.js"; import { createCombatState, drawCards, endTurn, playCard } from "./state.js"; +beforeAll(async () => { + await Promise.all([initCards(), initEnemies()]); +}); + describe("createCombatState", () => { test("creates initial state with shuffled deck and correct values", () => { const state = createCombatState("ironclad", "jaw_worm");