-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
81 lines (68 loc) · 1.31 KB
/
parse.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
package ikaWeapon
import (
"encoding/json"
"io"
)
type strmap struct {
Key string
}
type player struct {
Team string
Weapon strmap
}
type rawPlayerJSON struct {
ID int
Lobby strmap
Rule strmap
Players []player
}
func decodeJSONBattleSingle(r io.Reader) (*Battle, error) {
d := json.NewDecoder(r)
raw := rawPlayerJSON{}
err := d.Decode(&raw)
if err != nil {
return nil, err
}
battle := raw.getBattle()
return &battle, nil
}
func decodeJSONBattle(r io.Reader) ([]Battle, error) {
d := json.NewDecoder(r)
raw := []rawPlayerJSON{}
err := d.Decode(&raw)
if err != nil {
return nil, err
}
var battle []Battle
for _, r := range raw {
battle = append(battle, r.getBattle())
}
return battle, nil
}
func (r rawPlayerJSON) getBattle() Battle {
b := Battle{ID: r.ID, Lobby: r.Lobby.Key, Rule: r.Rule.Key}
for _, p := range r.Players {
switch p.Team {
case "my":
b.TeamA.Weapon = append(b.TeamA.Weapon, p.Weapon.Key)
case "his":
b.TeamB.Weapon = append(b.TeamB.Weapon, p.Weapon.Key)
default:
// do nothing
}
}
return b
}
func decodeJSONWeaponKeys(r io.Reader) ([]string, error) {
d := json.NewDecoder(r)
var raw []strmap
err := d.Decode(&raw)
if err != nil {
return nil, err
}
var list []string
for _, r := range raw {
list = append(list, r.Key)
}
return list, nil
}