ConceptCreator/src/app/app.component.ts
Sebastian Böckelmann 216d9aec07
All checks were successful
E2E Testing / test (push) Successful in 1m33s
Load and Parse Character InventorySlots
2024-05-11 15:10:57 +02:00

325 lines
14 KiB
TypeScript

import {Component, NgZone, OnInit, ViewChild} from '@angular/core';
import {MatDrawerContainer} from "@angular/material/sidenav";
import {EditorComponent} from "./editor/editor.component";
import {
ScriptAccountOverviewComponent
} from "./side-overviews/script-account-overview/script-account-overview.component";
import {MatDialog} from "@angular/material/dialog";
import {DeleteConfirmationDialogComponent} from "./delete-confirmation-dialog/delete-confirmation-dialog.component";
import {GamescriptOverviewComponent} from "./side-overviews/gamescript-overview/gamescript-overview.component";
import {ModelComponentType} from "./project/game-model/ModelComponentType";
import {ModelComponentTypeUtillities} from "./project/game-model/ModelComponentTypeUtillities";
import {ScriptAccount} from "./project/game-model/scriptAccounts/ScriptAccount";
import {Gamesystem} from "./project/game-model/gamesystems/Gamesystem";
import {ModelComponent} from "./project/game-model/ModelComponent";
import {GameModel} from "./project/game-model/GameModel";
import {StoredGameModel} from "../../app/storage/StoredGameModel";
import {GamesystemParser} from "./project/parser/gamesystemParser/GamesystemParser";
import {ScriptAccountParser} from "./project/parser/ScriptAccountParser";
import {ElectronService} from "./core/services";
import {ScriptAccountSerializer} from "./project/serializer/ScriptAccountSerializer";
import {StoreComponent} from "../../app/storage/StoreComponent";
import {GamesystemSerializer} from "./project/serializer/GamesystemSerializer";
import {Character} from "./project/game-model/characters/Character";
import {CharacterOverviewComponent} from "./side-overviews/character-overview/character-overview.component";
import {CharacterSerializer} from "./project/serializer/CharacterSerializer";
import {CharacterParser} from "./project/parser/characterParser/CharacterParser";
import {TemplateType} from "./project/game-model/templates/TemplateType";
import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
import {ItemSerializer} from "./project/serializer/ItemSerializer";
import {ItemgroupParser} from "./project/parser/itemParser/ItemgroupParser";
import {ItemParser} from "./project/parser/itemParser/ItemParser";
import {ItemgroupCreator} from "./project/game-model/utils/creator/ItemgroupCreator";
import {ItemOverviewComponent} from "./side-overviews/item-overview/item-overview.component";
import {Overview} from "./side-overviews/Overview";
import {ItemCreator} from "./project/game-model/utils/creator/ItemCreator";
import {ScriptAccountCreator} from "./project/game-model/utils/creator/ScriptAccountCreator";
import {CharacterCreator} from "./project/game-model/utils/creator/CharacterCreator";
import {GamesystemCreator} from "./project/game-model/utils/creator/GamesystemCreator";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
openContent : ModelComponentType | undefined = undefined;
@ViewChild('drawer') drawer: MatDrawerContainer|undefined
@ViewChild('editor') editor: EditorComponent|undefined
@ViewChild('scriptAccountOverview') scriptAccountOverview: ScriptAccountOverviewComponent | undefined
@ViewChild('gamesystemOverview') gamesystemOverview: GamescriptOverviewComponent | undefined
@ViewChild('characterOverview') characterOverview: CharacterOverviewComponent | undefined
@ViewChild('itemOverview') itemOverview: ItemOverviewComponent | undefined
gameModel: GameModel | undefined
constructor(private electronService: ElectronService,
private dialog: MatDialog,
private zone: NgZone
) {
if(electronService.isElectron) {
electronService.ipcRenderer.on('context-menu', (event: any, message: string) => {
this.zone.run(() => {
this.onContextMenuMessageRecieved(message);
});
})
electronService.ipcRenderer.on('get-project-data', (event: any, message: string) => {
this.zone.run(() => {
this.onSaveProject();
})
})
electronService.ipcRenderer.on('open-project', (event: any, loadedProject: StoredGameModel) => {
this.zone.run(() => {
this.onLoadProject(loadedProject)
})
})
}
}
onContextMenuMessageRecieved(message: string) {
if(message == "edit") {
this.onEditModelComponent();
} else if(message == "delete") {
this.onDeleteModelComponent();
} else if(message.startsWith("new")) {
const splittedMessage = message.split("-");
const modelComponentType = ModelComponentTypeUtillities.fromString(splittedMessage[1]);
if(modelComponentType !== undefined) {
let creationContext = "";
if(splittedMessage.length > 2) {
creationContext = splittedMessage[2];
}
let componentCreator;
switch (modelComponentType) {
case ModelComponentType.ITEMGROUP: {
componentCreator = new ItemgroupCreator(creationContext, this.gameModel!, this.selectedModelComponent);
} break
case ModelComponentType.ITEM: {
componentCreator = new ItemCreator(creationContext, this.gameModel!, this.selectedModelComponent);
} break
case ModelComponentType.SCRIPTACCOUNT: {
componentCreator = new ScriptAccountCreator(creationContext, this.gameModel!, this.selectedModelComponent);
} break
case ModelComponentType.CHARACTER: {
componentCreator = new CharacterCreator(creationContext, this.gameModel!, this.selectedModelComponent);
} break
case ModelComponentType.GAMESYTEM: {
componentCreator = new GamesystemCreator(creationContext, this.gameModel!, this.selectedModelComponent);
} break
}
if(componentCreator) {
const createdModel = componentCreator.createModelComponent();
this.openModelComponent(createdModel!)
const openedOverview = this.openedOverview;
if(openedOverview) {
openedOverview.refresh()
}
} else {
console.log("[ERROR] Unknown Creation Command: ", message)
}
}
}
}
private onEditModelComponent() {
switch (this.openContent!) {
case ModelComponentType.SCRIPTACCOUNT: {
if(this.scriptAccountOverview!.selectedScriptAccount != undefined) {
this.editor!.openGameModelComponent(this.scriptAccountOverview!.selectedScriptAccount);
}
} break;
case ModelComponentType.GAMESYTEM: {
if(this.gamesystemOverview!.selectedGamesystem != undefined) {
const gamesystem = this.gameModel!.findGamesystem(this.gamesystemOverview!.selectedGamesystemName!);
this.editor!.openGameModelComponent(gamesystem!);
}
} break
case ModelComponentType.CHARACTER: {
if(this.characterOverview!.selectedCharacter != undefined) {
this.editor!.openGameModelComponent(this.characterOverview!.selectedCharacter);
}
}break
}
}
private onDeleteModelComponent() {
const affectedModelComponent = this.getSelectedModelComponent();
console.log("Affected ModelComponent: ", affectedModelComponent)
const dialogRef = this.dialog.open(DeleteConfirmationDialogComponent, {data: affectedModelComponent, minWidth: "400px"});
dialogRef.afterClosed().subscribe(res => {
if(res != undefined && res) {
if(affectedModelComponent instanceof ScriptAccount) {
this.gameModel!.removeScriptAccount(affectedModelComponent);
//this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.SCRIPTACCOUNT))
} else if(affectedModelComponent instanceof Gamesystem) {
this.gameModel!.removeGamesystem(affectedModelComponent);
//this.electronService.ipcRenderer.send('delete-component', new DeleteModel(affectedModelComponent.componentName, ModelComponentType.GAMESYTEM))
this.gamesystemOverview!.refresh()
} else if(affectedModelComponent instanceof Character) {
this.gameModel!.removeCharacter(affectedModelComponent)
}
}
})
}
private getSelectedModelComponent(): ModelComponent | undefined {
if(this.openContent == ModelComponentType.SCRIPTACCOUNT) {
if(this.scriptAccountOverview != undefined) {
return this.scriptAccountOverview.selectedScriptAccount;
} else {
console.log("[WARN] [App.component] ScriptAccountOverview is undefined")
}
} else if(this.openContent == ModelComponentType.GAMESYTEM){
if(this.gamesystemOverview != undefined) {
return this.gamesystemOverview.getSelectedGamesystem()
} else {
console.log("[WARN] [App.component] GamesystemOverview is undefined")
}
} else if(this.openContent == ModelComponentType.CHARACTER) {
if(this.characterOverview != undefined) {
return this.characterOverview.selectedCharacter;
} else {
console.log("[WARN] [App.component] ScriptAccountOverview is undefined")
}
}
return undefined;
}
ngOnInit() {
}
onLoadProject(storedGameModel: StoredGameModel) {
const gameModel = new GameModel(storedGameModel.gameModelName)
const scriptAccounts = ScriptAccountParser.parseScriptAccounts(storedGameModel.storedScriptAccounts);
const gamesystemParser = new GamesystemParser(scriptAccounts);
const gamesystems = gamesystemParser.parseStoredGamesystems(storedGameModel.storedGamesystems);
gameModel.scriptAccounts = scriptAccounts
gameModel.gamesystems = gamesystems
gameModel.generateProductSystemContents()
const itemgroupParser = new ItemgroupParser();
itemgroupParser.parseItemgroups(storedGameModel.storedItemgroups);
gameModel.itemgroups = itemgroupParser.getParsedTopItemgroups();
const itemParser = new ItemParser(itemgroupParser.getParsedItemgroups())
itemParser.parseItems(storedGameModel.storedItems);
const characterTemplateSystems = gameModel.getTemplateSystems(TemplateType.CHARACTER).map(templateSystem => templateSystem as SimpleTemplateGamesystem)
const characterRelationTemplateSystems = gameModel.getTemplateSystems(TemplateType.CHARACTER_RELATION).map(templateSystem => templateSystem as SimpleTemplateGamesystem)
const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts, itemgroupParser.getCharacteristics(), itemgroupParser.getParsedItemgroups());
gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters)
this.gameModel = gameModel;
}
onSaveProject() {
if(this.gameModel != undefined) {
const storedScriptAccounts = ScriptAccountSerializer.serializeScriptAccounts(this.gameModel.scriptAccounts)
const storedGamesystems: StoreComponent[] = GamesystemSerializer.serializeGamesystems(this.gameModel.gamesystems)
const storedCharacters: StoreComponent[] = CharacterSerializer.serializeCharacters(this.gameModel.characters)
const itemSerializer = new ItemSerializer();
const storedItemgroups: StoreComponent[] = itemSerializer.serializeItemgroups(this.gameModel!.itemgroups);
const storedItems: StoreComponent[] = itemSerializer.getSerializedItems()
const storeModel = new StoredGameModel(this.gameModel.gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups, storedItems)
if(this.electronService.isElectron) {
this.electronService.ipcRenderer.send('save-model', storeModel)
}
}
}
openScriptAccountsOverview() {
this.openContent = ModelComponentType.SCRIPTACCOUNT
this.drawer!.open();
}
openGamesystemsOverview() {
this.openContent = ModelComponentType.GAMESYTEM;
this.drawer!.open();
}
openCharactersOverview() {
this.openContent = ModelComponentType.CHARACTER
this.drawer!.open()
}
openOverview(overviewType: ModelComponentType) {
this.openContent = overviewType;
this.drawer!.open();
}
protected readonly ModelComponentType = ModelComponentType;
closeContentOverview() {
this.drawer!.close();
this.openContent = undefined;
}
protected readonly ModelComponentTypeUtillities = ModelComponentTypeUtillities;
openModelComponent(modelComponent: ModelComponent) {
if(this.editor != undefined) {
this.editor.openGameModelComponent(modelComponent);
} else {
console.log("[WARN] [App.Component] Editor is undefined")
}
}
onModelNameUpdate() {
if(this.openContent == ModelComponentType.GAMESYTEM) {
this.gamesystemOverview!.refresh();
}
}
resetSelection() {
if(this.gamesystemOverview != undefined) {
this.gamesystemOverview.resetSelectedGamesystem()
}
}
get selectedModelComponent(): ModelComponent | undefined {
switch (this.openContent) {
case ModelComponentType.GAMESYTEM: return this.gamesystemOverview!.getSelectedGamesystem();
case ModelComponentType.CHARACTER: return this.characterOverview!.selectedCharacter;
case ModelComponentType.SCRIPTACCOUNT: return this.scriptAccountOverview!.selectedScriptAccount;
case ModelComponentType.ITEMGROUP: return this.itemOverview!.selectedModelComponent;
case ModelComponentType.ITEM: return this.itemOverview!.selectedModelComponent;
default: return undefined
}
}
get openedOverview(): Overview | undefined {
if(this.openContent === ModelComponentType.ITEMGROUP || this.openContent === ModelComponentType.ITEM) {
return this.itemOverview;
} else if(this.openContent === ModelComponentType.SCRIPTACCOUNT) {
return this.scriptAccountOverview;
} else if(this.openContent === ModelComponentType.CHARACTER) {
return this.characterOverview;
} else if(this.openContent === ModelComponentType.GAMESYTEM) {
return this.gamesystemOverview;
}
}
}