Skip to content
1        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';89export const HONOR_PER_KILL = 10;10export const GOLD_PER_KILL = 10;11export const GOLD_PER_VICTORY = 30;1213export function createProfile(starter) {14  if (!HEROINE_IDS.includes(starter)) throw new Error('Choose one of the three heroines.');15  return {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. */24export function validateProfile(profile) {25  if (!profile || !Array.isArray(profile.party) || profile.party.length === 0) {26    throw new Error('The saved campaign is damaged.');27  }28  for (const id of profile.party) {29    if (!HEROINE_IDS.includes(id) || !Array.isArray(profile.cards?.[id])) {30      throw new Error('The saved campaign is damaged.');31    }32    for (const cardId of profile.cards[id]) cardById(cardId);33  }34  if (!Number.isInteger(profile.honor) || !Number.isInteger(profile.gold)) {35    throw new Error('The saved campaign is damaged.');36  }37  return profile;38}3940export function recruitCost(id) {41  return RECRUIT_HONOR[id];42}4344export function recruit(profile, id) {45  if (!HEROINE_IDS.includes(id)) throw new Error('No such heroine.');46  if (profile.party.includes(id)) throw new Error(`${id} already fights with you.`);47  if (profile.party.length >= 3) throw new Error('The party is already full.');48  const cost = recruitCost(id);49  if (profile.honor < cost) throw new Error(`Recruiting costs ${cost} honor.`);50  profile.honor -= cost;51  profile.party.push(id);52  profile.cards[id] = [...STARTING_CARDS[id]];53}5455export function buyCard(profile, cardId) {56  const card = cardById(cardId);57  if (!profile.party.includes(card.heroine)) {58    throw new Error(`Recruit ${card.heroine} before buying her cards.`);59  }60  if (profile.cards[card.heroine].includes(cardId)) throw new Error(`${card.name} is already owned.`);61  const price = cardPrice(card);62  if (profile.gold < price) throw new Error(`${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. */68export function marketCards(profile) {69  return 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. */76export function rewardsFor(kills, outcome) {77  const victory = outcome === 'victory';78  return {79    victory,80    kills,81    honor: kills * HONOR_PER_KILL,82    gold: kills * GOLD_PER_KILL + (victory ? GOLD_PER_VICTORY : 0),83  };84}8586export function applyRewards(profile, rewards) {87  profile.honor += rewards.honor;88  profile.gold += rewards.gold;89}