| import { db } from './index'; |
| import type { GameState, Encounter, EncounterType } from './schema'; |
|
|
| |
| export async function getOrCreateGameState(): Promise<GameState> { |
| const states = await db.gameState.toArray(); |
| |
| if (states.length > 0) { |
| |
| await db.gameState.update(states[0].id!, { lastPlayed: new Date() }); |
| return states[0]; |
| } |
| |
| |
| const newState: Omit<GameState, 'id'> = { |
| lastEncounterRefresh: new Date(), |
| lastPlayed: new Date(), |
| progressPoints: 0, |
| trainersDefeated: 0, |
| picletsCapured: 0, |
| battlesLost: 0 |
| }; |
| |
| const id = await db.gameState.add(newState); |
| return { ...newState, id }; |
| } |
|
|
| |
| export async function updateProgress(updates: Partial<GameState>): Promise<void> { |
| const state = await getOrCreateGameState(); |
| await db.gameState.update(state.id!, updates); |
| } |
|
|
| |
| export async function incrementCounter(counter: 'trainersDefeated' | 'picletsCapured' | 'battlesLost'): Promise<void> { |
| const state = await getOrCreateGameState(); |
| await db.gameState.update(state.id!, { |
| [counter]: state[counter] + 1 |
| }); |
| } |
|
|
| |
| export async function addProgressPoints(points: number): Promise<void> { |
| const state = await getOrCreateGameState(); |
| const newPoints = Math.min(1000, state.progressPoints + points); |
| await db.gameState.update(state.id!, { |
| progressPoints: newPoints |
| }); |
| } |
|
|
| |
| export async function createEncounter(encounter: Omit<Encounter, 'id' | 'createdAt'>): Promise<number> { |
| return await db.encounters.add({ |
| ...encounter, |
| createdAt: new Date() |
| }); |
| } |
|
|
| |
| export async function getRecentEncounters(limit: number = 10): Promise<Encounter[]> { |
| return await db.encounters |
| .orderBy('createdAt') |
| .reverse() |
| .limit(limit) |
| .toArray(); |
| } |
|
|
| |
| export async function getEncountersByType(type: EncounterType): Promise<Encounter[]> { |
| return await db.encounters |
| .where('type') |
| .equals(type) |
| .toArray(); |
| } |
|
|
| |
| export async function shouldRefreshEncounters(): Promise<boolean> { |
| const state = await getOrCreateGameState(); |
| const hoursSinceRefresh = (Date.now() - state.lastEncounterRefresh.getTime()) / (1000 * 60 * 60); |
| return hoursSinceRefresh >= 1; |
| } |
|
|
| |
| export async function markEncountersRefreshed(): Promise<void> { |
| const state = await getOrCreateGameState(); |
| await db.gameState.update(state.id!, { |
| lastEncounterRefresh: new Date() |
| }); |
| } |