inventory-items-2 #44

Closed
sebastian wants to merge 32 commits from inventory-items-2 into inventory
11 changed files with 186 additions and 27 deletions
Showing only changes of commit f01d8c4dab - Show all commits

View File

@ -10,6 +10,8 @@ const CharacterLoader_1 = require("./CharacterLoader");
const ItemLoader_1 = require("./ItemLoader");
class GameModelLoader {
constructor(gameModelDir) {
this.loadedItemgroups = [];
this.loadedItems = [];
this.gameModelDir = gameModelDir;
}
loadGameModel() {
@ -17,8 +19,8 @@ class GameModelLoader {
const storedScriptAccounts = this.loadScriptAccountComponents();
const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters();
const storedItemgroups = this.loadItemgroups();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups, []);
this.loadItemsAndItemgroups();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, this.loadedItemgroups, this.loadedItems);
}
loadScriptAccountComponents() {
const scriptAccountDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.SCRIPTACCOUNT_DIR_NAME);
@ -35,10 +37,12 @@ class GameModelLoader {
const characterLoader = new CharacterLoader_1.CharacterLoader(characterDir);
return characterLoader.loadCharacters();
}
loadItemgroups() {
loadItemsAndItemgroups() {
const itemgroupDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.ITEMGROUP_DIR_NAME);
const itemgroupLoader = new ItemLoader_1.ItemLoader(itemgroupDir);
return itemgroupLoader.loadItemgroups();
itemgroupLoader.loadItemgroups();
this.loadedItems = itemgroupLoader.loadedItems;
this.loadedItemgroups = itemgroupLoader.loadedItemgroups;
}
}
exports.GameModelLoader = GameModelLoader;

View File

