/
match.go
417 lines (348 loc) · 9.58 KB
/
match.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
//"github.com/golang/glog"
"github.com/henyouqian/lwutil"
"net/http"
"time"
)
type Match struct {
Id uint32
Name string
GameId uint32
Begin int64
End int64
Sort string
TryMax uint32
TryPrice uint32
}
func newMatch(w http.ResponseWriter, r *http.Request) {
lwutil.CheckMathod(r, "POST")
rc := redisPool.Get()
defer rc.Close()
session, err := findSession(w, r, rc)
lwutil.CheckError(err, "err_auth")
checkAdmin(session)
appid := session.Appid
if appid == 0 {
lwutil.SendError("err_auth", "Please login with app secret")
}
//input
var in struct {
Name string
GameId uint32
Begin string
End string
TryMax uint32
TryPrice uint32
}
err = lwutil.DecodeRequestBody(r, &in)
lwutil.CheckError(err, "err_decode_body")
if in.Name == "" || in.Begin == "" || in.End == "" || in.GameId == 0 {
lwutil.SendError("err_input", "Missing Name || Begin || End || Gameid")
}
if in.TryMax == 0 {
in.TryMax = 1
}
//game info
game, err := findGame(in.GameId, appid)
lwutil.CheckError(err, "err_game")
//times
const timeform = "2006-01-02 15:04:05"
begin, err := time.ParseInLocation(timeform, in.Begin, time.Local)
lwutil.CheckError(err, "")
end, err := time.ParseInLocation(timeform, in.End, time.Local)
lwutil.CheckError(err, "")
beginUnix := begin.Unix()
endUnix := end.Unix()
if endUnix-beginUnix <= 60 {
lwutil.SendError("err_input", "endUnix - beginUnix must > 60 seconds")
}
if time.Now().Unix() > endUnix {
lwutil.SendError("err_input", "end time before now")
}
//
matchId, err := redis.Int(rc.Do("incr", "idGen/match"))
lwutil.CheckError(err, "")
match := Match{
uint32(matchId),
in.Name,
in.GameId,
beginUnix,
endUnix,
game.Sort,
in.TryMax,
in.TryPrice,
}
matchJson, err := json.Marshal(match)
lwutil.CheckError(err, "")
key := fmt.Sprintf("%d+%d", appid, matchId)
rc.Send("hset", "matches", key, matchJson)
key = fmt.Sprintf("matchesInApp/%d", appid)
rc.Send("zadd", key, endUnix, matchId)
rc.Flush()
for i := 0; i < 2; i++ {
_, err = rc.Receive()
lwutil.CheckError(err, "")
}
//reply
lwutil.WriteResponse(w, match)
}
func delMatch(w http.ResponseWriter, r *http.Request) {
lwutil.CheckMathod(r, "POST")
rc := redisPool.Get()
defer rc.Close()
session, err := findSession(w, r, rc)
lwutil.CheckError(err, "err_auth")
checkAdmin(session)
appid := session.Appid
if appid == 0 {
lwutil.SendError("err_auth", "Please login with app secret")
}
//input
matchIds := make([]int, 0, 8)
err = lwutil.DecodeRequestBody(r, &matchIds)
lwutil.CheckError(err, "err_decode_body")
//redis
key := fmt.Sprintf("matchesInApp/%d", appid)
params := make([]interface{}, 0, 8)
params = append(params, key)
matchIdsItf := make([]interface{}, len(matchIds))
for i, v := range matchIds {
matchIdsItf[i] = v
}
params = append(params, matchIdsItf...)
rc.Send("zrem", params...)
args := make([]interface{}, len(matchIds)+1)
args[0] = "matches"
for i, matchId := range matchIds {
key = fmt.Sprintf("%d+%d", appid, matchId)
args[i+1] = key
}
rc.Send("hdel", args...)
rc.Flush()
_, err = rc.Receive()
lwutil.CheckError(err, "")
delNum, err := rc.Receive()
lwutil.CheckError(err, "")
//reply
lwutil.WriteResponse(w, delNum)
}
func listMatch(w http.ResponseWriter, r *http.Request) {
lwutil.CheckMathod(r, "POST")
rc := redisPool.Get()
defer rc.Close()
session, err := findSession(w, r, rc)
lwutil.CheckError(err, "err_auth")
appid := session.Appid
if appid == 0 {
lwutil.SendError("err_auth", "Please login with app secret")
}
nowUnix := time.Now().Unix()
//get matchIds
key := fmt.Sprintf("matchesInApp/%d", appid)
matchIdValues, err := redis.Values(rc.Do("zrangebyscore", key, nowUnix, "+inf"))
lwutil.CheckError(err, "")
args := make([]interface{}, len(matchIdValues)+1)
args[0] = "matches"
for i, v := range matchIdValues {
var id int
id, err := redis.Int(v, err)
lwutil.CheckError(err, "")
matchkey := fmt.Sprintf("%d+%d", appid, id)
args[i+1] = matchkey
}
//get match data
matchesValues, err := redis.Values(rc.Do("hmget", args...))
matches := make([]Match, len(matchesValues))
for i, v := range matchesValues {
var match Match
err = json.Unmarshal(v.([]byte), &match)
lwutil.CheckError(err, "")
matches[i] = match
}
//out
type OutMatch struct {
Id uint32
Name string
GameId uint32
Begin int64
End int64
Sort string
TryMax uint32
TryPrice uint32
TryNum uint32
}
outMatches := make([]OutMatch, len(matches))
// get try number
for _, match := range matches {
tryNumKey := makeTryNumKey(match.Id)
rc.Send("hget", tryNumKey, session.Userid)
}
err = rc.Flush()
lwutil.CheckError(err, "")
for i, match := range matches {
tryNum, err := redis.Int(rc.Receive())
if err != nil && err != redis.ErrNil {
lwutil.CheckError(err, "")
}
outMatches[i] = OutMatch{
Id: match.Id,
Name: match.Name,
GameId: match.GameId,
Begin: match.Begin,
End: match.End,
Sort: match.Sort,
TryMax: match.TryMax,
TryPrice: match.TryPrice,
TryNum: uint32(tryNum),
}
}
lwutil.WriteResponse(w, outMatches)
}
func startMatch(w http.ResponseWriter, r *http.Request) {
lwutil.CheckMathod(r, "POST")
rc := redisPool.Get()
defer rc.Close()
session, err := findSession(w, r, rc)
lwutil.CheckError(err, "err_auth")
appid := session.Appid
if appid == 0 {
lwutil.SendError("err_auth", "Please login with app secret")
}
//input
var in struct {
MatchId uint32
}
err = lwutil.DecodeRequestBody(r, &in)
lwutil.CheckError(err, "err_decode_body")
//get match info
key := fmt.Sprintf("%d+%d", appid, in.MatchId)
matchJson, err := redis.Bytes(rc.Do("hget", "matches", key))
lwutil.CheckError(err, "err_not_found")
var match Match
err = json.Unmarshal(matchJson, &match)
lwutil.CheckError(err, "")
//check time
now := time.Now().Unix()
if now < match.Begin || now >= match.End-MATCH_TRY_DURATION_SEC {
lwutil.SendError("err_time", "now < match.Begin || now >= match.End-MATCH_TRY_DURATION_SEC")
}
//incr and check try number
tryNumKey := makeTryNumKey(in.MatchId)
tryNum, err := redis.Int(rc.Do("hget", tryNumKey, session.Userid))
if err != nil && err != redis.ErrNil {
lwutil.CheckError(err, "")
}
if uint32(tryNum) >= match.TryMax {
lwutil.SendError("err_no_try", "no try left")
}
_, err = rc.Do("hincrby", tryNumKey, session.Userid, 1)
lwutil.CheckError(err, "")
tryNum++
//new try secret
trySecret := lwutil.GenUUID()
_, err = rc.Do("setex", fmt.Sprintf("trySecrets/%s", trySecret), MATCH_TRY_DURATION_SEC, in.MatchId)
lwutil.CheckError(err, "")
//out
out := struct {
Secret string
TryNum uint32
}{trySecret, uint32(tryNum)}
lwutil.WriteResponse(w, out)
}
func addScore(w http.ResponseWriter, r *http.Request) {
lwutil.CheckMathod(r, "POST")
rc := redisPool.Get()
defer rc.Close()
session, err := findSession(w, r, rc)
lwutil.CheckError(err, "err_auth")
appid := session.Appid
if appid == 0 {
lwutil.SendError("err_auth", "Please login with app secret")
}
//input
var in struct {
TrySecret string
Score int64
}
err = lwutil.DecodeRequestBody(r, &in)
lwutil.CheckError(err, "err_decode_body")
//use secret to get matchId
matchIdRaw, err := rc.Do("get", fmt.Sprintf("trySecrets/%s", in.TrySecret))
lwutil.CheckError(err, "")
if matchIdRaw == nil {
lwutil.SendError("err_secret", "")
}
matchId64, err := redis.Int64(matchIdRaw, err)
lwutil.CheckError(err, "")
matchId := uint32(matchId64)
//get match info and prev score
keyLeaderboard := makeLeaderboardKey(matchId)
matchKey := fmt.Sprintf("%d+%d", appid, matchId)
rc.Send("hget", "matches", matchKey)
rc.Send("zscore", keyLeaderboard, session.Userid)
rc.Flush()
matchJs, err := redis.Bytes(rc.Receive())
lwutil.CheckError(err, "")
var match Match
err = json.Unmarshal(matchJs, &match)
lwutil.CheckError(err, "")
prevScore, err := redis.Int64(rc.Receive())
needOverwrite := false
if err == redis.ErrNil {
needOverwrite = true
} else {
lwutil.CheckError(err, "")
if match.Sort == SORT_ASC {
if in.Score < prevScore {
needOverwrite = true
}
} else if match.Sort == SORT_DESC {
if in.Score > prevScore {
needOverwrite = true
}
} else {
lwutil.SendError("", "invalid match.Sort: "+match.Sort)
}
}
//del from failboard and add to leaderboard and delete secret
if needOverwrite {
rc.Send("zadd", keyLeaderboard, in.Score, session.Userid)
}
rc.Send("zrank", keyLeaderboard, session.Userid)
rc.Send("del", fmt.Sprintf("trySecrets/%s", in.TrySecret))
err = rc.Flush()
lwutil.CheckError(err, "")
if needOverwrite {
_, err := rc.Receive()
lwutil.CheckError(err, "")
}
rank, err := redis.Int(rc.Receive())
lwutil.CheckError(err, "")
rank++
//reply
lwutil.WriteResponse(w, rank)
}
func makeLeaderboardKey(matchId uint32) string {
return fmt.Sprintf("leaderboard/%d", matchId)
}
func makeTryNumKey(matchId uint32) string {
return fmt.Sprintf("trynum/%d", matchId)
}
func regMatch() {
http.Handle("/match/new", lwutil.ReqHandler(newMatch))
http.Handle("/match/del", lwutil.ReqHandler(delMatch))
http.Handle("/match/list", lwutil.ReqHandler(listMatch))
http.Handle("/match/start", lwutil.ReqHandler(startMatch))
http.Handle("/match/addscore", lwutil.ReqHandler(addScore))
}
/*
matches: Hash{field: [appid+matchId] int, value: match Match.json}
matchesInApp: SortedSet{score: matchEndTimeUnix, member: matchId}
trySecrets/<trySecret>: String(matchId int)
trynum/<matchId int>: Hash{field: userId int, value: tryNum int}
leaderboard/<matchId int>: SortedSet{score: matchScore int, member: userId int}
*/