issue-12 #14

Merged
sebastian merged 6 commits from issue-12 into main 2024-02-17 15:14:34 +01:00
28 changed files with 671 additions and 25 deletions

11
app/LoadModel.js Normal file
View File

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoadModel = void 0;
class LoadModel {
constructor(jsonString, modelType) {
this.jsonString = jsonString;
this.modelType = modelType;
}
}
exports.LoadModel = LoadModel;
//# sourceMappingURL=LoadModel.js.map

12
app/LoadModel.ts Normal file
View File

@ -0,0 +1,12 @@
import {ModelComponentType} from "../src/app/game-model/ModelComponentType";
export class LoadModel {
jsonString: string
modelType: ModelComponentType
constructor(jsonString: string, modelType: ModelComponentType) {
this.jsonString = jsonString;
this.modelType = modelType;
}
}

11
app/LoadedProject.js Normal file
View File

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoadedProject = void 0;
class LoadedProject {
constructor(projectName, loadedModels) {
this.projectName = projectName;
this.loadedModels = loadedModels;
}
}
exports.LoadedProject = LoadedProject;
//# sourceMappingURL=LoadedProject.js.map

12
app/LoadedProject.ts Normal file
View File

@ -0,0 +1,12 @@
import {LoadModel} from "./LoadModel";
export class LoadedProject {
projectName: string
loadedModels: LoadModel[]
constructor(projectName: string, loadedModels: LoadModel[]) {
this.projectName = projectName;
this.loadedModels = loadedModels;
}
}

12
app/RecursiveLoadModel.js Normal file
View File

@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecursiveLoadModel = void 0;
const LoadModel_1 = require("./LoadModel");
class RecursiveLoadModel extends LoadModel_1.LoadModel {
constructor(jsonString, modelType, parentLoadModelname) {
super(jsonString, modelType);
this.parentLoadModelname = parentLoadModelname;
}
}
exports.RecursiveLoadModel = RecursiveLoadModel;
//# sourceMappingURL=RecursiveLoadModel.js.map

12
app/RecursiveLoadModel.ts Normal file
View File

@ -0,0 +1,12 @@
import {LoadModel} from "./LoadModel";
import {ModelComponentType} from "../src/app/game-model/ModelComponentType";
export class RecursiveLoadModel extends LoadModel {
parentLoadModelname: string
constructor(jsonString: string, modelType: ModelComponentType, parentLoadModelname: string) {
super(jsonString, modelType);
this.parentLoadModelname = parentLoadModelname;
}
}

100
app/SaveProject.js Normal file
View File

@ -0,0 +1,100 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveProject = void 0;
const fs = require("fs");
const path = require("node:path");
const LoadModel_1 = require("./LoadModel");
const ModelComponentType_1 = require("../src/app/game-model/ModelComponentType");
const LoadedProject_1 = require("./LoadedProject");
const RecursiveLoadModel_1 = require("./RecursiveLoadModel");
class SaveProject {
static saveProject(projectDir, storageModels) {
if (!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir, { recursive: true });
}
console.log(storageModels);
storageModels.forEach(storageModel => {
let modelDir = path.join(projectDir, storageModel.storageRootDir);
storageModel.storagePath.forEach(pathElement => modelDir = path.join(modelDir, pathElement));
if (!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, { recursive: true });
}
const filePath = path.join(modelDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString, 'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
}
else {
console.log('JSON file saved successfully:', filePath);
}
});
});
}
static loadProject(projectDir) {
let loadedScriptAccounts = SaveProject.loadScriptAccounts(projectDir);
loadedScriptAccounts = loadedScriptAccounts.concat(SaveProject.loadGamesystems(projectDir));
return new LoadedProject_1.LoadedProject(path.basename(projectDir), loadedScriptAccounts);
}
static loadScriptAccounts(projectDir) {
const scriptAccountDir = path.join(projectDir, "script-accounts");
if (!fs.existsSync(scriptAccountDir)) {
return [];
}
const loadedScriptAccounts = [];
const scriptAccountFileNames = fs.readdirSync(scriptAccountDir);
scriptAccountFileNames.forEach(scriptAccountFileName => {
const scriptAccountFile = path.join(scriptAccountDir, scriptAccountFileName);
const scriptAccountData = fs.readFileSync(scriptAccountFile, 'utf-8');
loadedScriptAccounts.push({
modelType: ModelComponentType_1.ModelComponentType.SCRIPTACCOUNT,
jsonString: scriptAccountData
});
});
return loadedScriptAccounts;
}
static loadGamesystems(projectDir) {
const gamesystemDir = path.join(projectDir, "gamesystems");
const loadedGamesystems = this.loadGamesystemsRecursively(gamesystemDir);
console.log("LoadedGamesystems: ", loadedGamesystems.length);
return loadedGamesystems;
}
static loadGamesystemsRecursively(gamesystemDir) {
let loadedGamesystems = [];
const gamesystemFileNames = fs.readdirSync(gamesystemDir);
gamesystemFileNames.forEach(fileName => {
const gamesystemPath = path.join(gamesystemDir, fileName);
if (fs.lstatSync(gamesystemPath).isDirectory()) {
const childModels = SaveProject.loadGamesystemsRecursively(gamesystemPath);
loadedGamesystems = loadedGamesystems.concat(childModels);
}
else {
const gamesystemData = fs.readFileSync(path.join(gamesystemDir, fileName), "utf-8");
if (path.parse(fileName).name === path.basename(gamesystemDir)) {
if ((path.basename(gamesystemDir) === path.parse(fileName).name) && path.basename(path.parse(gamesystemDir).dir) === "gamesystems") {
const loadedModel = new LoadModel_1.LoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM);
loadedGamesystems.unshift(loadedModel);
}
else {
const loadedModel = new RecursiveLoadModel_1.RecursiveLoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM, path.basename(gamesystemDir));
loadedGamesystems.unshift(loadedModel);
}
}
else {
const secondCon = path.basename(gamesystemDir) === path.parse(fileName).name;
const thirdCon = path.basename(path.parse(gamesystemDir).dir) === "gamesystems";
if (path.basename(gamesystemDir) === "gamesystems") {
const loadedModel = new LoadModel_1.LoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM);
loadedGamesystems.push(loadedModel);
}
else {
const loadedModel = new RecursiveLoadModel_1.RecursiveLoadModel(gamesystemData, ModelComponentType_1.ModelComponentType.GAMESYTEM, path.basename(gamesystemDir));
loadedGamesystems.push(loadedModel);
}
}
}
});
return loadedGamesystems;
}
}
exports.SaveProject = SaveProject;
//# sourceMappingURL=SaveProject.js.map

