addon/.support/utils/entities/encounter.ts

62 lines
2 KiB
TypeScript
Raw Normal View History

import { TolgeeKey, TreeNode } from '../../types';
import { writeAlsoFemaleTranslation } from '../translation';
export function buildTree(inputs: string[], layer = ''): TreeNode[] {
const root: TreeNode[] = [];
inputs
.filter((input) => new RegExp(`_(${layer})([0-9]{2})$`).test(input))
.forEach((input) => {
const layerIndex = input.split('_').at(-1);
root.push({
key: input,
children: buildTree(inputs, layerIndex),
});
});
return root;
}
export const getEncounterKey = (tolgeeKey: TolgeeKey): string => {
const [, difficulty, instance, type, key] = tolgeeKey.keyName.split('_');
return [tolgeeKey.keyDescription, difficulty, instance, type, key].join('_');
};
export const getEncounterGroupKey = (tolgeeKey: TolgeeKey): string => {
const [, difficulty, instance] = tolgeeKey.keyName.split('_');
return [tolgeeKey.keyDescription, difficulty, instance].join('_');
};
export const writeEncounter = (tolgeeKey: TolgeeKey): string => {
let luaRecord = '';
const keyName = getEncounterKey(tolgeeKey);
luaRecord += `addon.data.encounter["${keyName}"] = {\n`;
luaRecord += writeAlsoFemaleTranslation(tolgeeKey.translations);
luaRecord += `}\n`;
return luaRecord;
};
const writeStructure = (structure: TreeNode[], layer = 0): string => {
let luaRecord = '';
const tab = '\t'.repeat(layer);
const layerIndex = layer + 1;
structure.forEach((node) => {
const hasEmptyChildren = node.children.length === 0;
luaRecord += `${tab}{\n`;
luaRecord += `\t${tab}key = "${node.key}",\n`;
luaRecord += `\t${tab}children = {${hasEmptyChildren ? '' : '\n'}`;
luaRecord += `${writeStructure(node.children, layerIndex)}`;
luaRecord += `${hasEmptyChildren ? '' : `\t${tab}`}},\n`;
luaRecord += `${tab}},\n`;
});
return luaRecord;
};
export const writeEncounterStructure = (key: string, structure: TreeNode[]): string => {
let luaRecord = '';
luaRecord += `addon.data.encounter["${key}"] = {\n`;
luaRecord += writeStructure(structure);
luaRecord += `}\n`;
return luaRecord;
};