inventory-items-2 #44

Closed
sebastian wants to merge 32 commits from inventory-items-2 into inventory
13 changed files with 87 additions and 10 deletions
Showing only changes of commit 73edf03fa0 - Show all commits

View File

@ -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,6 +17,7 @@ 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 storedItemgroupsd = this.loadItemgroups();
return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, [], []); return new StoredGameModel_1.StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, [], []);
} }
loadScriptAccountComponents() { loadScriptAccountComponents() {
@ -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();
} }
loadItemgroups() {
const itemgroupDir = path.join(this.gameModelDir, ModelComponentFileDirectory_1.ModelComponentFileDirectory.ITEMGROUP_DIR_NAME);
const itemgroupLoader = new ItemLoader_1.ItemLoader(itemgroupDir);
return itemgroupLoader.loadItemgroups();
}
} }
exports.GameModelLoader = GameModelLoader; exports.GameModelLoader = GameModelLoader;
//# sourceMappingURL=GameModelLoader.js.map //# sourceMappingURL=GameModelLoader.js.map

View File

@ -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 storedItemgroups = this.loadItemgroups()
return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, [], []); return new StoredGameModel(gameModelName, storedScriptAccounts, storedGamesystems, storedCharacters, storedItemgroups, []);
} }
private loadScriptAccountComponents() { private loadScriptAccountComponents() {
@ -44,7 +46,9 @@ export class GameModelLoader {
} }
private loadItemgroups() {
const itemgroupDir = path.join(this.gameModelDir, ModelComponentFileDirectory.ITEMGROUP_DIR_NAME);
const itemgroupLoader = new ItemLoader(itemgroupDir);
return itemgroupLoader.loadItemgroups();
}
} }

View File

@ -0,0 +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";
export class ItemLoader {
itemDir: string
constructor(itemDir: string) {
this.itemDir = itemDir;
}
loadItemgroups(): StoreComponent[] {
return this.loadItemgroupsRecursively(this.itemDir);
}
private loadItemgroupsRecursively(currentDir: string): StoreComponent[] {
let loadedItemgroups : StoreComponent[] = []
const itemgroupFiles = FileUtils.listFilesInDirectory(currentDir);
itemgroupFiles.forEach(itemgroupFile => {
if(fs.lstatSync(itemgroupFile).isDirectory()) {
const childgroup = this.loadItemgroupsRecursively(itemgroupFile);
loadedItemgroups = loadedItemgroups.concat(childgroup);
} else if(path.basename(itemgroupFile).endsWith(".json") && this.fileRepresentsItemgroup(itemgroupFile)){
const loadedItemgroup = this.loadSingleItemgroup(itemgroupFile)!;
loadedItemgroups.push(loadedItemgroup)
}
})
return loadedItemgroups;
}
private loadSingleItemgroup(itemgroupFile: string): StoreComponent | undefined {
if(itemgroupFile.endsWith(".json")) {
const data = fs.readFileSync(itemgroupFile, "utf-8");
return new StoreComponent(data, itemgroupFile, ModelComponentType.ITEMGROUP);
}
return undefined
}
private fileRepresentsItemgroup(file: string): boolean {
return path.basename(path.dirname(file)) === path.parse(path.basename(file)).name;
}
}

View File

@ -19,7 +19,6 @@ class ItemgroupStorage {
const completeFileName = path.join(this.itemgroupDir, file); const completeFileName = path.join(this.itemgroupDir, file);
const itemgroupDirectory = path.join(...itemgroup.fileName.split("/").slice(0, -1)); const itemgroupDirectory = path.join(...itemgroup.fileName.split("/").slice(0, -1));
const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory); const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory);
console.log(completeItemgroupDirectory);
FileUtils_1.FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory); FileUtils_1.FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory);
fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8"); fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8");
} }

View File

