File size: 3,949 Bytes
465b043 c703ea3 465b043 c703ea3 465b043 c703ea3 465b043 c703ea3 99ecf7d 7428b13 7ea1165 c703ea3 7428b13 c703ea3 465b043 7428b13 c703ea3 a21678d 465b043 c703ea3 465b043 c703ea3 90a88fc c703ea3 a21678d c703ea3 90a88fc c703ea3 465b043 c703ea3 23a36f6 c703ea3 465b043 c703ea3 465b043 a21678d c703ea3 465b043 c703ea3 465b043 c703ea3 465b043 c703ea3 465b043 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import { db } from './index';
import type { Encounter, PicletInstance } from './schema';
import { EncounterType } from './schema';
import { getOrCreateGameState, markEncountersRefreshed } from './gameState';
import { getCaughtPiclets } from './piclets';
// Configuration
const ENCOUNTER_REFRESH_HOURS = 2;
const MIN_WILD_ENCOUNTERS = 2;
const MAX_WILD_ENCOUNTERS = 4;
export class EncounterService {
// Check if encounters should be refreshed
static async shouldRefreshEncounters(): Promise<boolean> {
const state = await getOrCreateGameState();
const hoursSinceRefresh = (Date.now() - state.lastEncounterRefresh.getTime()) / (1000 * 60 * 60);
return hoursSinceRefresh >= ENCOUNTER_REFRESH_HOURS;
}
// Force encounter refresh
static async forceEncounterRefresh(): Promise<void> {
await db.encounters.clear();
await markEncountersRefreshed();
}
// Get current encounters
static async getCurrentEncounters(): Promise<Encounter[]> {
return await db.encounters
.orderBy('createdAt')
.reverse()
.toArray();
}
// Clear all encounters
static async clearEncounters(): Promise<void> {
await db.encounters.clear();
}
// Generate new encounters - simplified to only wild battles
static async generateEncounters(): Promise<Encounter[]> {
const encounters: Omit<Encounter, 'id'>[] = [];
// Check if player has any Piclets
const caughtPiclets = await getCaughtPiclets();
if (caughtPiclets.length === 0) {
// No Piclets yet - show empty state, they need to scan first
console.log('Player has no caught piclets - returning empty encounters');
await db.encounters.clear();
await markEncountersRefreshed();
return [];
}
// Player has Piclets - generate wild battle encounters
console.log('Generating wild battle encounters');
const wildEncounters = await this.generateWildBattleEncounters();
encounters.push(...wildEncounters);
// Clear existing encounters and add new ones
await db.encounters.clear();
for (const encounter of encounters) {
await db.encounters.add(encounter);
}
await markEncountersRefreshed();
return await this.getCurrentEncounters();
}
// Generate wild battle encounters using existing caught Piclets
private static async generateWildBattleEncounters(): Promise<Omit<Encounter, 'id'>[]> {
const encounters: Omit<Encounter, 'id'>[] = [];
// Get all caught piclets to use as potential opponents
const caughtPiclets = await getCaughtPiclets();
if (caughtPiclets.length === 0) {
return encounters;
}
// Generate 2-4 random wild encounters
const numEncounters = Math.floor(Math.random() * (MAX_WILD_ENCOUNTERS - MIN_WILD_ENCOUNTERS + 1)) + MIN_WILD_ENCOUNTERS;
for (let i = 0; i < numEncounters; i++) {
// Pick a random Piclet from caught ones to use as opponent
const randomPiclet = caughtPiclets[Math.floor(Math.random() * caughtPiclets.length)];
// Use the piclet's name for display
const displayName = randomPiclet.typeId.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
const encounter: Omit<Encounter, 'id'> = {
type: EncounterType.WILD_PICLET,
title: `Wild ${displayName}`,
description: `A wild ${displayName} appears! Ready for battle?`,
picletInstanceId: randomPiclet.id,
picletTypeId: randomPiclet.typeId,
enemyLevel: 5, // Simplified - no level variance needed
createdAt: new Date()
};
encounters.push(encounter);
}
return encounters;
}
// Get a specific encounter by ID
static async getEncounter(id: number): Promise<Encounter | undefined> {
return await db.encounters.get(id);
}
// Delete an encounter (after it's been completed)
static async deleteEncounter(id: number): Promise<void> {
await db.encounters.delete(id);
}
} |