From d032a23da7bbcc2fa9ebb8da0b63955b1e6c75fd Mon Sep 17 00:00:00 2001 From: 陆恒 Date: Tue, 12 May 2020 16:27:41 +0800 Subject: [PATCH] 存钱罐相关接口 --- src/HttpServer/jsonconf/jsonconf.go | 13 +++++++++++++ src/HttpServer/logic/datadef.go | 43 ++++++++++++++++++++++++++++++++++++++++++- src/HttpServer/logic/errordef.go | 4 ++++ src/HttpServer/logic/function.go | 8 ++++++-- src/HttpServer/logic/httpserver.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/HttpServer/logic/logic.go | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 374 insertions(+), 4 deletions(-) diff --git a/src/HttpServer/jsonconf/jsonconf.go b/src/HttpServer/jsonconf/jsonconf.go index 80e27c0..e21ae5c 100644 --- a/src/HttpServer/jsonconf/jsonconf.go +++ b/src/HttpServer/jsonconf/jsonconf.go @@ -64,6 +64,19 @@ func GetJsonConf() *GameConfig { } +func GetMoneyBoxCfg(lv int) *MoneyBoxLevelDesc { + rt := new(MoneyBoxLevelDesc) + rt = nil + for _,val := range g_jsonconf.MoneyBoxConfig { + if val.Level == lv { + rt = &val + break + } + } + + return rt +} + func file_get_contents(path string) ([]byte, error) { f, err := os.Open(path) if err != nil { diff --git a/src/HttpServer/logic/datadef.go b/src/HttpServer/logic/datadef.go index cd55b8e..63814f3 100644 --- a/src/HttpServer/logic/datadef.go +++ b/src/HttpServer/logic/datadef.go @@ -8,12 +8,53 @@ type UserLoginReq struct { type UserLoginResp struct { Code int `json:"code"` Message string `json:"message"` - Loginday int `json:"loginday"` + Walletgold int `json:"walletgold"` Leftads int `json:"leftads"` Guangold int `json:"guangold"` } + +type WatchadsResp struct { + Code int `json:"code"` + Message string `json:"message"` + Leftads int `json:"leftads"` + +} + +type QueryguaninfoResp struct { + Code int `json:"code"` + Message string `json:"message"` + Leftcnt int `json:"leftcnt"` + Totalcnt int `json:"totalcnt"` + Guangold int `json:"guangold"` + Minget int `json:"minget"` + Maxget int `json:"maxget"` + Loginday int `json:"loginday"` +} + + +type GetguangoldReq struct { + Goldnum int `json:"goldnum"` +} + +type GetguangoldResp struct { + Code int `json:"code"` + Message string `json:"message"` + Guangold int `json:"guangold"` +} + +type DrawguangoldReq struct { + Goldnum int `json:"goldnum"` +} + +type DrawguangoldResp struct { + Code int `json:"code"` + Message string `json:"message"` + Guangold int `json:"guangold"` + Wallgold int `json:"wallgold"` +} + //********************************************************************************************************** type TaskInfo struct { diff --git a/src/HttpServer/logic/errordef.go b/src/HttpServer/logic/errordef.go index 511bcb8..d3172c4 100644 --- a/src/HttpServer/logic/errordef.go +++ b/src/HttpServer/logic/errordef.go @@ -5,4 +5,8 @@ const ( ERROR_OK =0 //没有错误 ERROR_SRV_ERROR =1 //服务器错误 ERROR_JSONUNMASH_ERROR =2 //json解析错误 + ERROR_WATCHADS_ZEOR =3 //看视频次数已经耗尽 + ERROR_GETCONFIG_FAILED =4 //获取配置失败 + ERROR_GUANGOLD_NOTENOUGH =5 //存钱罐金币不足 + ERROR_DRAWGUAN_FAILED =6 //从存钱罐提取金币不满足限制 ) \ No newline at end of file diff --git a/src/HttpServer/logic/function.go b/src/HttpServer/logic/function.go index 2181759..a14b53c 100644 --- a/src/HttpServer/logic/function.go +++ b/src/HttpServer/logic/function.go @@ -57,13 +57,17 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) error{ resp.Guangold = initdata.GuanGold resp.Leftads = initdata.WatchAddsTime - resp.Loginday = initdata.ContinueLoginDay + resp.Walletgold = initdata.RealGold err := SaveUserInfo(&initdata) return err } +//计算当日可提取次数 +func (u *UserData) CalcTotalCnt() int { + return 0 +} func (u *UserData) HandlePassDay() { isdiffday := false @@ -119,7 +123,7 @@ func GetUserData(uuid int, resp *UserLoginResp) error{ //此处要处理一下跨天逻辑 data.HandlePassDay() - resp.Loginday = data.ContinueLoginDay + resp.Walletgold = data.RealGold resp.Leftads = data.WatchAddsTime resp.Guangold = data.GuanGold diff --git a/src/HttpServer/logic/httpserver.go b/src/HttpServer/logic/httpserver.go index 71c1e86..77acc4c 100644 --- a/src/HttpServer/logic/httpserver.go +++ b/src/HttpServer/logic/httpserver.go @@ -38,12 +38,102 @@ func startServerHttpServe() { //------------------------------------------------------------- - http.HandleFunc("/elimatestar/login", UserLogin) //登录 + http.HandleFunc("/eliminatestar/login", UserLogin) //登录 + http.HandleFunc("/eliminatestar/watchads", Watchads) //观看激励视频 + http.HandleFunc("/eliminatestar/queryguaninfo", Queryguaninfo) //观看激励视频 + http.HandleFunc("/eliminatestar/getguangold", Getguangold) //获取金币到存钱罐 + http.HandleFunc("/eliminatestar/drawguangold", Drawguangold) //提取存钱罐的金币到个人钱包 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) CheckErr(err) } +func Drawguangold(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("Drawguangold , body:%v,uuid=%v", s, Uuid) + + HandlerDrawguangold(w, s, Uuid) +} + + +func Getguangold(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("Getguangold , body:%v,uuid=%v", s, Uuid) + + HandlerGetguangold(w, s, Uuid) +} + +func Queryguaninfo(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("Queryguaninfo , body:%v,uuid=%v", s, Uuid) + + HandlerQueryguaninfo(w, s, Uuid) +} + + +func Watchads(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("Watchads , body:%v,uuid=%v", s, Uuid) + + HandlerWatchads(w, s, Uuid) +} + func UserLogin(w http.ResponseWriter, r *http.Request) { Uuid := 0 @@ -65,3 +155,5 @@ func UserLogin(w http.ResponseWriter, r *http.Request) { HandlerLogin(w, s, Uuid) } + + diff --git a/src/HttpServer/logic/logic.go b/src/HttpServer/logic/logic.go index c7db86a..1e9fb90 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" @@ -10,6 +11,215 @@ import ( "strconv" ) + +func HandlerQueryguaninfo(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp QueryguaninfoResp + resp.Code = 0 + for { + uinfo,err := GetUserInfo(uuid) + if err != nil || uinfo == nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_SRV_ERROR + break + } + + //获取存钱罐等级配置 + mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv) + if mcfg==nil { + logger.Error("GetMoneyBoxCfg failed err=%v", err) + resp.Message = "ERROR_GETCONFIG_FAILED" + resp.Code = ERROR_GETCONFIG_FAILED + break + } + + + resp.Guangold = uinfo.GuanGold + resp.Loginday = uinfo.ContinueLoginDay + resp.Totalcnt = uinfo.CalcTotalCnt() + resp.Leftcnt = resp.Totalcnt - uinfo.GetFromGuanCnt + resp.Maxget = mcfg.Max + resp.Minget = mcfg.Min + resp.Code = ERROR_OK + break + + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) +} + +func HandlerWatchads(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp WatchadsResp + resp.Code = 0 + + for { + uinfo,err := GetUserInfo(uuid) + if err != nil || uinfo == nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_SRV_ERROR + break + } + + if uinfo.WatchAddsTime == 0 { + logger.Error("WatchAddsTime zero") + resp.Message = "WatchAddsTime zero" + resp.Code = ERROR_WATCHADS_ZEOR + break + } + + uinfo.WatchAddsTime -- + err = SaveUserInfo(uinfo) + if err != nil { + logger.Error("redis failed err=%v", err) + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_SRV_ERROR + break + } + + resp.Code = ERROR_OK + break + + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) +} + + + +func HandlerDrawguangold(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp DrawguangoldResp + resp.Code = 0 + var rdata DrawguangoldReq + err := json.Unmarshal([]byte(data), &rdata) + for { + if err != nil { + logger.Info("json decode HandlerDrawguangold data failed:%v,for:%v", err, data) + resp.Message = "json unmarshal failed" + resp.Code = ERROR_JSONUNMASH_ERROR + break + } + + uinfo,err := GetUserInfo(uuid) + if err != nil || uinfo == nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_SRV_ERROR + break + } + //首先判断一下存钱罐的金币是否足够 + if rdata.Goldnum < uinfo.GuanGold { + logger.Error("guangold not enough ") + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_GUANGOLD_NOTENOUGH + break + } + + //判断一下是否满足提取条件 + totalcnt := uinfo.CalcTotalCnt() + if totalcnt <= uinfo.GetFromGuanCnt { + logger.Error("guangold not enough ") + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_GUANGOLD_NOTENOUGH + break + } + + uinfo.GetFromGuanCnt ++ + // + mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv) + if mcfg==nil { + logger.Error("GetMoneyBoxCfg failed err=%v", err) + resp.Message = "ERROR_GETCONFIG_FAILED" + resp.Code = ERROR_GETCONFIG_FAILED + break + } + + if rdata.Goldnum < mcfg.Min || rdata.Goldnum > mcfg.Max { + logger.Error("GetMoneyBoxCfg failed err=%v", err) + resp.Message = "ERROR_DRAWGUAN_FAILED" + resp.Code = ERROR_DRAWGUAN_FAILED + break + } + + uinfo.GuanGold = uinfo.GuanGold - rdata.Goldnum + uinfo.RealGold += rdata.Goldnum + + //todo 此处需要调佣SDK接口通知金币变化 + + + resp.Guangold = uinfo.GuanGold + resp.Wallgold = uinfo.RealGold + + SaveUserInfo(uinfo) + + resp.Code = ERROR_OK + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) + +} + + +func HandlerGetguangold(w http.ResponseWriter, data string, uuid int) { + SetHeader(w) + var resp GetguangoldResp + resp.Code = 0 + var rdata GetguangoldReq + err := json.Unmarshal([]byte(data), &rdata) + for { + if err != nil { + logger.Info("json decode HandlerGetguangold data failed:%v,for:%v", err, data) + resp.Message = "json unmarshal failed" + resp.Code = ERROR_JSONUNMASH_ERROR + break + } + + uinfo,err := GetUserInfo(uuid) + if err != nil || uinfo == nil{ + logger.Error("redis failed err=%v", err) + resp.Message = "ERROR_SRV_ERROR" + resp.Code = ERROR_SRV_ERROR + break + } + + + mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv) + if mcfg==nil { + logger.Error("GetMoneyBoxCfg failed err=%v", err) + resp.Message = "ERROR_GETCONFIG_FAILED" + resp.Code = ERROR_GETCONFIG_FAILED + break + } + + uinfo.GuanGold += rdata.Goldnum + if uinfo.GuanGold >= mcfg.Max { + uinfo.GuanGold = mcfg.Max + } + + resp.Guangold = uinfo.GuanGold + + SaveUserInfo(uinfo) + + resp.Code = ERROR_OK + break + } + + //回包 + respstr, _ := json.Marshal(&resp) + fmt.Fprint(w, string(respstr)) + +} + func HandlerLogin(w http.ResponseWriter, data string, uuid int) { SetHeader(w) var resp UserLoginResp @@ -21,6 +231,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { logger.Info("json decode HandlerLogin data failed:%v,for:%v", err, data) resp.Message = "json unmarshal failed" resp.Code = ERROR_JSONUNMASH_ERROR + break } //先判断一下是否是新用户 @@ -29,6 +240,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { logger.Error("redis failed err=%v", err) resp.Message = "ERROR_SRV_ERROR" resp.Code = ERROR_SRV_ERROR + break } if !isexist { @@ -44,7 +256,11 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { logger.Error("redis failed err=%v", err) resp.Message = "ERROR_SRV_ERROR" resp.Code = ERROR_SRV_ERROR + break } + + resp.Code = ERROR_OK + break } //回包 -- libgit2 0.21.0