-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafting_list.go
125 lines (112 loc) · 3.15 KB
/
crafting_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"fmt"
"strings"
)
type CraftingList map[string]CraftingItem
func (this CraftingList) addRecipe(name, description string, output int, ingredientList []Ingredient) {
//add the recipe
newCraftingItem := newItem(name, description)
if output > 0 {
newCraftingItem.Recipes = []Recipe{Recipe{output, ingredientList}}
//add or update the ingredients
for _, anIngredient := range ingredientList {
newIngredient := anIngredient.toItem()
newIngredient.usedIn = []string{name}
this.insertItem(newIngredient)
}
}
this.insertItem(newCraftingItem)
}
//only to be used when inserting a single new recipe and all its ingredients into the crafting list
func (this CraftingList) insertItem(item CraftingItem) {
key := strings.ToLower(item.Name)
oldItem, OK := this[key]
if !OK {
this[key] = item
} else {
if item.Description != "" {
oldItem.Description = item.Description
}
oldItem.Recipes = append(oldItem.Recipes, item.Recipes...)
if len(item.usedIn) != 0 {
if !contains(oldItem.usedIn, item.usedIn[0]) { //not sure why I'm only checking the first index here
oldItem.usedIn = append(oldItem.usedIn, item.usedIn...)
}
}
this[key] = oldItem
}
}
func (this CraftingList) listRecipes(itemName string) []Recipe {
itemName = strings.ToLower(itemName)
return this[itemName].Recipes
}
func (this CraftingList) getItem(itemName string) (item CraftingItem, OK bool) {
item, OK = this[strings.ToLower(itemName)]
return
}
func (this CraftingList) listUses(itemName string, recursion bool) []string {
itemName = strings.ToLower(itemName)
NUses := this[itemName].usedIn
if recursion && len(NUses) > 0 {
ARC := []string{}
for _, NUse := range NUses {
ARC2 := []string{}
NPlusOneUses := this.listUses(NUse, recursion)
for _, NPlusOneUse := range NPlusOneUses {
use := fmt.Sprintf("%s -> %s", NUse, NPlusOneUse)
ARC2 = append(ARC2, use)
}
if len(ARC2) == 0 {
ARC = append(ARC, NUse)
} else {
ARC = append(ARC, ARC2...)
}
}
return ARC
}
return NUses
}
func (this CraftingList) getElementTree(itemName string) (Tree, bool) {
itemName = strings.ToLower(itemName)
tree, OK := newTree(this, itemName)
if OK {
tree.walk()
}
return tree, OK
}
func (this CraftingList) updateDescription(itemName, description string) {
itemName = strings.ToLower(itemName)
item := this[itemName]
item.Description = description
this[itemName] = item
}
func (this CraftingList) toBytes() []byte {
list := []CraftingItem{}
for _, item := range this {
if len(item.Recipes) > 0 || item.Description != "" {
list = append(list, item)
}
}
b, _ := json.MarshalIndent(list, "", "\t")
return b
}
//this is gonnaa be a problem later for items with no recipes but with descriptions!!
func (this CraftingList) fromBytes(b []byte) (err error) {
list := []CraftingItem{}
err = json.Unmarshal(b, &list)
if err != nil {
return
}
for _, item := range list {
for _, recipe := range item.Recipes {
this.addRecipe(item.Name, item.Description, recipe.Output, recipe.Ingredients)
}
if len(item.Recipes) == 0 {
basicItem := newItem(item.Name, item.Description)
this.insertItem(basicItem)
}
}
return
}