Save Gamesystems and Ignore save status and type of saved gamemodelcomponent
All checks were successful
E2E Testing / test (push) Successful in 1m28s

This commit is contained in:
Sebastian Böckelmann 2024-02-17 10:13:32 +01:00
parent 78a264aa26
commit e873688cb7
12 changed files with 128 additions and 48 deletions

View File

@ -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 });
}

View File

@ -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});
}

View File

@ -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;

View File

@ -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)
}

View File

@ -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);
}

View File

@ -10,6 +10,4 @@ export abstract class SaveComponent {
onSaveContent() {
this.unsaved = false;
}
abstract save(): string;
}

View File

@ -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;
}
}

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,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

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

View File

@ -55,6 +55,4 @@ export class SimpleGamesystem extends Gamesystem<SimpleState, SimpleTransition>
return updated;
}
}

View File

@ -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)
}
}