First solution for storing ScriptAccounts
All checks were successful
E2E Testing / test (push) Successful in 1m36s

This commit is contained in:
Sebastian Böckelmann 2024-02-16 21:40:21 +01:00
parent bd9cb7e7c5
commit 599a7cdd1b
13 changed files with 161 additions and 5 deletions

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,14 @@
import {app, BrowserWindow, screen, Menu, ipcMain} from 'electron'; import {app, BrowserWindow, screen, Menu, ipcMain, ipcRenderer} 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/StorageModel";
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 +93,56 @@ 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[]) => {
const directoryPath = 'testModel/'
if(!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, {recursive: true});
}
storageModels.forEach(storageModel => {
const modelDir = path.join(directoryPath, storageModel.storageDir);
if(!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, {recursive: true});
}
const filePath = path.join(directoryPath, storageModel.storageDir, 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);
}
})
})
})
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 +171,23 @@ try {
} }
}); });
} catch (e) { } catch (e) {
// Catch Error // Catch Error
// throw e; // throw e;
} }
function createNewProject() {
}
function openProject() {
}
function saveProject() {
console.log("Clicked SaveProject")
win!.webContents.send('get-project-data')
}

View File

@ -50,11 +50,22 @@ 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();
})
})
} else { } else {
console.log('Run in browser'); console.log('Run in browser');
} }
} }
saveGameModel() {
const storageModels = this.gameModel!.save()
this.electronService.ipcRenderer.send('save-model', storageModels)
}
onContextMenuMessageRecieved(message: string) { onContextMenuMessageRecieved(message: string) {
if(message == "edit") { if(message == "edit") {
this.onEditModelComponent(); this.onEditModelComponent();

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 "./StorageModel";
export class GameModel { export class GameModel {
private readonly _gameModelName: string private readonly _gameModelName: string
@ -90,4 +91,17 @@ export class GameModel {
} }
} }
save() {
const storageModels: StorageModel[] = [];
this.scriptAccounts.forEach(scriptAccount => {
const scriptAccountJson = scriptAccount.save();
storageModels.push({
fileName: scriptAccount.componentName,
jsonString: scriptAccountJson,
storageDir: "script-accounts"
})
})
return storageModels;
}
} }

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;
} }
@ -9,5 +11,5 @@ export abstract class SaveComponent {
this.unsaved = false; this.unsaved = false;
} }
abstract save(): void; abstract save(): string;
} }

View File

@ -0,0 +1,12 @@
export 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

@ -27,7 +27,7 @@ export abstract class Gamesystem<S, T> extends ModelComponent{
save() { save() {
return JSON.stringify(this);
} }
} }

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;
@ -8,7 +9,7 @@ export class ScriptAccount extends ModelComponent{
super(componentName, componentDescription, ModelComponentType.SCRIPTACCOUNT); super(componentName, componentDescription, ModelComponentType.SCRIPTACCOUNT);
} }
save(): void { save(): string {
return JSON.stringify(this, null, SaveComponent.JSON_INDENT)
} }
} }

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
}

View File

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