|
|
|
|
|
|
|
|
|
import type { PicletInstance } from '$lib/db/schema'; |
|
import type { PicletDefinition, Move, BaseStats, SpecialAbility } from '$lib/battle-engine/types'; |
|
import type { PicletStats } from '$lib/types'; |
|
import { PicletType } from '$lib/types/picletTypes'; |
|
import { recalculatePicletStats } from '$lib/services/levelingService'; |
|
import { getUnlockedMoves, isSpecialAbilityUnlocked } from '$lib/services/unlockLevels'; |
|
|
|
|
|
|
|
|
|
|
|
export function picletInstanceToBattleDefinition(instance: PicletInstance): PicletDefinition { |
|
|
|
const updatedInstance = recalculatePicletStats(instance); |
|
|
|
|
|
const baseStats: BaseStats = { |
|
hp: updatedInstance.maxHp, |
|
attack: updatedInstance.attack, |
|
defense: updatedInstance.defense, |
|
speed: updatedInstance.speed |
|
}; |
|
|
|
|
|
const unlockedMoves = getUnlockedMoves(instance.moves, updatedInstance.level); |
|
const movepool: Move[] = unlockedMoves.map(move => ({ |
|
name: move.name, |
|
type: move.type, |
|
power: move.power, |
|
accuracy: move.accuracy, |
|
pp: move.pp, |
|
priority: move.priority, |
|
flags: move.flags, |
|
effects: move.effects |
|
})); |
|
|
|
|
|
if (movepool.length === 0) { |
|
console.warn(`Piclet ${instance.nickname} has no unlocked moves at level ${updatedInstance.level}!`); |
|
|
|
if (instance.moves.length > 0) { |
|
const firstMove = instance.moves[0]; |
|
movepool.push({ |
|
name: firstMove.name, |
|
type: firstMove.type, |
|
power: firstMove.power, |
|
accuracy: firstMove.accuracy, |
|
pp: firstMove.pp, |
|
priority: firstMove.priority, |
|
flags: firstMove.flags, |
|
effects: firstMove.effects |
|
}); |
|
} |
|
} |
|
|
|
|
|
if (!instance.specialAbility) { |
|
throw new Error('Piclet must have a special ability.'); |
|
} |
|
|
|
|
|
let specialAbility: SpecialAbility | undefined; |
|
if (isSpecialAbilityUnlocked(instance.specialAbilityUnlockLevel, updatedInstance.level)) { |
|
specialAbility = instance.specialAbility; |
|
} else { |
|
|
|
specialAbility = { |
|
name: "Locked Ability", |
|
description: `Unlocks at level ${instance.specialAbilityUnlockLevel}`, |
|
effects: [] |
|
}; |
|
} |
|
|
|
|
|
const bst = baseStats.hp + baseStats.attack + baseStats.defense + baseStats.speed; |
|
let tier: 'low' | 'medium' | 'high' | 'legendary'; |
|
if (bst <= 300) tier = 'low'; |
|
else if (bst <= 400) tier = 'medium'; |
|
else if (bst <= 500) tier = 'high'; |
|
else tier = 'legendary'; |
|
|
|
return { |
|
name: instance.nickname || instance.typeId, |
|
description: instance.concept, |
|
tier, |
|
primaryType: instance.primaryType, |
|
secondaryType: instance.secondaryType, |
|
baseStats, |
|
nature: instance.nature, |
|
specialAbility: specialAbility!, |
|
movepool |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function battlePicletToInstance(battlePiclet: any, originalInstance: PicletInstance): PicletInstance { |
|
return { |
|
...originalInstance, |
|
currentHp: battlePiclet.currentHp, |
|
level: battlePiclet.level, |
|
attack: battlePiclet.attack, |
|
defense: battlePiclet.defense, |
|
speed: battlePiclet.speed, |
|
|
|
moves: battlePiclet.moves.map((moveData: any, index: number) => ({ |
|
...originalInstance.moves[index], |
|
currentPp: moveData.currentPP |
|
})) |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
export function picletStatsToBattleDefinition(stats: PicletStats, name: string, concept: string): PicletDefinition { |
|
return { |
|
name: stats.name || name, |
|
description: stats.description || concept, |
|
tier: stats.tier, |
|
primaryType: stats.primaryType as PicletType, |
|
secondaryType: stats.secondaryType as PicletType || undefined, |
|
baseStats: stats.baseStats, |
|
nature: stats.nature, |
|
specialAbility: { |
|
...stats.specialAbility, |
|
description: `Special ability of ${stats.name || name}` |
|
} as any, |
|
movepool: stats.movepool as any |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function stripBattlePrefix(name: string): string { |
|
if (name.startsWith('player-')) { |
|
return name.substring('player-'.length); |
|
} |
|
if (name.startsWith('enemy-')) { |
|
return name.substring('enemy-'.length); |
|
} |
|
return name; |
|
} |