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 309 additions and 8 deletions
Showing only changes of commit 8085b07264 - Show all commits

View File

@ -7,6 +7,7 @@ const ModelComponentFileDirectory_1 = require("../ModelComponentFileDirectory");
const ScriptAccountLoader_1 = require("./ScriptAccountLoader");
const GamesystemLoader_1 = require("./GamesystemLoader");
const CharacterLoader_1 = require("./CharacterLoader");
const ItemLoader_1 = require("./ItemLoader");
class GameModelLoader {
constructor(gameModelDir) {
this.gameModelDir = gameModelDir;
@ -16,7 +17,8 @@ class GameModelLoader {
const storedScriptAccounts = this.loadScriptAccountComponents();
const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, []);
const storedItems = this.loadItems();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItems);
}
loadScriptAccountComponents() {
const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME);
@ -33,6 +35,11 @@ class GameModelLoader {
const characterLoader = new CharacterLoader_1.CharacterLoader(characterDir);
return characterLoader.loadCharacters();
}
loadItems() {
const itemDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.ITEM_DIR_NAME);
const itemLoader = new ItemLoader_1.ItemLoader(itemDir);
return itemLoader.loadItems();
}
}
exports.GameModelLoader = GameModelLoader;
//# sourceMappingURL=GameModelLoader.js.map

View File

@ -6,6 +6,7 @@ import {ModelComponentFileDirectory} from "../ModelComponentFileDirectory";
import {ScriptAccountLoader} from "./ScriptAccountLoader";
import {GamesystemLoader} from "./GamesystemLoader";
import {CharacterLoader} from "./CharacterLoader";
import {ItemLoader} from "./ItemLoader";
export class GameModelLoader {
gameModelDir: string
@ -21,8 +22,9 @@ export class GameModelLoader {
const storedScriptAccounts = this.loadScriptAccountComponents();
const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters()
const storedItems = this.loadItems();
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, []);
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItems);
}
private loadScriptAccountComponents() {
@ -44,7 +46,9 @@ export class GameModelLoader {
}
private loadItems(): StoreComponent[] {
const itemDir = path.join(this.gameModelDir, ModelComponentFileDirectory.ITEM_DIR_NAME);
const itemLoader = new ItemLoader(itemDir)
return itemLoader.loadItems();
}
}

View File

@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemLoader = void 0;
const fs = require("node:fs");
const StoreComponent_1 = require("../StoreComponent");
const ModelComponentType_1 = require("../../../src/app/project/game-model/ModelComponentType");
const FileUtils_1 = require("../FileUtils");
class ItemLoader {
constructor(itemDir) {
this.itemDir = itemDir;
}
loadItems() {
const itemFiles = FileUtils_1.FileUtils.listFilesInDirectory(this.itemDir);
const loadedItems = [];
itemFiles.forEach(itemFile => {
const loadedItem = this.loadItem(itemFile);
if (loadedItem != undefined) {
loadedItems.push(loadedItem);
}
});
return loadedItems;
}
loadItem(itemFile) {
if (itemFile.endsWith(".json")) {
const itemData = fs.readFileSync(itemFile, 'utf-8');
return new StoreComponent_1.StoreComponent(itemData, itemFile, ModelComponentType_1.ModelComponentType.ITEM);
}
}
}
exports.ItemLoader = ItemLoader;
//# sourceMappingURL=ItemLoader.js.map

View File

@ -0,0 +1,33 @@
import * as fs from "node:fs";
import {StoreComponent} from "../StoreComponent";
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
import {FileUtils} from "../FileUtils";
export class ItemLoader {
private itemDir: string
constructor(itemDir: string) {
this.itemDir = itemDir;
}
loadItems() {
const itemFiles = FileUtils.listFilesInDirectory(this.itemDir);
const loadedItems :StoreComponent[] = []
itemFiles.forEach(itemFile => {
const loadedItem = this.loadItem(itemFile);
if(loadedItem != undefined) {
loadedItems.push(loadedItem);
}
})
return loadedItems;
}
private loadItem(itemFile: string) {
if(itemFile.endsWith(".json")) {
const itemData = fs.readFileSync(itemFile, 'utf-8');
return new StoreComponent(itemData, itemFile, ModelComponentType.ITEM)
}
}
}

View File

@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemStorage = void 0;
const FileUtils_1 = require("../FileUtils");
const path = require("node:path");
const fs = require("node:fs");
class ItemStorage {
constructor(itemDir) {
this.itemDir = itemDir;
FileUtils_1.FileUtils.prepareDirectoryFroWriting(this.itemDir);
}
storeItem(storedItems) {
this.persistDeletedItems(storedItems);
storedItems.forEach(item => this.storeSingleItem(item));
}
persistDeletedItems(existingItems) {
const itemFiles = FileUtils_1.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);
}
});
}
storeSingleItem(item) {
const completeItemFile = path.join(this.itemDir, item.fileName + ".json");
fs.writeFileSync(completeItemFile, item.jsonString, 'utf-8');
}
}
exports.ItemStorage = ItemStorage;
//# sourceMappingURL=ItemStorage.js.map