@ -24,7 +24,6 @@ export class ItemgroupStorage {
const itemgroupDirectory = path.join(... itemgroup.fileName.split("/").slice(0, -1)) const itemgroupDirectory = path.join(... itemgroup.fileName.split("/").slice(0, -1))
const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory) const completeItemgroupDirectory = path.join(this.itemgroupDir, itemgroupDirectory)
console.log(completeItemgroupDirectory)
FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory); FileUtils.prepareDirectoryFroWriting(completeItemgroupDirectory);
fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8") fs.writeFileSync(completeFileName + ".json", itemgroup.jsonString, "utf-8")
} }

View File

@ -1,9 +1,11 @@
import {ItemGroup} from "./ItemGroup"; import {ItemGroup} from "./ItemGroup";
import {ItemGroupCharacteristic} from "./ItemgroupCharacteristic"; import {ItemGroupCharacteristic} from "./ItemgroupCharacteristic";
import {ItemgroupType} from "./ItemgroupType";
export class AbstractItemGroup extends ItemGroup { export class AbstractItemGroup extends ItemGroup {
children: ItemGroup[] = []; children: ItemGroup[] = [];
itemgroupType = ItemgroupType.ABSTRACT
addChildItemgroup(itemGroup: ItemGroup) { addChildItemgroup(itemGroup: ItemGroup) {
this.children.push(itemGroup) this.children.push(itemGroup)

View File

@ -1,10 +1,12 @@
import {ItemGroup} from "./ItemGroup"; import {ItemGroup} from "./ItemGroup";
import {Item} from "./Item"; import {Item} from "./Item";
import {ItemGroupCharacteristic} from "./ItemgroupCharacteristic"; import {ItemGroupCharacteristic} from "./ItemgroupCharacteristic";
import {ItemgroupType} from "./ItemgroupType";
export class ConcreteItemGroup extends ItemGroup { export class ConcreteItemGroup extends ItemGroup {
items: Item[] = []; items: Item[] = [];
itemgroupType = ItemgroupType.CONCRETE
addItem(item: Item, parentItemgroups: ItemGroup[]) { addItem(item: Item, parentItemgroups: ItemGroup[]) {
if(this.findItemByName(item.componentName) == undefined) { if(this.findItemByName(item.componentName) == undefined) {

View File

@ -0,0 +1,4 @@
export enum ItemgroupType {
CONCRETE,
ABSTRACT
}

View File

@ -0,0 +1,8 @@
import {ItemGroup} from "../../game-model/inventory/ItemGroup";
export class ItemgroupParser {
private parsedItemgroups: ItemGroup[] = []
}

View File

@ -7,5 +7,6 @@
"characteristicDescription": "" "characteristicDescription": ""
} }
], ],
"manuallyInheritedItems": [] "manuallyInheritedItems": [],
"itemgroupType": 1
} }

View File

@ -3,6 +3,7 @@
"componentDescription": "", "componentDescription": "",
"itemGroupCharacteristics": [], "itemGroupCharacteristics": [],
"manuallyInheritedItems": [], "manuallyInheritedItems": [],
"itemgroupType": 0,
"parentGroup": { "parentGroup": {
"componentName": "Clothing", "componentName": "Clothing",
"componentDescription": "", "componentDescription": "",
@ -12,6 +13,7 @@
"characteristicDescription": "" "characteristicDescription": ""
} }
], ],
"manuallyInheritedItems": [] "manuallyInheritedItems": [],
"itemgroupType": 1
} }
} }

View File

@ -3,6 +3,7 @@
"componentDescription": "", "componentDescription": "",
"itemGroupCharacteristics": [], "itemGroupCharacteristics": [],
"manuallyInheritedItems": [], "manuallyInheritedItems": [],
"itemgroupType": 0,
"parentGroup": { "parentGroup": {
"componentName": "Clothing", "componentName": "Clothing",
"componentDescription": "", "componentDescription": "",
@ -12,6 +13,7 @@
"characteristicDescription": "" "characteristicDescription": ""
} }
], ],
"manuallyInheritedItems": [] "manuallyInheritedItems": [],
"itemgroupType": 1
} }
} }

View File

@ -18,5 +18,6 @@
} }
] ]
} }
] ],
"itemgroupType": 1
} }