LINE1 10 20 30 40 50 60 70 80 90 1001// The player's campaign record: who is in the party, which cards each heroine2// owns, and the two currencies. Plain data only, so it saves as JSON.3//4// Honor is earned per Torment unit killed and spent recruiting heroines. Gold is5// earned per kill and per victory and spent on cards in the village market.67import { CARDS, HEROINE_IDS, RECRUIT_HONOR, STARTING_CARDS, cardById, cardPrice } from'./catalog.js';89exportconst HONOR_PER_KILL = 10;10exportconst GOLD_PER_KILL = 10;11exportconst GOLD_PER_VICTORY = 30;1213exportfunctioncreateProfile(starter) {14if (!HEROINE_IDS.includes(starter)) thrownewError('Choose one of the three heroines.');15return {16 party: [starter],17 cards: { [starter]: [...STARTING_CARDS[starter]] },18 honor: 0,19 gold: 0,20 };21}2223/** Throws a player-facing error if the saved profile cannot be used. */24exportfunctionvalidateProfile(profile) {25if (!profile || !Array.isArray(profile.party) || profile.party.length === 0) {26thrownewError('The saved campaign is damaged.');27 }28for (const id of profile.party) {29if (!HEROINE_IDS.includes(id) || !Array.isArray(profile.cards?.[id])) {30thrownewError('The saved campaign is damaged.');31 }32for (const cardId of profile.cards[id]) cardById(cardId);33 }34if (!Number.isInteger(profile.honor) || !Number.isInteger(profile.gold)) {35thrownewError('The saved campaign is damaged.');36 }37return profile;38}3940exportfunctionrecruitCost(id) {41return RECRUIT_HONOR[id];42}4344exportfunctionrecruit(profile, id) {45if (!HEROINE_IDS.includes(id)) thrownewError('No such heroine.');46if (profile.party.includes(id)) thrownewError(`${id} already fights with you.`);47if (profile.party.length >= 3) thrownewError('The party is already full.');48const cost = recruitCost(id);49if (profile.honor < cost) thrownewError(`Recruiting costs ${cost} honor.`);50 profile.honor -= cost;51 profile.party.push(id);52 profile.cards[id] = [...STARTING_CARDS[id]];53}5455exportfunctionbuyCard(profile, cardId) {56const card = cardById(cardId);57if (!profile.party.includes(card.heroine)) {58thrownewError(`Recruit ${card.heroine} before buying her cards.`);59 }60if (profile.cards[card.heroine].includes(cardId)) thrownewError(`${card.name} is already owned.`);61const price = cardPrice(card);62if (profile.gold < price) thrownewError(`${card.name} costs ${price} gold.`);63 profile.gold -= price;64 profile.cards[card.heroine].push(cardId);65}6667/** Cards the market sells: every card of a recruited heroine that she does not own. */68exportfunctionmarketCards(profile) {69return CARDS.filter(70 (card) =>71 profile.party.includes(card.heroine) && !profile.cards[card.heroine].includes(card.id),72 );73}7475/** What a finished battle pays out. Kills pay even in defeat. */76exportfunctionrewardsFor(kills, outcome) {77const victory = outcome === 'victory';78return {79 victory,80 kills,81 honor: kills * HONOR_PER_KILL,82 gold: kills * GOLD_PER_KILL + (victory ? GOLD_PER_VICTORY : 0),83 };84}8586exportfunctionapplyRewards(profile, rewards) {87 profile.honor += rewards.honor;88 profile.gold += rewards.gold;89}