110
app/SaveProject.ts Normal file
View File

@ -0,0 +1,110 @@
import {StorageModel} from "../src/app/game-model/fs/StorageModel";
import * as fs from "fs";
import * as path from "node:path";
import {LoadModel} from "./LoadModel";
import {ModelComponentType} from "../src/app/game-model/ModelComponentType";
import {LoadedProject} from "./LoadedProject";
import {RecursiveLoadModel} from "./RecursiveLoadModel";
export class SaveProject {
static saveProject(projectDir: string, storageModels: StorageModel[]) {
if(!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir, {recursive: true});
}
console.log(storageModels)
storageModels.forEach(storageModel => {
let modelDir = path.join(projectDir, storageModel.storageRootDir);
storageModel.storagePath.forEach(pathElement => modelDir = path.join(modelDir, pathElement));
if(!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, {recursive: true});
}
const filePath = path.join(modelDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString ,'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
} else {
console.log('JSON file saved successfully:', filePath);
}
})
})
}
static loadProject(projectDir: string) {
let loadedScriptAccounts: LoadModel[] = SaveProject.loadScriptAccounts(projectDir)
loadedScriptAccounts = loadedScriptAccounts.concat(SaveProject.loadGamesystems(projectDir))
return new LoadedProject(path.basename(projectDir), loadedScriptAccounts);
}
static loadScriptAccounts(projectDir: string): LoadModel[] {
const scriptAccountDir = path.join(projectDir, "script-accounts");
if (!fs.existsSync(scriptAccountDir)) {
return [];
}
const loadedScriptAccounts: LoadModel[] = [];
const scriptAccountFileNames = fs.readdirSync(scriptAccountDir);
scriptAccountFileNames.forEach(scriptAccountFileName => {
const scriptAccountFile = path.join(scriptAccountDir, scriptAccountFileName)
const scriptAccountData: string = fs.readFileSync(scriptAccountFile, 'utf-8');
loadedScriptAccounts.push({
modelType: ModelComponentType.SCRIPTACCOUNT,
jsonString: scriptAccountData
});
})
return loadedScriptAccounts;
}
static loadGamesystems(projectDir: string): LoadModel[] {
const gamesystemDir = path.join(projectDir, "gamesystems");
const loadedGamesystems = this.loadGamesystemsRecursively(gamesystemDir);
console.log("LoadedGamesystems: ", loadedGamesystems.length);
return loadedGamesystems;
}
static loadGamesystemsRecursively(gamesystemDir: string): LoadModel[] {
let loadedGamesystems: LoadModel[] = [];
const gamesystemFileNames = fs.readdirSync(gamesystemDir);
gamesystemFileNames.forEach(fileName => {
const gamesystemPath = path.join(gamesystemDir, fileName);
if(fs.lstatSync(gamesystemPath).isDirectory()) {
const childModels: LoadModel[] = SaveProject.loadGamesystemsRecursively(gamesystemPath);
loadedGamesystems = loadedGamesystems.concat(childModels);
} else {
const gamesystemData = fs.readFileSync(path.join(gamesystemDir, fileName), "utf-8");
if(path.parse(fileName).name === path.basename(gamesystemDir) ) {
if((path.basename(gamesystemDir) === path.parse(fileName).name) && path.basename(path.parse(gamesystemDir).dir) === "gamesystems") {
const loadedModel = new LoadModel(gamesystemData, ModelComponentType.GAMESYTEM);
loadedGamesystems.unshift(loadedModel)
} else {
const loadedModel = new RecursiveLoadModel(gamesystemData, ModelComponentType.GAMESYTEM, path.basename(gamesystemDir))
loadedGamesystems.unshift(loadedModel);
}
} else {
const secondCon = path.basename(gamesystemDir) === path.parse(fileName).name
const thirdCon = path.basename(path.parse(gamesystemDir).dir) === "gamesystems"
if(path.basename(gamesystemDir) === "gamesystems"){
const loadedModel = new LoadModel(gamesystemData, ModelComponentType.GAMESYTEM)
loadedGamesystems.push(loadedModel);
} else {
const loadedModel = new RecursiveLoadModel(gamesystemData, ModelComponentType.GAMESYTEM, path.basename(gamesystemDir))
loadedGamesystems.push(loadedModel);
}
}
}
})
return loadedGamesystems;
}
}