@ -11,6 +11,9 @@ import {ItemLoader} from "./ItemLoader";
export class GameModelLoader {
gameModelDir: string
loadedItemgroups: StoreComponent[] = []
loadedItems: StoreComponent[] = []
constructor(gameModelDir: string) {
this.gameModelDir = gameModelDir;
@ -22,9 +25,9 @@ export class GameModelLoader {
const storedScriptAccounts = this.loadScriptAccountComponents();
const storedGamesystems = this.loadGamesystems();
const storedCharacters = this.loadCharacters()
const storedItemgroups = this.loadItemgroups()
this.loadItemsAndItemgroups();
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups, []);
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, this.loadedItemgroups, this.loadedItems);
}
private loadScriptAccountComponents() {
@ -46,9 +49,12 @@ export class GameModelLoader {
}
private loadItemgroups() {
private loadItemsAndItemgroups() {
const itemgroupDir = path.join(this.gameModelDir, ModelComponentFileDirectory.ITEMGROUP_DIR_NAME);
const itemgroupLoader = new ItemLoader(itemgroupDir);
return itemgroupLoader.loadItemgroups();
itemgroupLoader.loadItemgroups();
this.loadedItems = itemgroupLoader.loadedItems;
this.loadedItemgroups = itemgroupLoader.loadedItemgroups;
}
}

View File

@ -3,30 +3,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemLoader = void 0;
const StoreComponent_1 = require("../StoreComponent");
const FileUtils_1 = require("../FileUtils");
const fs = require("node:fs");
const path = require("node:path");
const ModelComponentType_1 = require("../../../src/app/project/game-model/ModelComponentType");
const fs = require("node:fs");
class ItemLoader {
constructor(itemDir) {
this._loadedItemgroups = [];
this._loadedItems = [];
this.itemDir = itemDir;
}
loadItemgroups() {
return this.loadItemgroupsRecursively(this.itemDir);
this.loadItemgroupsRecursively(this.itemDir);
}
get loadedItemgroups() {
return this._loadedItemgroups;
}
get loadedItems() {
return this._loadedItems;
}
loadItemgroupsRecursively(currentDir) {
let loadedItemgroups = [];
const itemgroupFiles = FileUtils_1.FileUtils.listFilesInDirectory(currentDir);
itemgroupFiles.forEach(itemgroupFile => {
if (fs.lstatSync(itemgroupFile).isDirectory()) {
const childgroup = this.loadItemgroupsRecursively(itemgroupFile);
loadedItemgroups = loadedItemgroups.concat(childgroup);
this.loadItemgroupsRecursively(itemgroupFile);
}
else if (path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)) {
const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile);
loadedItemgroups.push(loadedItemgroup);
this._loadedItemgroups.push(loadedItemgroup);
}
else if (path.basename(itemgroupFile).endsWith(".json")) {
const loadedItem = this.loadSingleItem(itemgroupFile);
this._loadedItems.push(loadedItem);
}
});
return loadedItemgroups;
}
loadSingleItemgroup(itemgroupFile) {
if (itemgroupFile.endsWith(".json")) {
@ -35,6 +44,13 @@ class ItemLoader {
}
return undefined;
}
loadSingleItem(itemFile) {
if (itemFile.endsWith(".json")) {
const data = fs.readFileSync(itemFile, "utf-8");
return new StoreComponent_1.StoreComponent(data, itemFile, ModelComponentType_1.ModelComponentType.ITEM);
}
return undefined;
}
fileRepresentsItemgroup(file) {
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
}

View File

@ -1,35 +1,46 @@
import {StoreComponent} from "../StoreComponent";
import {FileUtils} from "../FileUtils";
import * as fs from "node:fs";
import * as path from "node:path";
import {ModelComponentType} from "../../../src/app/project/game-model/ModelComponentType";
import * as fs from "node:fs";
export class ItemLoader {
itemDir: string
private _loadedItemgroups: StoreComponent[] = []
private _loadedItems: StoreComponent[] = []
constructor(itemDir: string) {
this.itemDir = itemDir;
}
loadItemgroups(): StoreComponent[] {
return this.loadItemgroupsRecursively(this.itemDir);
loadItemgroups(){
this.loadItemgroupsRecursively(this.itemDir);
}
private loadItemgroupsRecursively(currentDir: string): StoreComponent[] {
let loadedItemgroups : StoreComponent[] = []
get loadedItemgroups(): StoreComponent[] {
return this._loadedItemgroups;
}
get loadedItems(): StoreComponent[] {
return this._loadedItems;
}
private loadItemgroupsRecursively(currentDir: string) {
const itemgroupFiles = FileUtils.listFilesInDirectory(currentDir);
itemgroupFiles.forEach(itemgroupFile => {
if(fs.lstatSync(itemgroupFile).isDirectory()) {
const childgroup = this.loadItemgroupsRecursively(itemgroupFile);
loadedItemgroups = loadedItemgroups.concat(childgroup);
this.loadItemgroupsRecursively(itemgroupFile);
} else if(path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)){
const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile)!;
loadedItemgroups.push(loadedItemgroup)
this._loadedItemgroups.push(loadedItemgroup)
} else if(path.basename(itemgroupFile).endsWith(".json")) {
const loadedItem = this.loadSingleItem(itemgroupFile)!;
this._loadedItems.push(loadedItem);
}
})
return loadedItemgroups;
}
private loadSingleItemgroup(itemgroupFile: string): StoreComponent | undefined {
@ -40,6 +51,14 @@ export class ItemLoader {
return undefined
}
private loadSingleItem(itemFile: string) {
if(itemFile.endsWith(".json")) {
const data = fs.readFileSync(itemFile, "utf-8");
return new StoreComponent(data, itemFile, ModelComponentType.ITEM);
}
return undefined
}
private fileRepresentsItemgroup(file: string): boolean {
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
}

View File

@ -29,6 +29,7 @@ import {TemplateTypeUtilities} from "./project/game-model/templates/TemplateType
import {SimpleTemplateGamesystem} from "./project/game-model/templates/simpleGamesystem/SimpleTemplateGamesystem";
import {ItemSerializer} from "./project/serializer/ItemSerializer";
import {ItemgroupParser} from "./project/parser/itemParser/ItemgroupParser";
import {ItemParser} from "./project/parser/itemParser/ItemParser";
@Component({
selector: 'app-root',
@ -227,9 +228,11 @@ export class AppComponent implements OnInit{
const itemgroupParser = new ItemgroupParser();
itemgroupParser.parseItemgroups(storedGameModel.storedItemgroups);
gameModel.itemgroups = itemgroupParser.getParsedTopItemgroups();
const itemParser = new ItemParser(itemgroupParser.getParsedItemgroups())
itemParser.parseItems(storedGameModel.storedItems);
this.gameModel = gameModel;
}

View File

@ -0,0 +1,87 @@
import {ItemGroup} from "../../game-model/inventory/ItemGroup";
import {StoreComponent} from "../../../../../app/storage/StoreComponent";
import {Item} from "../../game-model/inventory/Item";
import {ModelComponentType} from "../../game-model/ModelComponentType";
import {ConcreteItemGroup} from "../../game-model/inventory/ConcreteItemGroup";
import {ItemgroupCharacteristicValue} from "../../game-model/inventory/ItemgroupCharacteristicValue";
export class ItemParser {
parsedItemgroups: ItemGroup[] = []
constructor(itemgroups: ItemGroup[] = []) {
this.parsedItemgroups = itemgroups;
}
parseItems(storeComponents: StoreComponent[]): Item[] {
const parsedItems: Item[] = []
storeComponents.forEach(storeComponent => parsedItems.push(this.parseSingleItem(storeComponent)));
return parsedItems;
}
private parseSingleItem(storeComponent: StoreComponent) {
const parsedData = JSON.parse(storeComponent.jsonString);
const componentName = parsedData.componentName;
const componentDescription = parsedData.componentDescription;
const manuallyInheritedGroups = this.parseInheritedGroups(parsedData.manuallyInheritedGroups);
const hierarchyInheritedGroups = this.parseInheritedGroups(parsedData.hierarchyInheritedGroups);
const item = new Item(componentName, componentDescription, ModelComponentType.ITEM);
item.manuallyInheritedGroups = manuallyInheritedGroups;
item.hierarchyInheritedGroups = hierarchyInheritedGroups;
const owningGroup = hierarchyInheritedGroups.find(itemgroup => itemgroup instanceof ConcreteItemGroup);
if (owningGroup) {
(owningGroup as ConcreteItemGroup).items.push(item);
} else {
console.log("[ERROR] No group found for item: ", item.componentName)
}
item.itemCharacteristicValues = this.parseItemCharacteristicValues(parsedData.itemCharacteristicValues);
return item;
}
private parseInheritedGroups(inheritedGroupsData: any) {
const inheritedGroups: ItemGroup[] = []
for(let i=0; i<inheritedGroupsData.length; i++) {
const inheritedGroupName = inheritedGroupsData[i];
const matchedGroup = this.parsedItemgroups.find(group => group.componentName === inheritedGroupName);
if (matchedGroup) {
inheritedGroups.push(matchedGroup);
}
}
return inheritedGroups;
}
private parseItemCharacteristicValues(data: any): ItemgroupCharacteristicValue[] {
const values: ItemgroupCharacteristicValue[] = []
for(let i=0; i<data.length; i++) {
const key = this.findItemCharacteristic(data[i].key);
if (key) {
const characteristicValue = new ItemgroupCharacteristicValue(key!, data[i].value)
values.push(characteristicValue)
} else {
console.log("[ERROR] Characteristic not found: ", data[i].key)
}
}
return values;
}
private findItemCharacteristic(itemCharacteristicName: string) {
for(let i=0; i<this.parsedItemgroups.length; i++) {
const characteristic = this.parsedItemgroups[i].itemGroupCharacteristics
.find(characteristic=> characteristic.characteristicName === itemCharacteristicName);
if(characteristic != undefined) {
return characteristic;
}
}
return undefined;
}
}

View File

@ -58,6 +58,7 @@ export class ItemgroupParser {
if(parentgroup != undefined) {
const abstractParentgroup = parentgroup as AbstractItemGroup;
abstractParentgroup.addChildItemgroup(itemgroup)
this.parsedItemgroups.push(itemgroup)
} else {
this.parsedItemgroups.push(itemgroup);
this.topParsedItemgroups.push(itemgroup);
@ -78,11 +79,16 @@ export class ItemgroupParser {
const name = itemgroupCharacteristicData.characteristicName;
const description = itemgroupCharacteristicData.characteristicDescription;
return new ItemGroupCharacteristic(name, description, itemgroup);
const characteristic = new ItemGroupCharacteristic(name, description, itemgroup);
characteristic.itemgroup.addItemgroupCharacteristic(characteristic);
return characteristic;
}
getParsedTopItemgroups() {
return this.topParsedItemgroups;
}
getParsedItemgroups() {
return this.parsedItemgroups;
}
}

View File

@ -12,7 +12,7 @@ export class ItemSerializer {
private serializedItemgroups: StoreComponent[] = []
private serializedItems: StoreComponent[] = []
private static ignoredGroupKeys: string[] = ['type', 'unsaved', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
private static ignoredItemKeys: string[] = ['type', 'unsaved', 'hierarchyInheritedGroups']
private static ignoredItemKeys: string[] = ['type', 'unsaved']
public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] {
itemgroups.forEach(itemgroup => {

10
src/app/shared/Tupel.ts Normal file
View File

@ -0,0 +1,10 @@
export class Tupel<A, B> {
value00: A
value01: B
constructor(value00: A, value01: B) {
this.value00 = value00;
this.value01 = value01;
}
}

View File

@ -4,6 +4,10 @@
"manuallyInheritedGroups": [
"Inventory"
],
"hierarchyInheritedGroups": [
"Clothing",
"Hose"
],
"itemCharacteristicValues": [
{
"key": "Test0",

View File

@ -2,6 +2,10 @@
"componentName": "Hose 2",
"componentDescription": "",
"manuallyInheritedGroups": [],
"hierarchyInheritedGroups": [
"Clothing",
"Hose"
],
"itemCharacteristicValues": [
{
"key": "Test0",