Fraser commited on
Commit
11f6bcf
·
1 Parent(s): 38d460f

fix encounters

Browse files
src/lib/components/Pages/Encounters.svelte CHANGED
@@ -5,6 +5,7 @@
5
  import { EncounterType } from '$lib/db/schema';
6
  import { EncounterService } from '$lib/db/encounterService';
7
  import { getOrCreateGameState, incrementCounter, addProgressPoints } from '$lib/db/gameState';
 
8
  import { db } from '$lib/db';
9
  import { uiStore } from '$lib/stores/ui';
10
  import Battle from './Battle.svelte';
@@ -290,8 +291,6 @@
290
  const level = encounter.enemyLevel;
291
 
292
  // Calculate current stats based on level (using template's base stats)
293
- const calculateStat = (base: number, level: number) => Math.floor((base * level) / 50 + 5);
294
- const calculateHp = (base: number, level: number) => Math.floor((base * level) / 50 + level + 10);
295
 
296
  const maxHp = calculateHp(templatePiclet.baseHp, level);
297
 
 
5
  import { EncounterType } from '$lib/db/schema';
6
  import { EncounterService } from '$lib/db/encounterService';
7
  import { getOrCreateGameState, incrementCounter, addProgressPoints } from '$lib/db/gameState';
8
+ import { calculateStat, calculateHp } from '$lib/services/levelingService';
9
  import { db } from '$lib/db';
10
  import { uiStore } from '$lib/stores/ui';
11
  import Battle from './Battle.svelte';
 
291
  const level = encounter.enemyLevel;
292
 
293
  // Calculate current stats based on level (using template's base stats)
 
 
294
 
295
  const maxHp = calculateHp(templatePiclet.baseHp, level);
296
 
src/lib/db/encounterService.ts CHANGED
@@ -3,6 +3,7 @@ import type { Encounter, PicletInstance } from './schema';
3
  import { EncounterType } from './schema';
4
  import { getOrCreateGameState, markEncountersRefreshed } from './gameState';
5
  import { getCaughtPiclets, getUncaughtPiclets } from './piclets';
 
6
 
7
  // Configuration
8
  const ENCOUNTER_REFRESH_HOURS = 2;
@@ -129,17 +130,17 @@ export class EncounterService {
129
  // Get player's average level
130
  const avgLevel = await this.getPlayerAverageLevel();
131
 
132
- // Get caught piclets only (these can appear as wild encounters)
133
- const caughtPiclets = await getCaughtPiclets();
134
- console.log('Caught piclets for wild encounters:', caughtPiclets.length);
135
 
136
- if (caughtPiclets.length === 0) {
137
- console.log('No caught piclets - returning empty wild encounters');
138
  return encounters;
139
  }
140
 
141
- // Use caught piclets as templates for wild encounters
142
- const availablePiclets = caughtPiclets;
143
  console.log('Available piclets for encounters:', availablePiclets.map(p => p.typeId));
144
 
145
  const encounterCount = MIN_WILD_ENCOUNTERS + Math.floor(Math.random() * (MAX_WILD_ENCOUNTERS - MIN_WILD_ENCOUNTERS + 1));
@@ -192,31 +193,66 @@ export class EncounterService {
192
  }
193
 
194
 
195
- // Catch a wild piclet (creates a new instance based on existing piclet type)
196
  static async catchWildPiclet(encounter: Encounter): Promise<PicletInstance> {
197
  if (!encounter.picletTypeId) throw new Error('No piclet type specified');
198
 
199
- // First try to find a caught piclet instance with this typeId to use as a template
200
- const caughtPiclets = await getCaughtPiclets();
201
- let templatePiclet = caughtPiclets.find(p => p.typeId === encounter.picletTypeId);
202
 
203
- // If no caught template found, try uncaught piclets (for first catch scenario)
204
- if (!templatePiclet) {
205
- const uncaughtPiclets = await getUncaughtPiclets();
206
- templatePiclet = uncaughtPiclets.find(p => p.typeId === encounter.picletTypeId);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  }
208
 
 
 
 
 
209
  if (!templatePiclet) {
210
  throw new Error(`Piclet type not found: ${encounter.picletTypeId}`);
211
  }
212
 
213
- // Create a new piclet instance based on the template but with different stats/level
214
  const newLevel = encounter.enemyLevel || 5;
215
 
216
- // Calculate new stats based on level (using template's base stats)
217
- const calculateStat = (base: number, level: number) => Math.floor((base * level) / 50 + 5);
218
- const calculateHp = (base: number, level: number) => Math.floor((base * level) / 50 + level + 10);
219
-
220
  const newPiclet: Omit<PicletInstance, 'id'> = {
221
  ...templatePiclet,
222
  level: newLevel,
@@ -235,22 +271,13 @@ export class EncounterService {
235
  currentPp: move.pp
236
  })),
237
 
238
- // Clear roster info for wild catch
239
  isInRoster: false,
240
  rosterPosition: undefined,
 
241
  caughtAt: new Date()
242
  };
243
 
244
- // Wild piclets are always caught
245
- newPiclet.caught = true;
246
-
247
- // Set roster position 0 if this is the first caught piclet
248
- const existingCaughtPiclets = await getCaughtPiclets();
249
- if (existingCaughtPiclets.length === 0) { // This will be the first caught piclet
250
- newPiclet.rosterPosition = 0;
251
- newPiclet.isInRoster = true;
252
- }
253
-
254
  // Save the new piclet
