/
spell.go
146 lines (129 loc) · 3.26 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
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
}
func fetchSpells() ([]Spell, error) {
url := "https://api.open5e.com/spells/"
allSpells := []Spell{}
for url != "" {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("cannot read response:", err)
return nil, err
}
var jsonResp SpellAPI
if err := json.Unmarshal(body, &jsonResp); err != nil {
fmt.Println("cannot parse json:", err)
return nil, err
}
url = jsonResp.Next
standard := spellAPIToStandard(&jsonResp.Results)
allSpells = append(allSpells, *standard...)
}
return allSpells, nil
}
func spellAPIToStandard(spells *[]SpellTemp) *[]Spell {
a := []Spell{}
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
s.Components = strings.Split(spell.Components, ", ")
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, ", ") {
if class != "Ritual Caster" {
s.Classes = append(s.Classes, struct{ Name string }{class})
}
}
if spell.Archetype != "" {
for _, arch := range strings.Split(spell.Archetype, "<br/> ") {
parts := strings.Split(arch, ": ")
subclass := parts[0] + " (" + parts[1] + ")"
s.Subclasses = append(s.Subclasses, struct{ Name string }{subclass})
}
}
if spell.Circles != "" {
for _, circle := range strings.Split(spell.Circles, ", ") {
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
}