Commit 0704cc8f2575bf81cac3727625f463b911c14c63

Authored by 陆恒
1 parent 98eecaf8
Exists in master

任务系统

src/HttpServer/jsonconf/DailyTaskConfig.json
@@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
27 "id": 9, 27 "id": 9,
28 "task": "在线%d%mins", 28 "task": "在线%d%mins",
29 "num": 60, 29 "num": 60,
30 - "reward": [3,1.2], 30 + "reward": [3,12000],
31 "RewardVideo": 1, 31 "RewardVideo": 1,
32 "type": 1 32 "type": 1
33 }, 33 },
src/HttpServer/logic/constdef.go
@@ -2,46 +2,45 @@ package logic @@ -2,46 +2,45 @@ package logic
2 2
3 //任务成就类型枚举 协议上报用 3 //任务成就类型枚举 协议上报用
4 const ( 4 const (
5 - TASKTYPE_PASSLEVEL = 1 //表示通过x关卡  
6 - TASKTYPE_GETREDBAG = 2 //3表示观看x次广告 5 + TASKTYPE_HEQIANG = 1 //表示合成枪
  6 + TASKTYPE_JIEQIANG = 2 //表示解锁x枪
7 TASKTYPE_WATCHADS = 3 //领取x次红包 7 TASKTYPE_WATCHADS = 3 //领取x次红包
8 - TASKTYPE_KILLSTAR = 4 //4表示消除x颗星星  
9 - TASKTYPE_USEITEM = 5 //5表示使用x次道具  
10 - TASKTYPE_PLAYSMALLGAMES = 6 //6表示试玩15s小游戏 8 + TASKTYPE_GETFLYBOX = 4 //表示领取x空投宝箱
  9 + TASKTYPE_TURNTABLE = 5 //5表示x次转盘
  10 + TASKTYPE_SHOPBUY = 6 //6表示商店购买x次
  11 + TASKTYPE_PASSLEVEL = 7 //表示闯关x次
  12 +
11 13
12 ) 14 )
13 15
14 //任务类型 对应表 16 //任务类型 对应表
15 const ( 17 const (
16 - DAILY_TASKTYPE_LOGIN = 1 //1 每日登录  
17 - DAILY_TASKTYPE_GETGUANCNT = 2 //提取存钱罐  
18 - DAILY_TASKTYPE_PASSLEVEL = 3 //近日通过关  
19 - DAILY_TASKTYPE_USEITEM = 4 //使用道具数  
20 - DAILY_TASKTYPE_GETREDBAG = 5 //领取红包次数  
21 - DAILY_TASKTYPE_GETGUANGOLD = 6 //收取金币x枚  
22 - DAILY_TASKTYPE_ONLINEMIN = 7 //在线分钟数  
23 - DAILY_TASKTYPE_PLAYSMALLGAME = 8 //试玩小游戏15s 18 + DAILY_TASKTYPE_ONLINE = 1 //在线x分钟
  19 + DAILY_TASKTYPE_HECHENG = 2 //合成x把枪
  20 + DAILY_TASKTYPE_JIEQIANG = 3 //解锁x把枪
  21 + DAILY_TASKTYPE_WATCHADS = 4 //g观看x广告
  22 + DAILY_TASKTYPE_GETFLYBOX = 5 //领取空投宝箱
  23 + DAILY_TASKTYPE_TURNTABLE = 6 //转盘
  24 + DAILY_TASKTYPE_SHOPBUY = 7 //商店购买枪
  25 + DAILY_TASKTYPE_PASSLEVEL = 8 //闯关
24 ) 26 )
25 27
26 //成就类型 对应表 28 //成就类型 对应表
27 const ( 29 const (
28 ACHIEVETYPE_WATCHADDS = 1 //累计观看广告次数 30 ACHIEVETYPE_WATCHADDS = 1 //累计观看广告次数
29 - ACHIEVETYPE_USERLV = 2 //人物等级  
30 - ACHIEVETYPE_KILLSTAR = 3 //累计消除星星数  
31 - ACHIEVETYPE_GETGUANCNT = 4 //累计存钱罐提取  
32 - ACHIEVETYPE_USEITEMCNT = 5 //累计道具使用 31 + ACHIEVETYPE_HEQIANG = 2 //合枪
  32 + ACHIEVETYPE_JIEQIANG = 3 //解枪
  33 + ACHIEVETYPE_PASSLEVEL = 4 //累计闯关
  34 +
33 35
34 ) 36 )
35 37
36 //奖励类型枚举 38 //奖励类型枚举
37 const ( 39 const (
38 - REWARDTYPE_STAR = 1 //星星  
39 - REWARDTYPE_CHUI = 2  
40 - REWARDTYPE_SHUA = 3  
41 - REWARDTYPE_SWAP = 4  
42 - REWARDTYPE_ELIMITE = 5  
43 - REWARDTYPE_EXP = 6  
44 - REWARDTYPE_GOLD = 7 40 + REWARDTYPE_GOLD = 1 //金币
  41 + REWARDTYPE_ZUAN = 2
  42 + REWARDTYPE_REDBAG = 3
  43 + REWARDTYPE_ROSE = 4
45 ) 44 )
46 45
47 const ( 46 const (
src/HttpServer/logic/datadef.go
1 package logic 1 package logic
2 2
  3 +type TaskListDesc struct {
  4 + Taskid int `json:"taskid"`
  5 + Taskstatus int `json:"taskstatus"`
  6 + Taskprogress int `json:"taskprogress"`
  7 +}
  8 +
  9 +type TaskList struct {
  10 + Taskdata []TaskListDesc `json:"taskdata"`
  11 +}
  12 +
  13 +type GettaskrewardReq struct {
  14 + Tasktype int `json:"tasktype"`
  15 + Taskid int `json:"taskid"`
  16 + Gameid string `json:"gameid"`
  17 + Channel string `json:"channel"`
  18 +}
3 19
  20 +type GettaskrewardData struct {
  21 + Rewardgold int `json:"rewardgold"`
  22 + Rewardzuan int `json:"rewardzuan"`
  23 + Rewardrose int `json:"rewardrose"`
  24 + Addredbag int `json:"addredbag"`
  25 + Sumredbag int `json:"sumredbag"`
  26 +}
  27 +type GettaskrewardResp struct {
  28 + Code int `json:"code"`
  29 + Message string `json:"message"`
  30 + Data GettaskrewardData `json:"data"`
  31 +}
  32 +
  33 +type QuerytaskinfoReq struct {
  34 + Tasktype int `json:"tasktype"`
  35 + Gameid string `json:"gameid"`
  36 + Channel string `json:"channel"`
  37 +}
  38 +
  39 +type QuerytaskinfoResp struct {
  40 + Code int `json:"code"`
  41 + Message string `json:"message"`
  42 + Data TaskList `json:"data"`
  43 +}
  44 +
  45 +type OnlinentfResp struct {
  46 + Code int `json:"code"`
  47 + Message string `json:"message"`
  48 +}
  49 +
  50 +type UpdatetaskReq struct {
  51 + Tasktype int `json:"tasktype"`
  52 + Value int `json:"value"`
  53 + Gameid string `json:"gameid"`
  54 + Channel string `json:"channel"`
  55 +}
  56 +
  57 +type UpdatetaskResp struct {
  58 + Code int `json:"code"`
  59 + Message string `json:"message"`
  60 +}
4 61
5 type UserLoginReq struct { 62 type UserLoginReq struct {
6 Gameid string `json:"gameid"` 63 Gameid string `json:"gameid"`
@@ -214,7 +271,7 @@ type UserData struct { @@ -214,7 +271,7 @@ type UserData struct {
214 SpecialLevel int //特殊关卡 271 SpecialLevel int //特殊关卡
215 GuanRedNum int //关卡红包领取进度 272 GuanRedNum int //关卡红包领取进度
216 JiRedNum int //等级红包领取进度 273 JiRedNum int //等级红包领取进度
217 - RandNum int //随机红包剩余次数 274 + RandNum int //时长红包
218 SpecialNum int //特殊关卡红包领取进度 275 SpecialNum int //特殊关卡红包领取进度
219 Tili int //体力 276 Tili int //体力
220 ReadNum int //玩家微转发阅读量 277 ReadNum int //玩家微转发阅读量
src/HttpServer/logic/function.go
@@ -12,6 +12,7 @@ import ( @@ -12,6 +12,7 @@ import (
12 "errors" 12 "errors"
13 "fmt" 13 "fmt"
14 "io/ioutil" 14 "io/ioutil"
  15 + "math/rand"
15 "net/http" 16 "net/http"
16 "sort" 17 "sort"
17 "strconv" 18 "strconv"
@@ -24,6 +25,21 @@ func SetHeader(w http.ResponseWriter) { @@ -24,6 +25,21 @@ func SetHeader(w http.ResponseWriter) {
24 w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid") 25 w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid")
25 } 26 }
26 27
  28 +func (v *TaskList) Len() int {
  29 + return len(v.Taskdata)
  30 +}
  31 +
  32 +func (v *TaskList) Swap(i, j int) {
  33 + v.Taskdata[i], v.Taskdata[j] = v.Taskdata[j], v.Taskdata[i]
  34 +}
  35 +
  36 +func (v *TaskList) Less(i, j int) bool {
  37 + if v.Taskdata[i].Taskstatus == v.Taskdata[j].Taskstatus {
  38 + return v.Taskdata[i].Taskid < v.Taskdata[j].Taskid
  39 + }
  40 + return v.Taskdata[i].Taskstatus > v.Taskdata[j].Taskstatus
  41 +}
  42 +
27 func SaveUserInfo(data *UserData, uniqueid string) error { 43 func SaveUserInfo(data *UserData, uniqueid string) error {
28 44
29 savestr, err := json.Marshal(data) 45 savestr, err := json.Marshal(data)
@@ -71,6 +87,24 @@ func (uinfo *UserData) GetWithDrawData(money float32) (int, *WithDrawDesc) { @@ -71,6 +87,24 @@ func (uinfo *UserData) GetWithDrawData(money float32) (int, *WithDrawDesc) {
71 return -1, nil 87 return -1, nil
72 } 88 }
73 89
  90 +func GetTaskConfig(taskid int) *jsonconf.AchieveDesc {
  91 + for _, val := range jsonconf.GetJsonConf().TaskConfig {
  92 + if val.Id == taskid {
  93 + return &val
  94 + }
  95 + }
  96 + return nil
  97 +}
  98 +
  99 +func GetAchieveConfig(taskid int) *jsonconf.AchieveDesc {
  100 + for _, val := range jsonconf.GetJsonConf().AchieventConfig {
  101 + if val.Id == taskid {
  102 + return &val
  103 + }
  104 + }
  105 + return nil
  106 +}
  107 +
74 func HandlerSyncuserdata(w http.ResponseWriter, data string, uuid int) { 108 func HandlerSyncuserdata(w http.ResponseWriter, data string, uuid int) {
75 SetHeader(w) 109 SetHeader(w)
76 var resp GetuserdataResp 110 var resp GetuserdataResp
@@ -124,6 +158,359 @@ func HandlerSyncuserdata(w http.ResponseWriter, data string, uuid int) { @@ -124,6 +158,359 @@ func HandlerSyncuserdata(w http.ResponseWriter, data string, uuid int) {
124 logger.Info("###HandlerSyncuserdata###rdata:%v", string(respstr)) 158 logger.Info("###HandlerSyncuserdata###rdata:%v", string(respstr))
125 } 159 }
126 160
  161 +func (uinfo *UserData) RewardUser(rtype int, num int, gameid string, channel string, uuid int) (int, int, int, int, int) {
  162 + logger.Info("RewardUser rtype=%v,num=%v", rtype, num)
  163 + switch rtype {
  164 + case REWARDTYPE_GOLD:
  165 + return num,0, 0, 0, 0
  166 + case REWARDTYPE_ZUAN:
  167 + return 0, num, 0, 0, 0
  168 + case REWARDTYPE_REDBAG:
  169 + readaddnum := rand.Intn(num/2) + num
  170 + goldnum, err := AddCoinToSdk(uuid, readaddnum, gameid, channel, 102)
  171 + if err != nil {
  172 + logger.Error("RewardUser failer err=%v", err)
  173 + }
  174 + uinfo.RealGold = goldnum
  175 + //uinfo.RealGold += num
  176 + return 0, 0, 0, readaddnum,uinfo.RealGold
  177 + case REWARDTYPE_ROSE:
  178 + return 0, 0, num, 0, 0
  179 +
  180 + default:
  181 + return 0, 0, 0, 0, 0
  182 + }
  183 + return 0, 0, 0, 0, 0
  184 +}
  185 +
  186 +//处理领取任务
  187 +func (uinfo *UserData) HandleGetTaskReward(req *GettaskrewardReq, resp *GettaskrewardResp, list *TaskList, uuid int) error {
  188 + isfind := false
  189 + index := -1
  190 + var taskdesc *TaskListDesc
  191 + for k, val := range list.Taskdata {
  192 + if val.Taskid == req.Taskid {
  193 + isfind = true
  194 + index = k
  195 + taskdesc = &val
  196 + break
  197 + }
  198 + }
  199 +
  200 + logger.Info("HandleGetTaskReward index=%v taskdesc=%v", index, taskdesc)
  201 + if isfind && taskdesc != nil && index != -1 {
  202 + if taskdesc.Taskstatus != 2 {
  203 + //状态不是可领取状态 无法领取
  204 + resp.Code = 1
  205 + resp.Message = "当前任务未完成或已领取"
  206 + return errors.New("当前任务未完成或已领取")
  207 + }
  208 +
  209 + //状态对的情况 如果是成就下需要判断前置条件是否被
  210 + if req.Tasktype == 2 && index > 0 {
  211 + precfg := GetAchieveConfig(list.Taskdata[index-1].Taskid)
  212 + nowcfg := GetAchieveConfig(taskdesc.Taskid)
  213 + logger.Info("HandleGetTaskRewardGetTaskConfig precfg=%v nowcfg=%v", precfg, nowcfg)
  214 + if precfg != nil && nowcfg != nil && precfg.TaskType == nowcfg.TaskType && list.Taskdata[index-1].Taskstatus != 0 {
  215 + resp.Code = 2
  216 + resp.Message = "前置任务还未领取"
  217 + return errors.New("当前任务未完成或已领取")
  218 + }
  219 + }
  220 + } else {
  221 + resp.Code = 1
  222 + resp.Message = "当前任务id错误"
  223 + return errors.New("当前任务id错误")
  224 + }
  225 +
  226 + var cfg *jsonconf.AchieveDesc
  227 + if req.Tasktype == 2 {
  228 + cfg = GetAchieveConfig(taskdesc.Taskid)
  229 + if cfg == nil || len(cfg.Reward) < 2 {
  230 + resp.Code = 1
  231 + resp.Message = "网络错误"
  232 + return errors.New("获取配置失败")
  233 + }
  234 + } else {
  235 + cfg = GetTaskConfig(taskdesc.Taskid)
  236 + if cfg == nil || len(cfg.Reward) < 2 {
  237 + resp.Code = 1
  238 + resp.Message = "网络错误"
  239 + return errors.New("获取配置失败")
  240 + }
  241 + }
  242 +
  243 + //将状态置位已领取
  244 + list.Taskdata[index].Taskstatus = 0
  245 + //发奖励
  246 + sub := len(cfg.Reward) / 2
  247 + for i := 0; i < sub; i++ {
  248 + rtype := cfg.Reward[i*2]
  249 + rnum := cfg.Reward[i*2+1]
  250 + r1, r2, r3, r4, r5 := uinfo.RewardUser(rtype, rnum, req.Gameid, req.Channel, uuid)
  251 +
  252 + resp.Data.Rewardgold += r1
  253 + resp.Data.Rewardzuan += r2
  254 + resp.Data.Rewardrose += r3
  255 + resp.Data.Addredbag += r4
  256 + resp.Data.Sumredbag = r5
  257 + }
  258 +
  259 + return nil
  260 +}
  261 +
  262 +func (uinfo *UserData) CalcAchieveProgress(ttype int) int {
  263 + rtval := 0
  264 + switch ttype {
  265 + case ACHIEVETYPE_WATCHADDS:
  266 + rtval = uinfo.Achieve.WatchAdsCnt
  267 + case ACHIEVETYPE_HEQIANG:
  268 + rtval = uinfo.Achieve.HeQiang
  269 + case ACHIEVETYPE_JIEQIANG:
  270 + rtval = uinfo.Achieve.JieQiang
  271 + case ACHIEVETYPE_PASSLEVEL:
  272 + rtval = uinfo.Achieve.PassLevel
  273 + default:
  274 + logger.Error("CalcAchieveProgress type wrong ttype=%v", ttype)
  275 + }
  276 +
  277 + return rtval
  278 +}
  279 +
  280 +//成就返回的是新的成就列表
  281 +func (uinfo *UserData) CalcAchieveList(list *TaskList, uniqueid string) *TaskList {
  282 + newlist := new(TaskList)
  283 + curtype := -1 //记录当前类型
  284 +
  285 + //第一遍首先将所有进度设置为正确的状态
  286 + for k, val := range list.Taskdata {
  287 + cfg := GetAchieveConfig(val.Taskid)
  288 + if cfg == nil {
  289 + logger.Error("CalcAchieveList GetTaskConfig failed id=%v", val.Taskid)
  290 + continue
  291 + }
  292 +
  293 + list.Taskdata[k].Taskprogress = uinfo.CalcAchieveProgress(cfg.TaskType)
  294 + if val.Taskstatus == 1 {
  295 + switch cfg.TaskType {
  296 + case ACHIEVETYPE_WATCHADDS:
  297 + if uinfo.Achieve.WatchAdsCnt >= cfg.Num {
  298 + list.Taskdata[k].Taskstatus = 2
  299 + }
  300 + case ACHIEVETYPE_HEQIANG:
  301 + if uinfo.Achieve.HeQiang >= cfg.Num {
  302 + list.Taskdata[k].Taskstatus = 2
  303 + }
  304 + case ACHIEVETYPE_JIEQIANG:
  305 + if uinfo.Achieve.JieQiang >= cfg.Num {
  306 + list.Taskdata[k].Taskstatus = 2
  307 + }
  308 + case ACHIEVETYPE_PASSLEVEL:
  309 + if uinfo.Achieve.PassLevel >= cfg.Num {
  310 + list.Taskdata[k].Taskstatus = 2
  311 + }
  312 + default:
  313 + logger.Error("CalcAchieveProgress type wrong ttype=%v", cfg.TaskType)
  314 + }
  315 + }
  316 +
  317 + }
  318 +
  319 + for k, val := range list.Taskdata {
  320 +
  321 + cfg := GetAchieveConfig(val.Taskid)
  322 + if cfg == nil {
  323 + logger.Error("CalcAchieveList GetTaskConfig failed id=%v", val.Taskid)
  324 + continue
  325 + }
  326 +
  327 + if val.Taskstatus == 2 {
  328 + //未领取已完成 只需要加入第一种
  329 + if curtype == cfg.TaskType {
  330 + continue
  331 + } else {
  332 + //将类型加入返回列表
  333 + newlist.Taskdata = append(newlist.Taskdata, list.Taskdata[k])
  334 + curtype = cfg.TaskType
  335 + }
  336 + } else if val.Taskstatus == 1 {
  337 + //未完成 返回第一个
  338 + if curtype == cfg.TaskType {
  339 + continue
  340 + } else {
  341 + //将类型加入返回列表
  342 + newlist.Taskdata = append(newlist.Taskdata, list.Taskdata[k])
  343 + curtype = cfg.TaskType
  344 + }
  345 + } else {
  346 + //已领取 需要判断一下是否最后一项
  347 + if k != len(list.Taskdata)-1 {
  348 + //不是最后一条数据
  349 + nextcfg := GetAchieveConfig(list.Taskdata[k+1].Taskid)
  350 + if nextcfg != nil {
  351 + if nextcfg.TaskType != cfg.TaskType {
  352 + //下面一天不是此类型了
  353 + newlist.Taskdata = append(newlist.Taskdata, list.Taskdata[k])
  354 + curtype = cfg.TaskType
  355 + }
  356 + }
  357 +
  358 + } else {
  359 + //最后一条 加入
  360 + newlist.Taskdata = append(newlist.Taskdata, list.Taskdata[k])
  361 + curtype = cfg.TaskType
  362 + }
  363 + }
  364 +
  365 + }
  366 +
  367 + //保存旧的列表
  368 + SaveTaskInfo(uniqueid, 2, list)
  369 +
  370 + //将返回的列表排序
  371 + sort.Sort(newlist)
  372 + logger.Info("CalcAchieveList alist=%v", newlist)
  373 + return newlist
  374 +}
  375 +
  376 +func (uinfo *UserData) CalcTaskList(list *TaskList, uniqueid string) {
  377 +
  378 + for k, val := range list.Taskdata {
  379 + //未完成的才做判断
  380 + if val.Taskstatus != 1 {
  381 + continue
  382 + }
  383 + cfg := GetTaskConfig(val.Taskid)
  384 + if cfg == nil {
  385 + logger.Error("CalcTaskList GetTaskConfig failed id=%v", val.Taskid)
  386 + continue
  387 + }
  388 +
  389 + if cfg.TaskType == DAILY_TASKTYPE_ONLINE {
  390 + list.Taskdata[k].Taskprogress = uinfo.Task.OnlineMin
  391 + if uinfo.Task.OnlineMin >= cfg.Num {
  392 + list.Taskdata[k].Taskstatus = 2
  393 + }
  394 + } else if cfg.TaskType == DAILY_TASKTYPE_HECHENG {
  395 + list.Taskdata[k].Taskprogress = uinfo.Task.HeQiang
  396 + if uinfo.Task.HeQiang >= cfg.Num {
  397 + list.Taskdata[k].Taskstatus = 2
  398 + }
  399 + } else if cfg.TaskType == DAILY_TASKTYPE_JIEQIANG {
  400 + list.Taskdata[k].Taskprogress = uinfo.Task.JieQiang
  401 + if uinfo.Task.JieQiang >= cfg.Num {
  402 + list.Taskdata[k].Taskstatus = 2
  403 + }
  404 + } else if cfg.TaskType == DAILY_TASKTYPE_WATCHADS {
  405 + list.Taskdata[k].Taskprogress = uinfo.Task.WatchAdCnt
  406 + if uinfo.Task.WatchAdCnt >= cfg.Num {
  407 + list.Taskdata[k].Taskstatus = 2
  408 + }
  409 + } else if cfg.TaskType == DAILY_TASKTYPE_GETFLYBOX {
  410 + list.Taskdata[k].Taskprogress = uinfo.Task.GetKongBox
  411 + if uinfo.Task.GetKongBox >= cfg.Num {
  412 + list.Taskdata[k].Taskstatus = 2
  413 + }
  414 + } else if cfg.TaskType == DAILY_TASKTYPE_TURNTABLE {
  415 + list.Taskdata[k].Taskprogress = uinfo.Task.TurnTableCnt
  416 + if uinfo.Task.TurnTableCnt >= cfg.Num {
  417 + list.Taskdata[k].Taskstatus = 2
  418 + }
  419 + } else if cfg.TaskType == DAILY_TASKTYPE_SHOPBUY {
  420 + list.Taskdata[k].Taskprogress = uinfo.Task.ShopBuyGun
  421 + if uinfo.Task.ShopBuyGun >= cfg.Num {
  422 + list.Taskdata[k].Taskstatus = 2
  423 + }
  424 + } else if cfg.TaskType == DAILY_TASKTYPE_PASSLEVEL {
  425 + list.Taskdata[k].Taskprogress = uinfo.Task.PassLevel
  426 + if uinfo.Task.PassLevel > 0 {
  427 + list.Taskdata[k].Taskstatus = 2
  428 + }
  429 + }
  430 +
  431 + }
  432 +
  433 + //需要保存到redis
  434 + SaveTaskInfo(uniqueid, 1, list)
  435 +
  436 + //排序
  437 + sort.Sort(list)
  438 +}
  439 +
  440 +func SaveTaskInfo(uuid string, tasktype int, list *TaskList) error {
  441 + savestr, err := json.Marshal(list)
  442 + if err != nil {
  443 + logger.Error("SaveTaskInfo err =%v", err)
  444 + return err
  445 + }
  446 +
  447 + if tasktype == 1 {
  448 + err = redishandler.GetRedisClient().HSet(redis.USER_TASKINFO_LIST, uuid, string(savestr))
  449 + } else if tasktype == 2 {
  450 + err = redishandler.GetRedisClient().HSet(redis.USER_ACHIEVEMENTINFO_LIST, uuid, string(savestr))
  451 + //logger.Info("SaveTaskInfo savestr=%v", string(savestr))
  452 + } else {
  453 + logger.Error("tasktype invalid")
  454 + return errors.New("tasktype invalid")
  455 + }
  456 + return err
  457 +}
  458 +
  459 +func GetTaskInfo(uuid int, tasktype int, channel string) (*TaskList, error) {
  460 + unqiueuuid := strconv.Itoa(uuid) + channel
  461 + var data string
  462 + var err error
  463 + if tasktype == 1 {
  464 + data, err = redishandler.GetRedisClient().HGet(redis.USER_TASKINFO_LIST, unqiueuuid)
  465 + //兼容老的数据
  466 + if err != nil {
  467 + //没有读到数据 则去读取一下老的id
  468 + olddata, err := redishandler.GetRedisClient().HGet(redis.USER_TASKINFO_LIST, strconv.Itoa(uuid))
  469 + if err == nil {
  470 + err = redishandler.GetRedisClient().HSet(redis.USER_TASKINFO_LIST, unqiueuuid, olddata)
  471 + if err == nil {
  472 + //删除老的数据 以后都走新的数据
  473 + err = redishandler.GetRedisClient().HDel(redis.USER_TASKINFO_LIST, strconv.Itoa(uuid))
  474 + logger.Info("GetTaskInfo uuid=%v do set", uuid)
  475 + }
  476 + data, err = redishandler.GetRedisClient().HGet(redis.USER_TASKINFO_LIST, unqiueuuid)
  477 + }
  478 + }
  479 + } else if tasktype == 2 {
  480 + data, err = redishandler.GetRedisClient().HGet(redis.USER_ACHIEVEMENTINFO_LIST, unqiueuuid)
  481 + //成就这一块需要兼容老的数据
  482 + if err != nil {
  483 + //没有读到数据 则去读取一下老的id
  484 + olddata, err := redishandler.GetRedisClient().HGet(redis.USER_ACHIEVEMENTINFO_LIST, strconv.Itoa(uuid))
  485 + if err == nil {
  486 + err = redishandler.GetRedisClient().HSet(redis.USER_ACHIEVEMENTINFO_LIST, unqiueuuid, olddata)
  487 + if err == nil {
  488 + //删除老的数据 以后都走新的数据
  489 + err = redishandler.GetRedisClient().HDel(redis.USER_ACHIEVEMENTINFO_LIST, strconv.Itoa(uuid))
  490 + logger.Info("GetTaskInfo uuid=%v do set", uuid)
  491 + }
  492 + data, err = redishandler.GetRedisClient().HGet(redis.USER_ACHIEVEMENTINFO_LIST, unqiueuuid)
  493 + }
  494 + }
  495 + } else {
  496 + logger.Error("tasktype invalid")
  497 + return nil, errors.New("tasktype invalid")
  498 + }
  499 +
  500 + if err != nil {
  501 + return nil, err
  502 + }
  503 +
  504 + var list TaskList
  505 + err = json.Unmarshal([]byte(data), &list)
  506 + if err != nil {
  507 + logger.Error("GetTaskInfo err=%v", err)
  508 + return nil, err
  509 + }
  510 +
  511 + return &list, err
  512 +}
  513 +
127 514
128 515
129 516
src/HttpServer/logic/httpserver.go
@@ -48,6 +48,11 @@ func startServerHttpServe() { @@ -48,6 +48,11 @@ func startServerHttpServe() {
48 http.HandleFunc("/gunshot/uploadtili", Uploadtili) //上报当前体力 48 http.HandleFunc("/gunshot/uploadtili", Uploadtili) //上报当前体力
49 http.HandleFunc("/gunshot/fetchredbag", Fetchredbag) //领取红包 49 http.HandleFunc("/gunshot/fetchredbag", Fetchredbag) //领取红包
50 50
  51 + http.HandleFunc("/gunshot/onlinentf", Onlinentf) //在线通知
  52 + http.HandleFunc("/gunshot/updatetaskandachieve", Updatetaskandachieve) //上报任务事件进度
  53 + http.HandleFunc("/gunshot/querytaskinfo", Querytaskinfo) //拉取任务或者成就列表
  54 + http.HandleFunc("/gunshot/gettaskreward", Gettaskreward) //领取任务或者成就奖励
  55 +
51 56
52 57
53 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) 58 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil)
@@ -118,6 +123,90 @@ func Uploadtili(w http.ResponseWriter, r *http.Request) { @@ -118,6 +123,90 @@ func Uploadtili(w http.ResponseWriter, r *http.Request) {
118 HandlerUploadtili(w, s, Uuid) 123 HandlerUploadtili(w, s, Uuid)
119 } 124 }
120 125
  126 +func Onlinentf(w http.ResponseWriter, r *http.Request) {
  127 +
  128 + Uuid := 0
  129 + if len(r.Header) > 0 {
  130 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  131 + }
  132 +
  133 + if Uuid == 0 {
  134 + SetHeader(w)
  135 + //logger.Error("Uuid is nil!")
  136 + return
  137 + }
  138 + result, _ := ioutil.ReadAll(r.Body)
  139 + r.Body.Close()
  140 +
  141 + s := string(result)
  142 + logger.Info("Onlinentf , body:%v,uuid=%v", s, Uuid)
  143 +
  144 + HandlerOnlinentf(w, s, Uuid)
  145 +}
  146 +
  147 +func Updatetaskandachieve(w http.ResponseWriter, r *http.Request) {
  148 +
  149 + Uuid := 0
  150 + if len(r.Header) > 0 {
  151 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  152 + }
  153 +
  154 + if Uuid == 0 {
  155 + SetHeader(w)
  156 + //logger.Error("Uuid is nil!")
  157 + return
  158 + }
  159 + result, _ := ioutil.ReadAll(r.Body)
  160 + r.Body.Close()
  161 +
  162 + s := string(result)
  163 + logger.Info("Fetchredbag , body:%v,uuid=%v", s, Uuid)
  164 +
  165 + HandlerUpdatetaskandachieve(w, s, Uuid)
  166 +}
  167 +
  168 +func Querytaskinfo(w http.ResponseWriter, r *http.Request) {
  169 +
  170 + Uuid := 0
  171 + if len(r.Header) > 0 {
  172 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  173 + }
  174 +
  175 + if Uuid == 0 {
  176 + SetHeader(w)
  177 + //logger.Error("Uuid is nil!")
  178 + return
  179 + }
  180 + result, _ := ioutil.ReadAll(r.Body)
  181 + r.Body.Close()
  182 +
  183 + s := string(result)
  184 + logger.Info("Querytaskinfo , body:%v,uuid=%v", s, Uuid)
  185 +
  186 + HandlerQuerytaskinfo(w, s, Uuid)
  187 +}
  188 +
  189 +func Gettaskreward(w http.ResponseWriter, r *http.Request) {
  190 +
  191 + Uuid := 0
  192 + if len(r.Header) > 0 {
  193 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  194 + }
  195 +
  196 + if Uuid == 0 {
  197 + SetHeader(w)
  198 + //logger.Error("Uuid is nil!")
  199 + return
  200 + }
  201 + result, _ := ioutil.ReadAll(r.Body)
  202 + r.Body.Close()
  203 +
  204 + s := string(result)
  205 + logger.Info("Gettaskreward , body:%v,uuid=%v", s, Uuid)
  206 +
  207 + HandlerGettaskreward(w, s, Uuid)
  208 +}
  209 +
121 func Fetchredbag(w http.ResponseWriter, r *http.Request) { 210 func Fetchredbag(w http.ResponseWriter, r *http.Request) {
122 211
123 Uuid := 0 212 Uuid := 0
src/HttpServer/logic/logic.go
@@ -208,6 +208,227 @@ func HandlerGetcash(w http.ResponseWriter, data string, uuid int) { @@ -208,6 +208,227 @@ func HandlerGetcash(w http.ResponseWriter, data string, uuid int) {
208 208
209 } 209 }
210 210
  211 +func HandlerOnlinentf(w http.ResponseWriter, data string, uuid int) {
  212 + SetHeader(w)
  213 + var resp OnlinentfResp
  214 + resp.Code = 0
  215 + var rdata CommReq
  216 + err := json.Unmarshal([]byte(data), &rdata)
  217 + for {
  218 +
  219 + if err != nil {
  220 + logger.Info("json decode HandlerOnlinentf data failed:%v,for:%v", err, data)
  221 + resp.Message = "json解析错误"
  222 + resp.Code = 1
  223 + break
  224 + }
  225 + //需要加上渠道才是唯一的玩家id,不同渠道视为不同数据
  226 + uniqueuuid := strconv.Itoa(uuid) + rdata.Channel
  227 + uinfo, err := GetUserInfo(uniqueuuid)
  228 + if err != nil || uinfo == nil {
  229 + logger.Error("redis failed err=%v", err)
  230 + resp.Message = "服务器错误"
  231 + resp.Code = 1
  232 + break
  233 + }
  234 +
  235 + uinfo.Task.OnlineMin++
  236 +
  237 + SaveUserInfo(uinfo, uniqueuuid)
  238 +
  239 + resp.Code = 0
  240 + break
  241 + }
  242 +
  243 + //回包
  244 + respstr, _ := json.Marshal(&resp)
  245 + fmt.Fprint(w, string(respstr))
  246 +
  247 +}
  248 +
  249 +func HandlerQuerytaskinfo(w http.ResponseWriter, data string, uuid int) {
  250 + SetHeader(w)
  251 + var resp QuerytaskinfoResp
  252 + resp.Code = 0
  253 + var rdata QuerytaskinfoReq
  254 + err := json.Unmarshal([]byte(data), &rdata)
  255 + for {
  256 + if err != nil {
  257 + logger.Info("json decode HandlerQuerytaskinfo data failed:%v,for:%v", err, data)
  258 + resp.Message = "网络错误"
  259 + resp.Code = 1
  260 + break
  261 + }
  262 + //需要加上渠道才是唯一的玩家id,不同渠道视为不同数据
  263 + uniqueuuid := strconv.Itoa(uuid) + rdata.Channel
  264 + uinfo, err := GetUserInfo(uniqueuuid)
  265 + if err != nil || uinfo == nil {
  266 + logger.Error("redis failed err=%v", err)
  267 + resp.Message = "服务器错误"
  268 + resp.Code = 1
  269 + break
  270 + }
  271 +
  272 + uinfo.HandlePassDay(uuid, rdata.Channel)
  273 +
  274 + //需要先计算出任务或者成就
  275 + var tasklist *TaskList
  276 + //先查询一下当前的任务数据
  277 + tasklist, err = GetTaskInfo(uuid, rdata.Tasktype, rdata.Channel)
  278 + if err != nil || tasklist == nil {
  279 + logger.Error("redis failed err=%v", err)
  280 + resp.Message = "服务器错误"
  281 + resp.Code = 1
  282 + break
  283 + }
  284 +
  285 + if rdata.Tasktype == 1 {
  286 + uinfo.CalcTaskList(tasklist, uniqueuuid)
  287 + } else if rdata.Tasktype == 2 {
  288 + tasklist = uinfo.CalcAchieveList(tasklist, uniqueuuid)
  289 + } else {
  290 + uinfo.CalcTaskList(tasklist, uniqueuuid)
  291 + }
  292 +
  293 + if tasklist == nil {
  294 + logger.Error("redis failed err=%v", err)
  295 + resp.Message = "服务器错误"
  296 + resp.Code = 1
  297 + break
  298 + }
  299 +
  300 + resp.Data.Taskdata = append(resp.Data.Taskdata, tasklist.Taskdata...)
  301 +
  302 + resp.Code = 0
  303 + break
  304 + }
  305 +
  306 + //回包
  307 + logger.Info("HandlerQuerytaskinfo,uuid=%v resp=%v", uuid, resp)
  308 + respstr, _ := json.Marshal(&resp)
  309 + fmt.Fprint(w, string(respstr))
  310 +
  311 +}
  312 +
  313 +func HandlerGettaskreward(w http.ResponseWriter, data string, uuid int) {
  314 + SetHeader(w)
  315 + var resp GettaskrewardResp
  316 + resp.Code = 0
  317 + var rdata GettaskrewardReq
  318 + err := json.Unmarshal([]byte(data), &rdata)
  319 + for {
  320 + if err != nil {
  321 + logger.Info("json decode HandlerGettaskreward data failed:%v,for:%v", err, data)
  322 + resp.Message = "网络错误"
  323 + resp.Code = 1
  324 + break
  325 + }
  326 +
  327 + //需要加上渠道才是唯一的玩家id,不同渠道视为不同数据
  328 + uniqueuuid := strconv.Itoa(uuid) + rdata.Channel
  329 + uinfo, err := GetUserInfo(uniqueuuid)
  330 + if err != nil || uinfo == nil {
  331 + logger.Error("redis failed err=%v", err)
  332 + resp.Message = "服务器错误"
  333 + resp.Code = 1
  334 + break
  335 + }
  336 +
  337 + //需要先计算出任务或者成就
  338 + var tasklist *TaskList
  339 + //先查询一下当前的任务数据
  340 + tasklist, err = GetTaskInfo(uuid, rdata.Tasktype, rdata.Channel)
  341 + if err != nil || tasklist == nil {
  342 + logger.Error("GetTaskInfo failed err=%v", err)
  343 + resp.Message = "服务器错误"
  344 + resp.Code = 1
  345 + break
  346 + }
  347 +
  348 + err = uinfo.HandleGetTaskReward(&rdata, &resp, tasklist, uuid)
  349 + logger.Info("HandlerGettaskreward tasklist=%v", tasklist)
  350 + logger.Info("HandlerGettaskreward uinfo=%v", uinfo)
  351 + if err != nil {
  352 + logger.Error("HandleGetTaskReward failed err=%v", err)
  353 + resp.Message = "奖励已经领取"
  354 + resp.Code = 1
  355 + break
  356 + }
  357 +
  358 + //保存数据
  359 + err = SaveUserInfo(uinfo, uniqueuuid)
  360 + err = SaveTaskInfo(uniqueuuid, rdata.Tasktype, tasklist)
  361 +
  362 + resp.Code = 0
  363 + break
  364 + }
  365 +
  366 + //回包
  367 + respstr, _ := json.Marshal(&resp)
  368 + fmt.Fprint(w, string(respstr))
  369 +
  370 +}
  371 +
  372 +func HandlerUpdatetaskandachieve(w http.ResponseWriter, data string, uuid int) {
  373 + SetHeader(w)
  374 + llock.Lock()
  375 + defer llock.Unlock()
  376 + var resp UpdatetaskResp
  377 + resp.Code = 0
  378 + var rdata UpdatetaskReq
  379 + err := json.Unmarshal([]byte(data), &rdata)
  380 + for {
  381 +
  382 + if err != nil {
  383 + logger.Info("json decode HandlerUpdatetaskandachieve data failed:%v,for:%v", err, data)
  384 + resp.Message = "json解析错误"
  385 + resp.Code = 1
  386 + break
  387 + }
  388 + //需要加上渠道才是唯一的玩家id,不同渠道视为不同数据
  389 + uniqueuuid := strconv.Itoa(uuid) + rdata.Channel
  390 + uinfo, err := GetUserInfo(uniqueuuid)
  391 + if err != nil || uinfo == nil {
  392 + logger.Error("redis failed err=%v", err)
  393 + resp.Message = "服务器错误"
  394 + resp.Code = 1
  395 + break
  396 + }
  397 +
  398 + switch rdata.Tasktype {
  399 + case TASKTYPE_HEQIANG:
  400 + uinfo.Task.HeQiang += rdata.Value
  401 + uinfo.Achieve.HeQiang += rdata.Value
  402 + case TASKTYPE_JIEQIANG:
  403 + uinfo.Task.JieQiang += rdata.Value
  404 + uinfo.Achieve.JieQiang += rdata.Value
  405 + case TASKTYPE_WATCHADS:
  406 + uinfo.Task.WatchAdCnt += rdata.Value
  407 + uinfo.Achieve.WatchAdsCnt += rdata.Value
  408 + case TASKTYPE_GETFLYBOX:
  409 + uinfo.Task.GetKongBox += rdata.Value
  410 + case TASKTYPE_TURNTABLE:
  411 + uinfo.Task.TurnTableCnt += rdata.Value
  412 + case TASKTYPE_SHOPBUY:
  413 + uinfo.Task.ShopBuyGun += rdata.Value
  414 + case TASKTYPE_PASSLEVEL:
  415 + uinfo.Task.PassLevel += rdata.Value
  416 + uinfo.Achieve.PassLevel += rdata.Value
  417 + default:
  418 + logger.Error("HandlerUpdatetaskandachieve type failed")
  419 + }
  420 + logger.Info("HandlerUpdatetaskandachieve uinfo=%v rdata=%v", uinfo, rdata)
  421 + SaveUserInfo(uinfo, uniqueuuid)
  422 + resp.Code = 0
  423 + break
  424 + }
  425 +
  426 + //回包
  427 + respstr, _ := json.Marshal(&resp)
  428 + fmt.Fprint(w, string(respstr))
  429 +
  430 +}
  431 +
211 func HandlerUploadtili(w http.ResponseWriter, data string, uuid int) { 432 func HandlerUploadtili(w http.ResponseWriter, data string, uuid int) {
212 SetHeader(w) 433 SetHeader(w)
213 llock.Lock() 434 llock.Lock()
@@ -666,7 +887,15 @@ func (u *UserData) HandlePassDay(uuid int, channel string) { @@ -666,7 +887,15 @@ func (u *UserData) HandlePassDay(uuid int, channel string) {
666 u.GetCashCnt = 0 887 u.GetCashCnt = 0
667 u.RandNum = RANDNUMLIMIT 888 u.RandNum = RANDNUMLIMIT
668 889
669 - 890 + //任务数据重置
  891 + u.Task.PassLevel = 0
  892 + u.Task.ShopBuyGun = 0
  893 + u.Task.TurnTableCnt = 0
  894 + u.Task.GetKongBox = 0
  895 + u.Task.JieQiang = 0
  896 + u.Task.HeQiang = 0
  897 + u.Task.WatchAdCnt = 0
  898 + u.Task.OnlineMin = 0
670 899
671 //签到数据处理 900 //签到数据处理
672 u.IsSignToday = 0 901 u.IsSignToday = 0
src/common/redis/def.go
@@ -2,5 +2,6 @@ package redis @@ -2,5 +2,6 @@ package redis
2 2
3 const ( 3 const (
4 USER_DATA_KEY = "BRAINHOLE_USER_DATA_KEY" //玩家数据 4 USER_DATA_KEY = "BRAINHOLE_USER_DATA_KEY" //玩家数据
5 - 5 + USER_TASKINFO_LIST = "STARSTAR_USER_TASKINFO_LIST" //任务列表数据缓存
  6 + USER_ACHIEVEMENTINFO_LIST = "STARSTAR_USER_ACHIEVEMENTINFO_LIST" //成就列表数据缓存
6 ) 7 )