-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.go
73 lines (61 loc) · 1.55 KB
/
class.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
package retropg
import (
"context"
"encoding/json"
"github.com/kralamoure/retro"
"github.com/kralamoure/retro/retrotyp"
)
func (r *Db) Classes(ctx context.Context) (classes map[retrotyp.ClassId]retro.Class, err error) {
query := "SELECT id, name, label, short_description, description, spells, boost_costs" +
" FROM retro_static.classes;"
rows, err := r.pool.Query(ctx, query)
if err != nil {
return
}
defer rows.Close()
classes = make(map[retrotyp.ClassId]retro.Class)
for rows.Next() {
var class retro.Class
var boostCostsStr string
err = rows.Scan(&class.Id, &class.Name, &class.Label, &class.ShortDescription, &class.Description,
&class.Spells, &boostCostsStr)
if err != nil {
return
}
var boostCosts [][][]int
err := json.Unmarshal([]byte(boostCostsStr), &boostCosts)
if err != nil {
return nil, err
}
for i, v := range boostCosts {
characteristic := make([]retro.ClassBoostCost, len(v))
for i, v := range v {
var cost retro.ClassBoostCost
cost.Quantity = v[0]
cost.Cost = v[1]
if len(v) >= 3 {
cost.Bonus = v[2]
} else {
cost.Bonus = 1
}
characteristic[i] = cost
}
switch i {
case 0:
class.BoostCosts.Vitality = characteristic
case 1:
class.BoostCosts.Wisdom = characteristic
case 2:
class.BoostCosts.Strength = characteristic
case 3:
class.BoostCosts.Intelligence = characteristic
case 4:
class.BoostCosts.Chance = characteristic
case 5:
class.BoostCosts.Agility = characteristic
}
}
classes[class.Id] = class
}
return
}