inventory-items-2 #44

Closed
sebastian wants to merge 32 commits from inventory-items-2 into inventory
9 changed files with 131 additions and 65 deletions
Showing only changes of commit 2e29d4cde7 - Show all commits

View File

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

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 {ItemgroupParser} from "./project/parser/itemParser/ItemgroupParser";
@Component({
selector: 'app-root',
@ -224,6 +225,11 @@ export class AppComponent implements OnInit{
const characterParser = new CharacterParser(characterTemplateSystems, characterRelationTemplateSystems, gameModel.scriptAccounts);
gameModel.characters = characterParser.parseCharacters(storedGameModel.storedCharacters)
const itemgroupParser = new ItemgroupParser();
itemgroupParser.parseItemgroups(storedGameModel.storedItemgroups);
gameModel.itemgroups = itemgroupParser.getParsedTopItemgroups();
this.gameModel = gameModel;
}

View File

@ -28,16 +28,6 @@ export class GameModel {
constructor(gameModelName: string) {
this.gameModelName = gameModelName;
this.addAbstractItemgroup("Clothing", undefined);
this.addAbstractItemgroup("Inventory", undefined);
this.addConcreteItemgroup("Oberteil", GameModel.findItemgroupByName("Clothing", this.itemgroups)! as AbstractItemGroup);
this.addConcreteItemgroup("Hose", GameModel.findItemgroupByName("Clothing", this.itemgroups)! as AbstractItemGroup);
const item = this.addItem("Hose 1", "Hose");
item!.addInheritedGroup(GameModel.findItemgroupByName("Inventory", this.itemgroups!)!)
console.log(GameModel.findItemgroupByName("Inventory", this.itemgroups!)!.manuallyInheritedItems)
this.addItem("Hose 2", "Hose");
}
addAbstractItemgroup(groupName: string, parentgroup: AbstractItemGroup | undefined) {

View File

@ -1,8 +1,88 @@
import {ItemGroup} from "../../game-model/inventory/ItemGroup";
import {StoreComponent} from "../../../../../app/storage/StoreComponent";
import {ItemGroupCharacteristic} from "../../game-model/inventory/ItemgroupCharacteristic";
import {AbstractItemGroup} from "../../game-model/inventory/AbstractItemGroup";
import {ModelComponentType} from "../../game-model/ModelComponentType";
import {ConcreteItemGroup} from "../../game-model/inventory/ConcreteItemGroup";
export class ItemgroupParser {
private parsedItemgroups: ItemGroup[] = []
private topParsedItemgroups: ItemGroup[] = []
parseItemgroups(storeComponents: StoreComponent[]) {
const topologicalSort = this.topologicalSort(storeComponents);
topologicalSort.forEach(storeComponent => this.parseSingleItemgroup(storeComponent));
return this.parsedItemgroups;
}
private topologicalSort(storeComponents: StoreComponent[]) {
const sortedObjects: StoreComponent[] = []
const visited: {[key: string]: boolean} = {};
const visit = (object: StoreComponent) => {
const jsonData = JSON.parse(object.jsonString)
if(visited[jsonData.componentName]) return;
visited[jsonData.componentName] = true;
if(jsonData.parent) {
const parentObject = storeComponents.find(obj => JSON.parse(obj.jsonString).componentName === jsonData.parent);
if(parentObject) {
visit(parentObject);
}
}
sortedObjects.push(object);
}
storeComponents.forEach(object => visit(object));
return sortedObjects;
}
parseSingleItemgroup(storeComponent: StoreComponent) {
const data = JSON.parse(storeComponent.jsonString);
const componentName = data.componentName;
const componentDescription = data.componentDescription;
const parentgroupName = data.parentgroup;
let itemgroup;
if(data.itemgroupType == 0) {
itemgroup = new ConcreteItemGroup(componentName, componentDescription, ModelComponentType.ITEMGROUP);
} else {
itemgroup = new AbstractItemGroup(componentName, componentDescription, ModelComponentType.ITEMGROUP);
}
const parentgroup = this.parsedItemgroups.find(parent => parent.componentName === parentgroupName);
if(parentgroup != undefined) {
const abstractParentgroup = parentgroup as AbstractItemGroup;
abstractParentgroup.addChildItemgroup(itemgroup)
} else {
this.parsedItemgroups.push(itemgroup);
this.topParsedItemgroups.push(itemgroup);
}
this.parseItemgroupCharacteristics(data.itemGroupCharacteristics, itemgroup);
}
parseItemgroupCharacteristics(itemgroupCharacteristicsData: any, itemgroup: ItemGroup): ItemGroupCharacteristic[] {
const itemgroupCharacteristics: ItemGroupCharacteristic[] = []
for(let i=0; i<itemgroupCharacteristicsData.length; i++) {
this.parseSingleItemgroupCharacteristic(itemgroupCharacteristicsData[i], itemgroup)
}
return itemgroupCharacteristics;
}
private parseSingleItemgroupCharacteristic(itemgroupCharacteristicData: any, itemgroup: ItemGroup) {
const name = itemgroupCharacteristicData.characteristicName;
const description = itemgroupCharacteristicData.characteristicDescription;
return new ItemGroupCharacteristic(name, description, itemgroup);
}
getParsedTopItemgroups() {
return this.topParsedItemgroups;
}
}

View File

@ -5,12 +5,13 @@ import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup";
import {ModelComponentType} from "../game-model/ModelComponentType";
import {SerializeConstants} from "./SerializeConstants";
import {Item} from "../game-model/inventory/Item";
import {ItemgroupType} from "../game-model/inventory/ItemgroupType";
export class ItemSerializer {
private serializedItemgroups: StoreComponent[] = []
private serializedItems: StoreComponent[] = []
private static ignoredGroupKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
private static ignoredGroupKeys: string[] = ['type', 'unsaved', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"]
private static ignoredItemKeys: string[] = ['type', 'unsaved', 'hierarchyInheritedGroups']
public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] {
@ -22,33 +23,59 @@ export class ItemSerializer {
private serializeSingleItemgroupHierarchy(itemgroup: ItemGroup) {
if(itemgroup instanceof AbstractItemGroup) {
const storeComponent = this.serializeItemgroup(itemgroup)
const storeComponent = this.serializeAbstractItemgroup(itemgroup)
this.serializedItemgroups.push(storeComponent)
itemgroup.children.forEach(child => this.serializeSingleItemgroupHierarchy(child));
} else {
const storeComponent = this.serializeItemgroup(itemgroup);
const storeComponent = this.serializeConcreteItemgroup(itemgroup as ConcreteItemGroup);
this.serializedItemgroups.push(storeComponent)
this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup);
}
}
private serializeItemgroup(itemgroup: ItemGroup): StoreComponent {
private serializeAbstractItemgroup(itemgroup: AbstractItemGroup): StoreComponent {
const componentType = ModelComponentType.ITEMGROUP;
const fileName = ItemSerializer.computeItemgroupPath(itemgroup);
const jsonString = JSON.stringify(itemgroup, (key, value) => {
if(ItemSerializer.ignoredGroupKeys.includes(key)) {
return undefined
} else {
if(key === 'key') {
return value.characteristicName
} else {
return value;
const jsonObject = {
componentName: itemgroup.componentName,
componentDescription: itemgroup.componentDescription,
itemGroupCharacteristics: itemgroup.itemGroupCharacteristics.map(characteristic => {
return {
characteristicName: characteristic.characteristicName,
characteristicDescription: characteristic.characteristicDescription
}
}),
parentgroup: itemgroup.parentGroup,
itemgroupType: ItemgroupType.ABSTRACT
};
}
const jsonString = JSON.stringify(jsonObject, (key, value) => {
return value;
}, SerializeConstants.JSON_INDENT);
return new StoreComponent(jsonString, fileName, componentType)
}
private serializeConcreteItemgroup(itemgroup: ConcreteItemGroup) {
const componentType = ModelComponentType.ITEMGROUP;
const fileName = ItemSerializer.computeItemgroupPath(itemgroup);
const jsonObject = {
componentName: itemgroup.componentName,
componentDescription: itemgroup.componentDescription,
itemGroupCharacteristics: itemgroup.itemGroupCharacteristics.map(characteristic => {
return {
characteristicName: characteristic.characteristicName,
characteristicDescription: characteristic.characteristicDescription
}
}),
itemgroupType: ItemgroupType.CONCRETE,
parentgroup: itemgroup.parentGroup?.componentName
};
const jsonString = JSON.stringify(jsonObject, (key, value) => {
return value;
}, SerializeConstants.JSON_INDENT);
return new StoreComponent(jsonString, fileName, componentType)
}

View File

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

View File

@ -2,18 +2,6 @@
"componentName": "Hose",
"componentDescription": "",
"itemGroupCharacteristics": [],
"manuallyInheritedItems": [],
"itemgroupType": 0,
"parentGroup": {
"componentName": "Clothing",
"componentDescription": "",
"itemGroupCharacteristics": [
{
"characteristicName": "Test0",
"characteristicDescription": ""
}
],
"manuallyInheritedItems": [],
"itemgroupType": 1
}
"parentgroup": "Clothing"
}

View File

@ -2,18 +2,6 @@
"componentName": "Oberteil",
"componentDescription": "",
"itemGroupCharacteristics": [],
"manuallyInheritedItems": [],
"itemgroupType": 0,
"parentGroup": {
"componentName": "Clothing",
"componentDescription": "",
"itemGroupCharacteristics": [
{
"characteristicName": "Test0",
"characteristicDescription": ""
}
],
"manuallyInheritedItems": [],
"itemgroupType": 1
}
"parentgroup": "Clothing"
}

View File

@ -7,17 +7,5 @@
"characteristicDescription": ""
}
],
"manuallyInheritedItems": [
{
"componentName": "Hose 1",
"componentDescription": "",
"itemCharacteristicValues": [
{
"key": "Test0",
"value": 0
}
]
}
],
"itemgroupType": 1
}