Replace JSON imports with fetch for browser compatibility
This commit is contained in:
parent
29a4a380ca
commit
dd80f894f8
8 changed files with 53 additions and 12 deletions
10
src/cards.js
10
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];
|
||||
|
|
|
|||
|
|
@ -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", () => {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
|
|
|
|||
|
|
@ -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", () => {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue