Spaces:
Runtime error
Runtime error
Download bot2/src/data.ts from spaceship0/dragonbot-new: direct link, hf CLI and curl.
- Browser
- Download file 7.01 kB
-
https://huggingface.co/spaces/spaceship0/dragonbot-new/resolve/main/bot2/src/data.ts
- Command line
-
hf download hf://spaces/spaceship0/dragonbot-new/bot2/src/data.ts
-
curl -L -o data.ts https://huggingface.co/spaces/spaceship0/dragonbot-new/resolve/main/bot2/src/data.ts
7.01 kB
| // data.ts | |
| import fs from "fs"; | |
| import path from "path"; | |
| import { fileURLToPath } from "url"; | |
| import type { Rarity, GrowthStage, BotRole } from "/dragons.js"; | |
| import { | |
| BASE_STATS, | |
| getStageForLevel, | |
| expForNextLevel, | |
| randomSpecies, | |
| RARITY_ORDER, | |
| } from "./dragons.js"; | |
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | |
| const DATA_FILE = path.resolve(__dirname, "/data/data.json"); | |
| export interface DragonStats { | |
| hp: number; | |
| atk: number; | |
| def: number; | |
| spd: number; | |
| luck: number; | |
| } | |
| export interface Dragon { | |
| id: string; | |
| species: string; | |
| name: string; | |
| rarity: Rarity; | |
| level: number; | |
| exp: number; | |
| stage: GrowthStage; | |
| stats: DragonStats; | |
| currentHp?: number; // Current HP (for injury system) | |
| hunger: number; // 0β100 | |
| thirst: number; // 0β100 | |
| equipment: { | |
| sword?: string; | |
| orb?: string; | |
| }; | |
| spells?: string[]; // Array of learned spell IDs | |
| cooldowns: { | |
| train?: number; | |
| play?: number; | |
| cuddle?: number; | |
| pet?: number; | |
| }; | |
| } | |
| export interface Egg { | |
| id: string; | |
| rarity: Rarity; | |
| hatchAt: number; | |
| } | |
| export interface ActiveMission { | |
| dragonId: string; | |
| type: "Short" | "Long" | "Risky"; | |
| endsAt: number; | |
| baseReward: number; | |
| } | |
| export type QuestActionType = | |
| | "work" | |
| | "battle_win" | |
| | "train" | |
| | "explore" | |
| | "drill" | |
| | "patrol" | |
| | "scavenge" | |
| | "hunt"; | |
| export interface ActiveQuest { | |
| type: QuestActionType; | |
| label: string; | |
| target: number; | |
| progress: number; | |
| reward: number; | |
| expiresAt: number; | |
| } | |
| export interface UserData { | |
| coins: number; | |
| dragons: Dragon[]; | |
| eggs: Egg[]; | |
| inventory: Record<string, number>; | |
| role: BotRole | null; | |
| lastDaily: number; | |
| dailyStreak: number; | |
| lastDailyDate: string; | |
| lastWork: number; | |
| activeDragonId: string | null; | |
| mission: ActiveMission | null; | |
| userCooldowns: Record<string, number>; | |
| arena: { wave: number; lastReset: string }; | |
| quest: ActiveQuest | null; | |
| } | |
| export interface GameData { | |
| users: Record<string, UserData>; | |
| } | |
| let gameData: GameData = { users: {} }; | |
| let saveTimer: ReturnType<typeof setTimeout> | null = null; | |
| let lastSaveTime = 0; | |
| export function loadData(): GameData { | |
| try { | |
| if (fs.existsSync(DATA_FILE)) { | |
| gameData = JSON.parse(fs.readFileSync(DATA_FILE, "utf-8")); | |
| console.log( | |
| `[Data] Loaded ${Object.keys(gameData.users).length} users from data.json`, | |
| ); | |
| } else { | |
| gameData = { users: {} }; | |
| console.log("[Data] No data file found, starting fresh."); | |
| } | |
| } catch (err) { | |
| console.error("[Data] Failed to load data:", err); | |
| gameData = { users: {} }; | |
| } | |
| return gameData; | |
| } | |
| export function saveData(): void { | |
| try { | |
| const now = Date.now(); | |
| fs.writeFileSync(DATA_FILE, JSON.stringify(gameData, null, 2), "utf-8"); | |
| console.log( | |
| `[Data] β Successfully saved at ${new Date().toLocaleTimeString()}`, | |
| ); | |
| lastSaveTime = now; | |
| } catch (err) { | |
| console.error("[Data] β Failed to save data:", err); | |
| } | |
| } | |
| export function scheduleSave(): void { | |
| if (saveTimer) clearTimeout(saveTimer); | |
| saveTimer = setTimeout(() => { | |
| saveData(); | |
| }, 2000); | |
| } | |
| export function getUser(userId: string): UserData { | |
| if (!gameData.users[userId]) { | |
| gameData.users[userId] = { | |
| coins: 0, | |
| dragons: [], | |
| eggs: [], | |
| inventory: {}, | |
| role: null, | |
| lastDaily: 0, | |
| dailyStreak: 0, | |
| lastDailyDate: "", | |
| lastWork: 0, | |
| activeDragonId: null, | |
| mission: null, | |
| userCooldowns: {}, | |
| arena: { wave: 0, lastReset: "" }, | |
| quest: null, | |
| }; | |
| } | |
| const u = gameData.users[userId]; | |
| if (!u.userCooldowns) u.userCooldowns = {}; | |
| if (!u.arena) u.arena = { wave: 0, lastReset: "" }; | |
| if (u.quest === undefined) u.quest = null; | |
| // Initialize dragon spells and currentHp if missing | |
| u.dragons.forEach((d) => { | |
| if (!d.spells) d.spells = []; | |
| if (d.currentHp === undefined) d.currentHp = d.stats.hp; | |
| }); | |
| return u; | |
| } | |
| export function resetUser(userId: string): void { | |
| gameData.users[userId] = { | |
| userCooldowns: {}, | |
| arena: { wave: 0, lastReset: "" }, | |
| quest: null, | |
| coins: 0, | |
| dragons: [], | |
| eggs: [], | |
| inventory: {}, | |
| role: null, | |
| lastDaily: 0, | |
| dailyStreak: 0, | |
| lastDailyDate: "", | |
| lastWork: 0, | |
| activeDragonId: null, | |
| mission: null, | |
| }; | |
| } | |
| export function getActiveDragon(user: UserData): Dragon | null { | |
| if (!user.activeDragonId) return null; | |
| return user.dragons.find((d) => d.id === user.activeDragonId) ?? null; | |
| } | |
| export function getDragonById(user: UserData, id: string): Dragon | null { | |
| return user.dragons.find((d) => d.id === id) ?? null; | |
| } | |
| let dragonIdCounter = Date.now(); | |
| export function createDragon(species: string, rarity: Rarity): Dragon { | |
| const base = BASE_STATS[rarity]; | |
| const variance = () => Math.floor(Math.random() * 5) - 2; | |
| const hp = base.hp + variance(); | |
| return { | |
| id: `d${++dragonIdCounter}`, | |
| species, | |
| name: species, | |
| rarity, | |
| level: 1, | |
| exp: 0, | |
| stage: "Baby", | |
| stats: { | |
| hp: hp, | |
| atk: base.atk + variance(), | |
| def: base.def + variance(), | |
| spd: base.spd + variance(), | |
| luck: base.luck + variance(), | |
| }, | |
| currentHp: hp, // Initialize current HP to max HP | |
| hunger: 80, | |
| thirst: 80, | |
| equipment: {}, | |
| spells: [], // Start with no spells | |
| cooldowns: {}, | |
| }; | |
| } | |
| export function addExpToDragon(dragon: Dragon, exp: number): string[] { | |
| const messages: string[] = []; | |
| dragon.exp += exp; | |
| const needed = expForNextLevel(dragon.level); | |
| while (dragon.exp >= expForNextLevel(dragon.level)) { | |
| dragon.exp -= expForNextLevel(dragon.level); | |
| dragon.level++; | |
| const oldStage = dragon.stage; | |
| dragon.stage = getStageForLevel(dragon.level); | |
| // Stat increases | |
| dragon.stats.hp += 3; | |
| dragon.stats.atk += 2; | |
| dragon.stats.def += 2; | |
| dragon.stats.spd += 2; | |
| dragon.stats.luck += 1; | |
| // Heal dragon on level up | |
| if (dragon.currentHp !== undefined) { | |
| dragon.currentHp = Math.min(dragon.stats.hp, dragon.currentHp + 10); | |
| } | |
| messages.push( | |
| `π **${dragon.name}** (${dragon.species}) leveled up to **Lv.${dragon.level}**!`, | |
| ); | |
| if (dragon.stage !== oldStage) { | |
| messages.push( | |
| `β¨ **${dragon.name}** evolved to **${dragon.stage}** stage!`, | |
| ); | |
| } | |
| } | |
| return messages; | |
| } | |
| export function addItemToInventory( | |
| user: UserData, | |
| itemKey: string, | |
| amount = 1, | |
| ): void { | |
| user.inventory[itemKey] = (user.inventory[itemKey] ?? 0) + amount; | |
| } | |
| export function removeItemFromInventory( | |
| user: UserData, | |
| itemKey: string, | |
| amount = 1, | |
| ): boolean { | |
| const current = user.inventory[itemKey] ?? 0; | |
| if (current < amount) return false; | |
| user.inventory[itemKey] = current - amount; | |
| if (user.inventory[itemKey] === 0) delete user.inventory[itemKey]; | |
| return true; | |
| } | |
| export function getRarityIndex(rarity: Rarity): number { | |
| return RARITY_ORDER.indexOf(rarity); | |
| } | |
| export function todayDateString(): string { | |
| return new Date().toISOString().slice(0, 10); | |
| } | |