inventory-items #42
@ -7,6 +7,7 @@ const ModelComponentFileDirectory_1 = require("../ModelComponentFileDirectory");
|
|||||||
const ScriptAccountLoader_1 = require("./ScriptAccountLoader");
|
const ScriptAccountLoader_1 = require("./ScriptAccountLoader");
|
||||||
const GamesystemLoader_1 = require("./GamesystemLoader");
|
const GamesystemLoader_1 = require("./GamesystemLoader");
|
||||||
const CharacterLoader_1 = require("./CharacterLoader");
|
const CharacterLoader_1 = require("./CharacterLoader");
|
||||||
|
const ItemLoader_1 = require("./ItemLoader");
|
||||||
class GameModelLoader {
|
class GameModelLoader {
|
||||||
constructor(gameModelDir) {
|
constructor(gameModelDir) {
|
||||||
this.gameModelDir = gameModelDir;
|
this.gameModelDir = gameModelDir;
|
||||||
@ -16,7 +17,8 @@ class GameModelLoader {
|
|||||||
const storedScriptAccounts = this.loadScriptAccountComponents();
|
const storedScriptAccounts = this.loadScriptAccountComponents();
|
||||||
const storedGamesystems = this.loadGamesystems();
|
const storedGamesystems = this.loadGamesystems();
|
||||||
const storedCharacters = this.loadCharacters();
|
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() {
|
loadScriptAccountComponents() {
|
||||||
const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME);
|
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);
|
const characterLoader = new CharacterLoader_1.CharacterLoader(characterDir);
|
||||||
return characterLoader.loadCharacters();
|
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;
|
exports.GameModelLoader = GameModelLoader;
|
||||||
//# sourceMappingURL=GameModelLoader.js.map
|
//# sourceMappingURL=GameModelLoader.js.map
|
@ -6,6 +6,7 @@ import {ModelComponentFileDirectory} from "../ModelComponentFileDirectory";
|
|||||||
import {ScriptAccountLoader} from "./ScriptAccountLoader";
|
import {ScriptAccountLoader} from "./ScriptAccountLoader";
|
||||||
import {GamesystemLoader} from "./GamesystemLoader";
|
import {GamesystemLoader} from "./GamesystemLoader";
|
||||||
import {CharacterLoader} from "./CharacterLoader";
|
import {CharacterLoader} from "./CharacterLoader";
|
||||||
|
import {ItemLoader} from "./ItemLoader";
|
||||||
|
|
||||||
export class GameModelLoader {
|
export class GameModelLoader {
|
||||||
gameModelDir: string
|
gameModelDir: string
|
||||||
@ -21,8 +22,9 @@ export class GameModelLoader {
|
|||||||
const storedScriptAccounts = this.loadScriptAccountComponents();
|
const storedScriptAccounts = this.loadScriptAccountComponents();
|
||||||
const storedGamesystems = this.loadGamesystems();
|
const storedGamesystems = this.loadGamesystems();
|
||||||
const storedCharacters = this.loadCharacters()
|
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() {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
31
app/storage/loader/ItemLoader.js
Normal file
31
app/storage/loader/ItemLoader.js
Normal 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
|
33
app/storage/loader/ItemLoader.ts
Normal file
33
app/storage/loader/ItemLoader.ts
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
app/storage/storing/ItemStorage.js
Normal file
31
app/storage/storing/ItemStorage.js
Normal 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
|
@ -28,6 +28,7 @@ import {TemplateType} from "./project/game-model/templates/TemplateType";
|
|||||||
import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateTypeUtilities";
|
import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateTypeUtilities";
|
||||||
import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
|
import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
|
||||||
import {ItemSerializer} from "./project/serializer/ItemSerializer";
|
import {ItemSerializer} from "./project/serializer/ItemSerializer";
|
||||||
|
import {ItemParser} from "./project/parser/ItemParser";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
@ -224,6 +225,8 @@ export class AppComponent implements OnInit{
|
|||||||
const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts);
|
const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts);
|
||||||
gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters)
|
gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters)
|
||||||
|
|
||||||
|
gameModel.inventoryItems = ItemParser.parseItems(storedGameModel.storedItems)
|
||||||
|
|
||||||
this.gameModel = gameModel;
|
this.gameModel = gameModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,9 +23,6 @@ export class GameModel {
|
|||||||
|
|
||||||
constructor(gameModelName: string) {
|
constructor(gameModelName: string) {
|
||||||
this.gameModelName = gameModelName;
|
this.gameModelName = gameModelName;
|
||||||
|
|
||||||
this.createInventoryItem("Item 1")
|
|
||||||
this.createInventoryItem("Item 2")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addGamesystem(gamesystem: Gamesystem<any, any>) {
|
addGamesystem(gamesystem: Gamesystem<any, any>) {
|
||||||
|
85
src/app/project/parser/ItemParser.ts
Normal file
85
src/app/project/parser/ItemParser.ts
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
55
testModel/items/Item 1.json
Normal file
55
testModel/items/Item 1.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
55
testModel/items/Item 2.json
Normal file
55
testModel/items/Item 2.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user