12
app/StorageModel.ts Normal file
View File

@ -0,0 +1,12 @@
class StorageModel {
jsonString: string
fileName: string
storageDir: string
constructor(jsonString: string, fileName: string, storageDir: string) {
this.jsonString = jsonString;
this.fileName = fileName;
this.storageDir = storageDir;
}
}

View File

@ -1,11 +1,15 @@
import {app, BrowserWindow, screen, Menu, ipcMain} from 'electron'; import {app, BrowserWindow, screen, Menu, ipcMain, dialog, globalShortcut} from 'electron';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import {json} from "node:stream/consumers";
import {StorageModel} from "../src/app/game-model/fs/StorageModel";
import {SaveProject} from "./SaveProject";
let win: BrowserWindow | null = null; let win: BrowserWindow | null = null;
const args = process.argv.slice(1), const args = process.argv.slice(1),
serve = args.some(val => val === '--serve'); serve = args.some(val => val === '--serve');
let projectDirectory = "testModel"
function createWindow(): BrowserWindow { function createWindow(): BrowserWindow {
const size = screen.getPrimaryDisplay().workAreaSize; const size = screen.getPrimaryDisplay().workAreaSize;
@ -90,6 +94,38 @@ function createWindow(): BrowserWindow {
contextMenu.popup({ window: win!, x: params.x, y: params.y }); contextMenu.popup({ window: win!, x: params.x, y: params.y });
}) })
ipcMain.on('save-model', (event, storageModels: StorageModel[]) => {
console.log("Save Model")
SaveProject.saveProject(projectDirectory, storageModels);
})
const menuTemplate = [
{
label: 'File',
submenu: [
{
label: "New Project",
click: () => {
createNewProject();
}
},
{
label: "Open Project",
click: () => {
openProject()
}
},
{
label: "Save",
click: () => {
saveProject();
}
}
]
}
]
const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu)
return win; return win;
} }
@ -118,7 +154,38 @@ try {
} }
}); });
app.whenReady().then(() => {
globalShortcut.register('CommandOrControl+S', () => {
saveProject();
})
})
} catch (e) { } catch (e) {
// Catch Error // Catch Error
// throw e; // throw e;
} }
function createNewProject() {
}
function openProject() {
const selectedPaths = dialog.showOpenDialogSync(win!, {
title: "Open Game-Model",
properties: [
"openDirectory",
]
})
if(selectedPaths != undefined) {
projectDirectory = selectedPaths[0];
console.log("Open Project-Directory: ", projectDirectory)
const loadedProject = SaveProject.loadProject(projectDirectory)
win!.webContents.send("open-project", loadedProject)
}
}
function saveProject() {
win!.webContents.send('get-project-data')
}

View File

