ranklist.go
5.68 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
package main
import (
"client/sdkdb"
"client/sdkredis"
"common"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
)
type Msg struct {
ErrCode string `json:"code"`
ErrMsg string `json:"msg"`
Obj interface{} `json:"obj,omitempty"`
}
func main() {
configPath := flag.String("config", "./sdkRoute.conf", "config path")
flag.Parse()
fmt.Printf("config[%v]", *configPath)
config := common.NewConfig(*configPath)
// db初始化
sdkdb.DBInit()
// 连接数据库
dberr := sdkdb.DBRegis(config.DBMysql)
if dberr != nil {
log.Printf("DBRegis err[%v]\n", dberr)
return
}
// 设置数据库日志
sdkdb.SetLogDebug(true, os.Stdout)
// redis初始化
sdkredis.RedisInit(config)
// http服务配置
sdkhttp := common.NewHttp(config)
// app
sdkhttp.RegisterFunc("RankList", RankList)
sdkhttp.RegisterFunc("SubmitScore", SubmitScore)
sdkhttp.RegisterFunc("SingleRank", SingleRank)
sdkhttp.RegisterFunc("SingleRangeRank", SingleRangeRank)
// web
sdkhttp.RegisterFunc("CreateRank", CreateRank)
// 激活http服务
sdkhttp.Activate("ranklist")
}
/**
* 获取排行数据
*/
func RankList(c *gin.Context) {
gtid := c.Query("gtid")
gid := c.Query("gid")
toptype := c.Query("toptype")
start, _ := strconv.Atoi(c.Query("start"))
stop, _ := strconv.Atoi(c.Query("stop"))
log.Printf("RankList gtid[%s] gid[%s] toptype[%s] start[%d] stop[%d]\n", gtid, gid, toptype, start, stop)
igts := sdkredis.IceGametopRedisSort(gtid, gid, toptype, start, stop)
msg := new(Msg)
if len(igts) == 0 {
err := errors.New("ranklist has no user")
msg.ErrCode = "105103"
msg.ErrMsg = err.Error()
} else if igts == nil {
err := errors.New("gametop name or game name not exist")
msg.ErrCode = "105102"
msg.ErrMsg = err.Error()
} else {
msg.ErrCode = "0"
msg.ErrMsg = "success"
mapresult := make(map[string]interface{})
mapresult["ranklist"] = igts
msg.Obj = mapresult
}
jsonByte, jsonerr := json.Marshal(msg)
if jsonerr != nil {
log.Printf("marshal err[%v]\n", jsonerr)
}
jsonStr := string(jsonByte)
c.String(http.StatusOK, jsonStr)
}
/**
* 提交分数
*/
func SubmitScore(c *gin.Context) {
uid := c.Request.FormValue("uid")
gtid := c.Request.FormValue("gtid")
gid := c.Request.FormValue("gid")
score := c.Request.FormValue("score")
log.Printf("SubmitScore uid[%s] gtid[%s] gid[%s] score[%s]\n", uid, gtid, gid, score)
msg := new(Msg)
err := sdkredis.IceGametopRedisSubmitScore(uid, gtid, gid, score)
if err != nil {
msg.ErrCode = "105101"
msg.ErrMsg = err.Error()
}
err = sdkdb.IceGameTopMysqlSubmitScore(uid, gtid, gid, score)
if err != nil {
msg.ErrCode = "105102"
msg.ErrMsg = err.Error()
} else {
msg.ErrCode = "0"
msg.ErrMsg = "success"
}
jsonByte, jsonerr := json.Marshal(msg)
if jsonerr != nil {
log.Printf("marshal err[%v]\n", jsonerr)
}
jsonStr := string(jsonByte)
c.String(http.StatusOK, jsonStr)
}
/**
* 获得单个用户的排行信息
*/
func SingleRank(c *gin.Context) {
uid := c.Query("uid")
gtid := c.Query("gtid")
gid := c.Query("gid")
toptype := c.Query("toptype")
log.Printf("SingleRank uid[%s] gtid[%s] gid[%s] toptype[%s]\n", uid, gtid, gid, toptype)
igt := sdkredis.IceGameTopRedisGetSingleUserRankInfo(uid, gtid, gid, toptype)
fmt.Println(igt)
msg := new(Msg)
if igt == nil {
err := errors.New("gametop name or game name not exist")
msg.ErrMsg = err.Error()
msg.ErrCode = "105101"
} else {
msg.ErrCode = "0"
msg.ErrMsg = "success"
msg.Obj = igt
}
jsonByte, jsonerr := json.Marshal(msg)
if jsonerr != nil {
log.Printf("marshal err[%v]\n", jsonerr)
}
jsonStr := string(jsonByte)
c.String(http.StatusOK, jsonStr)
}
/**
* 获得单个用户以及前后用户的排行信息
*/
func SingleRangeRank(c *gin.Context) {
uid := c.Query("uid")
gtid := c.Query("gtid")
gid := c.Query("gid")
toptype := c.Query("toptype")
prerank := c.Query("prerank")
nextrank := c.Query("nextrank")
log.Printf("SingleRank uid[%s] gtid[%s] gid[%s] toptype[%s] prerank[%s] nextrank[%s]\n", uid, gtid, gid, toptype, prerank, nextrank)
igt, resIndex, err := sdkredis.IceGameTopRedisGetSingleUserRangeRankInfo(uid, gtid, gid, toptype, prerank, nextrank)
fmt.Println(igt)
log.Println(resIndex)
log.Println(err)
infoList := make(map[string]interface{}, 0)
msg := new(Msg)
if err != nil {
log.Println(err)
msg.ErrMsg = err.Error()
msg.ErrCode = resIndex.(string)
} else {
infoList["info_list"] = igt
infoList["user_index"] = resIndex.(int)
msg.ErrCode = "0"
msg.ErrMsg = "success"
msg.Obj = infoList
}
jsonByte, jsonerr := json.Marshal(msg)
if jsonerr != nil {
log.Printf("marshal err[%v]\n", jsonerr)
}
jsonStr := string(jsonByte)
c.String(http.StatusOK, jsonStr)
}
/**
* 创建排行榜
*/
func CreateRank(c *gin.Context) {
gtid := c.Request.FormValue("gtid")
gid := c.Request.FormValue("gid")
toptype := c.Request.FormValue("toptype")
gtname := c.Request.FormValue("gtname")
sorcetype := c.Request.FormValue("sorcetype")
sorcesub := c.Request.FormValue("sorcesub")
sorceorder := c.Request.FormValue("sorceorder")
if err := sdkdb.IceGameTopSetInsert(gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder); err != nil {
c.String(http.StatusOK, err.Error())
}
sdkredis.IceGameTopRedisCreateRank(gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder)
log.Printf("CreateRank gtid[%s] gid[%s] toptype[%s] gtname[%s] sorcetype[%s] sorcesub[%s] sorceorder[%s]\n", gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder)
msg := new(Msg)
msg.ErrCode = "0"
msg.ErrMsg = "success"
jsonByte, jsonerr := json.Marshal(msg)
if jsonerr != nil {
log.Printf("marshal err[%v]\n", jsonerr)
}
jsonStr := string(jsonByte)
c.String(http.StatusOK, jsonStr)
}