LINE1 10 20 30 40 50 60 70 80 90123456import { Battle } from './battle.js';7import { CHARACTERS, ENCOUNTERS, battleSpec } from './catalog.js';8import { SAVE_VERSION } from './saves.js';9import {10 applyRewards,11 buyCard,12 createProfile,13 recruit,14 rewardsFor,15 validateProfile,16} from './profile.js';1718export class Session {19 constructor(seed = 0x5eed) {20 this.screen = 'title';21 this.profile = null;22 this.encounterIndex = 0;23 this.battle = null;24 this.outcome = null;25 this.rewards = null;26 this.seed = Number(seed) >>> 0;27 this.serial = 0;28 29 30 this.revision = 0;31 }3233 newGame(starter) {34 this.profile = createProfile(starter);35 this.battle = null;36 this.outcome = null;37 this.rewards = null;38 this.encounterIndex = 0;39 this.screen = 'village';40 this.serial += 1;41 }4243 start(index = this.encounterIndex) {44 if (!this.profile) throw new Error('Start a new game first.');45 if (!Number.isInteger(index) || index < 0 || index >= ENCOUNTERS.length) {46 throw new Error('That encounter does not exist.');47 }48 this.encounterIndex = index;49 this.seed = (Math.imul(this.seed, 1664525) + 1013904223) >>> 0;50 this.battle = new Battle(battleSpec(index, this.party()), this.seed);51 this.screen = 'battle';52 this.outcome = null;53 this.rewards = null;54 this.serial += 1;55 }5657 58 party() {59 return this.profile.party.map((character) => ({60 character,61 cards: [...this.profile.cards[character]],62 }));63 }6465 settle() {66 if (!this.battle || !this.battle.outcome || this.screen !== 'battle') return;67 const kills = this.battle.units.filter(68 (unit) => unit.team === 'torment' && unit.health <= 0,69 ).length;70 this.outcome = this.battle.outcome;71 this.rewards = rewardsFor(kills, this.outcome);72 applyRewards(this.profile, this.rewards);73 this.screen = 'result';74 }7576 receive(command) {77 if (!command || typeof command !== 'object') throw new Error('Unknown command.');78 this.revision += 1;79 switch (command.type) {80 case 'newGame':81 this.newGame(command.starter);82 return [];83 case 'start':84 this.start(command.index);85 return [];86 case 'retry':87 this.start(this.encounterIndex);88 return [];89 case 'village':90 91 92 this.screen = 'village';93 this.battle = null;94 this.outcome = null;95 this.rewards = null;96 return [];97 case 'title':98 this.screen = 'title';99 this.battle = null;100 this.outcome = null;101 this.rewards = null;102 return [];103 case 'recruit':104 this.requireProfile();105 recruit(this.profile, command.heroine);106 return [];107 case 'buy':108 this.requireProfile();109 buyCard(this.profile, command.card);110 return [];111 case 'battle': {112 if (this.screen !== 'battle' || !this.battle) {113 throw new Error('No battle is in progress.');114 }115 const events = this.battle.receive(command.command);116 this.settle();117 return events;118 }119 default:120 throw new Error('Unknown command.');121 }122 }123124 requireProfile() {125 if (!this.profile) throw new Error('Start a new game first.');126 }127128 needsEnemyTurn() {129 return this.screen === 'battle' && Boolean(this.battle?.needsEnemyTurn());130 }131132 enemyAct() {133 if (!this.needsEnemyTurn()) return [];134 this.revision += 1;135 const events = this.battle.runEnemyTurn();136 this.settle();137 return events;138 }139140 drainEvents() {141 return this.battle ? this.battle.drainEvents() : [];142 }143144 145 canSave() {146 return this.profile !== null && (this.screen === 'village' || this.screen === 'battle');147 }148149 150 serialize() {151 if (!this.canSave()) throw new Error('The game can only be saved in the village or a battle.');152 return {153 version: SAVE_VERSION,154 screen: this.screen,155 seed: this.seed,156 profile: structuredClone(this.profile),157 encounterIndex: this.encounterIndex,158 battle: this.battle ? this.battle.toState() : null,159 };160 }161162 163 saveLabel() {164 const names = this.profile.party.map((id) => CHARACTERS[id].name).join(', ');165 return {166 where:167 this.screen === 'battle'168 ? ENCOUNTERS[this.encounterIndex].name169 : 'The village',170 party: names,171 honor: this.profile.honor,172 gold: this.profile.gold,173 };174 }175176 177 load(state) {178 if (!state || state.version !== SAVE_VERSION) {179 throw new Error('This save was made by a different version of the game.');180 }181 const profile = validateProfile(structuredClone(state.profile));182 const battle = state.battle ? Battle.fromState(state.battle) : null;183 if (state.screen === 'battle' && (!battle || battle.outcome)) {184 throw new Error('The saved battle is damaged.');185 }186 if (!ENCOUNTERS[state.encounterIndex]) throw new Error('The saved battle is damaged.');187188 this.profile = profile;189 this.seed = Number(state.seed) >>> 0;190 this.encounterIndex = state.encounterIndex;191 this.battle = state.screen === 'battle' ? battle : null;192 this.outcome = null;193 this.rewards = null;194 this.screen = this.battle ? 'battle' : 'village';195 196 this.serial += 1;197 this.revision += 1;198 }199200 snapshot() {201 return {202 screen: this.screen,203 revision: this.revision,204 serial: this.serial,205 profile: this.profile,206 encounterIndex: this.encounterIndex,207 outcome: this.outcome,208 rewards: this.rewards,209 encounters: ENCOUNTERS.map((encounter) => ({210 index: encounter.index,211 name: encounter.name,212 approach: encounter.approach,213 subtitle: encounter.subtitle,214 enemies: encounter.enemies.length,215 })),216 battle: this.battle ? this.battle.snapshot() : null,217 };218 }219}