-
Notifications
You must be signed in to change notification settings - Fork 0
/
spell.go
175 lines (155 loc) · 4.34 KB
/
spell.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
type Spells []Spell
// These methods make Spells type satisfy the sort.Sort interface
// It may not be a good idea to sort with raw values, but not pointer
// values, but we shall see is it a bottleneck when benchmarks are written
func (s Spells) Len() int { return len(s) }
func (s Spells) Less(i, j int) bool { return s[i].Index < s[j].Index }
func (s Spells) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type Spell struct {
Index string
Name string
Desc string
HigherLevel string
Range string
Components []string
Material string
Ritual bool
Duration string
Concentration bool
CastingTime string `json:"casting_time"`
Level int
School struct{ Name string }
Classes []struct{ Name string }
Subclasses []struct{ Name string }
}
type SpellAPI struct {
Count int
Next string
//previous string
Results []SpellTemp
}
type SpellTemp struct {
Index string `json:"slug"`
Name string
Desc string
HigherLevel string `json:"higher_level"`
Range string
Components string
Material string
Ritual string
Duration string
Concentration string
CastingTime string `json:"casting_time"`
Level int `json:"level_int"` // api has level and level_int
School string
Class string `json:"dnd_class"`
Archetype string
Circles string
}
// Modified during testing
var fetchFunc func(string) ([]byte, error) = __fetchSpellsAPI
func fetchSpells(url string, data *Spells) error {
allSpells := Spells{}
for url != "" {
body, err := fetchFunc(url)
if err != nil {
fmt.Println("cannot read response:", err)
return err
}
var jsonResp SpellAPI
if err := json.Unmarshal(body, &jsonResp); err != nil {
if e, ok := err.(*json.SyntaxError); ok {
return fmt.Errorf("Cannot parse json: %s, at byte offset %d", err, e.Offset)
}
return fmt.Errorf("Cannot parse json: %s", err)
}
url = jsonResp.Next
standard := spellAPIToStandard(&jsonResp.Results)
allSpells = append(allSpells, *standard...)
}
*data = allSpells
return nil
}
func __fetchSpellsAPI(url string) ([]byte, error) {
r, err := http.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
return io.ReadAll(r.Body)
}
func spellAPIToStandard(spells *[]SpellTemp) *Spells {
a := Spells{}
for _, spell := range *spells {
s := Spell{}
s.Index = spell.Index
s.Name = spell.Name
s.Desc = spell.Desc
s.HigherLevel = spell.HigherLevel
s.Range = spell.Range
if spell.Components != "" {
s.Components = strings.Split(spell.Components, ",")
for i, c := range s.Components {
s.Components[i] = strings.TrimSpace(c)
}
}
s.Material = spell.Material
if spell.Ritual == "yes" {
s.Ritual = true
}
s.Duration = spell.Duration
if spell.Concentration == "yes" {
s.Concentration = true
}
s.CastingTime = spell.CastingTime
s.Level = spell.Level
s.School = struct{ Name string }{spell.School}
for _, class := range strings.Split(spell.Class, ",") {
class = strings.TrimSpace(class)
if class != "Ritual Caster" {
s.Classes = append(s.Classes, struct{ Name string }{class})
}
}
if spell.Archetype != "" {
for _, arch := range strings.Split(spell.Archetype, ",") {
arch = strings.TrimSpace(arch)
parts := strings.Split(arch, ": ")
// if there are at least 2 parts, continue, if there are not,
// there is a problem in parsing without a doubt
if len(parts) >= 2 {
subclass := parts[0] + " (" + parts[1] + ")"
s.Subclasses = append(s.Subclasses, struct{ Name string }{subclass})
}
}
}
if spell.Circles != "" {
for _, circle := range strings.Split(spell.Circles, ",") {
circle = strings.TrimSpace(circle)
subclass := "Druid (" + circle + ")"
// There is an inconsistency in the api where the circle is sometimes
// contained in the archeatypes, this fixes that
var contained bool
for _, subcl := range s.Subclasses {
if subcl.Name == subclass {
contained = true
break
}
}
if contained {
continue
}
s.Subclasses = append(s.Subclasses, struct{ Name string }{subclass})
}
}
a = append(a, s)
}
return &a
}