Introduce Project Service as interface to persistance of gamemodel
Some checks failed
E2E Testing / test (push) Failing after 1m31s

This commit is contained in:
Sebastian Böckelmann 2024-03-20 09:26:52 +01:00
parent 8016f55e85
commit b31f72475a
8 changed files with 175 additions and 0 deletions

17
app/storage/FileUtils.js Normal file
View File

@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUtils = void 0;
const fs = require("fs");
const path = require("node:path");
class FileUtils {
static listFilesInDirectory(directory) {
if (fs.lstatSync(directory).isDirectory()) {
return fs.readdirSync(directory).map(fileName => path.join(directory, fileName));
}
else {
return [];
}
}
}
exports.FileUtils = FileUtils;
//# sourceMappingURL=FileUtils.js.map

View File

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelComponentFileDirectory = void 0;
class ModelComponentFileDirectory {
}
exports.ModelComponentFileDirectory = ModelComponentFileDirectory;
ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME = "script-accounts";
ModelComponentFileDirectory.GAMESYSTEM_DIR_NAME = "gamesystems";
ModelComponentFileDirectory.GAMESYSTEM_SIMPLE_DIR_NAME = "simple";
ModelComponentFileDirectory.GAMESYSTEM_PRODUCT_DIR_NAME = "product";
//# sourceMappingURL=ModelComponentFileDirectory.js.map

View File

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

View File

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

View File

@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameModelLoader = void 0;
const StoredGameModel_1 = require("../StoredGameModel");
const path = require("node:path");
const ModelComponentFileDirectory_1 = require("../ModelComponentFileDirectory");
const ScriptAccountLoader_1 = require("./ScriptAccountLoader");
class GameModelLoader {
constructor(gameModelDir) {
this.gameModelDir = gameModelDir;
}
loadGameModel() {
const gameModelName = path.basename(this.gameModelDir);
const gameModelComponents = this.loadGameModelComponents();
return new StoredGameModel_1.StoredGameModel(gameModelName, gameModelComponents);
}
loadGameModelComponents() {
let gameModelComponents = this.loadScriptAccountComponents();
gameModelComponents = gameModelComponents.concat(this.loadGamesystems());
return gameModelComponents;
}
loadScriptAccountComponents() {
const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME);
const scriptAccountLoader = new ScriptAccountLoader_1.ScriptAccountLoader(scriptAccountDir);
return scriptAccountLoader.loadScriptAccounts();
}
loadGamesystems() {
return [];
}
}
exports.GameModelLoader = GameModelLoader;
//# sourceMappingURL=GameModelLoader.js.map

View File

@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScriptAccountLoader = void 0;
const StoreComponent_1 = require("../StoreComponent");
const FileUtils_1 = require("../FileUtils");
const ModelComponentType_1 = require("../../../src/app/project/game-model/ModelComponentType");
const fs = require("fs");
class ScriptAccountLoader {
constructor(scriptAccountDir) {
this.scriptAccountDir = scriptAccountDir;
}
loadScriptAccounts() {
const scriptAccountFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.scriptAccountDir);
const loadedScriptAccounts = [];
scriptAccountFiles.forEach(scriptAccountFile => {
const loadedScriptAccount = this.loadScriptAccount(scriptAccountFile);
if (loadedScriptAccount != undefined) {
loadedScriptAccounts.push(loadedScriptAccount);
}
});
return loadedScriptAccounts;
}
loadScriptAccount(scriptAccountFile) {
if (scriptAccountFile.endsWith(".json")) {
const scriptAccountData = fs.readFileSync(scriptAccountFile, 'utf-8');
return new StoreComponent_1.StoreComponent(scriptAccountData, scriptAccountFile, ModelComponentType_1.ModelComponentType.SCRIPTACCOUNT);
}
}
}
exports.ScriptAccountLoader = ScriptAccountLoader;
//# sourceMappingURL=ScriptAccountLoader.js.map

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ProjectService } from './project.service';
describe('ProjectService', () => {
let service: ProjectService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ProjectService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,45 @@
import { Injectable } from '@angular/core';
import {ElectronService} from "../core/services";
import {GameModel} from "./game-model/GameModel";
import {StoredGameModel} from "../../../app/storage/StoredGameModel";
import {ScriptAccount} from "./game-model/scriptAccounts/ScriptAccount";
import {ModelComponentType} from "./game-model/ModelComponentType";
import {ScriptAccountParser} from "./parser/ScriptAccountParser";
@Injectable({
providedIn: 'root'
})
export class ProjectService {
gameModel: GameModel = new GameModel("New GameModel")
constructor(private electronService: ElectronService) {
if(electronService.isElectron) {
electronService.ipcRenderer.on('get-project-data', (event: any, message: string) => {
this.saveProject();
})
electronService.ipcRenderer.on('open-project', (event: any, loadedProject: StoredGameModel) => {
this.loadProject(loadedProject);
})
}
}
saveProject() {
}
loadProject(storedGameModel: StoredGameModel) {
const gameModel = new GameModel(storedGameModel.gameModelName)
storedGameModel.loadedModels.forEach(storedComponent => {
switch (storedComponent.componentType) {
case ModelComponentType.SCRIPTACCOUNT: {
const scriptAccount = ScriptAccountParser.parseScriptAccount(storedComponent);
gameModel.addScriptAccount(scriptAccount);
}
}
})
this.gameModel = gameModel;
}
}