inventory-items #42

Merged
sebastian merged 9 commits from inventory-items into main 2024-04-20 11:39:35 +02:00
10 changed files with 90 additions and 5 deletions
Showing only changes of commit 1c632c6833 - Show all commits

View File

@ -8,6 +8,7 @@ import {ModelComponentFileDirectory} from "./storage/ModelComponentFileDirectory
import {GamesystemStorage} from "./storage/storing/GamesystemStorage";
import {Character} from "../src/app/project/game-model/characters/Character";
import {CharacterStorage} from "./storage/storing/CharacterStorage";
import {ItemStorage} from "./storage/storing/ItemStorage";
let win: BrowserWindow | null = null;
const args = process.argv.slice(1),
@ -247,6 +248,9 @@ function recieveGameModelToStore(gameModel: StoredGameModel) {
const characterStorage = new CharacterStorage(path.join(projectDirectory, ModelComponentFileDirectory.CHARACTER_DIR_NAME))
characterStorage.storeCharacters(gameModel.storedCharacters)
const itemStorage = new ItemStorage(path.join(projectDirectory, ModelComponentFileDirectory.ITEM_DIR_NAME))
itemStorage.storeItem(gameModel.storedItems);
}
/*function deleteComponent(component: DeleteModel) {

View File

@ -7,4 +7,5 @@ exports.ModelComponentFileDirectory = ModelComponentFileDirectory;
ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME = "script-accounts";
ModelComponentFileDirectory.GAMESYSTEM_DIR_NAME = "gamesystems";
ModelComponentFileDirectory.CHARACTER_DIR_NAME = "characters";
ModelComponentFileDirectory.ITEM_DIR_NAME = "items";
//# sourceMappingURL=ModelComponentFileDirectory.js.map

View File

@ -2,4 +2,5 @@ export class ModelComponentFileDirectory {
public static SCRIPTACCOUNT_DIR_NAME = "script-accounts"
public static GAMESYSTEM_DIR_NAME = "gamesystems";
public static CHARACTER_DIR_NAME = "characters";
public static ITEM_DIR_NAME = "items";
}

View File

@ -2,11 +2,12 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.StoredGameModel = void 0;
class StoredGameModel {
constructor(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters) {
constructor(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItems) {
this.gameModelName = gameModelName;
this.storedGamesystems = storedGamesystems;
this.storedScriptAccounts = storedScriptAccounts;
this.storedCharacters = storedCharacters;
this.storedItems = storedItems;
}
}
exports.StoredGameModel = StoredGameModel;

View File

@ -6,13 +6,15 @@ export class StoredGameModel {
storedGamesystems: StoreComponent[]
storedScriptAccounts: StoreComponent[]
storedCharacters: StoreComponent[]
storedItems: StoreComponent[]
constructor(gameModelName: string, storedScriptAccounts: StoreComponent[], storedGamesystems: StoreComponent[],
storedCharacters: StoreComponent[]) {
storedCharacters: StoreComponent[], storedItems: StoreComponent[]) {
this.gameModelName = gameModelName;
this.storedGamesystems = storedGamesystems;
this.storedScriptAccounts = storedScriptAccounts;
this.storedCharacters = storedCharacters;
this.storedItems = storedItems;
}
}

View File

@ -16,7 +16,7 @@ class GameModelLoader {
const storedScriptAccounts = this.loadScriptAccountComponents();
const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters);
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, []);
}
loadScriptAccountComponents() {
const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME);

View File

@ -22,7 +22,7 @@ export class GameModelLoader {
const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters()
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters);
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, []);
}
private loadScriptAccountComponents() {

View File

@ -0,0 +1,33 @@
import {FileUtils} from "../FileUtils";
import {StoreComponent} from "../StoreComponent";
import * as path from "node:path";
import * as fs from "node:fs";
export class ItemStorage {
private itemDir: string
constructor(itemDir: string) {
this.itemDir = itemDir;
FileUtils.prepareDirectoryFroWriting(this.itemDir);
}
storeItem(storedItems: StoreComponent[]) {
this.persistDeletedItems(storedItems);
storedItems.forEach(item => this.storeSingleItem(item))
}
private persistDeletedItems(existingItems: StoreComponent[]) {
const itemFiles = FileUtils.listFilesInDirectory(this.itemDir);
itemFiles.forEach(itemFile => {
const itemFileName = path.parse(path.basename(itemFile)).name
if(existingItems.find(item => item.fileName === itemFileName) == undefined) {
fs.unlinkSync(itemFile)
}
})
}
private storeSingleItem(item: StoreComponent) {
const completeItemFile = path.join(this.itemDir, item.fileName + ".json");
fs.writeFileSync(completeItemFile, item.jsonString, 'utf-8')
}
}

View File

@ -27,6 +27,7 @@ import {CharacterParser} from "./project/parser/characterParser/CharacterParser"
import {TemplateType} from "./project/game-model/templates/TemplateType";
import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateTypeUtilities";
import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
import {ItemSerializer} from "./project/serializer/ItemSerializer";
@Component({
selector: 'app-root',
@ -231,7 +232,11 @@ export class AppComponent implements OnInit{
const storedScriptAccounts = ScriptAccountSerializer.serializeScriptAccounts(this.gameModel.scriptAccounts)
const storedGamesystems: StoreComponent[] = GamesystemSerializer.serializeGamesystems(this.gameModel.gamesystems)
const storedCharacters: StoreComponent[] = CharacterSerializer.serializeCharacters(this.gameModel.characters)
const storeModel = new StoredGameModel(this.gameModel.gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters)
const storedItems: StoreComponent[] = ItemSerializer.serializeItems(this.gameModel.inventoryItems);
console.log(this.gameModel.inventoryItems)
const storeModel = new StoredGameModel(this.gameModel.gameModelName, storedScriptAccounts,
storedGamesystems, storedCharacters, storedItems)
if(this.electronService.isElectron) {
this.electronService.ipcRenderer.send('save-model', storeModel)

View File

@ -0,0 +1,38 @@
import {Item} from "../game-model/inventory/Item";
import {StoreComponent} from "../../../../app/storage/StoreComponent";
import {SerializeConstants} from "./SerializeConstants";
import {ItemQuality} from "../game-model/inventory/ItemQuality";
import {ItemPropertyDescription} from "../game-model/inventory/ItemPropertyDescription";
import {ItemProperty} from "../game-model/inventory/ItemProperty";
export class ItemSerializer {
public static serializeItems(items: Item[]): StoreComponent[] {
const storedItems: StoreComponent[] = []
items.forEach(item => storedItems.push(this.serializeSingleItem(item)))
return storedItems;
}
private static serializeSingleItem(item: Item): StoreComponent {
const fileName = item.componentName;
const componentType = item.type;
const jsonString = JSON.stringify(item,(key, value) => {
if(key === 'perQualityProperties') {
return value;
} else if(key == 'quality_propertes') {
return value.map((prop: ItemProperty) => ({
...prop,
propertyDescription: undefined
}));
}
if(key === 'unsaved' || key === 'type') {
return undefined;
} else {
return value;
}
}, SerializeConstants.JSON_INDENT)
return new StoreComponent(jsonString, fileName, componentType);
}
}