Files
elysium/apps/api/src/data/initialState.ts
T
hikari 4d7e624358
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 1m3s
CI / Lint, Build & Test (push) Successful in 1m7s
fix: turn off auto-boss/auto-quest on failure and surface status (#46)
## Summary

- Auto-boss now turns itself **off** when a boss fight is **lost**, so the player can reassess rather than the system silently looping. A "🤖 Last fight: [Boss] —  Lost" status line appears in the boss panel.
- Auto-boss also turns off (with an ⚠️ error message) when the API call fails outright (e.g. party has no adventurers), replacing the previous behaviour of silently hammering the API every animation frame.
- Auto-quest now turns itself **off** whenever a quest fails the random-chance check, detected inside the tick's `setState` callback immediately after `applyTick`.
- `autoBoss: false` and `autoQuest: false` are now part of `initialGameState`, so these fields persist through save/load cycles from the very first session — preventing a race window where the boss-route DB write could strip them before the first auto-save.
- `toggleAutoBoss` clears both `autoBossLastResult` and `autoBossError` on each toggle so the panel always reflects the current session cleanly.

## Test plan

- [x] `pnpm lint` — 0 errors, 0 warnings
- [x] `pnpm build` — all packages clean
- [x] `pnpm test` — 100% coverage maintained across the board

Closes #40

 This issue was created with help from Hikari~ 🌸

Reviewed-on: #46
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
2026-03-09 21:12:03 -07:00

109 lines
3.1 KiB
TypeScript

/**
* @file Initial game state data.
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { defaultAchievements } from "./achievements.js";
import { defaultAdventurers } from "./adventurers.js";
import { defaultBosses } from "./bosses.js";
import { defaultEquipment } from "./equipment.js";
import { defaultExplorations } from "./explorations.js";
import { defaultQuests } from "./quests.js";
import { currentSchemaVersion } from "./schemaVersion.js";
import { defaultUpgrades } from "./upgrades.js";
import { defaultZones } from "./zones.js";
import type {
ApotheosisData,
ExplorationState,
GameState,
Player,
PrestigeData,
TranscendenceData,
} from "@elysium/types";
const initialPrestige: PrestigeData = {
count: 0,
productionMultiplier: 1,
purchasedUpgradeIds: [],
runestones: 0,
};
const initialTranscendence: TranscendenceData = {
count: 0,
echoCombatMultiplier: 1,
echoIncomeMultiplier: 1,
echoMetaMultiplier: 1,
echoPrestigeRunestoneMultiplier: 1,
echoPrestigeThresholdMultiplier: 1,
echoes: 0,
purchasedUpgradeIds: [],
};
const initialApotheosis: ApotheosisData = {
count: 0,
};
const initialExploration: ExplorationState = {
areas: defaultExplorations.map((area) => {
return {
id: area.id,
status:
area.zoneId === "verdant_vale"
? ("available" as const)
: ("locked" as const),
};
}),
craftedClickMultiplier: 1,
craftedCombatMultiplier: 1,
craftedEssenceMultiplier: 1,
craftedGoldMultiplier: 1,
craftedRecipeIds: [],
materials: [],
};
/**
* Builds an initial game state for a new player.
* @param player - The player data from Discord OAuth.
* @param characterName - The character name chosen by the player.
* @returns A fresh GameState object.
*/
const initialGameState = (
player: Player,
characterName: string,
): GameState => {
return {
achievements: structuredClone(defaultAchievements),
adventurers: structuredClone(defaultAdventurers),
apotheosis: { ...initialApotheosis },
autoBoss: false,
autoQuest: false,
baseClickPower: 1,
bosses: structuredClone(defaultBosses),
companions: { activeCompanionId: null, unlockedCompanionIds: [] },
equipment: structuredClone(defaultEquipment),
exploration: structuredClone(initialExploration),
lastTickAt: Date.now(),
player: {
...player,
characterName: characterName,
totalClicks: 0,
totalGoldEarned: 0,
},
prestige: initialPrestige,
quests: structuredClone(defaultQuests),
resources: {
crystals: 0,
essence: 0,
gold: 0,
runestones: 0,
},
schemaVersion: currentSchemaVersion,
transcendence: { ...initialTranscendence },
upgrades: structuredClone(defaultUpgrades),
zones: structuredClone(defaultZones),
};
};
export { initialExploration, initialGameState };