-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafting_item.go
71 lines (62 loc) · 1.6 KB
/
crafting_item.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
package main
import (
"fmt"
)
type CraftingItem struct {
Name string `json:"name"`
Description string `json:"description"`
Recipes []Recipe `json:"recipes"`
usedIn []string //lower case to hide it from JSON package
}
func newItem(name, description string) (item CraftingItem) {
item.Name = name
item.Description = description
return
}
type Recipe struct {
Output int `json:"Output"`
Ingredients []Ingredient `json:"ingredients"`
}
type Ingredient struct {
Name string `json:"name"`
Amount int `json:"amount"`
}
func (this Ingredient) toItem() CraftingItem {
return newItem(this.Name, "")
}
func (this CraftingItem) String() string {
descTemplate := "%s\n"
thisTemplate := "%s\n%s"
var descString string
if this.Description != "" {
descString = fmt.Sprintf(descTemplate, this.Description)
} //else leave it blank
outStr := fmt.Sprintf(thisTemplate, this.Name, descString)
for _, recipe := range this.Recipes {
outStr = outStr + recipe.String(this.Name)
}
if len(this.usedIn) > 0 {
outStr = outStr + "Can be used in: "
length := len(this.usedIn)
for index, use := range this.usedIn {
useStr := use
if index < length-1 {
useStr = useStr + ","
}
outStr = outStr + useStr + " "
}
}
return outStr
}
func (this Recipe) String(name string) string {
var outStr string
length := len(this.Ingredients)
for index, ing := range this.Ingredients {
outStr = outStr + fmt.Sprintf("%d %s", ing.Amount, ing.Name)
if index < length-1 {
outStr = outStr + " + "
}
}
outStr = outStr + fmt.Sprintf(" --> %d %s\n", this.Output, name)
return outStr
}