255
  const id = await db.picletInstances.add(newPiclet);
256
  return { ...newPiclet, id };
 
3
  import { EncounterType } from './schema';
4
  import { getOrCreateGameState, markEncountersRefreshed } from './gameState';
5
  import { getCaughtPiclets, getUncaughtPiclets } from './piclets';
6
+ import { calculateStat, calculateHp } from '../services/levelingService';
7
 
8
  // Configuration
9
  const ENCOUNTER_REFRESH_HOURS = 2;
 
130
  // Get player's average level
131
  const avgLevel = await this.getPlayerAverageLevel();
132
 
133
+ // Get uncaught piclets (these can appear as wild encounters to be caught)
134
+ const uncaughtPiclets = await getUncaughtPiclets();
135
+ console.log('Uncaught piclets for wild encounters:', uncaughtPiclets.length);
136
 
137
+ if (uncaughtPiclets.length === 0) {
138
+ console.log('No uncaught piclets - returning empty wild encounters');
139
  return encounters;
140
  }
141
 
142
+ // Use uncaught piclets as templates for wild encounters
143
+ const availablePiclets = uncaughtPiclets;
144
  console.log('Available piclets for encounters:', availablePiclets.map(p => p.typeId));
145
 
146
  const encounterCount = MIN_WILD_ENCOUNTERS + Math.floor(Math.random() * (MAX_WILD_ENCOUNTERS - MIN_WILD_ENCOUNTERS + 1));
 
193
  }
194
 
195
 
196
+ // Catch a wild piclet (marks uncaught piclet as caught, or creates new instance for recatches)
197
  static async catchWildPiclet(encounter: Encounter): Promise<PicletInstance> {
198
  if (!encounter.picletTypeId) throw new Error('No piclet type specified');
199
 
200
+ // First check if this is an uncaught piclet that can be directly marked as caught
201
+ const uncaughtPiclets = await getUncaughtPiclets();
202
+ const uncaughtPiclet = uncaughtPiclets.find(p => p.typeId === encounter.picletTypeId);
203
 
204
+ if (uncaughtPiclet) {
205
+ // This is the first time catching this type - mark the existing uncaught piclet as caught
206
+ const newLevel = encounter.enemyLevel || uncaughtPiclet.level;
207
+
208
+ // Update the existing uncaught piclet
209
+ const updates = {
210
+ caught: true,
211
+ caughtAt: new Date(),
212
+ level: newLevel,
213
+ xp: 0,
214
+ currentHp: calculateHp(uncaughtPiclet.baseHp, newLevel),
215
+ maxHp: calculateHp(uncaughtPiclet.baseHp, newLevel),
216
+ attack: calculateStat(uncaughtPiclet.baseAttack, newLevel),
217
+ defense: calculateStat(uncaughtPiclet.baseDefense, newLevel),
218
+ fieldAttack: calculateStat(uncaughtPiclet.baseFieldAttack, newLevel),
219
+ fieldDefense: calculateStat(uncaughtPiclet.baseFieldDefense, newLevel),
220
+ speed: calculateStat(uncaughtPiclet.baseSpeed, newLevel),
221
+
222
+ // Reset move PP to full
223
+ moves: uncaughtPiclet.moves.map(move => ({
224
+ ...move,
225
+ currentPp: move.pp
226
+ }))
227
+ };
228
+
229
+ // Set roster position 0 if this is the first caught piclet
230
+ const existingCaughtPiclets = await getCaughtPiclets();
231
+ if (existingCaughtPiclets.length === 0) {
232
+ Object.assign(updates, {
233
+ rosterPosition: 0,
234
+ isInRoster: true
235
+ });
236
+ }
237
+
238
+ // Update the existing piclet
239
+ await db.picletInstances.update(uncaughtPiclet.id!, updates);
240
+
241
+ // Return the updated piclet
242
+ return { ...uncaughtPiclet, ...updates };
243
  }
244
 
245
+ // If no uncaught piclet found, this is a recatch - create a new instance using caught piclet as template
246
+ const caughtPiclets = await getCaughtPiclets();
247
+ const templatePiclet = caughtPiclets.find(p => p.typeId === encounter.picletTypeId);
248
+
249
  if (!templatePiclet) {
250
  throw new Error(`Piclet type not found: ${encounter.picletTypeId}`);
251
  }
252
 
253
+ // Create a new piclet instance for recatch
254
  const newLevel = encounter.enemyLevel || 5;
255
 
 
 
 
 
256
  const newPiclet: Omit<PicletInstance, 'id'> = {
257
  ...templatePiclet,
258
  level: newLevel,
 
271
  currentPp: move.pp
272
  })),
273
 
274
+ // Clear roster info for new catch
275
  isInRoster: false,
276
  rosterPosition: undefined,
277
+ caught: true,
278
  caughtAt: new Date()
279
  };
280
 
 
 
 
 
 
 
 
 
 
 
281
  // Save the new piclet
282
  const id = await db.picletInstances.add(newPiclet);
283
  return { ...newPiclet, id };