From fbf1a5dc7b7b62bb408452214813715218c21402 Mon Sep 17 00:00:00 2001 From: Walidoux Date: Fri, 28 Apr 2023 14:41:16 +0100 Subject: [PATCH] refactor(controllers): classes and parse libraries, sets and palettes - Refactored several classes in controllers directory and modified parsing of libraries, sets and palettes. --- src/controllers/EffectMap.ts | 19 ++++-------- src/controllers/FigureData.ts | 57 ++++++++++++++--------------------- src/controllers/FigureMap.ts | 19 +++++------- src/controllers/FurniData.ts | 13 +++----- 4 files changed, 40 insertions(+), 68 deletions(-) diff --git a/src/controllers/EffectMap.ts b/src/controllers/EffectMap.ts index f8775ea..115733a 100644 --- a/src/controllers/EffectMap.ts +++ b/src/controllers/EffectMap.ts @@ -1,26 +1,19 @@ -import { Convertion } from '../config/Convertion' -import { parseData } from '../tools/parseData' import type { IEffectMap, IXML } from '../types' export class EffectMap { - public data: IEffectMap = { effects: [] } + public data: IEffectMap = {} + public fileName: string constructor(XML: IXML, fileName: string) { - this.parseLibrairies(XML.map.effect) + this.fileName = fileName - parseData(Convertion.gamedataConfigDir, fileName, this.data).catch((error) => { - return console.error(error) - }) + this.parseLibrairies(XML.map.effect) } private parseLibrairies(effects: any[]): void { for (const libraryXML of effects) { - this.data.effects.push({ - id: Number(libraryXML.id), - lib: libraryXML.lib, - type: libraryXML.type, - revision: Number(libraryXML.revision) - }) + this.data[libraryXML.type] == null && (this.data[libraryXML.type] = {}) + this.data[libraryXML.type][libraryXML.id] = libraryXML.lib } } } diff --git a/src/controllers/FigureData.ts b/src/controllers/FigureData.ts index 8a8c470..2990746 100644 --- a/src/controllers/FigureData.ts +++ b/src/controllers/FigureData.ts @@ -1,38 +1,27 @@ -import { parseData } from '../tools/parseData' -import type { - IFigureData, - IFigureDataColor, - IFigureDataPalette, - IFigureDataSet, - IFigureDataSetType, - IXML -} from '../types' -import { Convertion } from '../config/Convertion' +import type { IFigureData, IFigureDataPalette, IFigureDataSet, IFigureDataSetType, IXML } from '../types' export class FigureData { - public data: IFigureData = { palettes: [], setTypes: [] } + public data: IFigureData = { palettes: [], setTypes: {} } + public fileName: string constructor(XML: IXML, fileName: string) { + this.fileName = fileName + this.parsePalettes(XML.figuredata.colors.palette) this.parseSetTypes(XML.figuredata.sets.settype) - - parseData(Convertion.gamedataConfigDir, fileName, this.data).catch((error) => { - return console.error(error) - }) } private parsePalettes(palettes: any[]): void { for (const paletteXML of palettes) { - const palette: IFigureDataPalette = { id: Number(paletteXML.id), color: [] } + const palette = {} as IFigureDataPalette for (const colorXML of paletteXML.color) { - palette.color.push({ - id: Number(colorXML.id), + palette[Number(colorXML.id)] = { index: Number(colorXML.index), club: Number(colorXML.club), selectable: Boolean(colorXML.selectable), - hexCode: String(colorXML['#text' as keyof IFigureDataColor]) - }) + color: '#' + String(colorXML['#text']) + } } this.data.palettes.push(palette) @@ -42,24 +31,22 @@ export class FigureData { private parseSetTypes(setTypes: any[]): void { for (const setTypeXML of setTypes) { const settype: IFigureDataSetType = { - type: setTypeXML.type, paletteId: Number(setTypeXML.paletteid), - mandatoryF0: Boolean(setTypeXML.mand_f_0), - mandatoryF1: Boolean(setTypeXML.mand_f_1), - mandatoryM0: Boolean(setTypeXML.mand_m_0), - mandatoryM1: Boolean(setTypeXML.mand_m_1), - sets: [] + mandatoryF0: Boolean(Number(setTypeXML.mand_f_0)), + mandatoryF1: Boolean(Number(setTypeXML.mand_f_1)), + mandatoryM0: Boolean(Number(setTypeXML.mand_m_0)), + mandatoryM1: Boolean(Number(setTypeXML.mand_m_1)), + sets: {} } for (const setXML of setTypeXML.set) { const setType: IFigureDataSet = { - id: Number(setXML.id), gender: setXML.gender, club: Number(setXML.club), - colorable: Boolean(setXML.colorable), - selectable: Boolean(setXML.selectable), - preselectable: Boolean(setXML.preselectable), - sellable: Boolean(setXML.sellable), + colorable: Boolean(Number(setXML.colorable)), + selectable: Boolean(Number(setXML.selectable)), + preselectable: Boolean(Number(setXML.preselectable)), + sellable: setXML.sellable != null ? Boolean(Number(setXML.sellable)) : undefined, parts: [], hiddenLayers: [] } @@ -68,7 +55,7 @@ export class FigureData { setType.parts.push({ id: Number(partXML.id), type: partXML.type, - colorable: Boolean(partXML.colorable), + colorable: Boolean(Number(partXML.colorable)), index: Number(partXML.index), colorindex: Number(partXML.colorindex) }) @@ -78,14 +65,14 @@ export class FigureData { for (const hiddenLayerXML of Array.isArray(setXML.hiddenLayers) ? setXML.hiddenLayers : [setXML.hiddenLayers]) { - setType.hiddenLayers.push({ partType: hiddenLayerXML.partType }) + setType.hiddenLayers?.push(hiddenLayerXML.partType) } } - settype.sets.push(setType) + settype.sets[Number(setXML.id)] = setType } - this.data.setTypes.push(setTypeXML) + this.data.setTypes[String(setTypeXML.type)] = settype } } } diff --git a/src/controllers/FigureMap.ts b/src/controllers/FigureMap.ts index e088177..6d007ea 100644 --- a/src/controllers/FigureMap.ts +++ b/src/controllers/FigureMap.ts @@ -1,30 +1,25 @@ -import { Convertion } from '../config/Convertion' -import { parseData } from '../tools/parseData' import type { IFigureMap, IFigureMapLibrary, IXML } from '../types' export class FigureMap { - public data: IFigureMap = { libraries: [] } + public data: IFigureMap = { libraries: [], parts: {} } + public fileName: string constructor(XML: IXML, fileName: string) { - this.parseLibrairies(XML.map.lib) + this.fileName = fileName - parseData(Convertion.gamedataConfigDir, fileName, this.data).catch((error) => { - return console.error(error) - }) + this.parseLibrairies(XML.map.lib) } private parseLibrairies(librairies: any[]): void { for (const libraryXML of librairies) { - const library: IFigureMapLibrary = { id: libraryXML.id, revision: Number(libraryXML.revision), part: [] } + const library: IFigureMapLibrary = { id: libraryXML.id, revision: Number(libraryXML.revision) } for (const libraryPart of Array.isArray(libraryXML.part) ? libraryXML.part : [libraryXML.part]) { - library.part.push({ id: Number(libraryPart.id), type: libraryPart.type }) + this.data.parts[libraryPart.type] == null && (this.data.parts[libraryPart.type] = {}) + this.data.parts[libraryPart.type][Number(libraryPart.id)] = librairies.indexOf(libraryXML) } this.data.libraries.push(library) } } - - // maybe - /* public get classNamesAndRevisions(data: IFigureData): { [index: string]: string } {} */ } diff --git a/src/controllers/FurniData.ts b/src/controllers/FurniData.ts index bb83cd6..313fadf 100644 --- a/src/controllers/FurniData.ts +++ b/src/controllers/FurniData.ts @@ -1,17 +1,14 @@ -import { parseData } from '../tools/parseData' import type { IFurni, IFurniData, IXML, KeyValuePairs } from '../types' -import { Convertion } from '../config/Convertion' export class FurniData { public data: IFurniData = { roomItemTypes: [], wallItemTypes: [] } + public fileName: string constructor(data: IXML, fileName: string) { + this.fileName = fileName + this.parseRoomItemTypes(data.roomitemtypes.furnitype) this.parseWallItemTypes(data.roomitemtypes.furnitype) - - parseData(Convertion.gamedataConfigDir, fileName, this.data).catch((error) => { - return console.error(error) - }) } private parseRoomItemTypes(roomItems: IFurni[]): void { @@ -33,8 +30,8 @@ export class FurniData { return { className, revision } } - public get classNamesAndRevisions(): KeyValuePairs { - const entries: KeyValuePairs = {} + public get classNamesAndRevisions(): KeyValuePairs { + const entries: KeyValuePairs = {} for (const roomItem of this.data.roomItemTypes) { const { className, revision } = this.getClassNameRevision(roomItem)