ice_gametopREDIS.go
14.8 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**********************************************
* Copyright(c) 2016-2017 IceSimba
* All rights reserved.
*
* FileName: ice_gametopREDIS.go
* Author: cc
* Version: 1.0
* Date: 2017/07/28
* Description:
* Others:
* Function List:
IceGametopRedisAddSortSet 将数据添加到有序集
IceGametopRedisSort 分数排序
IceGametopRedisSubmitScore 用户提交分数
IceGameTopRedisGetSingleUserRankInfo 获取单个用户的排名信息
IceGameTopRedisCreateRank 创建排行榜
* History:
1.Date:
Author:
Modification:
**********************************************/
package sdkredis
import (
"client/sdkdb"
"common"
"errors"
"log"
"regexp"
"strconv"
"strings"
)
type IGT struct {
RankIndex int `json:"rank_index"`
UID string `json:"user_id"`
MyKey string `json:"score"`
NickName string `json:"nick_name"`
HeadPortraitURL string `json:"headportrait_url"`
}
/**
* 将数据添加到有序集
*/
func IceGametopRedisAddSortSet() {
// 连接redis
redisConn := RedisClient.GetConn()
defer RedisClose(redisConn)
// 取出排行榜设置数据,并添加到有序集
rankSetList, rsderr := RedisGetKeys(redisConn, "ice_gametopset:*gtname")
if rsderr != nil {
log.Printf("RedisGetKeys err[%v]\n", rsderr)
return
}
for _, rs := range rankSetList {
index0 := strings.Index(rs, ":gtid:")
index1 := strings.Index(rs, ":gid:")
index2 := strings.Index(rs, ":toptype:")
index3 := strings.Index(rs, ":gtname")
gtid := common.Substr2(rs, index0+6, index1)
gid := common.Substr2(rs, index1+5, index2)
toptype := common.Substr2(rs, index2+9, index3)
keys, err := RedisGetKeys(redisConn, "ice_gametop:*gtid:"+gtid+"*gid:"+gid+"*toptype:"+toptype+"*")
if err != nil {
RedisClose(redisConn)
log.Printf("RedisGetKeys keys[%s] err[%v]\n", keys, err)
return
}
for _, key := range keys {
value, _ := RedisGetKV(redisConn, key)
value_float64, _ := strconv.ParseFloat(value, 64)
RedisZAddSortSet(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, value_float64, key)
}
}
}
/**
* 分数排序
*/
func IceGametopRedisSort(gtid string, gid string, toptype string, start int, stop int) (igts []*IGT) {
// 连接redis
redisConn := RedisClient.GetConn()
defer RedisClose(redisConn)
// 获取当前排行榜是正序还是反序
var sorceorder string
iceGameTopSet, _, err := sdkdb.IceGameTopSetQueryOne(gtid, gid, toptype)
if err == nil {
if iceGameTopSet != nil {
sorceorder = iceGameTopSet.SORCEORDER
} else {
return
}
} else {
log.Printf("IceGameTopSetQueryOne err[%v]", err)
return
}
toptype = iceGameTopSet.TOPTYPE //不需要对传入的toptype做判断,根据gid和gtid就可以获得排行
// 获得有序sort
sortsetName := "sortset:gtid:" + gtid + ":gid:" + gid + ":toptype:" + toptype
log.Printf("sortsetName[%s]", sortsetName)
var (
strList []string
sorterr error
valuecount = 2 //需要使用正则获取特定值的数目
resultstr string //redis查询结果字符串
)
log.Printf("sorceorder[%v]", sorceorder)
if sorceorder == "0" {
strList, sorterr = RedisSort(redisConn, sortsetName, start-1, stop-1) // 从小到大
} else if sorceorder == "1" {
strList, sorterr = RedisReverseSort(redisConn, sortsetName, start-1, stop-1) // 从大到小
}
if sorterr != nil {
log.Println("RedisSort err[%v]", sorterr)
return
} else if len(strList) == 0 {
log.Println("rank has no user")
return []*IGT{}
}
igts = []*IGT{}
index := start
_, err = RedisDBSelect(redisConn, 4)
if err != nil {
log.Printf("select db err:", err)
}
for i := 0; i < len(strList); i += 2 {
igt := new(IGT)
igt.RankIndex = index
startIndex := strings.Index(strList[i], "uid:") + 4
endIndex := strings.Index(strList[i], ":gtid")
igt.UID = common.Substr2(strList[i], startIndex, endIndex)
//查询储存昵称以及头像url的redis
var err error
resultstr, err = RedisGetKV(redisConn, "GUID:"+igt.UID)
if err != nil {
log.Println("get redis err:", err)
}
r, err := regexp.Compile(`\|([^\|])*\|`)
if err != nil {
log.Println("err:", err)
}
// 使用正则匹配需要的值
result := r.FindAllString(resultstr, valuecount)
//去除用来匹配的特殊字符“|“
if result != nil {
for key, value := range result {
value = value[1 : len(value)-1]
log.Println(value)
result[key] = value
}
//result 为切片,储存了所有匹配到的特定值
igt.NickName = result[0] //第一个为昵称
igt.HeadPortraitURL = result[1] //第二个为头像url
}
igt.MyKey = strList[i+1]
igts = append(igts, igt)
index++
}
_, err = RedisDBSelect(redisConn, 2)
if err != nil {
log.Printf("select db err:", err)
}
return
}
/**
* 用户提交分数
*/
func IceGametopRedisSubmitScore(uid string, gtid string, gid string, score string) error {
// 连接redis
redisConn := RedisClient.GetConn()
defer RedisClose(redisConn)
iceGameTopSets, err := sdkdb.IceGameTopSetQuery(gtid, gid)
if iceGameTopSets == nil {
err = errors.New("gametop name or game name not exist")
return err
}
if len(iceGameTopSets) == 0 {
return err
}
if err != nil {
log.Println(err)
return err
}
// update time 2017-09-07 14:59:54 update by 徐振浩
// update start
//提交的分数
score_float64, _ := strconv.ParseFloat(score, 64)
//提交的排行榜类型
log.Println(iceGameTopSets)
toptype := iceGameTopSets[0].TOPTYPE
log.Printf("toptoptop[%v]", toptype)
//redis键值
ice_gametopK := "ice_gametop:uid:" + uid + ":gtid:" + gtid + ":gid:" + gid + ":toptype:" + toptype + ":mykey"
//获取原有分数
value, err := RedisGetKV(redisConn, ice_gametopK)
log.Println(value)
var value_float64 float64
if value == "" {
value_float64 = 0
} else {
if err != nil {
return err
}
value_float64, _ = strconv.ParseFloat(value, 64)
}
//进行比较,比原先分数高才会更新
log.Println(value_float64)
sortType, err := sdkdb.IceGameTopSetQuerySortType(gtid, gid)
if err != nil {
return err
}
if sortType == "0" {
if value_float64-score_float64 > 0.000001 || value == "" {
if reply, err := RedisSetKV(redisConn, ice_gametopK, score); err != nil {
RedisClose(redisConn)
log.Printf("RedisSetKV reply[%v] err[%v]", reply, err)
return err
}
// 更新有序集中的数据
log.Println(score_float64)
log.Println(ice_gametopK)
res, err := RedisZAddSortSet(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, score_float64, ice_gametopK)
if err != nil {
log.Println(err)
}
log.Println(res)
}
} else {
if score_float64-value_float64 > 0.000001 {
if reply, err := RedisSetKV(redisConn, ice_gametopK, score); err != nil {
RedisClose(redisConn)
log.Printf("RedisSetKV reply[%v] err[%v]", reply, err)
return err
}
// 更新有序集中的数据
log.Println(score_float64)
log.Println(ice_gametopK)
res, err := RedisZAddSortSet(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, score_float64, ice_gametopK)
if err != nil {
log.Println(err)
}
log.Println(res)
}
}
//update end
return nil
}
/**
* 获取单个用户的排名信息
*/
func IceGameTopRedisGetSingleUserRankInfo(uid string, gtid string, gid string, toptype string) (igt *IGT) {
// 连接redis
redisConn := RedisClient.GetConn()
defer RedisClose(redisConn)
// 获取当前排行榜是正序还是反序/
var sorceorder string
iceGameTopSet, _, err := sdkdb.IceGameTopSetQueryOne(gtid, gid, toptype)
if err == nil {
if iceGameTopSet != nil {
sorceorder = iceGameTopSet.SORCEORDER
toptype = iceGameTopSet.TOPTYPE //不需要对传入的toptype做判断,根据gid和gtid就可以获得排行
} else {
return
}
} else {
return
}
// 排序
var strList []string
var sorterr error
if sorceorder == "0" {
strList, sorterr = RedisSort(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, 0, -1)
} else if sorceorder == "1" {
strList, sorterr = RedisReverseSort(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, 0, -1)
}
if sorterr != nil {
log.Println("RedisSort err[%v]", sorterr)
return
} else if len(strList) == 0 {
log.Println("rank has no data")
}
igt = new(IGT)
index := 1
var resultstr string
var valuecount = 2
_, err = RedisDBSelect(redisConn, 4)
if err != nil {
log.Printf("select db err:", err)
}
for i := 0; i < len(strList); i += 2 {
startIndex := strings.Index(strList[i], "uid:") + 4
endIndex := strings.Index(strList[i], ":gtid")
currUID := common.Substr2(strList[i], startIndex, endIndex)
if currUID == uid {
igt.UID = uid
igt.RankIndex = index
igt.MyKey = strList[i+1]
//查询储存昵称以及头像url的redis
var err error
resultstr, err = RedisGetKV(redisConn, "GUID:"+igt.UID)
if err != nil {
log.Println("get redis err:", err)
}
r, err := regexp.Compile(`\|([^\|])*\|`)
if err != nil {
log.Println("err:", err)
}
// 使用正则匹配需要的值
result := r.FindAllString(resultstr, valuecount)
//去除用来匹配的特殊字符“|“
if result != nil {
for key, value := range result {
value = value[1 : len(value)-1]
log.Println(value)
result[key] = value
}
//result 为切片,储存了所有匹配到的特定值
igt.NickName = result[0] //第一个为昵称
igt.HeadPortraitURL = result[1] //第二个为头像url
}
}
index++
}
_, err = RedisDBSelect(redisConn, 2)
if err != nil {
log.Printf("select db err:", err)
}
return
}
/**
* 获取单个用户以及前后用户的排名信息
*/
func IceGameTopRedisGetSingleUserRangeRankInfo(uid string, gtid string, gid string, toptype string, prerank string, nextrank string) (res *[]IGT, resIndex interface{}, err error) {
// 连接redis
redisConn := RedisClient.GetConn()
defer RedisClose(redisConn)
// 获取当前排行榜是正序还是反序/
var sorceorder string
iceGameTopSet, _, err := sdkdb.IceGameTopSetQueryOne(gtid, gid, toptype)
if err == nil {
if iceGameTopSet != nil {
sorceorder = iceGameTopSet.SORCEORDER
toptype = iceGameTopSet.TOPTYPE //不需要对传入的toptype做判断,根据gid和gtid就可以获得排行
} else {
resIndex = "105200"
err = errors.New("get toptype err")
log.Println("iceGameTopSet is nil")
return nil, resIndex, err
}
} else {
resIndex = "105207"
return nil, resIndex, err
}
// 获取当前用户排名
rank, err := RedisZRank(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, "ice_gametop:uid:"+uid+":gtid:"+gtid+":gid:"+gid+":toptype:"+toptype+":mykey")
if err != nil {
resIndex = "105202"
return nil, resIndex, err
}
if rank == nil {
err = errors.New("用户未提交分数")
return nil, "105201", err
}
log.Println("key:", "ice_gametop:uid:"+uid+":gtid:"+gtid+":gid:"+gid+":toptype:"+toptype+":mykey")
log.Println("rank:", rank)
rankIndex := int(rank.(int64))
// 获取当前用户前x位和后x位的排名数据
var prerankIndex int
var nextrankIndex int
if prerank == "" {
prerankIndex = 1
} else {
prerankIndex, err = strconv.Atoi(prerank)
if err != nil {
resIndex = "105200"
return nil, resIndex, err
}
}
if nextrank == "" {
nextrankIndex = 1
} else {
nextrankIndex, err = strconv.Atoi(nextrank)
if err != nil {
resIndex = "105200"
return nil, resIndex, err
}
}
if rankIndex-prerankIndex < 0 {
prerankIndex = 0
resIndex = rankIndex + 1
} else {
resIndex = prerankIndex + 1
prerankIndex = rankIndex - prerankIndex
}
nextrankIndex = rankIndex + nextrankIndex
// 排序
var strList []string
if sorceorder == "1" {
strList, err = RedisSort(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, prerankIndex, nextrankIndex)
} else if sorceorder == "0" {
strList, err = RedisReverseSort(redisConn, "sortset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype, prerankIndex, nextrankIndex)
}
if err != nil {
resIndex = "105203"
log.Println("RedisSort err")
return nil, resIndex, err
} else if len(strList) == 0 {
err = errors.New("rank has no data")
resIndex = "105204"
return nil, resIndex, err
}
igt := make([]IGT, len(strList)/2)
log.Println(igt)
log.Println(len(igt))
index := 0
var resultstr string
var valuecount = 2
_, err = RedisDBSelect(redisConn, 4)
if err != nil {
resIndex = "105205"
log.Printf("select db err")
return nil, resIndex, err
}
for i := 0; i < len(strList); i += 2 {
startIndex := strings.Index(strList[i], "uid:") + 4
endIndex := strings.Index(strList[i], ":gtid")
currUID := common.Substr2(strList[i], startIndex, endIndex)
var s int
if i == 0 {
s = 0
} else {
s = i / 2
}
igt[s].UID = currUID
igt[s].RankIndex = prerankIndex + index + 1
igt[s].MyKey = strList[i+1]
//查询储存昵称以及头像url的redis
resultstr, err = RedisGetKV(redisConn, "GUID:"+igt[s].UID)
if err != nil {
resIndex = "105206"
return nil, resIndex, err
}
r, err := regexp.Compile(`\|([^\|])*\|`)
if err != nil {
resIndex = "105200"
return nil, resIndex, err
}
// 使用正则匹配需要的值
result := r.FindAllString(resultstr, valuecount)
//去除用来匹配的特殊字符“|“
if result != nil {
for key, value := range result {
value = value[1 : len(value)-1]
log.Println(value)
result[key] = value
}
//result 为切片,储存了所有匹配到的特定值
igt[s].NickName = result[0] //第一个为昵称
igt[s].HeadPortraitURL = result[1] //第二个为头像url
}
index++
}
_, err = RedisDBSelect(redisConn, 2)
if err != nil {
resIndex = "105205"
log.Printf("select db err:")
return nil, resIndex, err
}
log.Println(resIndex)
return &igt, resIndex, nil
}
func IceGameTopRedisCreateRank(gtid string, gid string, toptype string, gtname string, sorcetype string, sorcesub string, sorceorder string) error {
// 连接redis
redisConn := RedisClient.GetConn()
defer RedisClose(redisConn)
if reply, err := RedisSetKV(redisConn, "ice_gametopset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype+":gtname", gtname); err != nil {
RedisClose(redisConn)
log.Printf("RedisCommand replay[%v] err[%v]\n", reply, err)
return err
}
if reply, err := RedisSetKV(redisConn, "ice_gametopset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype+":sorcetype", sorcetype); err != nil {
RedisClose(redisConn)
log.Printf("RedisCommand replay[%v] err[%v]\n", reply, err)
return err
}
if reply, err := RedisSetKV(redisConn, "ice_gametopset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype+":sorcesub", sorcesub); err != nil {
RedisClose(redisConn)
log.Printf("RedisCommand replay[%v] err[%v]\n", reply, err)
return err
}
if reply, err := RedisSetKV(redisConn, "ice_gametopset:gtid:"+gtid+":gid:"+gid+":toptype:"+toptype+":sorceorder", sorceorder); err != nil {
RedisClose(redisConn)
log.Printf("RedisCommand replay[%v] err[%v]\n", reply, err)
return err
}
return nil
}