1
1
package match
2
2
3
- import (
4
- "encoding/json"
5
- )
6
-
7
3
// Matchlist defines a structure that holds
8
4
// lists of existing matches for all game types
9
5
type Matchlist struct {
@@ -12,209 +8,3 @@ type Matchlist struct {
12
8
LMS LMSMatches `json:"lms"`
13
9
Duel DuelMatches `json:"duel"`
14
10
}
15
-
16
- // MatchlistUnmarshaler unmarshals the contents
17
- // of the Matchlist structure from JSON byte slice
18
- type MatchlistUnmarshaler struct {
19
- Matchlist Matchlist
20
- }
21
-
22
- // UnmarshalJSON unmarshals the contents of a server's
23
- // response into the internal structure of match lists
24
- func (m * MatchlistUnmarshaler ) UnmarshalJSON (in []byte ) error {
25
- return json .Unmarshal (in , & m .Matchlist )
26
- // Read opening curly brace
27
- /* var t json.Token
28
- var err error
29
- if t, err = d.Token(); err != nil {
30
- return err
31
- }
32
-
33
- for d.More() {
34
- var gt string
35
- var ok bool
36
-
37
- // Get the game type identifier
38
- t, err = d.Token()
39
- gt, ok = t.(string)
40
- if !ok {
41
- return errors.New("Token not a string (game type)")
42
- }
43
- fmt.Printf("Processing match list for type %v\n", gt)
44
-
45
- if err = d.Decode(>); err != nil {
46
- fmt.Println("Failed to read game type identifier")
47
- return err
48
- }
49
-
50
- switch gt {
51
- case DeathMatch:
52
- err = m.unmarshalDMMatchlist(d)
53
- case CaptureTheFlag:
54
- err = m.unmarshalCTFMatchlist(d)
55
- case LastManStanding:
56
- err = m.unmarshalLMSMatchlist(d)
57
- case Duel:
58
- err = m.unmarshalDuelMatchlist(d)
59
- }
60
-
61
- if err != nil {
62
- return err
63
- }
64
- }
65
-
66
- return nil
67
- */
68
- }
69
-
70
- /*
71
- func (m *Matchlist) unmarshalDMMatchlist(d *json.Decoder) error {
72
- matches := make(DMMatches, 0, 5)
73
- if err := d.Decode(&matches); err != nil {
74
- return err
75
- }
76
-
77
- m.DM = matches
78
- return nil
79
- }
80
-
81
- func (m *Matchlist) unmarshalCTFMatchlist(d *json.Decoder) error {
82
- matches := make(CTFMatches, 0, 5)
83
- if err := d.Decode(&matches); err != nil {
84
- return err
85
- }
86
-
87
- m.CTF = matches
88
- return nil
89
- }
90
-
91
- func (m *Matchlist) unmarshalLMSMatchlist(d *json.Decoder) error {
92
- matches := make(LMSMatches, 0, 5)
93
- if err := d.Decode(&matches); err != nil {
94
- return err
95
- }
96
-
97
- m.LMS = matches
98
- return nil
99
- }
100
-
101
- func (m *Matchlist) unmarshalDuelMatchlist(d *json.Decoder) error {
102
- matches := make(DuelMatches, 0, 5)
103
- if err := d.Decode(&matches); err != nil {
104
- return err
105
- }
106
-
107
- m.Duel = matches
108
- return nil
109
- }
110
-
111
- /*
112
- // UnmarshalJSON unmarshals input byteslice into
113
- // a map structure, from which individual match
114
- // type instances are synthesized
115
- func (m *Matchlist) UnmarshalJSON(in []byte) error {
116
- // TODO Maybe I should do piecemeal unmarshaling
117
- // That way I should be able to leave the unmarshaling
118
- // to the sub-structures themselves.
119
- inMap := make(map[string]interface{})
120
- err := shared.DecodeJSON(in, &inMap)
121
- if err != nil {
122
- return err
123
- }
124
-
125
- for k, v := range inMap {
126
- // fmt.Printf("Key-value: K: %v, V: %v\n", k, v)
127
- if reflect.TypeOf(v).Kind() == reflect.Slice && len(v.([]interface{})) == 0 {
128
- continue
129
- }
130
-
131
- // fmt.Println("Type of non-empty v:", reflect.TypeOf(v))
132
- vMapSlice, ok := v.([]interface{})
133
- if !ok {
134
- return errors.New("Item in game type matchlist not a slice of maps")
135
- }
136
-
137
- switch k {
138
- case DeathMatch:
139
- err = m.unmarshalDMMatches(vMapSlice)
140
- case CaptureTheFlag:
141
- err = m.unmarshalCTFMatches(vMapSlice)
142
- case LastManStanding:
143
- err = m.unmarshalLMSMatches(vMapSlice)
144
- case Duel:
145
- err = m.unmarshalDuelMatches(vMapSlice)
146
- }
147
-
148
- if err != nil {
149
- return err
150
- }
151
- }
152
-
153
- return nil
154
- }
155
-
156
-
157
- func (m *Matchlist) unmarshalDMMatches(in []interface{}) error {
158
- for _, mMapIf := range in {
159
- mMap, ok := mMapIf.(map[string]interface{})
160
- if !ok {
161
- return errors.New("Item in slice not a map of values")
162
- }
163
-
164
- match, err := unmarshalDMMatch(mMap)
165
- if err != nil {
166
- return err
167
- }
168
- m.DM = append(m.DM, match)
169
- }
170
- return nil
171
- }
172
-
173
- func (m *Matchlist) unmarshalCTFMatches(in []interface{}) error {
174
- for _, mMapIf := range in {
175
- mMap, ok := mMapIf.(map[string]interface{})
176
- if !ok {
177
- return errors.New("Item in slice not a map of values")
178
- }
179
-
180
- match, err := unmarshalCTFMatch(mMap)
181
- if err != nil {
182
- return err
183
- }
184
- m.CTF = append(m.CTF, match)
185
- }
186
- return nil
187
- }
188
-
189
- func (m *Matchlist) unmarshalLMSMatches(in []interface{}) error {
190
- for _, mMapIf := range in {
191
- mMap, ok := mMapIf.(map[string]interface{})
192
- if !ok {
193
- return errors.New("Item in slice not a map of values")
194
- }
195
-
196
- match, err := unmarshalLMSMatch(mMap)
197
- if err != nil {
198
- return err
199
- }
200
- m.LMS = append(m.LMS, match)
201
- }
202
- return nil
203
- }
204
-
205
- func (m *Matchlist) unmarshalDuelMatches(in []interface{}) error {
206
- for _, mMapIf := range in {
207
- mMap, ok := mMapIf.(map[string]interface{})
208
- if !ok {
209
- return errors.New("Item in slice not a map of values")
210
- }
211
-
212
- match, err := unmarshalDuelMatch(mMap)
213
- if err != nil {
214
- return err
215
- }
216
- m.Duel = append(m.Duel, match)
217
- }
218
- return nil
219
- }
220
- */
0 commit comments