diff --git a/src/HttpServer/jsonconf/jsonconf.go b/src/HttpServer/jsonconf/jsonconf.go index 0b54193..7fa6c57 100644 --- a/src/HttpServer/jsonconf/jsonconf.go +++ b/src/HttpServer/jsonconf/jsonconf.go @@ -70,6 +70,20 @@ func GetCatConfig(lv int) *CatDesc { return nil } +//获取红包猫配置 +func GetRedCatConfig(lv int) *RedCatDesc { + reallv := lv - 100 + if reallv < 1 { + reallv = 1 + } + for _, v := range g_jsonconf.RedCatConfig { + if v.Id == reallv { + return &v + } + } + return nil +} + func file_get_contents(path string) ([]byte, error) { f, err := os.Open(path) diff --git a/src/HttpServer/logic/datadef.go b/src/HttpServer/logic/datadef.go index 6b338dc..21cee1a 100644 --- a/src/HttpServer/logic/datadef.go +++ b/src/HttpServer/logic/datadef.go @@ -1,39 +1,46 @@ package logic type UserLoginReq struct { - Lype int `json:"type"` + Lype int `json:"type"` UserId int `json:"userId"` - Fromid int `json:"fromid"` - + Fromid int `json:"fromid"` } type UserLoginData struct { - UserId string `json:"userId"` - AccessToken string `json:"accessToken"` - LoginType int `json:"loginType"` + UserId string `json:"userId"` + AccessToken string `json:"accessToken"` + LoginType int `json:"loginType"` Nickname string `json:"nickname"` - HeadImg string `json:"headImg"` + HeadImg string `json:"headImg"` } type UserLoginResp struct { - Code int `json:"code"` - Data UserLoginData `json:"data"` - Message string `json:"message"` + Code int `json:"code"` + Data UserLoginData `json:"data"` + Message string `json:"message"` } type GetUserDataReq struct { //Uuid int `json:"uuid"` } +type offlineRewardDesc struct { + OfflineTime int `json:"offlineTime"` + Income string `json:"income"` +} + +type GetUserDataData struct { + TimingReward bool `json:"timingReward"` + OfflineReward offlineRewardDesc `json:"offlineReward"` + Output string `json:"output"` + Coin string `json:"coin"` + Now int `json:"now"` +} + type GetUserDataResp struct { - Code int `json:"code"` - Message string `json:"message"` - Gold int64 `json:"gold"` - Love int64 `json:"love"` - Dougoldlefttime int `json:"dougoldlefttime"` - Automlefttime int `json:"automlefttime"` - Acclcteboxlefttime int `json:"acclcteboxlefttime"` - Israndgift int `json:"israndgift"` + Code int `json:"code"` + Message string `json:"message"` + Data GetUserDataData `json:"data"` } type DataDesc struct { @@ -135,12 +142,25 @@ type ClickRandGiftResp struct { } type DoBuyCatReq struct { - Lv int `json:"lv"` + CatId int `json:"catId"` +} + +type DoBuyCatCoin struct { + UserId int `json:"userId"` + UpdateTime int `json:"updateTime"` + Coin string `json:"coin"` + IcomeRate string `json:"icomeRate"` +} + +type DoBuyCatData struct { + Position int `json:"position"` + Price string `json:"price"` + Coin DoBuyCatCoin `json:"coin"` } type DoBuyCatResp struct { - Code int `json:"code"` - Message string `json:"message"` - Pos int `json:"pos"` + Code int `json:"code"` + Message string `json:"message"` + Data DoBuyCatData `json:"data"` } type QueryWareHouseResp struct { @@ -255,10 +275,11 @@ type UpdateUserInfoResp struct { type RankInfoDesc struct { Rank int `json:"rank"` + UserId int `json:"userId"` Headurl string `json:"headurl"` Nickname string `json:"nickname"` - Catlv int `json:"catlv"` - Goldnum int64 `json:"goldnum"` + CatName string `json:"text"` + Income int64 `json:"income"` } type QueryPlayerRankResp struct { @@ -322,14 +343,16 @@ type StartOnlineTaskResp struct { } type GetOfflineRewardReq struct { - Optype int `json:"optype"` + Reward int `json:"reward"` + Optype int `json:"type"` +} +type GetOfflineRewardData struct { } type GetOfflineRewardResp struct { - Code int `json:"code"` - Message string `json:"message"` - Gold int64 `json:"gold"` - Love int64 `json:"love"` + Code int `json:"code"` + Message string `json:"message"` + Data GetOfflineRewardData `json:"data"` } //********************************************************************************************************** @@ -402,9 +425,10 @@ type AchieveMentData struct { //玩家数据 type UserData struct { - UserId int //玩家id - RegTime int //注册时间 + UserId int //玩家id + RegTime int //注册时间 Gold int64 //金币 + GoldSum int64 //玩家累计金币 Love int64 //爱心值 Goldrate int64 //金币生成速率 Loverate int64 //爱心生产速率 diff --git a/src/HttpServer/logic/function.go b/src/HttpServer/logic/function.go index 4e25e53..73df733 100644 --- a/src/HttpServer/logic/function.go +++ b/src/HttpServer/logic/function.go @@ -40,7 +40,8 @@ func GetUserInfo(uid string) (*UserData, error) { //初始化玩家信息 func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { udata := new(UserData) - udata.Gold = 0 + udata.Gold = 100000 + udata.GoldSum += udata.Gold udata.Love = 0 udata.InviteId = data.Fromid udata.Highestlv = 0 @@ -68,7 +69,6 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { udata.Goldrate = 0 udata.Loverate = 0 - //初始化商店信息 initcatcfg := jsonconf.GetCatConfig(1) if initcatcfg == nil { @@ -77,7 +77,7 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { } var info BuyCatInfoData info.Buytime = 0 - info.CurPrice,_= strconv.ParseInt(initcatcfg.Price,10,64) + info.CurPrice, _ = strconv.ParseInt(initcatcfg.Price, 10, 64) info.IsMaxBuytime = 0 udata.BuyCatInfo = append(udata.BuyCatInfo, info) @@ -96,7 +96,6 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { //保存redis SaveUserInfo(udata, strconv.Itoa(uuid)) - //初始化玩家仓库信息 pware := new(UserWareHouseData) for i := 0; i < 15; i++ { @@ -113,4 +112,138 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) { //保存此次计算时间 nowtimestr := strconv.Itoa(int(time.Now().Unix())) redishandler.GetRedisClient().HSet(redis.USER_LAST_CALC_TIME, strconv.Itoa(uuid), nowtimestr) -} \ No newline at end of file +} + +//计算购买后猫箱子的位置,将箱子放到对应位置 返回值表示位置 -1表示失败都满了 +func getCatPutPos(uinfo *UserData, clv int) int { + pos := -1 + for k, v := range uinfo.PosInfo { + if v.Catlv == 0 { + uinfo.PosInfo[k].Catlv = clv + uinfo.PosInfo[k].Pos = k + //v.Catlv = clv + //v.Pos = k + pos = k + break + } + } + return pos +} + +func (udata *UserData) CalcGoldRate() int64 { + //计算出所有速率的和 + //计算方式为 首先计算合成界面猫的产出速率,然后计算红毛包的产出速率,最后计算仓库中的猫的产出速率 + var sumrate int64 + cfg37 := jsonconf.GetCatConfig(37) + if cfg37 == nil { + logger.Error("37cfg err!!") + return 0 + } + for _, v := range udata.PosInfo { + if v.Catlv > 0 && v.Catlv < 100 { + //有猫存在的要计算一下 + //非红包猫 + for _, vv := range jsonconf.GetJsonConf().CatConfig { + if vv.Id == v.Catlv { + onerate, _ := strconv.ParseInt(vv.Gold_get, 10, 64) + sumrate += onerate + break + } + } + } + + if v.Catlv > 100 { + //红包猫 需要判断是否产金币 + rcfg := jsonconf.GetRedCatConfig(v.Catlv) + if rcfg != nil { + if rcfg.Is_37 != 0 { + onerate, _ := strconv.ParseInt(cfg37.Gold_get, 10, 64) + sumrate += onerate + } + } else { + logger.Error("GetRedCatConfig failed lv=%v", v.Catlv) + } + } + } + + udatastr, err := redishandler.GetRedisClient().HGet(redis.USER_WAREHOUSE_INFO, strconv.Itoa(udata.UserId)) + if err != nil { + logger.Error("CalcGoldRate failed err=%v", err) + return sumrate + } + wdata := new(UserWareHouseData) + err = json.Unmarshal([]byte(udatastr), wdata) + if err != nil { + logger.Error("CalcGoldRate Unmarshal redis failed ") + return sumrate + } + for _, v := range wdata.Info { + rcfg := jsonconf.GetRedCatConfig(v.Warelv + 100) + if rcfg != nil { + if rcfg.Is_37 != 0 { + onerate, _ := strconv.ParseInt(cfg37.Gold_get, 10, 64) + sumrate += onerate + } + } else { + logger.Error("GetRedCatConfig failed lv=%v", v.Warelv+100) + } + } + + return sumrate +} + +//发生金币变动时保存排行榜 +func (udata *UserData) AddToRank() error { + err := redishandler.GetRedisClient().Zadd(redis.USER_GOLD_RANK, float64(udata.Gold), strconv.Itoa(udata.UserId)) + if err != nil { + logger.Error("AddToRank failed err=%v", err) + } + return err +} + +//计算最高等级猫的名字 +func (u *UserData) CalcHigestCatName() string { + higestlv := 0 + for _, val := range u.PosInfo { + if higestlv < val.Catlv { + higestlv = val.Catlv + } + } + + //计算仓库内的 + udatastr, err := redishandler.GetRedisClient().HGet(redis.USER_WAREHOUSE_INFO, strconv.Itoa(u.UserId)) + if err != nil { + logger.Error("CalcHigestCatName failed err=%v", err) + return "" + } + wdata := new(UserWareHouseData) + err = json.Unmarshal([]byte(udatastr), wdata) + if err != nil { + logger.Error("CalcHigestCatName Unmarshal redis failed ") + return "" + } + for _, val := range wdata.Info { + reallv := val.Warelv + 100 + if higestlv < reallv { + higestlv = reallv + } + } + + //计算猫名字 + if higestlv > 100 { + cfg := jsonconf.GetRedCatConfig(higestlv - 100) + if cfg == nil { + logger.Error("CalcHigestCatName failed getcfg higestlv=%v", higestlv) + return "" + } + return cfg.Name + } else { + cfg := jsonconf.GetCatConfig(higestlv) + if cfg == nil { + logger.Error("CalcHigestCatName failed getcfg higestlv=%v", higestlv) + return "" + } + return cfg.Name + } + +} diff --git a/src/HttpServer/logic/httpserver.go b/src/HttpServer/logic/httpserver.go index bbcb997..651b479 100644 --- a/src/HttpServer/logic/httpserver.go +++ b/src/HttpServer/logic/httpserver.go @@ -14,7 +14,6 @@ import ( "net/http" ) - func StartHttpServe() { startServerHttpServe() } @@ -169,17 +168,21 @@ func CheckErr(err error) { } func startServerHttpServe() { - http.HandleFunc("/api/account/login", UserLogin) //登录 - http.HandleFunc("/happycat/getuserdata", GetUserData) //拉取用户数据 - http.HandleFunc("/happycat/exchangetwoPos", ExchangePos) //交换位置 - http.HandleFunc("/happycat/clickcatbox", ClickCatBox) //请求点击猫箱子 - http.HandleFunc("/happycat/upgradecatbox", UpgradeCatBox) //请求点击猫箱子 - http.HandleFunc("/happycat/acclecte", AcclecteGold) //请求点击猫箱子 - http.HandleFunc("/happycat/automerge", AutoMerge) //请求自动合成 - http.HandleFunc("/happycat/generatebox", GenerateBox) //请求点击猫箱子 - http.HandleFunc("/happycat/clickrandgift", ClickRandGift) //请求点击猫箱子 - http.HandleFunc("/happycat/querybuycat", QueryBuyCat) //请求商店购买信息 - http.HandleFunc("/happycat/dobuycat", DoBuyCat) //购买猫 + http.HandleFunc("/api/account/login", UserLogin) //登录 + http.HandleFunc("/api/home/buy", DoBuyCat) //购买猫 + http.HandleFunc("/api/home/synResource", GetUserData) //同步资源 + http.HandleFunc("/api/home/recvOfflineReward", GetOfflineReward) //领取离线奖励 + http.HandleFunc("api/ranking/list", QueryPlayerRank) //排行榜 + + http.HandleFunc("/happycat/exchangetwoPos", ExchangePos) //交换位置 + http.HandleFunc("/happycat/clickcatbox", ClickCatBox) //请求点击猫箱子 + http.HandleFunc("/happycat/upgradecatbox", UpgradeCatBox) //请求点击猫箱子 + http.HandleFunc("/happycat/acclecte", AcclecteGold) //请求点击猫箱子 + http.HandleFunc("/happycat/automerge", AutoMerge) //请求自动合成 + http.HandleFunc("/happycat/generatebox", GenerateBox) //请求点击猫箱子 + http.HandleFunc("/happycat/clickrandgift", ClickRandGift) //请求点击猫箱子 + http.HandleFunc("/happycat/querybuycat", QueryBuyCat) //请求商店购买信息 + http.HandleFunc("/happycat/querwarehouse", QueryWareHouse) //请求仓库信息 http.HandleFunc("/happycat/putcattowarehouse", PutCattoWareHouse) //将合成界面的猫放入仓库 http.HandleFunc("/happycat/takecatoutfromwarehouse", TakeCatoutfromWareHouse) //将仓库的猫取出 @@ -192,14 +195,14 @@ func startServerHttpServe() { http.HandleFunc("/happycat/catshoplay ", CatShoPlay) //请求点击猫箱子 http.HandleFunc("/happycat/getcatshopreward ", GetCatShopReward) //请求点击猫箱子 http.HandleFunc("/happycat/acclectecatstory ", AcclecteCatStory) //请求点击猫箱子 - http.HandleFunc("/chappycatat/updateuserinfo ", UpdateUserInfo) //请求点击猫箱子 + http.HandleFunc("/chappycatat/updateuserinfo ", UpdateUserInfo) //请求点击猫箱子 http.HandleFunc("/happycat/queryplayerrank ", QueryPlayerRank) //请求点击猫箱子 http.HandleFunc("/happycat/querycompletetask ", QueryCompleteTask) //请求点击猫箱子 http.HandleFunc("/happycat/querycompleteachievement ", QueryCompleteAchievement) //请求点击猫箱子 http.HandleFunc("/happycat/gettaskreward ", GetTaskReward) //请求点击猫箱子 http.HandleFunc("/happycat/getachievereward ", GetAchieveReward) //请求点击猫箱子 http.HandleFunc("/happycat/startonlinetask ", StartOnlineTask) //请求开始在线时长任务 - http.HandleFunc("/happycat/getofflinereward ", GetOfflineReward) //请求开始在线时长任务 + //http.HandleFunc("/happycat/getofflinereward ", GetOfflineReward) //请求开始在线时长任务 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) CheckErr(err) @@ -208,7 +211,7 @@ func startServerHttpServe() { func GetOfflineReward(w http.ResponseWriter, r *http.Request) { Uuid := 0 if len(r.Header) > 0 { - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) } if Uuid == 0 { SetHeader(w) @@ -220,7 +223,7 @@ func GetOfflineReward(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("GetOfflineReward , body:%v,uuid=%v", s, Uuid) - //HandlerGetOfflineReward(w, s, Uuid) + HandlerGetOfflineReward(w, s, Uuid) } func StartOnlineTask(w http.ResponseWriter, r *http.Request) { @@ -334,7 +337,7 @@ func UpdateUserInfo(w http.ResponseWriter, r *http.Request) { func QueryPlayerRank(w http.ResponseWriter, r *http.Request) { Uuid := 0 if len(r.Header) > 0 { - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) } if Uuid == 0 { SetHeader(w) @@ -346,7 +349,7 @@ func QueryPlayerRank(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("QueryPlayerRank , body:%v,uuid=%v", s, Uuid) - //HandlerQueryPlayerRank(w, s, Uuid) + HandlerQueryPlayerRank(w, s, Uuid) } func AcclecteCatStory(w http.ResponseWriter, r *http.Request) { @@ -550,7 +553,7 @@ func TakeCatoutfromWareHouse(w http.ResponseWriter, r *http.Request) { func DoBuyCat(w http.ResponseWriter, r *http.Request) { Uuid := 0 if len(r.Header) > 0 { - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) } if Uuid == 0 { SetHeader(w) @@ -562,7 +565,7 @@ func DoBuyCat(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("DoBuyCat , body:%v,uuid=%v", s, Uuid) - //HandlerDoBuyCat(w, s, Uuid) + HandlerDoBuyCat(w, s, Uuid) } func QueryBuyCat(w http.ResponseWriter, r *http.Request) { @@ -616,7 +619,7 @@ func AutoMerge(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("AutoMerge , body:%v,uuid=%v", s, Uuid) -// HandlerAutoMerge(w, s, Uuid) + // HandlerAutoMerge(w, s, Uuid) } func ClickCatBox(w http.ResponseWriter, r *http.Request) { @@ -652,7 +655,7 @@ func GenerateBox(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("GenerateBox , body:%v,uuid=%v", s, Uuid) -// HandlerGenerateBox(w, s, Uuid) + // HandlerGenerateBox(w, s, Uuid) } func UpgradeCatBox(w http.ResponseWriter, r *http.Request) { @@ -670,7 +673,7 @@ func UpgradeCatBox(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("UpgradeCatBox , body:%v,uuid=%v", s, Uuid) -// HandlerUpgradeCatBox(w, s, Uuid) + // HandlerUpgradeCatBox(w, s, Uuid) } func AcclecteBoxRate(w http.ResponseWriter, r *http.Request) { @@ -688,7 +691,7 @@ func AcclecteBoxRate(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("AcclecteBoxRate , body:%v,uuid=%v", s, Uuid) -// HandlerAcclecteBoxRate(w, s, Uuid) + // HandlerAcclecteBoxRate(w, s, Uuid) } func AcclecteGold(w http.ResponseWriter, r *http.Request) { @@ -706,7 +709,7 @@ func AcclecteGold(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("AcclecteGold , body:%v,uuid=%v", s, Uuid) -// HandlerAcclecteGold(w, s, Uuid) + // HandlerAcclecteGold(w, s, Uuid) } func ExchangePos(w http.ResponseWriter, r *http.Request) { @@ -730,7 +733,7 @@ func ExchangePos(w http.ResponseWriter, r *http.Request) { func GetUserData(w http.ResponseWriter, r *http.Request) { Uuid := 0 if len(r.Header) > 0 { - Uuid, _ = strconv.Atoi(r.Header.Get("Uuid")) + Uuid, _ = strconv.Atoi(r.Header.Get("uid")) } if Uuid == 0 { SetHeader(w) @@ -742,7 +745,7 @@ func GetUserData(w http.ResponseWriter, r *http.Request) { s := string(result) logger.Info("GetUserData , body:%v,uuid=%v", s, Uuid) - //HandlerGetUserData(w, s, Uuid) + HandlerGetUserData(w, s, Uuid) } func UserLogin(w http.ResponseWriter, r *http.Request) { @@ -762,22 +765,22 @@ func UserLogin(w http.ResponseWriter, r *http.Request) { } /*if Uuid == 0 { - SetHeader(w) - /*var resp UserLoginResp - resp.Code = -1 - resp.Message = "uuid is nil" - respstr, _ := json.Marshal(&resp) - fmt.Fprint(w, string(respstr)) - logger.Error("UserLogin uuid=0! failed!")*/ - //return + SetHeader(w) + /*var resp UserLoginResp + resp.Code = -1 + resp.Message = "uuid is nil" + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) + logger.Error("UserLogin uuid=0! failed!")*/ + //return //} result, _ := ioutil.ReadAll(r.Body) r.Body.Close() s := string(result) - logger.Info("UserLogin , body:%v,uuid=%v token=%v", s, Uuid,Token) + logger.Info("UserLogin , body:%v,uuid=%v token=%v", s, Uuid, Token) - HandlerLogin(w, s, Uuid,Token) + HandlerLogin(w, s, Uuid, Token) } func ReviewAllianceHandler(w http.ResponseWriter, r *http.Request) { diff --git a/src/HttpServer/logic/logic.go b/src/HttpServer/logic/logic.go index 166761c..224bde4 100644 --- a/src/HttpServer/logic/logic.go +++ b/src/HttpServer/logic/logic.go @@ -1,6 +1,7 @@ package logic import ( + "HttpServer/jsonconf" "HttpServer/redishandler" "common/logger" "common/redis" @@ -9,11 +10,12 @@ import ( "net/http" "strconv" "sync" + "time" ) var ( //m_userInfo *beegomap.BeeMap //make(map[int32]*UserData - Maplock *sync.RWMutex + Maplock *sync.RWMutex ) func init() { @@ -27,7 +29,7 @@ func SetHeader(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid") } -func HandlerLogin(w http.ResponseWriter, data string, uuid int,token string) { +func HandlerLogin(w http.ResponseWriter, data string, uuid int, token string) { SetHeader(w) var resp UserLoginResp resp.Code = 0 @@ -52,7 +54,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int,token string) { InitUserInfo(&rdata, &resp, rdata.UserId) } else { - uinfo,err := GetUserInfo(strconv.Itoa(rdata.UserId)) + uinfo, err := GetUserInfo(strconv.Itoa(rdata.UserId)) if err != nil { logger.Info("GetUserInfo HandlerLogin data failed:%v,for:%v", err, data) resp.Message = "GetUserInfo failed" @@ -70,11 +72,267 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int,token string) { } + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) + + logger.Info("###HandlerLogin###rdata:%v", string(respstr)) +} + +func HandlerDoBuyCat(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp DoBuyCatResp + resp.Code = 0 + resp.Message = "success" + var rdata DoBuyCatReq + err := json.Unmarshal([]byte(data), &rdata) + for { + if err != nil { + logger.Error("HandlerDoBuyCat json unmarshal failed=%v", err) + resp.Code = 1 + resp.Message = "json failed" + break + } + uinfo, err := GetUserInfo(strconv.Itoa(uuid)) + if err != nil || uinfo == nil { + logger.Error("HandlerDoBuyCat getuserinfo failed=%v", err) + resp.Code = 1 + resp.Message = "get userinfo failed" + break + } + + maxlv := uinfo.Highestlv - 5 + if maxlv < 1 { + //最小1j + maxlv = 1 + } + if rdata.CatId > maxlv || rdata.CatId < 1 || rdata.CatId > len(uinfo.BuyCatInfo) { + logger.Error("HandlerDoBuyCat buy lv failed=%v", err) + resp.Code = 1 + resp.Message = "buy lv invalid" + break + } + + curprice := uinfo.BuyCatInfo[rdata.CatId-1].CurPrice + if uinfo.Gold < curprice { + logger.Error("HandlerDoBuyCat gold not enough failed=%v", err) + resp.Code = 1 + resp.Message = "gold not enough" + break + } + + //获取配置 + catcfg := jsonconf.GetCatConfig(rdata.CatId) + if catcfg == nil { + logger.Error("HandlerDoBuyCat get cat cfg failed=%v,lv=%v", err, rdata.CatId) + resp.Code = 1 + resp.Message = "get cat cfg failed" + break + } + + //需要找到一个位置 + catpos := getCatPutPos(uinfo, rdata.CatId) + if catpos < 0 { + logger.Error("HandlerDoBuyCat not enough place failed=%v", err) + resp.Code = 1 + resp.Message = "not enough place" + break + } + //扣钱 + uinfo.Gold -= curprice + + //重新计算价格 + if uinfo.BuyCatInfo[rdata.CatId-1].IsMaxBuytime == 0 { + uinfo.BuyCatInfo[rdata.CatId-1].CurPrice = int64(float64(uinfo.BuyCatInfo[rdata.CatId-1].CurPrice) * float64(catcfg.Ratio)) + uinfo.BuyCatInfo[rdata.CatId-1].Buytime++ + if uinfo.BuyCatInfo[rdata.CatId-1].Buytime >= catcfg.Increse_limit { + uinfo.BuyCatInfo[rdata.CatId-1].IsMaxBuytime = 1 + } + } + + //需要重新计算速率 + uinfo.CalcGoldRate() + + resp.Data.Price = strconv.FormatInt(uinfo.BuyCatInfo[rdata.CatId-1].CurPrice, 10) + resp.Data.Position = catpos + resp.Data.Coin.UserId = uuid + resp.Data.Coin.Coin = strconv.FormatInt(uinfo.Gold, 10) + resp.Data.Coin.IcomeRate = strconv.FormatInt(uinfo.Goldrate, 10) + resp.Data.Coin.UpdateTime = int(time.Now().Unix()) + + resp.Code = 0 + //保存 + SaveUserInfo(uinfo, strconv.Itoa(uuid)) + break + + } //回包 respstr, _ := json.Marshal(&resp) fmt.Fprint(w, string(respstr)) +} - logger.Info("###HandlerLogin###rdata:%v", string(respstr)) -} \ No newline at end of file +func HandlerGetUserData(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp GetUserDataResp + resp.Code = 0 + resp.Message = "success" + + for { + v, err := redishandler.GetRedisClient().HGet(redis.USER_LAST_CALC_TIME, strconv.Itoa(uuid)) + if err != nil { + logger.Info("CalcOfflineData get USER_LAST_CALC_TIME failed=%v", err) + resp.Message = "redishandler failed" + resp.Code = 1 + break + } + + lasttime, _ := strconv.Atoi(v) + nowtime := time.Now().Unix() + if nowtime-int64(lasttime) < 0 { + logger.Error("HandlerGetUserData nowtime=%v lasttime=%v", nowtime, lasttime) + resp.Message = "request time small than zero" + resp.Code = 1 + break + } + uinfo, err := GetUserInfo(strconv.Itoa(uuid)) + if err != nil || uinfo == nil { + logger.Error("HandlerGetUserData getuserinfo failed=%v", err) + resp.Code = 1 + resp.Message = "get userinfo failed" + break + } + + if nowtime-int64(lasttime) > 5*60 { + //算离线收益 + resp.Data.TimingReward = false + offsec := nowtime - int64(lasttime) + if offsec > 2*3600 { + offsec = 2 * 3600 + } + + uinfo.OfflineGold = offsec * uinfo.Goldrate + resp.Data.Coin = strconv.FormatInt(uinfo.Gold, 10) + resp.Data.Now = int(time.Now().Unix()) + resp.Data.Output = "0" + resp.Data.OfflineReward.OfflineTime = int(offsec) + resp.Data.OfflineReward.Income = strconv.FormatInt(uinfo.OfflineGold, 10) + } else { + //按费离线收益计算 + offsec := nowtime - int64(lasttime) + resp.Data.TimingReward = true + resp.Data.Now = int(time.Now().Unix()) + addgold := uinfo.Goldrate * offsec + uinfo.Gold = addgold + uinfo.GoldSum += addgold + uinfo.AddToRank() + resp.Data.Output = strconv.FormatInt(addgold, 10) + resp.Data.Coin = strconv.FormatInt(uinfo.Gold, 10) + } + + //保存此次计算时间 + nowtimestr := strconv.FormatInt(nowtime, 10) + redishandler.GetRedisClient().HSet(redis.USER_LAST_CALC_TIME, strconv.Itoa(uuid), nowtimestr) + logger.Info("HandlerGetUserData save USER_LAST_CALC_TIME time=%v", nowtimestr) + + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) +} + +func HandlerGetOfflineReward(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp GetOfflineRewardResp + resp.Code = 0 + resp.Message = "success" + var rdata GetOfflineRewardReq + + err := json.Unmarshal([]byte(data), &rdata) + for { + if err != nil { + logger.Error("HandlerGetOfflineReward json unmarshal failed=%v", err) + resp.Code = 1 + resp.Message = "json failed" + break + } + + uinfo, err := GetUserInfo(strconv.Itoa(uuid)) + if err != nil || uinfo == nil { + logger.Error("HandlerGetUserData getuserinfo failed=%v", err) + resp.Code = 1 + resp.Message = "get userinfo failed" + break + } + + addgold := uinfo.OfflineGold + + if rdata.Optype == 2 { + addgold *= 2 + + } + uinfo.Gold += addgold + uinfo.GoldSum += addgold + uinfo.AddToRank() + + //此处todo 记录离线领取的次数 + + //领取过后将离线金币清零 + uinfo.OfflineGold = 0 + + //保存 + SaveUserInfo(uinfo, strconv.Itoa(uuid)) + + resp.Code = 0 + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) +} + +func HandlerQueryPlayerRank(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp QueryPlayerRankResp + resp.Code = 0 + resp.Message = "success" + for { + //取100名 + vv, err := redishandler.GetRedisClient().ZRevRangewithIndex(redis.USER_GOLD_RANK, 0, 99) + if err == nil { + rank := 0 + for _, v := range vv { + rank++ + //logger.Info("TestMyredis k=%v,v=%v", k, v) + rinfobyte, _ := v.(string) + ruid, _ := strconv.Atoi(rinfobyte) + rindo, err := GetUserInfo(rinfobyte) + if err == nil && rindo != nil { + var tmp RankInfoDesc + tmp.UserId = ruid + tmp.Income = rindo.GoldSum + tmp.Nickname = rindo.NickName + tmp.Headurl = rindo.Head + tmp.Rank = rank + tmp.CatName = rindo.CalcHigestCatName() + resp.Data = append(resp.Data, tmp) + } + } + } else { + logger.Error("HandlerUpdateUserInfo redisfailed ") + resp.Code = 1 + resp.Message = "redisfailed" + break + } + resp.Code = 0 + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) +} -- libgit2 0.21.0