View File

@ -28,6 +28,7 @@ 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";
import {ItemParser} from "./project/parser/ItemParser";
@Component({
selector: 'app-root',
@ -224,6 +225,8 @@ export class AppComponent implements OnInit{
const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts);
gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters)
gameModel.inventoryItems = ItemParser.parseItems(storedGameModel.storedItems)
this.gameModel = gameModel;
}

View File

@ -23,9 +23,6 @@ export class GameModel {
constructor(gameModelName: string) {
this.gameModelName = gameModelName;
this.createInventoryItem("Item 1")
this.createInventoryItem("Item 2")
}
addGamesystem(gamesystem: Gamesystem<any, any>) {

View File

@ -0,0 +1,85 @@
import {StoreComponent} from "../../../../app/storage/StoreComponent";
import {Item} from "../game-model/inventory/Item";
import {ItemQuality} from "../game-model/inventory/ItemQuality";
import {ItemProperty} from "../game-model/inventory/ItemProperty";
import {ItemPropertyDescription} from "../game-model/inventory/ItemPropertyDescription";
import {ModelComponentType} from "../game-model/ModelComponentType";
export class ItemParser {
public static parseItems(items: StoreComponent[]): Item[] {
const parsedItems: Item[] = []
items.forEach(item => parsedItems.push(this.parseSingleItem(JSON.parse(item.jsonString))));
return parsedItems;
}
private static parseSingleItem(itemData: any): Item {
const componentName = itemData.componentName;
const componentDescription = itemData.componentDescription;
const perQualityProperties = this.parseQualityPropertyReferences(itemData.perQualityProperties);
const qualties = this.parseQualities(itemData.possible_qualities, perQualityProperties)
const itemProperties = this.parseItemProperties(itemData.itemProperties)
const item = new Item(componentName, componentDescription, ModelComponentType.ITEM);
item.possible_qualities = qualties;
item.itemProperties = itemProperties;
item.perQualityProperties = perQualityProperties;
return item;
}
private static parseItemProperties(propertyData: any): ItemProperty[] {
const itemProperties : ItemProperty[] = []
for(let i=0; i<propertyData.length; i++) {
const propertyName = propertyData[i].propertyName;
const propertyDescription = propertyData[i].propertyDescription;
const property = propertyData[i].property;
itemProperties.push(new ItemProperty(propertyName, propertyDescription, property))
}
return itemProperties;
}
private static parseQualities(qualityData: any, perQualityProperties: ItemPropertyDescription[]): ItemQuality[] {
const qualities: ItemQuality[] = []
for(let i=0; i<qualityData.length; i++){
const qualityStep = qualityData[i].quality_step;
const qualityProperties = this.parseQualityProperty(qualityData[i].quality_propertes, perQualityProperties)
const itemQuality = new ItemQuality(qualityStep);
itemQuality.quality_propertes = qualityProperties;
qualities.push(itemQuality)
}
return qualities;
}
private static parseQualityProperty(qualityPropertyData: any, propertyDescriptions: ItemPropertyDescription[]): ItemProperty[] {
const result: ItemProperty[] = []
for(let i=0; i<qualityPropertyData.length; i++){
const propertyName = qualityPropertyData[i].propertyName;
const propertyDescription = this.findMappingPropertyDescription(propertyDescriptions, propertyName)!.propertyDescription
const property = qualityPropertyData[i].property;
result.push(new ItemProperty(propertyName, propertyDescription, property))
}
return result;
}
private static findMappingPropertyDescription(propertyDescriptions: ItemPropertyDescription[], propertyName: string) {
return propertyDescriptions.find(property => property.propertyName === propertyName);
}
private static parseQualityPropertyReferences(qualityPropertyData: any): ItemPropertyDescription[] {
const result: ItemPropertyDescription[] = []
for(let i=0; i<qualityPropertyData.length; i++){
const propertyName = qualityPropertyData[i].propertyName;
const propertyDescription = qualityPropertyData[i].propertyDescription;
result.push(new ItemPropertyDescription(propertyName, propertyDescription))
}
return result;
}
}

View File

@ -0,0 +1,55 @@
{
"componentName": "Item 1",
"componentDescription": "",
"possible_qualities": [
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 0.25
},
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 0.5
},
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 0.75
},
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 1
}
],
"itemProperties": [
{
"propertyName": "Weight",
"propertyDescription": "Some Weights",
"property": 10
}
],
"perQualityProperties": [
{
"propertyName": "Price",
"propertyDescription": "Price to buy item"
}
]
}

View File

@ -0,0 +1,55 @@
{
"componentName": "Item 2",
"componentDescription": "",
"possible_qualities": [
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 0.25
},
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 0.5
},
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 0.75
},
{
"quality_propertes": [
{
"propertyName": "Price",
"property": 0
}
],
"quality_step": 1
}
],
"itemProperties": [
{
"propertyName": "Weight",
"propertyDescription": "Some Weights",
"property": 10
}
],
"perQualityProperties": [
{
"propertyName": "Price",
"propertyDescription": "Price to buy item"
}
]
}