Commit 27bd56386ce1d622d796a64f5b4b34e76e00ccba
1 parent
a3d78f8e
Exists in
master
提交欢乐招财猫接口
Showing
5 changed files
with
510 additions
and
78 deletions
Show diff stats
src/HttpServer/jsonconf/jsonconf.go
... | ... | @@ -70,6 +70,20 @@ func GetCatConfig(lv int) *CatDesc { |
70 | 70 | return nil |
71 | 71 | } |
72 | 72 | |
73 | +//获取红包猫配置 | |
74 | +func GetRedCatConfig(lv int) *RedCatDesc { | |
75 | + reallv := lv - 100 | |
76 | + if reallv < 1 { | |
77 | + reallv = 1 | |
78 | + } | |
79 | + for _, v := range g_jsonconf.RedCatConfig { | |
80 | + if v.Id == reallv { | |
81 | + return &v | |
82 | + } | |
83 | + } | |
84 | + return nil | |
85 | +} | |
86 | + | |
73 | 87 | |
74 | 88 | func file_get_contents(path string) ([]byte, error) { |
75 | 89 | f, err := os.Open(path) | ... | ... |
src/HttpServer/logic/datadef.go
1 | 1 | package logic |
2 | 2 | |
3 | 3 | type UserLoginReq struct { |
4 | - Lype int `json:"type"` | |
4 | + Lype int `json:"type"` | |
5 | 5 | UserId int `json:"userId"` |
6 | - Fromid int `json:"fromid"` | |
7 | - | |
6 | + Fromid int `json:"fromid"` | |
8 | 7 | } |
9 | 8 | |
10 | 9 | type UserLoginData struct { |
11 | - UserId string `json:"userId"` | |
12 | - AccessToken string `json:"accessToken"` | |
13 | - LoginType int `json:"loginType"` | |
10 | + UserId string `json:"userId"` | |
11 | + AccessToken string `json:"accessToken"` | |
12 | + LoginType int `json:"loginType"` | |
14 | 13 | Nickname string `json:"nickname"` |
15 | - HeadImg string `json:"headImg"` | |
14 | + HeadImg string `json:"headImg"` | |
16 | 15 | } |
17 | 16 | |
18 | 17 | type UserLoginResp struct { |
19 | - Code int `json:"code"` | |
20 | - Data UserLoginData `json:"data"` | |
21 | - Message string `json:"message"` | |
18 | + Code int `json:"code"` | |
19 | + Data UserLoginData `json:"data"` | |
20 | + Message string `json:"message"` | |
22 | 21 | } |
23 | 22 | |
24 | 23 | type GetUserDataReq struct { |
25 | 24 | //Uuid int `json:"uuid"` |
26 | 25 | } |
27 | 26 | |
27 | +type offlineRewardDesc struct { | |
28 | + OfflineTime int `json:"offlineTime"` | |
29 | + Income string `json:"income"` | |
30 | +} | |
31 | + | |
32 | +type GetUserDataData struct { | |
33 | + TimingReward bool `json:"timingReward"` | |
34 | + OfflineReward offlineRewardDesc `json:"offlineReward"` | |
35 | + Output string `json:"output"` | |
36 | + Coin string `json:"coin"` | |
37 | + Now int `json:"now"` | |
38 | +} | |
39 | + | |
28 | 40 | type GetUserDataResp struct { |
29 | - Code int `json:"code"` | |
30 | - Message string `json:"message"` | |
31 | - Gold int64 `json:"gold"` | |
32 | - Love int64 `json:"love"` | |
33 | - Dougoldlefttime int `json:"dougoldlefttime"` | |
34 | - Automlefttime int `json:"automlefttime"` | |
35 | - Acclcteboxlefttime int `json:"acclcteboxlefttime"` | |
36 | - Israndgift int `json:"israndgift"` | |
41 | + Code int `json:"code"` | |
42 | + Message string `json:"message"` | |
43 | + Data GetUserDataData `json:"data"` | |
37 | 44 | } |
38 | 45 | |
39 | 46 | type DataDesc struct { |
... | ... | @@ -135,12 +142,25 @@ type ClickRandGiftResp struct { |
135 | 142 | } |
136 | 143 | |
137 | 144 | type DoBuyCatReq struct { |
138 | - Lv int `json:"lv"` | |
145 | + CatId int `json:"catId"` | |
146 | +} | |
147 | + | |
148 | +type DoBuyCatCoin struct { | |
149 | + UserId int `json:"userId"` | |
150 | + UpdateTime int `json:"updateTime"` | |
151 | + Coin string `json:"coin"` | |
152 | + IcomeRate string `json:"icomeRate"` | |
153 | +} | |
154 | + | |
155 | +type DoBuyCatData struct { | |
156 | + Position int `json:"position"` | |
157 | + Price string `json:"price"` | |
158 | + Coin DoBuyCatCoin `json:"coin"` | |
139 | 159 | } |
140 | 160 | type DoBuyCatResp struct { |
141 | - Code int `json:"code"` | |
142 | - Message string `json:"message"` | |
143 | - Pos int `json:"pos"` | |
161 | + Code int `json:"code"` | |
162 | + Message string `json:"message"` | |
163 | + Data DoBuyCatData `json:"data"` | |
144 | 164 | } |
145 | 165 | |
146 | 166 | type QueryWareHouseResp struct { |
... | ... | @@ -255,10 +275,11 @@ type UpdateUserInfoResp struct { |
255 | 275 | |
256 | 276 | type RankInfoDesc struct { |
257 | 277 | Rank int `json:"rank"` |
278 | + UserId int `json:"userId"` | |
258 | 279 | Headurl string `json:"headurl"` |
259 | 280 | Nickname string `json:"nickname"` |
260 | - Catlv int `json:"catlv"` | |
261 | - Goldnum int64 `json:"goldnum"` | |
281 | + CatName string `json:"text"` | |
282 | + Income int64 `json:"income"` | |
262 | 283 | } |
263 | 284 | |
264 | 285 | type QueryPlayerRankResp struct { |
... | ... | @@ -322,14 +343,16 @@ type StartOnlineTaskResp struct { |
322 | 343 | } |
323 | 344 | |
324 | 345 | type GetOfflineRewardReq struct { |
325 | - Optype int `json:"optype"` | |
346 | + Reward int `json:"reward"` | |
347 | + Optype int `json:"type"` | |
348 | +} | |
349 | +type GetOfflineRewardData struct { | |
326 | 350 | } |
327 | 351 | |
328 | 352 | type GetOfflineRewardResp struct { |
329 | - Code int `json:"code"` | |
330 | - Message string `json:"message"` | |
331 | - Gold int64 `json:"gold"` | |
332 | - Love int64 `json:"love"` | |
353 | + Code int `json:"code"` | |
354 | + Message string `json:"message"` | |
355 | + Data GetOfflineRewardData `json:"data"` | |
333 | 356 | } |
334 | 357 | |
335 | 358 | //********************************************************************************************************** |
... | ... | @@ -402,9 +425,10 @@ type AchieveMentData struct { |
402 | 425 | |
403 | 426 | //玩家数据 |
404 | 427 | type UserData struct { |
405 | - UserId int //玩家id | |
406 | - RegTime int //注册时间 | |
428 | + UserId int //玩家id | |
429 | + RegTime int //注册时间 | |
407 | 430 | Gold int64 //金币 |
431 | + GoldSum int64 //玩家累计金币 | |
408 | 432 | Love int64 //爱心值 |
409 | 433 | Goldrate int64 //金币生成速率 |
410 | 434 | Loverate int64 //爱心生产速率 | ... | ... |
src/HttpServer/logic/function.go
... | ... | @@ -40,7 +40,8 @@ func GetUserInfo(uid string) (*UserData, error) { |
40 | 40 | //初始化玩家信息 |
41 | 41 | func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { |
42 | 42 | udata := new(UserData) |
43 | - udata.Gold = 0 | |
43 | + udata.Gold = 100000 | |
44 | + udata.GoldSum += udata.Gold | |
44 | 45 | udata.Love = 0 |
45 | 46 | udata.InviteId = data.Fromid |
46 | 47 | udata.Highestlv = 0 |
... | ... | @@ -68,7 +69,6 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { |
68 | 69 | udata.Goldrate = 0 |
69 | 70 | udata.Loverate = 0 |
70 | 71 | |
71 | - | |
72 | 72 | //初始化商店信息 |
73 | 73 | initcatcfg := jsonconf.GetCatConfig(1) |
74 | 74 | if initcatcfg == nil { |
... | ... | @@ -77,7 +77,7 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { |
77 | 77 | } |
78 | 78 | var info BuyCatInfoData |
79 | 79 | info.Buytime = 0 |
80 | - info.CurPrice,_= strconv.ParseInt(initcatcfg.Price,10,64) | |
80 | + info.CurPrice, _ = strconv.ParseInt(initcatcfg.Price, 10, 64) | |
81 | 81 | info.IsMaxBuytime = 0 |
82 | 82 | udata.BuyCatInfo = append(udata.BuyCatInfo, info) |
83 | 83 | |
... | ... | @@ -96,7 +96,6 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { |
96 | 96 | //保存redis |
97 | 97 | SaveUserInfo(udata, strconv.Itoa(uuid)) |
98 | 98 | |
99 | - | |
100 | 99 | //初始化玩家仓库信息 |
101 | 100 | pware := new(UserWareHouseData) |
102 | 101 | for i := 0; i < 15; i++ { |
... | ... | @@ -113,4 +112,138 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { |
113 | 112 | //保存此次计算时间 |
114 | 113 | nowtimestr := strconv.Itoa(int(time.Now().Unix())) |
115 | 114 | redishandler.GetRedisClient().HSet(redis.USER_LAST_CALC_TIME, strconv.Itoa(uuid), nowtimestr) |
116 | -} | |
117 | 115 | \ No newline at end of file |
116 | +} | |
117 | + | |
118 | +//计算购买后猫箱子的位置,将箱子放到对应位置 返回值表示位置 -1表示失败都满了 | |
119 | +func getCatPutPos(uinfo *UserData, clv int) int { | |
120 | + pos := -1 | |
121 | + for k, v := range uinfo.PosInfo { | |
122 | + if v.Catlv == 0 { | |
123 | + uinfo.PosInfo[k].Catlv = clv | |
124 | + uinfo.PosInfo[k].Pos = k | |
125 | + //v.Catlv = clv | |
126 | + //v.Pos = k | |
127 | + pos = k | |
128 | + break | |
129 | + } | |
130 | + } | |
131 | + return pos | |
132 | +} | |
133 | + | |
134 | +func (udata *UserData) CalcGoldRate() int64 { | |
135 | + //计算出所有速率的和 | |
136 | + //计算方式为 首先计算合成界面猫的产出速率,然后计算红毛包的产出速率,最后计算仓库中的猫的产出速率 | |
137 | + var sumrate int64 | |
138 | + cfg37 := jsonconf.GetCatConfig(37) | |
139 | + if cfg37 == nil { | |
140 | + logger.Error("37cfg err!!") | |
141 | + return 0 | |
142 | + } | |
143 | + for _, v := range udata.PosInfo { | |
144 | + if v.Catlv > 0 && v.Catlv < 100 { | |
145 | + //有猫存在的要计算一下 | |
146 | + //非红包猫 | |
147 | + for _, vv := range jsonconf.GetJsonConf().CatConfig { | |
148 | + if vv.Id == v.Catlv { | |
149 | + onerate, _ := strconv.ParseInt(vv.Gold_get, 10, 64) | |
150 | + sumrate += onerate | |
151 | + break | |
152 | + } | |
153 | + } | |
154 | + } | |
155 | + | |
156 | + if v.Catlv > 100 { | |
157 | + //红包猫 需要判断是否产金币 | |
158 | + rcfg := jsonconf.GetRedCatConfig(v.Catlv) | |
159 | + if rcfg != nil { | |
160 | + if rcfg.Is_37 != 0 { | |
161 | + onerate, _ := strconv.ParseInt(cfg37.Gold_get, 10, 64) | |
162 | + sumrate += onerate | |
163 | + } | |
164 | + } else { | |
165 | + logger.Error("GetRedCatConfig failed lv=%v", v.Catlv) | |
166 | + } | |
167 | + } | |
168 | + } | |
169 | + | |
170 | + udatastr, err := redishandler.GetRedisClient().HGet(redis.USER_WAREHOUSE_INFO, strconv.Itoa(udata.UserId)) | |
171 | + if err != nil { | |
172 | + logger.Error("CalcGoldRate failed err=%v", err) | |
173 | + return sumrate | |
174 | + } | |
175 | + wdata := new(UserWareHouseData) | |
176 | + err = json.Unmarshal([]byte(udatastr), wdata) | |
177 | + if err != nil { | |
178 | + logger.Error("CalcGoldRate Unmarshal redis failed ") | |
179 | + return sumrate | |
180 | + } | |
181 | + for _, v := range wdata.Info { | |
182 | + rcfg := jsonconf.GetRedCatConfig(v.Warelv + 100) | |
183 | + if rcfg != nil { | |
184 | + if rcfg.Is_37 != 0 { | |
185 | + onerate, _ := strconv.ParseInt(cfg37.Gold_get, 10, 64) | |
186 | + sumrate += onerate | |
187 | + } | |
188 | + } else { | |
189 | + logger.Error("GetRedCatConfig failed lv=%v", v.Warelv+100) | |
190 | + } | |
191 | + } | |
192 | + | |
193 | + return sumrate | |
194 | +} | |
195 | + | |
196 | +//发生金币变动时保存排行榜 | |
197 | +func (udata *UserData) AddToRank() error { | |
198 | + err := redishandler.GetRedisClient().Zadd(redis.USER_GOLD_RANK, float64(udata.Gold), strconv.Itoa(udata.UserId)) | |
199 | + if err != nil { | |
200 | + logger.Error("AddToRank failed err=%v", err) | |
201 | + } | |
202 | + return err | |
203 | +} | |
204 | + | |
205 | +//计算最高等级猫的名字 | |
206 | +func (u *UserData) CalcHigestCatName() string { | |
207 | + higestlv := 0 | |
208 | + for _, val := range u.PosInfo { | |
209 | + if higestlv < val.Catlv { | |
210 | + higestlv = val.Catlv | |
211 | + } | |
212 | + } | |
213 | + | |
214 | + //计算仓库内的 | |
215 | + udatastr, err := redishandler.GetRedisClient().HGet(redis.USER_WAREHOUSE_INFO, strconv.Itoa(u.UserId)) | |
216 | + if err != nil { | |
217 | + logger.Error("CalcHigestCatName failed err=%v", err) | |
218 | + return "" | |
219 | + } | |
220 | + wdata := new(UserWareHouseData) | |
221 | + err = json.Unmarshal([]byte(udatastr), wdata) | |
222 | + if err != nil { | |
223 | + logger.Error("CalcHigestCatName Unmarshal redis failed ") | |
224 | + return "" | |
225 | + } | |
226 | + for _, val := range wdata.Info { | |
227 | + reallv := val.Warelv + 100 | |
228 | + if higestlv < reallv { | |
229 | + higestlv = reallv | |
230 | + } | |
231 | + } | |
232 | + | |
233 | + //计算猫名字 | |
234 | + if higestlv > 100 { | |
235 | + cfg := jsonconf.GetRedCatConfig(higestlv - 100) | |
236 | + if cfg == nil { | |
237 | + logger.Error("CalcHigestCatName failed getcfg higestlv=%v", higestlv) | |
238 | + return "" | |
239 | + } | |
240 | + return cfg.Name | |
241 | + } else { | |
242 | + cfg := jsonconf.GetCatConfig(higestlv) | |
243 | + if cfg == nil { | |
244 | + logger.Error("CalcHigestCatName failed getcfg higestlv=%v", higestlv) | |
245 | + return "" | |
246 | + } | |
247 | + return cfg.Name | |
248 | + } | |
249 | + | |
250 | +} | ... | ... |
src/HttpServer/logic/httpserver.go
... | ... | @@ -14,7 +14,6 @@ import ( |
14 | 14 | "net/http" |
15 | 15 | ) |
16 | 16 | |
17 | - | |
18 | 17 | func StartHttpServe() { |
19 | 18 | startServerHttpServe() |
20 | 19 | } |
... | ... | @@ -169,17 +168,21 @@ func CheckErr(err error) { |
169 | 168 | } |
170 | 169 | |
171 | 170 | func startServerHttpServe() { |
172 | - http.HandleFunc("/api/account/login", UserLogin) //登录 | |
173 | - http.HandleFunc("/happycat/getuserdata", GetUserData) //拉取用户数据 | |
174 | - http.HandleFunc("/happycat/exchangetwoPos", ExchangePos) //交换位置 | |
175 | - http.HandleFunc("/happycat/clickcatbox", ClickCatBox) //请求点击猫箱子 | |
176 | - http.HandleFunc("/happycat/upgradecatbox", UpgradeCatBox) //请求点击猫箱子 | |
177 | - http.HandleFunc("/happycat/acclecte", AcclecteGold) //请求点击猫箱子 | |
178 | - http.HandleFunc("/happycat/automerge", AutoMerge) //请求自动合成 | |
179 | - http.HandleFunc("/happycat/generatebox", GenerateBox) //请求点击猫箱子 | |
180 | - http.HandleFunc("/happycat/clickrandgift", ClickRandGift) //请求点击猫箱子 | |
181 | - http.HandleFunc("/happycat/querybuycat", QueryBuyCat) //请求商店购买信息 | |
182 | - http.HandleFunc("/happycat/dobuycat", DoBuyCat) //购买猫 | |
171 | + http.HandleFunc("/api/account/login", UserLogin) //登录 | |
172 | + http.HandleFunc("/api/home/buy", DoBuyCat) //购买猫 | |
173 | + http.HandleFunc("/api/home/synResource", GetUserData) //同步资源 | |
174 | + http.HandleFunc("/api/home/recvOfflineReward", GetOfflineReward) //领取离线奖励 | |
175 | + http.HandleFunc("api/ranking/list", QueryPlayerRank) //排行榜 | |
176 | + | |
177 | + http.HandleFunc("/happycat/exchangetwoPos", ExchangePos) //交换位置 | |
178 | + http.HandleFunc("/happycat/clickcatbox", ClickCatBox) //请求点击猫箱子 | |
179 | + http.HandleFunc("/happycat/upgradecatbox", UpgradeCatBox) //请求点击猫箱子 | |
180 | + http.HandleFunc("/happycat/acclecte", AcclecteGold) //请求点击猫箱子 | |
181 | + http.HandleFunc("/happycat/automerge", AutoMerge) //请求自动合成 | |
182 | + http.HandleFunc("/happycat/generatebox", GenerateBox) //请求点击猫箱子 | |
183 | + http.HandleFunc("/happycat/clickrandgift", ClickRandGift) //请求点击猫箱子 | |
184 | + http.HandleFunc("/happycat/querybuycat", QueryBuyCat) //请求商店购买信息 | |
185 | + | |
183 | 186 | http.HandleFunc("/happycat/querwarehouse", QueryWareHouse) //请求仓库信息 |
184 | 187 | http.HandleFunc("/happycat/putcattowarehouse", PutCattoWareHouse) //将合成界面的猫放入仓库 |
185 | 188 | http.HandleFunc("/happycat/takecatoutfromwarehouse", TakeCatoutfromWareHouse) //将仓库的猫取出 |
... | ... | @@ -192,14 +195,14 @@ func startServerHttpServe() { |
192 | 195 | http.HandleFunc("/happycat/catshoplay ", CatShoPlay) //请求点击猫箱子 |
193 | 196 | http.HandleFunc("/happycat/getcatshopreward ", GetCatShopReward) //请求点击猫箱子 |
194 | 197 | http.HandleFunc("/happycat/acclectecatstory ", AcclecteCatStory) //请求点击猫箱子 |
195 | - http.HandleFunc("/chappycatat/updateuserinfo ", UpdateUserInfo) //请求点击猫箱子 | |
198 | + http.HandleFunc("/chappycatat/updateuserinfo ", UpdateUserInfo) //请求点击猫箱子 | |
196 | 199 | http.HandleFunc("/happycat/queryplayerrank ", QueryPlayerRank) //请求点击猫箱子 |
197 | 200 | http.HandleFunc("/happycat/querycompletetask ", QueryCompleteTask) //请求点击猫箱子 |
198 | 201 | http.HandleFunc("/happycat/querycompleteachievement ", QueryCompleteAchievement) //请求点击猫箱子 |
199 | 202 | http.HandleFunc("/happycat/gettaskreward ", GetTaskReward) //请求点击猫箱子 |
200 | 203 | http.HandleFunc("/happycat/getachievereward ", GetAchieveReward) //请求点击猫箱子 |
201 | 204 | http.HandleFunc("/happycat/startonlinetask ", StartOnlineTask) //请求开始在线时长任务 |
202 | - http.HandleFunc("/happycat/getofflinereward ", GetOfflineReward) //请求开始在线时长任务 | |
205 | + //http.HandleFunc("/happycat/getofflinereward ", GetOfflineReward) //请求开始在线时长任务 | |
203 | 206 | |
204 | 207 | err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) |
205 | 208 | CheckErr(err) |
... | ... | @@ -208,7 +211,7 @@ func startServerHttpServe() { |
208 | 211 | func GetOfflineReward(w http.ResponseWriter, r *http.Request) { |
209 | 212 | Uuid := 0 |
210 | 213 | if len(r.Header) > 0 { |
211 | - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) | |
214 | + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) | |
212 | 215 | } |
213 | 216 | if Uuid == 0 { |
214 | 217 | SetHeader(w) |
... | ... | @@ -220,7 +223,7 @@ func GetOfflineReward(w http.ResponseWriter, r *http.Request) { |
220 | 223 | s := string(result) |
221 | 224 | logger.Info("GetOfflineReward , body:%v,uuid=%v", s, Uuid) |
222 | 225 | |
223 | - //HandlerGetOfflineReward(w, s, Uuid) | |
226 | + HandlerGetOfflineReward(w, s, Uuid) | |
224 | 227 | } |
225 | 228 | |
226 | 229 | func StartOnlineTask(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -334,7 +337,7 @@ func UpdateUserInfo(w http.ResponseWriter, r *http.Request) { |
334 | 337 | func QueryPlayerRank(w http.ResponseWriter, r *http.Request) { |
335 | 338 | Uuid := 0 |
336 | 339 | if len(r.Header) > 0 { |
337 | - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) | |
340 | + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) | |
338 | 341 | } |
339 | 342 | if Uuid == 0 { |
340 | 343 | SetHeader(w) |
... | ... | @@ -346,7 +349,7 @@ func QueryPlayerRank(w http.ResponseWriter, r *http.Request) { |
346 | 349 | s := string(result) |
347 | 350 | logger.Info("QueryPlayerRank , body:%v,uuid=%v", s, Uuid) |
348 | 351 | |
349 | - //HandlerQueryPlayerRank(w, s, Uuid) | |
352 | + HandlerQueryPlayerRank(w, s, Uuid) | |
350 | 353 | } |
351 | 354 | |
352 | 355 | func AcclecteCatStory(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -550,7 +553,7 @@ func TakeCatoutfromWareHouse(w http.ResponseWriter, r *http.Request) { |
550 | 553 | func DoBuyCat(w http.ResponseWriter, r *http.Request) { |
551 | 554 | Uuid := 0 |
552 | 555 | if len(r.Header) > 0 { |
553 | - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) | |
556 | + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) | |
554 | 557 | } |
555 | 558 | if Uuid == 0 { |
556 | 559 | SetHeader(w) |
... | ... | @@ -562,7 +565,7 @@ func DoBuyCat(w http.ResponseWriter, r *http.Request) { |
562 | 565 | s := string(result) |
563 | 566 | logger.Info("DoBuyCat , body:%v,uuid=%v", s, Uuid) |
564 | 567 | |
565 | - //HandlerDoBuyCat(w, s, Uuid) | |
568 | + HandlerDoBuyCat(w, s, Uuid) | |
566 | 569 | } |
567 | 570 | |
568 | 571 | func QueryBuyCat(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -616,7 +619,7 @@ func AutoMerge(w http.ResponseWriter, r *http.Request) { |
616 | 619 | s := string(result) |
617 | 620 | logger.Info("AutoMerge , body:%v,uuid=%v", s, Uuid) |
618 | 621 | |
619 | -// HandlerAutoMerge(w, s, Uuid) | |
622 | + // HandlerAutoMerge(w, s, Uuid) | |
620 | 623 | } |
621 | 624 | |
622 | 625 | func ClickCatBox(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -652,7 +655,7 @@ func GenerateBox(w http.ResponseWriter, r *http.Request) { |
652 | 655 | s := string(result) |
653 | 656 | logger.Info("GenerateBox , body:%v,uuid=%v", s, Uuid) |
654 | 657 | |
655 | -// HandlerGenerateBox(w, s, Uuid) | |
658 | + // HandlerGenerateBox(w, s, Uuid) | |
656 | 659 | } |
657 | 660 | |
658 | 661 | func UpgradeCatBox(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -670,7 +673,7 @@ func UpgradeCatBox(w http.ResponseWriter, r *http.Request) { |
670 | 673 | s := string(result) |
671 | 674 | logger.Info("UpgradeCatBox , body:%v,uuid=%v", s, Uuid) |
672 | 675 | |
673 | -// HandlerUpgradeCatBox(w, s, Uuid) | |
676 | + // HandlerUpgradeCatBox(w, s, Uuid) | |
674 | 677 | } |
675 | 678 | |
676 | 679 | func AcclecteBoxRate(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -688,7 +691,7 @@ func AcclecteBoxRate(w http.ResponseWriter, r *http.Request) { |
688 | 691 | s := string(result) |
689 | 692 | logger.Info("AcclecteBoxRate , body:%v,uuid=%v", s, Uuid) |
690 | 693 | |
691 | -// HandlerAcclecteBoxRate(w, s, Uuid) | |
694 | + // HandlerAcclecteBoxRate(w, s, Uuid) | |
692 | 695 | } |
693 | 696 | |
694 | 697 | func AcclecteGold(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -706,7 +709,7 @@ func AcclecteGold(w http.ResponseWriter, r *http.Request) { |
706 | 709 | s := string(result) |
707 | 710 | logger.Info("AcclecteGold , body:%v,uuid=%v", s, Uuid) |
708 | 711 | |
709 | -// HandlerAcclecteGold(w, s, Uuid) | |
712 | + // HandlerAcclecteGold(w, s, Uuid) | |
710 | 713 | } |
711 | 714 | |
712 | 715 | func ExchangePos(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -730,7 +733,7 @@ func ExchangePos(w http.ResponseWriter, r *http.Request) { |
730 | 733 | func GetUserData(w http.ResponseWriter, r *http.Request) { |
731 | 734 | Uuid := 0 |
732 | 735 | if len(r.Header) > 0 { |
733 | - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) | |
736 | + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) | |
734 | 737 | } |
735 | 738 | if Uuid == 0 { |
736 | 739 | SetHeader(w) |
... | ... | @@ -742,7 +745,7 @@ func GetUserData(w http.ResponseWriter, r *http.Request) { |
742 | 745 | s := string(result) |
743 | 746 | logger.Info("GetUserData , body:%v,uuid=%v", s, Uuid) |
744 | 747 | |
745 | - //HandlerGetUserData(w, s, Uuid) | |
748 | + HandlerGetUserData(w, s, Uuid) | |
746 | 749 | } |
747 | 750 | |
748 | 751 | func UserLogin(w http.ResponseWriter, r *http.Request) { |
... | ... | @@ -762,22 +765,22 @@ func UserLogin(w http.ResponseWriter, r *http.Request) { |
762 | 765 | } |
763 | 766 | |
764 | 767 | /*if Uuid == 0 { |
765 | - SetHeader(w) | |
766 | - /*var resp UserLoginResp | |
767 | - resp.Code = -1 | |
768 | - resp.Message = "uuid is nil" | |
769 | - respstr, _ := json.Marshal(&resp) | |
770 | - fmt.Fprint(w, string(respstr)) | |
771 | - logger.Error("UserLogin uuid=0! failed!")*/ | |
772 | - //return | |
768 | + SetHeader(w) | |
769 | + /*var resp UserLoginResp | |
770 | + resp.Code = -1 | |
771 | + resp.Message = "uuid is nil" | |
772 | + respstr, _ := json.Marshal(&resp) | |
773 | + fmt.Fprint(w, string(respstr)) | |
774 | + logger.Error("UserLogin uuid=0! failed!")*/ | |
775 | + //return | |
773 | 776 | //} |
774 | 777 | result, _ := ioutil.ReadAll(r.Body) |
775 | 778 | r.Body.Close() |
776 | 779 | |
777 | 780 | s := string(result) |
778 | - logger.Info("UserLogin , body:%v,uuid=%v token=%v", s, Uuid,Token) | |
781 | + logger.Info("UserLogin , body:%v,uuid=%v token=%v", s, Uuid, Token) | |
779 | 782 | |
780 | - HandlerLogin(w, s, Uuid,Token) | |
783 | + HandlerLogin(w, s, Uuid, Token) | |
781 | 784 | } |
782 | 785 | |
783 | 786 | func ReviewAllianceHandler(w http.ResponseWriter, r *http.Request) { | ... | ... |
src/HttpServer/logic/logic.go
1 | 1 | package logic |
2 | 2 | |
3 | 3 | import ( |
4 | + "HttpServer/jsonconf" | |
4 | 5 | "HttpServer/redishandler" |
5 | 6 | "common/logger" |
6 | 7 | "common/redis" |
... | ... | @@ -9,11 +10,12 @@ import ( |
9 | 10 | "net/http" |
10 | 11 | "strconv" |
11 | 12 | "sync" |
13 | + "time" | |
12 | 14 | ) |
13 | 15 | |
14 | 16 | var ( |
15 | 17 | //m_userInfo *beegomap.BeeMap //make(map[int32]*UserData |
16 | - Maplock *sync.RWMutex | |
18 | + Maplock *sync.RWMutex | |
17 | 19 | ) |
18 | 20 | |
19 | 21 | func init() { |
... | ... | @@ -27,7 +29,7 @@ func SetHeader(w http.ResponseWriter) { |
27 | 29 | w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid") |
28 | 30 | } |
29 | 31 | |
30 | -func HandlerLogin(w http.ResponseWriter, data string, uuid int,token string) { | |
32 | +func HandlerLogin(w http.ResponseWriter, data string, uuid int, token string) { | |
31 | 33 | SetHeader(w) |
32 | 34 | var resp UserLoginResp |
33 | 35 | resp.Code = 0 |
... | ... | @@ -52,7 +54,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int,token string) { |
52 | 54 | InitUserInfo(&rdata, &resp, rdata.UserId) |
53 | 55 | |
54 | 56 | } else { |
55 | - uinfo,err := GetUserInfo(strconv.Itoa(rdata.UserId)) | |
57 | + uinfo, err := GetUserInfo(strconv.Itoa(rdata.UserId)) | |
56 | 58 | if err != nil { |
57 | 59 | logger.Info("GetUserInfo HandlerLogin data failed:%v,for:%v", err, data) |
58 | 60 | resp.Message = "GetUserInfo failed" |
... | ... | @@ -70,11 +72,267 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int,token string) { |
70 | 72 | |
71 | 73 | } |
72 | 74 | |
75 | + //回包 | |
76 | + respstr, _ := json.Marshal(&resp) | |
77 | + fmt.Fprint(w, string(respstr)) | |
78 | + | |
79 | + logger.Info("###HandlerLogin###rdata:%v", string(respstr)) | |
80 | +} | |
81 | + | |
82 | +func HandlerDoBuyCat(w http.ResponseWriter, data string, uuid int) { | |
83 | + SetHeader(w) | |
84 | + var resp DoBuyCatResp | |
85 | + resp.Code = 0 | |
86 | + resp.Message = "success" | |
87 | + var rdata DoBuyCatReq | |
88 | + err := json.Unmarshal([]byte(data), &rdata) | |
89 | + for { | |
90 | + if err != nil { | |
91 | + logger.Error("HandlerDoBuyCat json unmarshal failed=%v", err) | |
92 | + resp.Code = 1 | |
93 | + resp.Message = "json failed" | |
94 | + break | |
95 | + } | |
96 | + uinfo, err := GetUserInfo(strconv.Itoa(uuid)) | |
97 | + if err != nil || uinfo == nil { | |
98 | + logger.Error("HandlerDoBuyCat getuserinfo failed=%v", err) | |
99 | + resp.Code = 1 | |
100 | + resp.Message = "get userinfo failed" | |
101 | + break | |
102 | + } | |
103 | + | |
104 | + maxlv := uinfo.Highestlv - 5 | |
105 | + if maxlv < 1 { | |
106 | + //最小1j | |
107 | + maxlv = 1 | |
108 | + } | |
109 | + if rdata.CatId > maxlv || rdata.CatId < 1 || rdata.CatId > len(uinfo.BuyCatInfo) { | |
110 | + logger.Error("HandlerDoBuyCat buy lv failed=%v", err) | |
111 | + resp.Code = 1 | |
112 | + resp.Message = "buy lv invalid" | |
113 | + break | |
114 | + } | |
115 | + | |
116 | + curprice := uinfo.BuyCatInfo[rdata.CatId-1].CurPrice | |
117 | + if uinfo.Gold < curprice { | |
118 | + logger.Error("HandlerDoBuyCat gold not enough failed=%v", err) | |
119 | + resp.Code = 1 | |
120 | + resp.Message = "gold not enough" | |
121 | + break | |
122 | + } | |
123 | + | |
124 | + //获取配置 | |
125 | + catcfg := jsonconf.GetCatConfig(rdata.CatId) | |
126 | + if catcfg == nil { | |
127 | + logger.Error("HandlerDoBuyCat get cat cfg failed=%v,lv=%v", err, rdata.CatId) | |
128 | + resp.Code = 1 | |
129 | + resp.Message = "get cat cfg failed" | |
130 | + break | |
131 | + } | |
132 | + | |
133 | + //需要找到一个位置 | |
134 | + catpos := getCatPutPos(uinfo, rdata.CatId) | |
135 | + if catpos < 0 { | |
136 | + logger.Error("HandlerDoBuyCat not enough place failed=%v", err) | |
137 | + resp.Code = 1 | |
138 | + resp.Message = "not enough place" | |
139 | + break | |
140 | + } | |
141 | + //扣钱 | |
142 | + uinfo.Gold -= curprice | |
143 | + | |
144 | + //重新计算价格 | |
145 | + if uinfo.BuyCatInfo[rdata.CatId-1].IsMaxBuytime == 0 { | |
146 | + uinfo.BuyCatInfo[rdata.CatId-1].CurPrice = int64(float64(uinfo.BuyCatInfo[rdata.CatId-1].CurPrice) * float64(catcfg.Ratio)) | |
147 | + uinfo.BuyCatInfo[rdata.CatId-1].Buytime++ | |
148 | + if uinfo.BuyCatInfo[rdata.CatId-1].Buytime >= catcfg.Increse_limit { | |
149 | + uinfo.BuyCatInfo[rdata.CatId-1].IsMaxBuytime = 1 | |
150 | + } | |
151 | + } | |
152 | + | |
153 | + //需要重新计算速率 | |
154 | + uinfo.CalcGoldRate() | |
155 | + | |
156 | + resp.Data.Price = strconv.FormatInt(uinfo.BuyCatInfo[rdata.CatId-1].CurPrice, 10) | |
157 | + resp.Data.Position = catpos | |
158 | + resp.Data.Coin.UserId = uuid | |
159 | + resp.Data.Coin.Coin = strconv.FormatInt(uinfo.Gold, 10) | |
160 | + resp.Data.Coin.IcomeRate = strconv.FormatInt(uinfo.Goldrate, 10) | |
161 | + resp.Data.Coin.UpdateTime = int(time.Now().Unix()) | |
162 | + | |
163 | + resp.Code = 0 | |
73 | 164 | |
165 | + //保存 | |
166 | + SaveUserInfo(uinfo, strconv.Itoa(uuid)) | |
74 | 167 | |
168 | + break | |
169 | + | |
170 | + } | |
75 | 171 | //回包 |
76 | 172 | respstr, _ := json.Marshal(&resp) |
77 | 173 | fmt.Fprint(w, string(respstr)) |
174 | +} | |
78 | 175 | |
79 | - logger.Info("###HandlerLogin###rdata:%v", string(respstr)) | |
80 | -} | |
81 | 176 | \ No newline at end of file |
177 | +func HandlerGetUserData(w http.ResponseWriter, data string, uuid int) { | |
178 | + SetHeader(w) | |
179 | + var resp GetUserDataResp | |
180 | + resp.Code = 0 | |
181 | + resp.Message = "success" | |
182 | + | |
183 | + for { | |
184 | + v, err := redishandler.GetRedisClient().HGet(redis.USER_LAST_CALC_TIME, strconv.Itoa(uuid)) | |
185 | + if err != nil { | |
186 | + logger.Info("CalcOfflineData get USER_LAST_CALC_TIME failed=%v", err) | |
187 | + resp.Message = "redishandler failed" | |
188 | + resp.Code = 1 | |
189 | + break | |
190 | + } | |
191 | + | |
192 | + lasttime, _ := strconv.Atoi(v) | |
193 | + nowtime := time.Now().Unix() | |
194 | + if nowtime-int64(lasttime) < 0 { | |
195 | + logger.Error("HandlerGetUserData nowtime=%v lasttime=%v", nowtime, lasttime) | |
196 | + resp.Message = "request time small than zero" | |
197 | + resp.Code = 1 | |
198 | + break | |
199 | + } | |
200 | + uinfo, err := GetUserInfo(strconv.Itoa(uuid)) | |
201 | + if err != nil || uinfo == nil { | |
202 | + logger.Error("HandlerGetUserData getuserinfo failed=%v", err) | |
203 | + resp.Code = 1 | |
204 | + resp.Message = "get userinfo failed" | |
205 | + break | |
206 | + } | |
207 | + | |
208 | + if nowtime-int64(lasttime) > 5*60 { | |
209 | + //算离线收益 | |
210 | + resp.Data.TimingReward = false | |
211 | + offsec := nowtime - int64(lasttime) | |
212 | + if offsec > 2*3600 { | |
213 | + offsec = 2 * 3600 | |
214 | + } | |
215 | + | |
216 | + uinfo.OfflineGold = offsec * uinfo.Goldrate | |
217 | + resp.Data.Coin = strconv.FormatInt(uinfo.Gold, 10) | |
218 | + resp.Data.Now = int(time.Now().Unix()) | |
219 | + resp.Data.Output = "0" | |
220 | + resp.Data.OfflineReward.OfflineTime = int(offsec) | |
221 | + resp.Data.OfflineReward.Income = strconv.FormatInt(uinfo.OfflineGold, 10) | |
222 | + } else { | |
223 | + //按费离线收益计算 | |
224 | + offsec := nowtime - int64(lasttime) | |
225 | + resp.Data.TimingReward = true | |
226 | + resp.Data.Now = int(time.Now().Unix()) | |
227 | + addgold := uinfo.Goldrate * offsec | |
228 | + uinfo.Gold = addgold | |
229 | + uinfo.GoldSum += addgold | |
230 | + uinfo.AddToRank() | |
231 | + resp.Data.Output = strconv.FormatInt(addgold, 10) | |
232 | + resp.Data.Coin = strconv.FormatInt(uinfo.Gold, 10) | |
233 | + } | |
234 | + | |
235 | + //保存此次计算时间 | |
236 | + nowtimestr := strconv.FormatInt(nowtime, 10) | |
237 | + redishandler.GetRedisClient().HSet(redis.USER_LAST_CALC_TIME, strconv.Itoa(uuid), nowtimestr) | |
238 | + logger.Info("HandlerGetUserData save USER_LAST_CALC_TIME time=%v", nowtimestr) | |
239 | + | |
240 | + break | |
241 | + } | |
242 | + | |
243 | + //回包 | |
244 | + respstr, _ := json.Marshal(&resp) | |
245 | + fmt.Fprint(w, string(respstr)) | |
246 | +} | |
247 | + | |
248 | +func HandlerGetOfflineReward(w http.ResponseWriter, data string, uuid int) { | |
249 | + SetHeader(w) | |
250 | + var resp GetOfflineRewardResp | |
251 | + resp.Code = 0 | |
252 | + resp.Message = "success" | |
253 | + var rdata GetOfflineRewardReq | |
254 | + | |
255 | + err := json.Unmarshal([]byte(data), &rdata) | |
256 | + for { | |
257 | + if err != nil { | |
258 | + logger.Error("HandlerGetOfflineReward json unmarshal failed=%v", err) | |
259 | + resp.Code = 1 | |
260 | + resp.Message = "json failed" | |
261 | + break | |
262 | + } | |
263 | + | |
264 | + uinfo, err := GetUserInfo(strconv.Itoa(uuid)) | |
265 | + if err != nil || uinfo == nil { | |
266 | + logger.Error("HandlerGetUserData getuserinfo failed=%v", err) | |
267 | + resp.Code = 1 | |
268 | + resp.Message = "get userinfo failed" | |
269 | + break | |
270 | + } | |
271 | + | |
272 | + addgold := uinfo.OfflineGold | |
273 | + | |
274 | + if rdata.Optype == 2 { | |
275 | + addgold *= 2 | |
276 | + | |
277 | + } | |
278 | + uinfo.Gold += addgold | |
279 | + uinfo.GoldSum += addgold | |
280 | + uinfo.AddToRank() | |
281 | + | |
282 | + //此处todo 记录离线领取的次数 | |
283 | + | |
284 | + //领取过后将离线金币清零 | |
285 | + uinfo.OfflineGold = 0 | |
286 | + | |
287 | + //保存 | |
288 | + SaveUserInfo(uinfo, strconv.Itoa(uuid)) | |
289 | + | |
290 | + resp.Code = 0 | |
291 | + break | |
292 | + } | |
293 | + | |
294 | + //回包 | |
295 | + respstr, _ := json.Marshal(&resp) | |
296 | + fmt.Fprint(w, string(respstr)) | |
297 | +} | |
298 | + | |
299 | +func HandlerQueryPlayerRank(w http.ResponseWriter, data string, uuid int) { | |
300 | + SetHeader(w) | |
301 | + var resp QueryPlayerRankResp | |
302 | + resp.Code = 0 | |
303 | + resp.Message = "success" | |
304 | + for { | |
305 | + //取100名 | |
306 | + vv, err := redishandler.GetRedisClient().ZRevRangewithIndex(redis.USER_GOLD_RANK, 0, 99) | |
307 | + if err == nil { | |
308 | + rank := 0 | |
309 | + for _, v := range vv { | |
310 | + rank++ | |
311 | + //logger.Info("TestMyredis k=%v,v=%v", k, v) | |
312 | + rinfobyte, _ := v.(string) | |
313 | + ruid, _ := strconv.Atoi(rinfobyte) | |
314 | + rindo, err := GetUserInfo(rinfobyte) | |
315 | + if err == nil && rindo != nil { | |
316 | + var tmp RankInfoDesc | |
317 | + tmp.UserId = ruid | |
318 | + tmp.Income = rindo.GoldSum | |
319 | + tmp.Nickname = rindo.NickName | |
320 | + tmp.Headurl = rindo.Head | |
321 | + tmp.Rank = rank | |
322 | + tmp.CatName = rindo.CalcHigestCatName() | |
323 | + resp.Data = append(resp.Data, tmp) | |
324 | + } | |
325 | + } | |
326 | + } else { | |
327 | + logger.Error("HandlerUpdateUserInfo redisfailed ") | |
328 | + resp.Code = 1 | |
329 | + resp.Message = "redisfailed" | |
330 | + break | |
331 | + } | |
332 | + resp.Code = 0 | |
333 | + break | |
334 | + } | |
335 | + | |
336 | + //回包 | |
337 | + respstr, _ := json.Marshal(&resp) | |
338 | + fmt.Fprint(w, string(respstr)) | |
339 | +} | ... | ... |