From e873688cb706c59f4e710ad4fdc481f14b87c705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Sat, 17 Feb 2024 10:13:32 +0100 Subject: [PATCH] Save Gamesystems and Ignore save status and type of saved gamemodelcomponent --- app/SaveProject.js | 4 +- app/SaveProject.ts | 6 +- app/main.ts | 2 +- src/app/app.component.ts | 22 ++++- src/app/game-model/GameModel.ts | 18 +--- src/app/game-model/SaveComponent.ts | 2 - src/app/game-model/StorageModel.ts | 12 --- src/app/game-model/fs/StorageModel.ts | 14 ++++ src/app/game-model/fs/store/StoreProject.ts | 84 +++++++++++++++++++ src/app/game-model/gamesystems/Gamesystem.ts | 5 -- .../gamesystems/SimpleGamesystem.ts | 2 - .../scriptAccounts/ScriptAccount.ts | 5 -- 12 files changed, 128 insertions(+), 48 deletions(-) delete mode 100644 src/app/game-model/StorageModel.ts create mode 100644 src/app/game-model/fs/StorageModel.ts create mode 100644 src/app/game-model/fs/store/StoreProject.ts diff --git a/app/SaveProject.js b/app/SaveProject.js index 41ad558..5b32ad8 100644 --- a/app/SaveProject.js +++ b/app/SaveProject.js @@ -10,8 +10,10 @@ class SaveProject { if (!fs.existsSync(projectDir)) { fs.mkdirSync(projectDir, { recursive: true }); } + console.log(storageModels); storageModels.forEach(storageModel => { - const modelDir = path.join(projectDir, storageModel.storageDir); + 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 }); } diff --git a/app/SaveProject.ts b/app/SaveProject.ts index 4175a16..cfe06cd 100644 --- a/app/SaveProject.ts +++ b/app/SaveProject.ts @@ -1,4 +1,4 @@ -import {StorageModel} from "../src/app/game-model/StorageModel"; +import {StorageModel} from "../src/app/game-model/fs/StorageModel"; import * as fs from "fs"; import * as path from "node:path"; import {LoadModel} from "./LoadModel"; @@ -13,8 +13,10 @@ export class SaveProject { fs.mkdirSync(projectDir, {recursive: true}); } + console.log(storageModels) storageModels.forEach(storageModel => { - const modelDir = path.join(projectDir, storageModel.storageDir); + 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}); } diff --git a/app/main.ts b/app/main.ts index 20acbd8..af69ce5 100644 --- a/app/main.ts +++ b/app/main.ts @@ -2,7 +2,7 @@ import {app, BrowserWindow, screen, Menu, ipcMain, dialog, globalShortcut} from import * as path from 'path'; import * as fs from 'fs'; import {json} from "node:stream/consumers"; -import {StorageModel} from "../src/app/game-model/StorageModel"; +import {StorageModel} from "../src/app/game-model/fs/StorageModel"; import {SaveProject} from "./SaveProject"; let win: BrowserWindow | null = null; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ec551fe..012ba8f 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -20,6 +20,7 @@ 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({ selector: 'app-root', @@ -62,6 +63,25 @@ export class AppComponent implements OnInit{ electronService.ipcRenderer.on('open-project', (event: any, loadedProject: LoadedProject) => { this.gameModel = ProcessLoadedProject.processLoadedProject(loadedProject) + const weather = new SimpleGamesystem("Weather"); + const season = new SimpleGamesystem("Season"); + + const springState = season.createState("Spring", "Spring, also known as springtime, is one of the four temperate seasons, succeeding winter and preceding summer."); + const summerState = season.createState("Summer", "Summer is the hottest and brightest of the four temperate seasons, occurring after spring and before autumn. "); + + const sunnyState = weather.createState("Sunny", "The sun is shining. No clouds, no rain, no storm."); + const rainingState = weather.createState("Raining", "It rains") + + season.createTransition(springState!, summerState!); + weather.createTransition(sunnyState!, rainingState!); + + const weather_season = new ProductGamesystem("Weather-Season"); + weather_season.addChildGamesystem(weather); + weather_season.addChildGamesystem(season); + + weather_season.createState([springState!, sunnyState!]); + + this.gameModel.addGamesystem(weather_season); }) } else { console.log('Run in browser'); @@ -69,7 +89,7 @@ export class AppComponent implements OnInit{ } saveGameModel() { - const storageModels = this.gameModel!.save() + const storageModels = StoreProject.storeProject(this.gameModel!); this.electronService.ipcRenderer.send('save-model', storageModels) } diff --git a/src/app/game-model/GameModel.ts b/src/app/game-model/GameModel.ts index bf928e0..0b1a189 100644 --- a/src/app/game-model/GameModel.ts +++ b/src/app/game-model/GameModel.ts @@ -4,7 +4,7 @@ import {Transition} from "./gamesystems/Transition"; import {State} from "./gamesystems/State"; import {ProductGamesystem} from "./gamesystems/ProductGamesystem"; import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem"; -import {StorageModel} from "./StorageModel"; +import {StorageModel} from "./fs/StorageModel"; export class GameModel { private readonly _gameModelName: string @@ -91,22 +91,6 @@ 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" - }) - - console.log(scriptAccount) - }) - - return storageModels; - } - addScriptAccount(scriptAccount: ScriptAccount) { this.scriptAccounts.push(scriptAccount); } diff --git a/src/app/game-model/SaveComponent.ts b/src/app/game-model/SaveComponent.ts index 631c529..cf2b2a8 100644 --- a/src/app/game-model/SaveComponent.ts +++ b/src/app/game-model/SaveComponent.ts @@ -10,6 +10,4 @@ export abstract class SaveComponent { onSaveContent() { this.unsaved = false; } - - abstract save(): string; } diff --git a/src/app/game-model/StorageModel.ts b/src/app/game-model/StorageModel.ts deleted file mode 100644 index 35b06c5..0000000 --- a/src/app/game-model/StorageModel.ts +++ /dev/null @@ -1,12 +0,0 @@ -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; - } -} diff --git a/src/app/game-model/fs/StorageModel.ts b/src/app/game-model/fs/StorageModel.ts new file mode 100644 index 0000000..800754e --- /dev/null +++ b/src/app/game-model/fs/StorageModel.ts @@ -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; + } +} diff --git a/src/app/game-model/fs/store/StoreProject.ts b/src/app/game-model/fs/store/StoreProject.ts new file mode 100644 index 0000000..6503281 --- /dev/null +++ b/src/app/game-model/fs/store/StoreProject.ts @@ -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[]): StorageModel[] { + let storedGamesystems: StorageModel[] = []; + gamesystems.forEach(gamesystem => { + const storageModels: StorageModel[] = StoreProject.storeIndividualGamesystem(gamesystem, []); + storedGamesystems = storedGamesystems.concat(storageModels); + }) + return storedGamesystems; + } + + static storeIndividualGamesystem(gamesystem: Gamesystem, 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 []; + } + } + +} diff --git a/src/app/game-model/gamesystems/Gamesystem.ts b/src/app/game-model/gamesystems/Gamesystem.ts index 505a830..a1f0d5c 100644 --- a/src/app/game-model/gamesystems/Gamesystem.ts +++ b/src/app/game-model/gamesystems/Gamesystem.ts @@ -25,9 +25,4 @@ export abstract class Gamesystem extends ModelComponent{ return true; } - - save() { - return JSON.stringify(this); - } - } diff --git a/src/app/game-model/gamesystems/SimpleGamesystem.ts b/src/app/game-model/gamesystems/SimpleGamesystem.ts index 4021c93..e9e73b0 100644 --- a/src/app/game-model/gamesystems/SimpleGamesystem.ts +++ b/src/app/game-model/gamesystems/SimpleGamesystem.ts @@ -55,6 +55,4 @@ export class SimpleGamesystem extends Gamesystem return updated; } - - } diff --git a/src/app/game-model/scriptAccounts/ScriptAccount.ts b/src/app/game-model/scriptAccounts/ScriptAccount.ts index 62b0636..65967f4 100644 --- a/src/app/game-model/scriptAccounts/ScriptAccount.ts +++ b/src/app/game-model/scriptAccounts/ScriptAccount.ts @@ -8,9 +8,4 @@ export class ScriptAccount extends ModelComponent{ constructor(componentName: string, componentDescription: string) { super(componentName, componentDescription, ModelComponentType.SCRIPTACCOUNT); } - - save(): string { - this.unsaved = false; - return JSON.stringify(this, null, SaveComponent.JSON_INDENT) - } }