This commit is contained in:
		
							parent
							
								
									fed0ca99bf
								
							
						
					
					
						commit
						1b2fd273bf
					
				| @ -4,11 +4,13 @@ import {AbstractItemGroup} from "../game-model/inventory/AbstractItemGroup"; | |||||||
| import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup"; | import {ConcreteItemGroup} from "../game-model/inventory/ConcreteItemGroup"; | ||||||
| import {ModelComponentType} from "../game-model/ModelComponentType"; | import {ModelComponentType} from "../game-model/ModelComponentType"; | ||||||
| import {SerializeConstants} from "./SerializeConstants"; | import {SerializeConstants} from "./SerializeConstants"; | ||||||
|  | import {Item} from "../game-model/inventory/Item"; | ||||||
| 
 | 
 | ||||||
| export class ItemSerializer { | export class ItemSerializer { | ||||||
| 
 | 
 | ||||||
|   private serializedItemgroups: StoreComponent[] = [] |   private serializedItemgroups: StoreComponent[] = [] | ||||||
|   private static ignoredKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"] |   private static ignoredGroupKeys: string[] = ['type', 'unsaved', 'children', "itemgroup", "manuallyInheritedGroups", "hierarchyInheritedGroups", "items"] | ||||||
|  |   private static ignoredItemKeys: string[] = ['type', 'unsaved', 'hierarchyInheritedGroups'] | ||||||
| 
 | 
 | ||||||
|   public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] { |   public serializeItemgroups(itemgroups: ItemGroup[]): StoreComponent[] { | ||||||
|     itemgroups.forEach(itemgroup => { |     itemgroups.forEach(itemgroup => { | ||||||
| @ -25,6 +27,8 @@ export class ItemSerializer { | |||||||
|     } else { |     } else { | ||||||
|       const storeComponent = this.serializeItemgroup(itemgroup); |       const storeComponent = this.serializeItemgroup(itemgroup); | ||||||
|       this.serializedItemgroups.push(storeComponent) |       this.serializedItemgroups.push(storeComponent) | ||||||
|  | 
 | ||||||
|  |       this.serializeItemsOfItemgroup(itemgroup as ConcreteItemGroup); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -33,7 +37,7 @@ export class ItemSerializer { | |||||||
|     const fileName = ItemSerializer.computeItemgroupPath(itemgroup); |     const fileName = ItemSerializer.computeItemgroupPath(itemgroup); | ||||||
| 
 | 
 | ||||||
|     const jsonString = JSON.stringify(itemgroup, (key, value) => { |     const jsonString = JSON.stringify(itemgroup, (key, value) => { | ||||||
|       if(ItemSerializer.ignoredKeys.includes(key)) { |       if(ItemSerializer.ignoredGroupKeys.includes(key)) { | ||||||
|         return undefined |         return undefined | ||||||
|       } else { |       } else { | ||||||
| 
 | 
 | ||||||
| @ -45,14 +49,17 @@ export class ItemSerializer { | |||||||
| 
 | 
 | ||||||
|       } |       } | ||||||
|     }, SerializeConstants.JSON_INDENT); |     }, SerializeConstants.JSON_INDENT); | ||||||
|     console.log(fileName) |  | ||||||
|     return new StoreComponent(jsonString, fileName, componentType) |     return new StoreComponent(jsonString, fileName, componentType) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   private static computeItemgroupPath(itemgroup: ItemGroup): string { |   private static computeItemgroupPath(itemgroup: ItemGroup, itemPath: boolean = false): string { | ||||||
|     const itemgroupPath: string[] = []; |     const itemgroupPath: string[] = []; | ||||||
|     itemgroupPath.push(itemgroup.componentName); |     itemgroupPath.push(itemgroup.componentName); | ||||||
|     itemgroupPath.push(itemgroup.componentName); | 
 | ||||||
|  |     if(!itemPath) { | ||||||
|  |       itemgroupPath.push(itemgroup.componentName); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|     while(itemgroup.parentGroup !== undefined) { |     while(itemgroup.parentGroup !== undefined) { | ||||||
|       itemgroupPath.unshift(itemgroup.parentGroup.componentName); |       itemgroupPath.unshift(itemgroup.parentGroup.componentName); | ||||||
| @ -61,4 +68,36 @@ export class ItemSerializer { | |||||||
| 
 | 
 | ||||||
|     return itemgroupPath.join("/"); |     return itemgroupPath.join("/"); | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   private serializeItemsOfItemgroup(itemgroup: ConcreteItemGroup) { | ||||||
|  |     const fileName = ItemSerializer.computeItemgroupPath(itemgroup, true); | ||||||
|  |     itemgroup.items.forEach(item => { | ||||||
|  |       const storeComponent = this.serializeSingleItem(fileName, item); | ||||||
|  |       this.serializedItemgroups.push(storeComponent) | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private serializeSingleItem(itemgroupPath: string, item: Item) { | ||||||
|  |     const itemFile = itemgroupPath + "/" + item.componentName; | ||||||
|  | 
 | ||||||
|  |     const jsonString = JSON.stringify(item, (key, value) => { | ||||||
|  |       if(ItemSerializer.ignoredItemKeys.includes(key)) { | ||||||
|  |         return undefined | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       if(value instanceof ItemGroup) { | ||||||
|  |         return value.componentName | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       if(key == "key") { | ||||||
|  |         return value.characteristicName | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       return value; | ||||||
|  |     }, SerializeConstants.JSON_INDENT) | ||||||
|  | 
 | ||||||
|  |     return new StoreComponent(jsonString, itemFile, ModelComponentType.ITEM) | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										13
									
								
								testModel/items/Clothing/Hose/Hose 1.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								testModel/items/Clothing/Hose/Hose 1.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,13 @@ | |||||||
|  | { | ||||||
|  |     "componentName": "Hose 1", | ||||||
|  |     "componentDescription": "", | ||||||
|  |     "manuallyInheritedGroups": [ | ||||||
|  |         "Inventory" | ||||||
|  |     ], | ||||||
|  |     "itemCharacteristicValues": [ | ||||||
|  |         { | ||||||
|  |             "key": "Test0", | ||||||
|  |             "value": 0 | ||||||
|  |         } | ||||||
|  |     ] | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								testModel/items/Clothing/Hose/Hose 2.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								testModel/items/Clothing/Hose/Hose 2.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | { | ||||||
|  |     "componentName": "Hose 2", | ||||||
|  |     "componentDescription": "", | ||||||
|  |     "manuallyInheritedGroups": [], | ||||||
|  |     "itemCharacteristicValues": [ | ||||||
|  |         { | ||||||
|  |             "key": "Test0", | ||||||
|  |             "value": 0 | ||||||
|  |         } | ||||||
|  |     ] | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user