Add CharacterBuilder class to unify and compile the features

This commit is contained in:
Clément Pons
2025-07-21 12:01:52 +02:00
parent 996b9711e4
commit a8dcc47a1b
15 changed files with 491 additions and 436 deletions

View File

@@ -1,7 +1,8 @@
import useDatabase from '~/composables/useDatabase';
import { defaultCharacter, type Ability, type Character, type CharacterConfig, type CompiledCharacter, type DoubleIndex, type Feature, type FeatureItem, type Level, type MainStat, type TrainingLevel, type TrainingOption } from '~/types/character';
import { type Character, type CharacterConfig, type CompiledCharacter, type DoubleIndex, type Level, type MainStat, type TrainingLevel, type TrainingOption } from '~/types/character';
import characterData from '#shared/character-config.json';
import { group } from '~/shared/general.util';
import { defaultCharacter, MAIN_STATS } from '~/shared/character';
export default defineCachedEventHandler(async (e) => {
const id = getRouterParam(e, "id");
@@ -80,7 +81,7 @@ function compileCharacter(character: Character & { username?: string }): Compile
action: [],
reaction: [],
freeaction: [],
misc: [],
passive: [],
},
abilities: {
athletics: 0,
@@ -130,90 +131,17 @@ function compileCharacter(character: Character & { username?: string }): Compile
magicelement: 0,
magicinstinct: 0,
},
resistance: {
stun: [0, 0],
bleed: [0, 0],
poison: [0, 0],
fear: [0, 0],
influence: [0, 0],
charm: [0, 0],
possesion: [0, 0],
precision: [0, 0],
knowledge: [0, 0],
instinct: [0, 0]
},
resistance: {},//Object.fromEntries(MAIN_STATS.map(e => [e as MainStat, [0, 0]])) as Record<MainStat, [number, number]>,
initiative: 0,
aspect: "",
notes: character.notes ?? "",
};
features.forEach(e => e[1].forEach(_e => _e.features?.forEach(f => applyFeature(compiled, f))));
//features.forEach(e => e[1].forEach(_e => _e.features?.forEach(f => applyFeature(compiled, f))));
return compiled;
}
function applyFeature(character: CompiledCharacter, f: FeatureItem)
{
switch(f.category)
{
case "action":
character.features.action.push(f.text);
return;
case "reaction":
character.features.reaction.push(f.text);
return;
case "freeaction":
character.features.freeaction.push(f.text);
return;
case "misc":
character.features.misc.push(f.text);
return;
case "asset":
if(f.type === 'add')
character[f.kind].push(f.asset);
else
character[f.kind] = character[f.kind].filter(e => e !== f.asset);
return;
case "value":
const path = f.property.split(".");
const object = path.slice(0, -1).reduce((p, v) => p[v], character as any);
switch(f.type)
{
case "add":
if(!['number'].includes(typeof object[path[path.length - 1]]))
break;
object[path[path.length - 1]] += f.value;
break;
case "remove":
if(!['number'].includes(typeof object[path[path.length - 1]]))
break;
object[path[path.length - 1]] -= f.value as number;
break;
case "set":
if(!['number', 'boolean'].includes(typeof object[path[path.length - 1]]))
break;
object[path[path.length - 1]] = f.value;
break;
default:
break;
}
return;
default:
return;
}
}
export function getFeaturesOf(stat: MainStat, progression: DoubleIndex<TrainingLevel>[]): TrainingOption[]
{
const config = characterData as CharacterConfig;