-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
175 lines (147 loc) · 3.76 KB
/
events.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package tflgame
import (
"encoding/json"
"time"
"tflgame/server/lib/cher"
)
type Event struct {
ID string `json:"id"`
Type string `json:"type"`
UserID string `json:"user_id"`
GameID *string `json:"game_id"`
Payload interface{} `json:"payload"`
CreatedAt time.Time `json:"created_at"`
}
type CreateUserPayload struct {
CreationID string `json:"creation_id"`
Handle string `json:"handle"`
Numeric string `json:"numeric"`
Pin *string `json:"pin,omitempty"`
}
type ChangeHandlePayload struct {
UserID string `json:"user_id"`
NewHandle string `json:"new_handle"`
NewNumeric string `json:"new_numeric"`
}
type ChangePinPayload struct {
UserID string `json:"user_id"`
Pin string `json:"pin,omitempty"`
}
type ReleaseHandlePayload struct {
UserID string `json:"user_id"`
Handle string `json:"handle"`
Numeric string `json:"numeric"`
}
type CreateGamePayload struct {
CreationID string `json:"creation_id"`
Prompts []Prompt `json:"prompts"`
DifficultyOptions DifficultyOptions `json:"difficulty_options"`
GameOptions GameOptions `json:"game_options"`
}
type AnswerPromptPayload struct {
PromptID string `json:"prompt_id"`
AnswerGiven string `json:"answer_given"`
Correct bool `json:"correct"`
}
type GiveHintPayload struct {
PromptID string `json:"prompt_id"`
NewPrompt string `json:"new_prompt"`
Lines []string `json:"lines"`
}
type FinishGamePayload struct {
Score int `json:"score"`
}
type RecalculateGameScorePayload struct {
Score int `json:"score"`
}
type RecalculateUserScorePayload struct {
Score int `json:"score"`
}
func PayloadHandler(eventType string, raw []byte, in *interface{}) error {
switch eventType {
case "create_user":
var payload CreateUserPayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "change_handle":
var payload ChangeHandlePayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "change_pin":
var payload ChangePinPayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "release_handle":
var payload ReleaseHandlePayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "create_game":
var payload CreateGamePayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "finish_game":
var payload FinishGamePayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "recalculate_game_score":
var payload RecalculateGameScorePayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "recalculate_user_score":
var payload RecalculateUserScorePayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "answer_prompt":
var payload AnswerPromptPayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
case "give_hint":
var payload GiveHintPayload
if err := json.Unmarshal(raw, &payload); err != nil {
return err
}
*in = payload
default:
return cher.New("unknown_event_type", cher.M{
"event_type": eventType,
})
}
return nil
}
func SafePublicPayload(in interface{}) (interface{}, error) {
// TODO(gm): this seems immature compared to other areas of event maanagement
// let's see if we can clean it up a little by using pointers?
var out interface{}
switch v := in.(type) {
case CreateUserPayload:
// remove "pin" hash
v.Pin = nil
out = v
case ChangePinPayload:
// remove "pin" hash
v.Pin = ""
out = v
default:
// no change
out = v
}
return out, nil
}