@ -16,13 +16,13 @@ export class ProductStateTrainer {
static PRODUCT_GAMESYSTEM_NAME = "Product Gamesystem"; static PRODUCT_GAMESYSTEM_NAME = "Product Gamesystem";
static givenFullProductGamesystemWithTwoStates() { static givenFullProductGamesystemWithTwoStates() {
const letter_Gamesystem = new SimpleGamesystem(this.LETTERS_GAMESYSTEM_NAME); const letter_Gamesystem = new SimpleGamesystem(this.LETTERS_GAMESYSTEM_NAME, "");
const letter_1 = letter_Gamesystem.createState(this.INNERSTATE_LETTER_1, "")!; const letter_1 = letter_Gamesystem.createState(this.INNERSTATE_LETTER_1, "")!;
const letter_2 = letter_Gamesystem.createState(this.INNERSTATE_LETTER_2, "")!; const letter_2 = letter_Gamesystem.createState(this.INNERSTATE_LETTER_2, "")!;
const number_gamesystem = new SimpleGamesystem(this.NUMBERS_GAMESYSTEM_NAME); const number_gamesystem = new SimpleGamesystem(this.NUMBERS_GAMESYSTEM_NAME, "");
const number_1 = number_gamesystem.createState(this.INNERSTATE_NUMBER_1, "")!; const number_1 = number_gamesystem.createState(this.INNERSTATE_NUMBER_1, "")!;
const number_2 = number_gamesystem.createState(this.INNERSTATE_NUMBER_2, "")!; const number_2 = number_gamesystem.createState(this.INNERSTATE_NUMBER_2, "")!;
const productGamesystem = new ProductGamesystem(this.PRODUCT_GAMESYSTEM_NAME); const productGamesystem = new ProductGamesystem(this.PRODUCT_GAMESYSTEM_NAME, "");
productGamesystem.states.push(new ProductState( [letter_1, number_1])); productGamesystem.states.push(new ProductState( [letter_1, number_1]));
productGamesystem.states.push(new ProductState( [letter_1, number_2])); productGamesystem.states.push(new ProductState( [letter_1, number_2]));

View File

@ -17,6 +17,10 @@ import {GamescriptOverviewComponent} from "./side-overviews/gamescript-overview/
import {SimpleGamesystem} from "./game-model/gamesystems/SimpleGamesystem"; import {SimpleGamesystem} from "./game-model/gamesystems/SimpleGamesystem";
import {ProductGamesystem} from "./game-model/gamesystems/ProductGamesystem"; import {ProductGamesystem} from "./game-model/gamesystems/ProductGamesystem";
import {ProductState} from "./game-model/gamesystems/ProductState"; import {ProductState} from "./game-model/gamesystems/ProductState";
import {LoadModel} from "../../app/LoadModel";
import {LoadedProject} from "../../app/LoadedProject";
import {ProcessLoadedProject} from "./game-model/fs/ProcessLoadedProject";
import {StoreProject} from "./game-model/fs/store/StoreProject";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -50,11 +54,26 @@ export class AppComponent implements OnInit{
this.onContextMenuMessageRecieved(message); this.onContextMenuMessageRecieved(message);
}); });
}) })
electronService.ipcRenderer.on('get-project-data', (event: any, message: string) => {
this.zone.run(() => {
this.saveGameModel();
})
})
electronService.ipcRenderer.on('open-project', (event: any, loadedProject: LoadedProject) => {
this.gameModel = ProcessLoadedProject.processLoadedProject(loadedProject)
})
} else { } else {
console.log('Run in browser'); console.log('Run in browser');
} }
} }
saveGameModel() {
const storageModels = StoreProject.storeProject(this.gameModel!);
this.electronService.ipcRenderer.send('save-model', storageModels)
}
onContextMenuMessageRecieved(message: string) { onContextMenuMessageRecieved(message: string) {
if(message == "edit") { if(message == "edit") {
this.onEditModelComponent(); this.onEditModelComponent();
@ -142,7 +161,7 @@ export class AppComponent implements OnInit{
} }
ngOnInit() { ngOnInit() {
this.gameModel = new GameModel("No More"); /*this.gameModel = new GameModel("No More");
this.gameModel.createScriptAccount("Temperature"); this.gameModel.createScriptAccount("Temperature");
this.gameModel.createScriptAccount("Luftfeuchtigkeit"); this.gameModel.createScriptAccount("Luftfeuchtigkeit");
@ -164,7 +183,7 @@ export class AppComponent implements OnInit{
weather_season.createState([springState!, sunnyState!]); weather_season.createState([springState!, sunnyState!]);
this.gameModel.addGamesystem(weather_season); this.gameModel.addGamesystem(weather_season);*/
} }
openScriptAccountsOverview() { openScriptAccountsOverview() {

View File

@ -10,7 +10,7 @@
<app-model-component-editor [modelComponent]="modelComponent" (onModelNameUpdated)="onModelNameUpdate()"></app-model-component-editor> <app-model-component-editor [modelComponent]="modelComponent" (onModelNameUpdated)="onModelNameUpdate()"></app-model-component-editor>
<app-script-account-editor *ngIf="modelComponent.type === ModelComponentType.SCRIPTACCOUNT" <app-script-account-editor *ngIf="modelComponent.type === ModelComponentType.SCRIPTACCOUNT"
[scriptAccount]="convertModelComponentToScriptAccount(modelComponent)"></app-script-account-editor> [scriptAccount]="convertModelComponentToScriptAccount(modelComponent)"></app-script-account-editor>
<app-gamesystem-editor *ngIf="modelComponent.type == ModelComponentType.GAMESYTEM" <app-gamesystem-editor *ngIf="modelComponent.type === ModelComponentType.GAMESYTEM"
[gamesystem]="convertModelComponentToGamesystem(modelComponent)" [gamesystem]="convertModelComponentToGamesystem(modelComponent)"
(onOpenGamesystemEditor)="openGameModelComponent($event)"></app-gamesystem-editor> (onOpenGamesystemEditor)="openGameModelComponent($event)"></app-gamesystem-editor>

View File

@ -10,4 +10,6 @@
<input matInput type="number" [formControl]="maxCtrl" (keypress)="onKeyPress($event)" (change)="onUpdateMaxValue()"> <input matInput type="number" [formControl]="maxCtrl" (keypress)="onKeyPress($event)" (change)="onUpdateMaxValue()">
<mat-error *ngIf="maxCtrl.hasError('required')">Please enter a valid number!</mat-error> <mat-error *ngIf="maxCtrl.hasError('required')">Please enter a valid number!</mat-error>
</mat-form-field> </mat-form-field>
<button mat-raised-button color="accent" (click)="save()">Save</button>
</div> </div>

View File

@ -6,6 +6,7 @@ import {MatInput} from "@angular/material/input";
import {FormControl, FormGroupDirective, FormsModule, NgForm, Validators} from "@angular/forms"; import {FormControl, FormGroupDirective, FormsModule, NgForm, Validators} from "@angular/forms";
import {NgIf} from "@angular/common"; import {NgIf} from "@angular/common";
import {ErrorStateMatcher} from "@angular/material/core"; import {ErrorStateMatcher} from "@angular/material/core";
import {ElectronService} from "../../core/services";
export class MyErrorStateMatcher implements ErrorStateMatcher { export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
@ -24,6 +25,9 @@ export class ScriptAccountEditorComponent implements OnInit{
minCtrl: FormControl = new FormControl(0, [Validators.required, Validators.pattern('^[0-9]*$')]); minCtrl: FormControl = new FormControl(0, [Validators.required, Validators.pattern('^[0-9]*$')]);
maxCtrl: FormControl = new FormControl(100, [Validators.required, Validators.pattern('^[0-9]*$')]); maxCtrl: FormControl = new FormControl(100, [Validators.required, Validators.pattern('^[0-9]*$')]);
matcher = new MyErrorStateMatcher(); matcher = new MyErrorStateMatcher();
constructor(private electronService: ElectronService) {
}
ngOnInit() { ngOnInit() {
this.minCtrl.setValue(this.scriptAccount!.minValue); this.minCtrl.setValue(this.scriptAccount!.minValue);
this.maxCtrl.setValue(this.scriptAccount!.maxValue); this.maxCtrl.setValue(this.scriptAccount!.maxValue);
@ -46,4 +50,9 @@ export class ScriptAccountEditorComponent implements OnInit{
this.scriptAccount!.maxValue = Number(this.maxCtrl.value); this.scriptAccount!.maxValue = Number(this.maxCtrl.value);
this.scriptAccount!.onModifyContent(); this.scriptAccount!.onModifyContent();
} }
save() {
const jsonString = JSON.stringify(this.scriptAccount!, null, 4);
this.electronService.ipcRenderer.send('save-json', jsonString, this.scriptAccount!.componentName + ".json");
}
} }

View File

@ -4,6 +4,7 @@ import {Transition} from "./gamesystems/Transition";
import {State} from "./gamesystems/State"; import {State} from "./gamesystems/State";
import {ProductGamesystem} from "./gamesystems/ProductGamesystem"; import {ProductGamesystem} from "./gamesystems/ProductGamesystem";
import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem"; import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem";
import {StorageModel} from "./fs/StorageModel";
export class GameModel { export class GameModel {
private readonly _gameModelName: string private readonly _gameModelName: string
@ -49,7 +50,7 @@ export class GameModel {
createGamesystem(gamesystemName: string, parentGamesystemName: string | undefined) { createGamesystem(gamesystemName: string, parentGamesystemName: string | undefined) {
if(gamesystemName != undefined && this.findGamesystem(gamesystemName) == undefined) { if(gamesystemName != undefined && this.findGamesystem(gamesystemName) == undefined) {
const simpleGamesystem = new SimpleGamesystem(gamesystemName); const simpleGamesystem = new SimpleGamesystem(gamesystemName, "");
if(parentGamesystemName != undefined) { if(parentGamesystemName != undefined) {
const parentGamesystem = this.findGamesystem(parentGamesystemName); const parentGamesystem = this.findGamesystem(parentGamesystemName);
if(parentGamesystem instanceof SimpleGamesystem) { if(parentGamesystem instanceof SimpleGamesystem) {
@ -90,4 +91,7 @@ export class GameModel {
} }
} }
addScriptAccount(scriptAccount: ScriptAccount) {
this.scriptAccounts.push(scriptAccount);
}
} }

View File

@ -1,6 +1,8 @@
export abstract class SaveComponent { export abstract class SaveComponent {
unsaved: boolean = false; unsaved: boolean = false;
static JSON_INDENT = 4;
onModifyContent() { onModifyContent() {
this.unsaved = true; this.unsaved = true;
} }
@ -8,6 +10,4 @@ export abstract class SaveComponent {
onSaveContent() { onSaveContent() {
this.unsaved = false; this.unsaved = false;
} }
abstract save(): void;
} }

View File

@ -0,0 +1,76 @@
import {GameModel} from "../GameModel";
import {LoadedProject} from "../../../../app/LoadedProject";
import {LoadModel} from "../../../../app/LoadModel";
import {ModelComponentType} from "../ModelComponentType";
import {ScriptAccount} from "../scriptAccounts/ScriptAccount";
import {RecursiveLoadModel} from "../../../../app/RecursiveLoadModel";
import {SimpleGamesystemParser} from "./parser/SimpleGamesystemParser";
import {SimpleGamesystem} from "../gamesystems/SimpleGamesystem";
import {ProductGamesystem} from "../gamesystems/ProductGamesystem";
import {ProductGamesystemParser} from "./parser/ProductGamesystemParser";
import {Gamesystem} from "../gamesystems/Gamesystem";
export class ProcessLoadedProject {
static processLoadedProject(loadedProject: LoadedProject): GameModel {
const gameModel = new GameModel(loadedProject.projectName);
loadedProject.loadedModels.forEach(loadedModel => this.processLoadedModel(gameModel, loadedModel))
//Generate product Gamesystems
this.generateProductGamesystems(gameModel.gamesystems)
return gameModel;
}
static generateProductGamesystems(gamesystems: Gamesystem<any, any>[]) {
gamesystems.forEach(gamesystem => {
if(gamesystem instanceof ProductGamesystem) {
gamesystem.generateFromChildsystems();
}
})
}
static processLoadedModel(gameModel: GameModel, loadedModel: LoadModel) {
switch (loadedModel.modelType) {
case ModelComponentType.SCRIPTACCOUNT: this.processLoadedScriptAccount(gameModel, loadedModel); break;
case ModelComponentType.GAMESYTEM: this.processLoadedGamesystem(gameModel, loadedModel); break;
}
}
static processLoadedScriptAccount(gameModel: GameModel, loadedModel: LoadModel) {
const scriptAccount = new ScriptAccount("", "");
Object.assign(scriptAccount, JSON.parse(loadedModel.jsonString));
gameModel.addScriptAccount(scriptAccount);
}
static processLoadedGamesystem(gameModel: GameModel, loadedModel: LoadModel) {
const parsedJsonString = JSON.parse(loadedModel.jsonString);
if(loadedModel.hasOwnProperty('parentLoadModelname')) {
const recursiveLoadModel = loadedModel as RecursiveLoadModel
console.log("Loaded Model should be an instance of recursivemodel")
if(parsedJsonString.hasOwnProperty('states') && parsedJsonString.hasOwnProperty('transitions')) {
//SimpleGamesystem
const simpleGamesystem: SimpleGamesystem = SimpleGamesystemParser.parseSimpleGamesystem(parsedJsonString);
const parentModel: ProductGamesystem = gameModel.findGamesystem(recursiveLoadModel.parentLoadModelname) as ProductGamesystem
parentModel.addChildGamesystem(simpleGamesystem);
} else {
console.log("Gamesystems: ", )
//ProductGamesystem
const productGamesystem: ProductGamesystem = ProductGamesystemParser.parseProductGamesystem(parsedJsonString);
const parentModel: ProductGamesystem = gameModel.findGamesystem(recursiveLoadModel.parentLoadModelname) as ProductGamesystem;
parentModel.addChildGamesystem(productGamesystem);
}
} else {
//Top Gamesystem
if(parsedJsonString.hasOwnProperty('states') && parsedJsonString.hasOwnProperty('transitions')) {
//SimpleGamesystem
const simpleGamesystem: SimpleGamesystem = SimpleGamesystemParser.parseSimpleGamesystem(parsedJsonString);
gameModel.addGamesystem(simpleGamesystem);
} else {
//ProductGamesystem
const productGamesystem = ProductGamesystemParser.parseProductGamesystem(parsedJsonString);
console.log("Generated Productsystem: ", productGamesystem)
gameModel.addGamesystem(productGamesystem);
}
}
}
}

View File

@ -0,0 +1,14 @@
export class StorageModel {
jsonString: string
fileName: string
storagePath: string[]
storageRootDir: string
constructor(jsonString: string, fileName: string, storagePath: string[], storageRootDir: string) {
this.jsonString = jsonString;
this.fileName = fileName;
this.storagePath = storagePath;
this.storageRootDir = storageRootDir;
}
}

View File

@ -0,0 +1,9 @@
import {ProductGamesystem} from "../../gamesystems/ProductGamesystem";
export class ProductGamesystemParser {
static parseProductGamesystem(jsonObject: any): ProductGamesystem {
const componentName = jsonObject.componentName;
const componentDescript = jsonObject.componentDescription;
return new ProductGamesystem(componentName, componentDescript);
}
}

View File

@ -0,0 +1,47 @@
import {SimpleGamesystem} from "../../gamesystems/SimpleGamesystem";
import {SimpleState} from "../../gamesystems/SimpleState";
import {SimpleTransition} from "../../gamesystems/SimpleTransition";
export class SimpleGamesystemParser {
static parseSimpleGamesystem(jsonObject: any) : SimpleGamesystem {
const gamesystemName = jsonObject.componentName;
const gamesystemDescription = jsonObject.componentDescription;
const simpleStates = SimpleGamesystemParser.parseSimpleStates(jsonObject)
const simpleTransitions = SimpleGamesystemParser.parseSimpleTransitions(jsonObject, simpleStates);
const gamesystem = new SimpleGamesystem(gamesystemName, gamesystemDescription);
gamesystem.states = simpleStates;
gamesystem.transitions = simpleTransitions;
return gamesystem;
}
static parseSimpleStates(jsonObject: any): SimpleState[] {
const states: SimpleState[] = [];
for(let i=0; i<jsonObject.states.length; i++) {
const state = new SimpleState("", "");
Object.assign(state, jsonObject.states[i]);
states.push(state);
}
return states;
}
static parseSimpleTransitions(jsonObject: any, states: SimpleState[]): SimpleTransition[] {
const transitions: SimpleTransition[] = [];
for(let i=0; i<jsonObject.transitions.length; i++) {
const startingStateLabel = jsonObject.transitions[i].startingState;
const endingStateLabel = jsonObject.transitions[i].endingState;
const startingState = states.find(state => state.stateLabel === startingStateLabel);
const endingState = states.find(state => state.stateLabel === endingStateLabel);
if(startingState != undefined && endingState != undefined) {
transitions.push(new SimpleTransition(startingState, endingState));
} else {
console.error("Starting or Ending State are not defined!", startingState, endingState)
}
}
return transitions;
}
}

View File

@ -0,0 +1,84 @@
import {StorageModel} from "../StorageModel";
import {GameModel} from "../../GameModel";
import {ScriptAccount} from "../../scriptAccounts/ScriptAccount";
import {Gamesystem} from "../../gamesystems/Gamesystem";
import {SimpleGamesystem} from "../../gamesystems/SimpleGamesystem";
import {ModelComponent} from "../../ModelComponent";
import {ProductGamesystem} from "../../gamesystems/ProductGamesystem";
export class StoreProject {
static JSON_INDENT = 4
static storeProject(gameModel: GameModel): StorageModel[] {
let storageModels: StorageModel[] = [];
storageModels = storageModels.concat(this.storeScriptAccounts(gameModel.scriptAccounts));
storageModels = storageModels.concat(this.storeGamesystems(gameModel.gamesystems));
return storageModels;
}
static storeScriptAccounts(scriptAcccounts: ScriptAccount[]): StorageModel[] {
const storedScriptAccounts: StorageModel[] = [];
scriptAcccounts.forEach(scriptAccount => {
storedScriptAccounts.push({
fileName: scriptAccount.componentName,
jsonString: JSON.stringify(scriptAccount, (key,value) => {
if(key === 'unsaved' || key === 'type') {
return undefined
} else {
return value;
}
}, StoreProject.JSON_INDENT),
storageRootDir: "script-accounts",
storagePath: []
})
scriptAccount.onSaveContent();
})
return storedScriptAccounts;
}
static storeGamesystems(gamesystems: Gamesystem<any, any>[]): StorageModel[] {
let storedGamesystems: StorageModel[] = [];
gamesystems.forEach(gamesystem => {
const storageModels: StorageModel[] = StoreProject.storeIndividualGamesystem(gamesystem, []);
storedGamesystems = storedGamesystems.concat(storageModels);
})
return storedGamesystems;
}
static storeIndividualGamesystem(gamesystem: Gamesystem<any, any>, storagePath: string[]): StorageModel[] {
if(gamesystem instanceof SimpleGamesystem) {
return [new StorageModel(JSON.stringify(gamesystem, (key, value) => {
if(key === 'startingState' || key === 'endingState') {
return value.stateLabel
}
if(key === 'incomingTransitions' || key === 'outgoingTransitions' || key === 'unsaved' || key === 'type') {
return undefined;
} else {
return value;
}
}, this.JSON_INDENT), gamesystem.componentName, storagePath, "gamesystems")];
} else if(gamesystem instanceof ProductGamesystem) {
const storageModels: StorageModel[] = [];
//Root-StorageModel
storagePath.push(gamesystem.componentName);
(gamesystem as ProductGamesystem).innerGamesystems.forEach(innerGamesystem => {
const innerStorageModels: StorageModel[] = StoreProject.storeIndividualGamesystem(innerGamesystem, storagePath)
innerStorageModels.forEach(storageModel => storageModels.push(storageModel))
})
const productData = {
'componentName': gamesystem.componentName,
'componentDescription': gamesystem.componentDescription
}
storageModels.push(new StorageModel(JSON.stringify(productData, null, this.JSON_INDENT), gamesystem.componentName, storagePath, "gamesystems"))
return storageModels;
} else {
return [];
}
}
}

View File

@ -7,8 +7,8 @@ export abstract class Gamesystem<S, T> extends ModelComponent{
states: S[] = []; states: S[] = [];
transitions: T[] = []; transitions: T[] = [];
constructor(gamesystemName: string) { constructor(gamesystemName: string, gamesystemDescription: string) {
super(gamesystemName, "", ModelComponentType.GAMESYTEM); super(gamesystemName, gamesystemDescription, ModelComponentType.GAMESYTEM);
} }
abstract createTransition(startingState: S, endingState: S): T|undefined; abstract createTransition(startingState: S, endingState: S): T|undefined;
@ -25,9 +25,4 @@ export abstract class Gamesystem<S, T> extends ModelComponent{
return true; return true;
} }
save() {
}
} }

View File

@ -14,7 +14,7 @@ export class ProductGamesystem extends Gamesystem<ProductState, ProductTransitio
parentGamesystem: ProductGamesystem | undefined parentGamesystem: ProductGamesystem | undefined
static constructFromSimpleGamesystem(simpleGamesystem: SimpleGamesystem, gameModel: GameModel) { static constructFromSimpleGamesystem(simpleGamesystem: SimpleGamesystem, gameModel: GameModel) {
const productGamesystem = new ProductGamesystem(simpleGamesystem.componentName); const productGamesystem = new ProductGamesystem(simpleGamesystem.componentName, simpleGamesystem.componentDescription);
const parentGamesystem = simpleGamesystem.parentGamesystem; const parentGamesystem = simpleGamesystem.parentGamesystem;
if(simpleGamesystem.states.length > 0) { if(simpleGamesystem.states.length > 0) {
@ -65,10 +65,25 @@ export class ProductGamesystem extends Gamesystem<ProductState, ProductTransitio
generateFromChildsystems() { generateFromChildsystems() {
//Assume that childsystems that are productsystems are already generated //Assume that childsystems that are productsystems are already generated
if(this.innerGamesystems.length < 2) {
//currently only safe-guard
return;
}
const integratedSystems: Gamesystem<any, any>[] = [this.innerGamesystems[0], this.innerGamesystems[1]]; const integratedSystems: Gamesystem<any, any>[] = [this.innerGamesystems[0], this.innerGamesystems[1]];
if(this.innerGamesystems[0] instanceof ProductGamesystem) {
this.innerGamesystems[0].generateFromChildsystems();
}
if(this.innerGamesystems[1] instanceof ProductGamesystem) {
this.innerGamesystems[1].generateFromChildsystems();
}
let gamesystem: ProductGamesystem = ProductGamesystem.generateFromChildsystems(this.innerGamesystems[0], this.innerGamesystems[1], false, integratedSystems); let gamesystem: ProductGamesystem = ProductGamesystem.generateFromChildsystems(this.innerGamesystems[0], this.innerGamesystems[1], false, integratedSystems);
for(let i=2; i<this.innerGamesystems.length; i++) { for(let i=2; i<this.innerGamesystems.length; i++) {
if(this.innerGamesystems[i] instanceof ProductGamesystem) {
(this.innerGamesystems[i] as ProductGamesystem).generateFromChildsystems();
}
integratedSystems.push(this.innerGamesystems[i]); integratedSystems.push(this.innerGamesystems[i]);
gamesystem = ProductGamesystem.generateFromChildsystems(gamesystem, this.innerGamesystems[i], true, integratedSystems); gamesystem = ProductGamesystem.generateFromChildsystems(gamesystem, this.innerGamesystems[i], true, integratedSystems);
} }
@ -77,7 +92,7 @@ export class ProductGamesystem extends Gamesystem<ProductState, ProductTransitio
} }
static generateFromChildsystems(leftSystem: Gamesystem<any, any>, rightSystem: Gamesystem<any, any>, left_temp: boolean, integratedSystems: Gamesystem<any, any>[]) { static generateFromChildsystems(leftSystem: Gamesystem<any, any>, rightSystem: Gamesystem<any, any>, left_temp: boolean, integratedSystems: Gamesystem<any, any>[]) {
const productGamesystem = new ProductGamesystem("Temporary Gamesystem"); const productGamesystem = new ProductGamesystem("Temporary Gamesystem", "");
integratedSystems.forEach(integratedSystem => productGamesystem.addChildGamesystem(integratedSystem)); integratedSystems.forEach(integratedSystem => productGamesystem.addChildGamesystem(integratedSystem));
leftSystem.states.forEach(leftState => { leftSystem.states.forEach(leftState => {

View File

@ -10,6 +10,8 @@ export class SimpleGamesystem extends Gamesystem<SimpleState, SimpleTransition>
parentGamesystem: ProductGamesystem | undefined parentGamesystem: ProductGamesystem | undefined
createState(label: string, description: string): SimpleState | undefined { createState(label: string, description: string): SimpleState | undefined {
if(label == null) { if(label == null) {
return undefined; return undefined;
@ -55,6 +57,4 @@ export class SimpleGamesystem extends Gamesystem<SimpleState, SimpleTransition>
return updated; return updated;
} }
} }

View File

@ -1,5 +1,6 @@
import {ModelComponent} from "../ModelComponent"; import {ModelComponent} from "../ModelComponent";
import {ModelComponentType} from "../ModelComponentType"; import {ModelComponentType} from "../ModelComponentType";
import {SaveComponent} from "../SaveComponent";
export class ScriptAccount extends ModelComponent{ export class ScriptAccount extends ModelComponent{
minValue: number = 0; minValue: number = 0;
@ -7,8 +8,4 @@ export class ScriptAccount extends ModelComponent{
constructor(componentName: string, componentDescription: string) { constructor(componentName: string, componentDescription: string) {
super(componentName, componentDescription, ModelComponentType.SCRIPTACCOUNT); super(componentName, componentDescription, ModelComponentType.SCRIPTACCOUNT);
} }
save(): void {
}
} }

View File

@ -0,0 +1,8 @@
{
"unsaved": false,
"componentName": "Luftfeuchtigkeit",
"componentDescription": "",
"type": 0,
"minValue": 0,
"maxValue": 100
}

View File

@ -0,0 +1,8 @@
{
"unsaved": false,
"componentName": "Temperature",
"componentDescription": "",
"type": 0,
"minValue": 0,
"maxValue": 100
}