-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
100 lines (81 loc) · 1.39 KB
/
json.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
package main
import (
"encoding/json"
"reflect"
"errors"
)
type Generic struct {
T string
Obj json.RawMessage
}
func genericJSON(i interface{}) ([]byte, error) {
b, err := json.Marshal(i)
if err != nil {
return nil, err
}
raw := json.RawMessage(b)
t := reflect.TypeOf(i).String()
g := Generic{t, raw}
return json.Marshal(g)
}
func getMsg(b []byte) (interface{}, error) {
var g Generic
json.Unmarshal(b, &g)
switch g.T {
case "main.hello":
var r hello
json.Unmarshal(g.Obj, &r)
return r, nil
case "main.changeCard":
var r changeCard
json.Unmarshal(g.Obj, &r)
return r, nil
case "main.timerStart":
var r timerStart
json.Unmarshal(g.Obj, &r)
return r, nil
case "main.endTurn":
var r endTurn
json.Unmarshal(g.Obj, &r)
return r, nil
default:
var r interface{}
return r, errors.New("Cannot recognize type")
}
}
type hello struct {
Name string
}
type helloACK struct {
Accepted bool
Text string
PlayerID int
TeamID int
CurrentPlayer int
}
type cardMsg struct {
Img string
}
type changeCard struct {
PlayerID int
}
type timerStart struct {
PlayerID int
}
type endTurn struct {
PlayerID int
}
type newTurn struct {
CurrentPlayer int
}
type playerEntry struct {
Name string
Team int
IsTurn bool
}
type playersList struct {
Players []playerEntry
}
func (this *playersList) addPlayer(pe playerEntry) {
this.Players = append(this.Players, pe)
}