|
1 | 1 | package match
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 |
| - "reflect" |
6 |
| - |
7 |
| - "github.com/mrclayman/rest-and-go/gameclient/client/shared" |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
8 | 6 | )
|
9 | 7 |
|
10 | 8 | // Matchlist defines a structure that holds
|
11 | 9 | // lists of existing matches for all game types
|
12 | 10 | type Matchlist struct {
|
13 |
| - DM DMMatches |
14 |
| - CTF CTFMatches |
15 |
| - LMS LMSMatches |
16 |
| - Duel DuelMatches |
| 11 | + DM DMMatches `json:"dm"` |
| 12 | + CTF CTFMatches `json:"ctf"` |
| 13 | + LMS LMSMatches `json:"lms"` |
| 14 | + Duel DuelMatches `json:"duel"` |
| 15 | +} |
| 16 | + |
| 17 | +// MatchlistUnmarshaler unmarshals the contents |
| 18 | +// of the Matchlist structure from JSON byte slice |
| 19 | +type MatchlistUnmarshaler struct { |
| 20 | + Matchlist Matchlist |
17 | 21 | }
|
18 | 22 |
|
| 23 | +// UnmarshalJSON unmarshals the contents of a server's |
| 24 | +// response into the internal structure of match lists |
| 25 | +func (m *MatchlistUnmarshaler) UnmarshalJSON(in []byte) error { |
| 26 | + d := json.NewDecoder(bytes.NewReader(in)) |
| 27 | + //d.UseNumber() |
| 28 | + |
| 29 | + d.Decode(&m.Matchlist) |
| 30 | + return nil |
| 31 | + // Read opening curly brace |
| 32 | + /* var t json.Token |
| 33 | + var err error |
| 34 | + if t, err = d.Token(); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | +
|
| 38 | + for d.More() { |
| 39 | + var gt string |
| 40 | + var ok bool |
| 41 | +
|
| 42 | + // Get the game type identifier |
| 43 | + t, err = d.Token() |
| 44 | + gt, ok = t.(string) |
| 45 | + if !ok { |
| 46 | + return errors.New("Token not a string (game type)") |
| 47 | + } |
| 48 | + fmt.Printf("Processing match list for type %v\n", gt) |
| 49 | +
|
| 50 | + if err = d.Decode(>); err != nil { |
| 51 | + fmt.Println("Failed to read game type identifier") |
| 52 | + return err |
| 53 | + } |
| 54 | +
|
| 55 | + switch gt { |
| 56 | + case DeathMatch: |
| 57 | + err = m.unmarshalDMMatchlist(d) |
| 58 | + case CaptureTheFlag: |
| 59 | + err = m.unmarshalCTFMatchlist(d) |
| 60 | + case LastManStanding: |
| 61 | + err = m.unmarshalLMSMatchlist(d) |
| 62 | + case Duel: |
| 63 | + err = m.unmarshalDuelMatchlist(d) |
| 64 | + } |
| 65 | +
|
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + return nil |
| 72 | + */ |
| 73 | +} |
| 74 | + |
| 75 | +func (m *Matchlist) unmarshalDMMatchlist(d *json.Decoder) error { |
| 76 | + matches := make(DMMatches, 0, 5) |
| 77 | + if err := d.Decode(&matches); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + m.DM = matches |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (m *Matchlist) unmarshalCTFMatchlist(d *json.Decoder) error { |
| 86 | + matches := make(CTFMatches, 0, 5) |
| 87 | + if err := d.Decode(&matches); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + m.CTF = matches |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (m *Matchlist) unmarshalLMSMatchlist(d *json.Decoder) error { |
| 96 | + matches := make(LMSMatches, 0, 5) |
| 97 | + if err := d.Decode(&matches); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + m.LMS = matches |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func (m *Matchlist) unmarshalDuelMatchlist(d *json.Decoder) error { |
| 106 | + matches := make(DuelMatches, 0, 5) |
| 107 | + if err := d.Decode(&matches); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + m.Duel = matches |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +/* |
19 | 116 | // UnmarshalJSON unmarshals input byteslice into
|
20 | 117 | // a map structure, from which individual match
|
21 | 118 | // type instances are synthesized
|
@@ -60,6 +157,7 @@ func (m *Matchlist) UnmarshalJSON(in []byte) error {
|
60 | 157 | return nil
|
61 | 158 | }
|
62 | 159 |
|
| 160 | +
|
63 | 161 | func (m *Matchlist) unmarshalDMMatches(in []interface{}) error {
|
64 | 162 | for _, mMapIf := range in {
|
65 | 163 | mMap, ok := mMapIf.(map[string]interface{})
|
@@ -123,3 +221,4 @@ func (m *Matchlist) unmarshalDuelMatches(in []interface{}) error {
|
123 | 221 | }
|
124 | 222 | return nil
|
125 | 223 | }
|
| 224 | +*/ |
0 commit comments