blob: f466a8fd6cd6fd3c460c50ad8ef505a1c576b4f5 (
plain)
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
101
102
103
104
|
package main
import (
"encoding/json"
"fmt"
)
type MessageWithClient struct {
Client *GameClient
Message *Message
}
type Message struct {
Type string `json:"type"`
Data MessageData `json:"data"`
}
type MessageData interface{}
type MessageDataConnect struct {
}
type MessageDataPrepare struct {
Problem string `json:"problem"`
}
type MessageDataReady struct {
}
type MessageDataStart struct {
StartTime string `json:"startTime"`
}
type MessageDataCode struct {
Code string `json:"code"`
}
type MessageDataScore struct {
Score int `json:"score"`
}
type MessageDataFinish struct {
YourScore *int `json:"yourScore"`
OpponentScore *int `json:"opponentScore"`
}
type MessageDataWatch struct {
Problem string `json:"problem"`
ScoreA *int `json:"scoreA"`
CodeA string `json:"codeA"`
ScoreB *int `json:"scoreB"`
CodeB string `json:"codeB"`
}
func (m *Message) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
if err := json.Unmarshal(raw["type"], &m.Type); err != nil {
return err
}
var err error
switch m.Type {
case "connect":
var data MessageDataConnect
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "prepare":
var data MessageDataPrepare
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "ready":
var data MessageDataReady
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "start":
var data MessageDataStart
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "code":
var data MessageDataCode
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "score":
var data MessageDataScore
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "finish":
var data MessageDataFinish
err = json.Unmarshal(raw["data"], &data)
m.Data = data
case "watch":
var data MessageDataWatch
err = json.Unmarshal(raw["data"], &data)
m.Data = data
default:
err = fmt.Errorf("unknown message type: %s", m.Type)
}
return err
}
|