ConceptCreator/src/app/project/parser/gamesystemParser/GamesystemParser.ts
Sebastian Böckelmann ed2f28760e
Some checks failed
E2E Testing / test (push) Failing after 1m30s
Refactor Gamesystem parsing
2024-03-20 11:19:32 +01:00

61 lines
2.4 KiB
TypeScript

import {StoreComponent} from "../../../../../app/storage/StoreComponent";
import {Gamesystem} from "../../game-model/gamesystems/Gamesystem";
import {ParsedParentGamesystems} from "./ParsedParentGamesystems";
import {ProductGamesystem} from "../../game-model/gamesystems/ProductGamesystem";
import {SimpleGamesystem} from "../../game-model/gamesystems/SimpleGamesystem";
export class GamesystemParser {
parsedParentGamesystems: ParsedParentGamesystems[] = []
parsedGamesystems: Gamesystem<any, any>[] = []
public parseGamesystem(storedGamesystem: StoreComponent) {
const parsedGamesystemData = JSON.parse(storedGamesystem.jsonString)
let parsedSystem: Gamesystem<any, any>
if(parsedGamesystemData.childsystems != undefined) {
parsedSystem = this.parseProductGamesystem(parsedGamesystemData)
} else {
parsedSystem = this.parseSimpleGamesystem(parsedGamesystemData)
}
this.parsedGamesystems.push(parsedSystem);
}
parseSimpleGamesystem(gamesystemData: any): SimpleGamesystem {
const simpleGamesystem = new SimpleGamesystem(gamesystemData.componentName, gamesystemData.componentDescription)
return simpleGamesystem
}
parseProductGamesystem(gamesystemData: any) {
const productGamesystem = new ProductGamesystem(gamesystemData.componentName, gamesystemData.componentDescription);
const childsystemNames: string[] = []
for(let i=0; i<gamesystemData.childsystems.length; i++) {
childsystemNames.push(gamesystemData.childsystems[i].componentName)
}
this.parsedParentGamesystems.push(new ParsedParentGamesystems(productGamesystem, childsystemNames))
return productGamesystem;
}
computeGamesystemStructure(): Gamesystem<any, any>[] {
const topGamesystems: Gamesystem<any, any>[] = []
this.parsedGamesystems.forEach(parsedGamesystem => {
const searchedParentsystem = this.findParentsystem(parsedGamesystem.componentName)
if(searchedParentsystem != undefined) {
searchedParentsystem.addChildGamesystem(parsedGamesystem)
} else {
topGamesystems.push(parsedGamesystem)
}
})
return topGamesystems
}
findParentsystem(childsystemName: string) {
for(let i=0; i<this.parsedParentGamesystems.length; i++) {
if(this.parsedParentGamesystems[i].childsystemNames.includes(childsystemName)) {
return this.parsedParentGamesystems[i].parentGamesystem;
}
}
return undefined
}
}