diff --git a/src/HttpServer/jsonconf/jsonconf.go b/src/HttpServer/jsonconf/jsonconf.go index e21ae5c..eb77a37 100644 --- a/src/HttpServer/jsonconf/jsonconf.go +++ b/src/HttpServer/jsonconf/jsonconf.go @@ -16,7 +16,7 @@ type AchieveDesc struct { Task string `json:"task"` Num int `json:"num"` Reward []int `json:"reward"` - + TaskType int `json:"type"` } type LevelDesc struct { diff --git a/src/HttpServer/logic/constdef.go b/src/HttpServer/logic/constdef.go index 95b073e..166136c 100644 --- a/src/HttpServer/logic/constdef.go +++ b/src/HttpServer/logic/constdef.go @@ -1,7 +1,7 @@ package logic -//任务成就类型枚举 -var ( +//任务成就类型枚举 协议上报用 +const ( TASKTYPE_PASSLEVEL = 1 //表示通过x关卡 TASKTYPE_GETREDBAG = 2 //领取x次红包 TASKTYPE_WATCHADS = 3 //3表示观看x次广告 @@ -9,6 +9,39 @@ var ( TASKTYPE_USEITEM = 5 //5表示使用x次道具 ) + +//任务类型 对应表 +const ( + DAILY_TASKTYPE_LOGIN =1 //1 每日登录 + DAILY_TASKTYPE_GETGUANCNT = 2 //提取存钱罐 + DAILY_TASKTYPE_PASSLEVEL = 3 //近日通过关 + DAILY_TASKTYPE_USEITEM = 4 //使用道具数 + DAILY_TASKTYPE_GETREDBAG = 5 //领取红包次数 + DAILY_TASKTYPE_GETGUANGOLD = 6 //收取金币x枚 + DAILY_TASKTYPE_ONLINEMIN = 7 //在线分钟数 +) + +//成就类型 对应表 +const ( + ACHIEVETYPE_WATCHADDS = 1 //累计观看广告次数 + ACHIEVETYPE_USERLV = 2 //人物等级 + ACHIEVETYPE_KILLSTAR = 3 //累计消除星星数 + ACHIEVETYPE_GETGUANCNT = 4 //累计存钱罐提取 + ACHIEVETYPE_USEITEMCNT = 5 //累计道具使用 + +) + +//奖励类型枚举 +const ( + REWARDTYPE_STAR = 1 //星星 + REWARDTYPE_CHUI =2 + REWARDTYPE_SHUA = 3 + REWARDTYPE_SWAP = 4 + REWARDTYPE_ELIMITE = 5 + REWARDTYPE_EXP = 6 + REWARDTYPE_GOLD = 7 +) + var ( WETCHATAPPID="wx572a2a5ec4538f33" WETCHATSERCRT = "b31e2e7406af88fe7395cd178bdb64fc" diff --git a/src/HttpServer/logic/datadef.go b/src/HttpServer/logic/datadef.go index 3ee0df7..321f851 100644 --- a/src/HttpServer/logic/datadef.go +++ b/src/HttpServer/logic/datadef.go @@ -122,8 +122,54 @@ type UpdatetaskResp struct { Message string `json:"message"` } + +type QuerytaskinfoReq struct { + Tasktype int `json:"tasktype"` +} + +type QuerytaskinfoResp struct { + Code int `json:"code"` + Message string `json:"message"` + Data TaskList `json:"data"` +} + + + +type GettaskrewardReq struct { + Tasktype int `json:"tasktype"` + Taskid int `json:"taskid"` + Gameid string `json:"gameid"` + Channel string `json:"channel"` +} + +type GettaskrewardData struct { + Lv int `json:"lv"` + Rewardstar int `json:"rewardstar"` + Chuiitem int `json:"chuiitem"` + Shuaitem int `json:"shuaitem"` + Swapitem int `json:"swapitem"` + Eliminitem int `json:"eliminitem"` + Goldnum int `json:"goldnum"` +} +type GettaskrewardResp struct { + Code int `json:"code"` + Message string `json:"message"` + Data GettaskrewardData `json:"data"` +} + + //********************************************************************************************************** +type TaskListDesc struct { + Taskid int `json:"tasktype"` + Taskstatus int `json:"taskstatus"` + Taskprogress int `json:"taskprogress"` +} + +type TaskList struct { + Taskdata []TaskListDesc `json:"taskdata"` +} + //其中提取次数和当日登陆在userdata中记录 type TaskInfo struct { OnlineMin int //在线分钟数 diff --git a/src/HttpServer/logic/errordef.go b/src/HttpServer/logic/errordef.go index 344153d..bc32dd1 100644 --- a/src/HttpServer/logic/errordef.go +++ b/src/HttpServer/logic/errordef.go @@ -16,4 +16,7 @@ const ( ERROR_WITHDRAWLVLIMIT =11 //提现等级不够 ERROR_WITHDRAWONLYONE =12 //新人专享只能提取一次 ERROR_PRENOTFINISH =13 //前置档位未提现 + ERROR_TASKCANNOTGET =14 //当前任务未完成或已领取 + ERROR_TASKID_WRONG =15 //当前任务id错误 + ERROR_TASKPRE_NOTGET =16 //前置任务还未领取 ) \ No newline at end of file diff --git a/src/HttpServer/logic/function.go b/src/HttpServer/logic/function.go index 88cd651..1368bab 100644 --- a/src/HttpServer/logic/function.go +++ b/src/HttpServer/logic/function.go @@ -6,11 +6,26 @@ import ( "common/logger" "common/redis" "encoding/json" + "errors" "net/http" + "sort" "strconv" "time" ) +func (v *TaskList) Len() int { + return len(v.Taskdata) +} + +func (v *TaskList) Swap(i, j int) { + v.Taskdata[i], v.Taskdata[j] = v.Taskdata[j], v.Taskdata[i] +} + +func (v *TaskList) Less(i, j int) bool { + return v.Taskdata[i].Taskstatus > v.Taskdata[j].Taskstatus +} + + func SetHeader(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域 w.Header().Set("Content-Type", "application/json") @@ -44,6 +59,346 @@ func GetUserInfo(uuid int) (*UserData,error) { return &tmp,nil } +func GetUserLvConfig(lv int) *jsonconf.LevelDesc { + for _,val := range jsonconf.GetJsonConf().LevelConfig { + if lv == val.Level { + return &val + } + } + return nil +} + +func GetTaskConfig(taskid int) *jsonconf.AchieveDesc { + for _,val := range jsonconf.GetJsonConf().TaskConfig { + if val.Id == taskid { + return &val + } + } + return nil +} + +func GetAchieveConfig(taskid int) *jsonconf.AchieveDesc { + for _,val := range jsonconf.GetJsonConf().AchieventConfig { + if val.Id == taskid { + return &val + } + } + return nil +} + +func (uinfo *UserData) CalcTaskList(list *TaskList) { + + for k,val := range list.Taskdata { + //未完成的才做判断 + if val.Taskstatus != 1 { + continue + } + cfg := GetTaskConfig(val.Taskid) + if cfg == nil { + logger.Error("CalcTaskList GetTaskConfig failed id=%v",val.Taskid) + continue + } + + + if cfg.TaskType == DAILY_TASKTYPE_LOGIN { + nowloginday := time.Now().Day() + lasttime := time.Unix(int64(uinfo.LastLoginTime), 0) + lastloginday := lasttime.Day() + + if nowloginday == lastloginday && val.Taskstatus == 1{ + list.Taskdata[k].Taskprogress = 1 + list.Taskdata[k].Taskstatus = 2 + } + }else if cfg.TaskType == DAILY_TASKTYPE_GETGUANCNT { + list.Taskdata[k].Taskprogress = uinfo.GetFromGuanCnt + //当日提取存钱罐次数 + if uinfo.GetFromGuanCnt >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + }else if cfg.TaskType == DAILY_TASKTYPE_PASSLEVEL { + list.Taskdata[k].Taskprogress = uinfo.Task.PassLevel + if uinfo.Task.PassLevel >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + }else if cfg.TaskType == DAILY_TASKTYPE_USEITEM { + list.Taskdata[k].Taskprogress = uinfo.Task.UseItemCnt + if uinfo.Task.UseItemCnt >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + } else if cfg.TaskType == DAILY_TASKTYPE_GETREDBAG { + list.Taskdata[k].Taskprogress = uinfo.Task.GetRedbagCnt + if uinfo.Task.GetRedbagCnt >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + }else if cfg.TaskType == DAILY_TASKTYPE_GETGUANGOLD { + list.Taskdata[k].Taskprogress = uinfo.Task.GetGuanGold + if uinfo.Task.GetGuanGold >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + }else if cfg.TaskType == DAILY_TASKTYPE_ONLINEMIN { + list.Taskdata[k].Taskprogress = uinfo.Task.OnlineMin + if uinfo.Task.OnlineMin >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + } + + } + + //需要保存到redis + SaveTaskInfo(uinfo.Userid,1,list) + + //排序 + sort.Sort(list) +} + +func (uinfo *UserData) CalcAchieveProgress(ttype int) int { + rtval := 0 + switch ttype { + case ACHIEVETYPE_WATCHADDS: + rtval = uinfo.Achieve.WatchAdsCnt + case ACHIEVETYPE_USERLV: + rtval = uinfo.Lv + case ACHIEVETYPE_KILLSTAR: + rtval = uinfo.Achieve.KillStar + case ACHIEVETYPE_GETGUANCNT: + rtval = uinfo.Achieve.SumGetGuan + case ACHIEVETYPE_USEITEMCNT: + rtval = uinfo.Achieve.SumUseItemCnt + default: + logger.Error("CalcAchieveProgress type wrong ttype=%v",ttype) + } + + return rtval +} + +//成就返回的是新的成就列表 +func (uinfo *UserData) CalcAchieveList(list *TaskList) *TaskList{ + newlist := new(TaskList) + curtype := -1 //记录当前类型 + + //第一遍首先将所有进度设置为正确的状态 + for k,val := range list.Taskdata { + cfg := GetAchieveConfig(val.Taskid) + if cfg == nil { + logger.Error("CalcAchieveList GetTaskConfig failed id=%v",val.Taskid) + continue + } + + list.Taskdata[k].Taskprogress =uinfo.CalcAchieveProgress(cfg.TaskType) + switch cfg.TaskType { + case ACHIEVETYPE_WATCHADDS: + if uinfo.Achieve.WatchAdsCnt >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + case ACHIEVETYPE_USERLV: + if uinfo.Lv >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + case ACHIEVETYPE_KILLSTAR: + if uinfo.Achieve.KillStar >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + case ACHIEVETYPE_GETGUANCNT: + if uinfo.Achieve.SumGetGuan >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + case ACHIEVETYPE_USEITEMCNT: + if uinfo.Achieve.SumUseItemCnt >= cfg.Num { + list.Taskdata[k].Taskstatus = 2 + } + default: + logger.Error("CalcAchieveProgress type wrong ttype=%v",cfg.TaskType) + } + } + + + for k,val := range list.Taskdata { + + cfg := GetAchieveConfig(val.Taskid) + if cfg == nil { + logger.Error("CalcAchieveList GetTaskConfig failed id=%v",val.Taskid) + continue + } + + if val.Taskstatus == 2 { + //未领取已完成 只需要加入第一种 + if curtype == cfg.TaskType { + continue + }else { + //将类型加入返回列表 + newlist.Taskdata = append(newlist.Taskdata,list.Taskdata[k]) + curtype = cfg.TaskType + } + }else if val.Taskstatus == 1 { + //未完成 不做处理 + }else { + //已领取 需要判断一下是否最后一项 + if k!= len(list.Taskdata)-1 { + //不是最后一条数据 + nextcfg := GetAchieveConfig(list.Taskdata[k+1].Taskid) + if nextcfg!=nil { + if nextcfg.TaskType != cfg.TaskType { + //下面一天不是此类型了 + newlist.Taskdata = append(newlist.Taskdata,list.Taskdata[k]) + curtype = cfg.TaskType + } + } + + } else { + //最后一条 加入 + newlist.Taskdata = append(newlist.Taskdata,list.Taskdata[k]) + curtype = cfg.TaskType + } + } + + + } + + //保存旧的列表 + SaveTaskInfo(uinfo.Userid,2,list) + + //将返回的列表排序 + sort.Sort(newlist) + return newlist +} + +func SaveTaskInfo(uuid ,tasktype int,list *TaskList) error { + savestr,err := json.Marshal(list) + if err != nil { + logger.Error("SaveTaskInfo err =%v",err) + return err + } + + if tasktype == 1 { + err = redishandler.GetRedisClient().HSet(redis.USER_TASKINFO_LIST,strconv.Itoa(uuid),string(savestr)) + }else if tasktype == 2 { + err = redishandler.GetRedisClient().HSet(redis.USER_ACHIEVEMENTINFO_LIST,strconv.Itoa(uuid),string(savestr)) + }else { + logger.Error("tasktype invalid") + return errors.New("tasktype invalid") + } + return err +} + +func GetTaskInfo(uuid ,tasktype int) (*TaskList,error) { + var data string + var err error + if tasktype == 1 { + data,err = redishandler.GetRedisClient().HGet(redis.USER_TASKINFO_LIST,strconv.Itoa(uuid)) + }else if tasktype == 2 { + data,err = redishandler.GetRedisClient().HGet(redis.USER_ACHIEVEMENTINFO_LIST,strconv.Itoa(uuid)) + }else { + logger.Error("tasktype invalid") + return nil,errors.New("tasktype invalid") + } + + var list TaskList + err = json.Unmarshal([]byte(data),&list) + if err != nil { + logger.Error("GetTaskInfo err=%v",err) + return nil,err + } + + return &list,err +} + +//处理领取任务 +func (uinfo *UserData) HandleGetTaskReward(req *GettaskrewardReq,resp *GettaskrewardResp,list *TaskList) error { + isfind := false + index := -1 + var taskdesc *TaskListDesc + for k,val := range list.Taskdata { + if val.Taskid == req.Taskid { + isfind = true + index = k + taskdesc = &val + break + } + } + + if isfind && taskdesc != nil && index != -1{ + if taskdesc.Taskstatus != 2 { + //状态不是可领取状态 无法领取 + resp.Code = ERROR_TASKCANNOTGET + resp.Message = "当前任务未完成或已领取" + return errors.New("当前任务未完成或已领取") + } + + //状态对的情况 如果是成就下需要判断前置条件是否被 + if req.Tasktype == 2 && index > 0 { + if list.Taskdata[index-1].Taskstatus != 0 { + resp.Code = ERROR_TASKPRE_NOTGET + resp.Message = "前置任务还未领取" + return errors.New("当前任务未完成或已领取") + } + } + }else { + resp.Code = ERROR_TASKID_WRONG + resp.Message = "当前任务id错误" + return errors.New("当前任务id错误") + } + + cfg := GetTaskConfig(taskdesc.Taskid) + if cfg == nil || len (cfg.Reward) < 2{ + resp.Code = ERROR_SRV_ERROR + resp.Message = "网络错误" + return errors.New("获取配置失败") + } + + //将状态置位已领取 + list.Taskdata[index].Taskstatus = 0 + //发奖励 + sub := len(cfg.Reward) / 2 + for i:=0;i 0 { + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) + } + + if Uuid == 0 { + SetHeader(w) + //logger.Error("Uuid is nil!") + return + } + result, _ := ioutil.ReadAll(r.Body) + r.Body.Close() + + s := string(result) + logger.Info("Gettaskreward , body:%v,uuid=%v", s, Uuid) + + HandlerGettaskreward(w, s, Uuid) +} + +func Querytaskinfo(w http.ResponseWriter, r *http.Request) { + + Uuid := 0 + if len(r.Header) > 0 { + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) + } + + if Uuid == 0 { + SetHeader(w) + //logger.Error("Uuid is nil!") + return + } + result, _ := ioutil.ReadAll(r.Body) + r.Body.Close() + + s := string(result) + logger.Info("Querytaskinfo , body:%v,uuid=%v", s, Uuid) + + HandlerQuerytaskinfo(w, s, Uuid) +} + + func Updatetaskandachieve(w http.ResponseWriter, r *http.Request) { Uuid := 0 diff --git a/src/HttpServer/logic/logic.go b/src/HttpServer/logic/logic.go index 039dbb0..7d3c957 100644 --- a/src/HttpServer/logic/logic.go +++ b/src/HttpServer/logic/logic.go @@ -202,6 +202,126 @@ func HandlerGetcash(w http.ResponseWriter, data string, uuid int) { } + + +func HandlerGettaskreward(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp GettaskrewardResp + resp.Code = 0 + var rdata GettaskrewardReq + err := json.Unmarshal([]byte(data), &rdata) + for { + if err != nil { + logger.Info("json decode HandlerGettaskreward data failed:%v,for:%v", err, data) + resp.Message = "网络错误" + resp.Code = ERROR_JSONUNMASH_ERROR + break + } + uinfo,err := GetUserInfo(uuid) + if err != nil || uinfo == nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "服务器错误" + resp.Code = ERROR_SRV_ERROR + break + } + + //需要先计算出任务或者成就 + var tasklist *TaskList + //先查询一下当前的任务数据 + tasklist,err = GetTaskInfo(uuid,rdata.Tasktype) + if err != nil || tasklist==nil{ + logger.Error("GetTaskInfo failed err=%v", err) + resp.Message = "服务器错误" + resp.Code = ERROR_SRV_ERROR + break + } + + + err = uinfo.HandleGetTaskReward(&rdata,&resp,tasklist) + if err != nil { + logger.Error("HandleGetTaskReward failed err=%v", err) + resp.Message = "服务器错误" + resp.Code = ERROR_SRV_ERROR + break + } + + //保存数据 + SaveUserInfo(uinfo) + SaveTaskInfo(uinfo.Userid,rdata.Tasktype,tasklist) + + resp.Code = ERROR_OK + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) + +} + + + +func HandlerQuerytaskinfo(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp QuerytaskinfoResp + resp.Code = 0 + var rdata QuerytaskinfoReq + err := json.Unmarshal([]byte(data), &rdata) + for { + if err != nil { + logger.Info("json decode HandlerQuerytaskinfo data failed:%v,for:%v", err, data) + resp.Message = "网络错误" + resp.Code = ERROR_JSONUNMASH_ERROR + break + } + uinfo,err := GetUserInfo(uuid) + if err != nil || uinfo == nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "服务器错误" + resp.Code = ERROR_SRV_ERROR + break + } + + //需要先计算出任务或者成就 + var tasklist *TaskList + //先查询一下当前的任务数据 + tasklist,err = GetTaskInfo(uuid,rdata.Tasktype) + if err != nil || tasklist==nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "服务器错误" + resp.Code = ERROR_SRV_ERROR + break + } + + + + if rdata.Tasktype == 1 { + uinfo.CalcTaskList(tasklist) + }else if rdata.Tasktype == 2 { + tasklist = uinfo.CalcAchieveList(tasklist) + }else { + uinfo.CalcTaskList(tasklist) + } + + if tasklist == nil { + logger.Error("redis failed err=%v", err) + resp.Message = "服务器错误" + resp.Code = ERROR_SRV_ERROR + break + } + + resp.Data.Taskdata = append(resp.Data.Taskdata,tasklist.Taskdata...) + + resp.Code = ERROR_OK + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) + +} + func HandlerUpdatetaskandachieve(w http.ResponseWriter, data string, uuid int) { SetHeader(w) var resp UpdatetaskResp @@ -368,6 +488,7 @@ func HandlerDrawguangold(w http.ResponseWriter, data string, uuid int) { } uinfo.GetFromGuanCnt ++ + uinfo.Achieve.SumGetGuan ++ // mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv) if mcfg==nil { @@ -451,6 +572,7 @@ func HandlerGetguangold(w http.ResponseWriter, data string, uuid int) { } resp.Data.Guangold = uinfo.GuanGold + uinfo.Task.GetGuanGold += rdata.Goldnum SaveUserInfo(uinfo) diff --git a/src/common/redis/def.go b/src/common/redis/def.go index aefb773..42ed9d5 100644 --- a/src/common/redis/def.go +++ b/src/common/redis/def.go @@ -3,5 +3,7 @@ package redis const ( USER_DATA_KEY = "STARSTAR_USER_DATA_KEY" //玩家数据 USER_WITHDRAW_RECORDLIST = "STARSTAR_USER_WITHDRAW_RECORDLIST" //玩家提现记录 + USER_TASKINFO_LIST = "STARSTAR_USER_TASKINFO_LIST" //任务列表数据缓存 + USER_ACHIEVEMENTINFO_LIST = "STARSTAR_USER_ACHIEVEMENTINFO_LIST" //成就列表数据缓存 ) -- libgit2 0.21.0