Commit d032a23da7bbcc2fa9ebb8da0b63955b1e6c75fd

Authored by 陆恒
1 parent a8930016

存钱罐相关接口

src/HttpServer/jsonconf/jsonconf.go
@@ -64,6 +64,19 @@ func GetJsonConf() *GameConfig { @@ -64,6 +64,19 @@ func GetJsonConf() *GameConfig {
64 } 64 }
65 65
66 66
  67 +func GetMoneyBoxCfg(lv int) *MoneyBoxLevelDesc {
  68 + rt := new(MoneyBoxLevelDesc)
  69 + rt = nil
  70 + for _,val := range g_jsonconf.MoneyBoxConfig {
  71 + if val.Level == lv {
  72 + rt = &val
  73 + break
  74 + }
  75 + }
  76 +
  77 + return rt
  78 +}
  79 +
67 func file_get_contents(path string) ([]byte, error) { 80 func file_get_contents(path string) ([]byte, error) {
68 f, err := os.Open(path) 81 f, err := os.Open(path)
69 if err != nil { 82 if err != nil {
src/HttpServer/logic/datadef.go
@@ -8,12 +8,53 @@ type UserLoginReq struct { @@ -8,12 +8,53 @@ type UserLoginReq struct {
8 type UserLoginResp struct { 8 type UserLoginResp struct {
9 Code int `json:"code"` 9 Code int `json:"code"`
10 Message string `json:"message"` 10 Message string `json:"message"`
11 - Loginday int `json:"loginday"` 11 + Walletgold int `json:"walletgold"`
12 Leftads int `json:"leftads"` 12 Leftads int `json:"leftads"`
13 Guangold int `json:"guangold"` 13 Guangold int `json:"guangold"`
14 14
15 } 15 }
16 16
  17 +
  18 +type WatchadsResp struct {
  19 + Code int `json:"code"`
  20 + Message string `json:"message"`
  21 + Leftads int `json:"leftads"`
  22 +
  23 +}
  24 +
  25 +type QueryguaninfoResp struct {
  26 + Code int `json:"code"`
  27 + Message string `json:"message"`
  28 + Leftcnt int `json:"leftcnt"`
  29 + Totalcnt int `json:"totalcnt"`
  30 + Guangold int `json:"guangold"`
  31 + Minget int `json:"minget"`
  32 + Maxget int `json:"maxget"`
  33 + Loginday int `json:"loginday"`
  34 +}
  35 +
  36 +
  37 +type GetguangoldReq struct {
  38 + Goldnum int `json:"goldnum"`
  39 +}
  40 +
  41 +type GetguangoldResp struct {
  42 + Code int `json:"code"`
  43 + Message string `json:"message"`
  44 + Guangold int `json:"guangold"`
  45 +}
  46 +
  47 +type DrawguangoldReq struct {
  48 + Goldnum int `json:"goldnum"`
  49 +}
  50 +
  51 +type DrawguangoldResp struct {
  52 + Code int `json:"code"`
  53 + Message string `json:"message"`
  54 + Guangold int `json:"guangold"`
  55 + Wallgold int `json:"wallgold"`
  56 +}
  57 +
17 //********************************************************************************************************** 58 //**********************************************************************************************************
18 59
19 type TaskInfo struct { 60 type TaskInfo struct {
src/HttpServer/logic/errordef.go
@@ -5,4 +5,8 @@ const ( @@ -5,4 +5,8 @@ const (
5 ERROR_OK =0 //没有错误 5 ERROR_OK =0 //没有错误
6 ERROR_SRV_ERROR =1 //服务器错误 6 ERROR_SRV_ERROR =1 //服务器错误
7 ERROR_JSONUNMASH_ERROR =2 //json解析错误 7 ERROR_JSONUNMASH_ERROR =2 //json解析错误
  8 + ERROR_WATCHADS_ZEOR =3 //看视频次数已经耗尽
  9 + ERROR_GETCONFIG_FAILED =4 //获取配置失败
  10 + ERROR_GUANGOLD_NOTENOUGH =5 //存钱罐金币不足
  11 + ERROR_DRAWGUAN_FAILED =6 //从存钱罐提取金币不满足限制
8 ) 12 )
9 \ No newline at end of file 13 \ No newline at end of file
src/HttpServer/logic/function.go
@@ -57,13 +57,17 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) error{ @@ -57,13 +57,17 @@ func InitUserInfo(data *UserLoginReq, resp *UserLoginResp, uuid int) error{
57 57
58 resp.Guangold = initdata.GuanGold 58 resp.Guangold = initdata.GuanGold
59 resp.Leftads = initdata.WatchAddsTime 59 resp.Leftads = initdata.WatchAddsTime
60 - resp.Loginday = initdata.ContinueLoginDay 60 + resp.Walletgold = initdata.RealGold
61 61
62 err := SaveUserInfo(&initdata) 62 err := SaveUserInfo(&initdata)
63 63
64 return err 64 return err
65 } 65 }
66 66
  67 +//计算当日可提取次数
  68 +func (u *UserData) CalcTotalCnt() int {
  69 + return 0
  70 +}
67 71
68 func (u *UserData) HandlePassDay() { 72 func (u *UserData) HandlePassDay() {
69 isdiffday := false 73 isdiffday := false
@@ -119,7 +123,7 @@ func GetUserData(uuid int, resp *UserLoginResp) error{ @@ -119,7 +123,7 @@ func GetUserData(uuid int, resp *UserLoginResp) error{
119 //此处要处理一下跨天逻辑 123 //此处要处理一下跨天逻辑
120 data.HandlePassDay() 124 data.HandlePassDay()
121 125
122 - resp.Loginday = data.ContinueLoginDay 126 + resp.Walletgold = data.RealGold
123 resp.Leftads = data.WatchAddsTime 127 resp.Leftads = data.WatchAddsTime
124 resp.Guangold = data.GuanGold 128 resp.Guangold = data.GuanGold
125 129
src/HttpServer/logic/httpserver.go
@@ -38,12 +38,102 @@ func startServerHttpServe() { @@ -38,12 +38,102 @@ func startServerHttpServe() {
38 //------------------------------------------------------------- 38 //-------------------------------------------------------------
39 39
40 40
41 - http.HandleFunc("/elimatestar/login", UserLogin) //登录 41 + http.HandleFunc("/eliminatestar/login", UserLogin) //登录
  42 + http.HandleFunc("/eliminatestar/watchads", Watchads) //观看激励视频
  43 + http.HandleFunc("/eliminatestar/queryguaninfo", Queryguaninfo) //观看激励视频
  44 + http.HandleFunc("/eliminatestar/getguangold", Getguangold) //获取金币到存钱罐
  45 + http.HandleFunc("/eliminatestar/drawguangold", Drawguangold) //提取存钱罐的金币到个人钱包
42 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) 46 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil)
43 CheckErr(err) 47 CheckErr(err)
44 } 48 }
45 49
46 50
  51 +func Drawguangold(w http.ResponseWriter, r *http.Request) {
  52 +
  53 + Uuid := 0
  54 + if len(r.Header) > 0 {
  55 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  56 + }
  57 +
  58 + if Uuid == 0 {
  59 + SetHeader(w)
  60 + logger.Error("Uuid is nil!")
  61 + return
  62 + }
  63 + result, _ := ioutil.ReadAll(r.Body)
  64 + r.Body.Close()
  65 +
  66 + s := string(result)
  67 + logger.Info("Drawguangold , body:%v,uuid=%v", s, Uuid)
  68 +
  69 + HandlerDrawguangold(w, s, Uuid)
  70 +}
  71 +
  72 +
  73 +func Getguangold(w http.ResponseWriter, r *http.Request) {
  74 +
  75 + Uuid := 0
  76 + if len(r.Header) > 0 {
  77 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  78 + }
  79 +
  80 + if Uuid == 0 {
  81 + SetHeader(w)
  82 + logger.Error("Uuid is nil!")
  83 + return
  84 + }
  85 + result, _ := ioutil.ReadAll(r.Body)
  86 + r.Body.Close()
  87 +
  88 + s := string(result)
  89 + logger.Info("Getguangold , body:%v,uuid=%v", s, Uuid)
  90 +
  91 + HandlerGetguangold(w, s, Uuid)
  92 +}
  93 +
  94 +func Queryguaninfo(w http.ResponseWriter, r *http.Request) {
  95 +
  96 + Uuid := 0
  97 + if len(r.Header) > 0 {
  98 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  99 + }
  100 +
  101 + if Uuid == 0 {
  102 + SetHeader(w)
  103 + logger.Error("Uuid is nil!")
  104 + return
  105 + }
  106 + result, _ := ioutil.ReadAll(r.Body)
  107 + r.Body.Close()
  108 +
  109 + s := string(result)
  110 + logger.Info("Queryguaninfo , body:%v,uuid=%v", s, Uuid)
  111 +
  112 + HandlerQueryguaninfo(w, s, Uuid)
  113 +}
  114 +
  115 +
  116 +func Watchads(w http.ResponseWriter, r *http.Request) {
  117 +
  118 + Uuid := 0
  119 + if len(r.Header) > 0 {
  120 + Uuid, _ = strconv.Atoi(r.Header.Get("Uuid"))
  121 + }
  122 +
  123 + if Uuid == 0 {
  124 + SetHeader(w)
  125 + logger.Error("Uuid is nil!")
  126 + return
  127 + }
  128 + result, _ := ioutil.ReadAll(r.Body)
  129 + r.Body.Close()
  130 +
  131 + s := string(result)
  132 + logger.Info("Watchads , body:%v,uuid=%v", s, Uuid)
  133 +
  134 + HandlerWatchads(w, s, Uuid)
  135 +}
  136 +
47 func UserLogin(w http.ResponseWriter, r *http.Request) { 137 func UserLogin(w http.ResponseWriter, r *http.Request) {
48 138
49 Uuid := 0 139 Uuid := 0
@@ -65,3 +155,5 @@ func UserLogin(w http.ResponseWriter, r *http.Request) { @@ -65,3 +155,5 @@ func UserLogin(w http.ResponseWriter, r *http.Request) {
65 HandlerLogin(w, s, Uuid) 155 HandlerLogin(w, s, Uuid)
66 } 156 }
67 157
  158 +
  159 +
src/HttpServer/logic/logic.go
1 package logic 1 package logic
2 2
3 import ( 3 import (
  4 + "HttpServer/jsonconf"
4 "HttpServer/redishandler" 5 "HttpServer/redishandler"
5 "common/logger" 6 "common/logger"
6 "common/redis" 7 "common/redis"
@@ -10,6 +11,215 @@ import ( @@ -10,6 +11,215 @@ import (
10 "strconv" 11 "strconv"
11 ) 12 )
12 13
  14 +
  15 +func HandlerQueryguaninfo(w http.ResponseWriter, data string, uuid int) {
  16 + SetHeader(w)
  17 + var resp QueryguaninfoResp
  18 + resp.Code = 0
  19 + for {
  20 + uinfo,err := GetUserInfo(uuid)
  21 + if err != nil || uinfo == nil{
  22 + logger.Error("redis failed err=%v", err)
  23 + resp.Message = "ERROR_SRV_ERROR"
  24 + resp.Code = ERROR_SRV_ERROR
  25 + break
  26 + }
  27 +
  28 + //获取存钱罐等级配置
  29 + mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv)
  30 + if mcfg==nil {
  31 + logger.Error("GetMoneyBoxCfg failed err=%v", err)
  32 + resp.Message = "ERROR_GETCONFIG_FAILED"
  33 + resp.Code = ERROR_GETCONFIG_FAILED
  34 + break
  35 + }
  36 +
  37 +
  38 + resp.Guangold = uinfo.GuanGold
  39 + resp.Loginday = uinfo.ContinueLoginDay
  40 + resp.Totalcnt = uinfo.CalcTotalCnt()
  41 + resp.Leftcnt = resp.Totalcnt - uinfo.GetFromGuanCnt
  42 + resp.Maxget = mcfg.Max
  43 + resp.Minget = mcfg.Min
  44 + resp.Code = ERROR_OK
  45 + break
  46 +
  47 + }
  48 +
  49 + //回包
  50 + respstr, _ := json.Marshal(&resp)
  51 + fmt.Fprint(w, string(respstr))
  52 +}
  53 +
  54 +func HandlerWatchads(w http.ResponseWriter, data string, uuid int) {
  55 + SetHeader(w)
  56 + var resp WatchadsResp
  57 + resp.Code = 0
  58 +
  59 + for {
  60 + uinfo,err := GetUserInfo(uuid)
  61 + if err != nil || uinfo == nil{
  62 + logger.Error("redis failed err=%v", err)
  63 + resp.Message = "ERROR_SRV_ERROR"
  64 + resp.Code = ERROR_SRV_ERROR
  65 + break
  66 + }
  67 +
  68 + if uinfo.WatchAddsTime == 0 {
  69 + logger.Error("WatchAddsTime zero")
  70 + resp.Message = "WatchAddsTime zero"
  71 + resp.Code = ERROR_WATCHADS_ZEOR
  72 + break
  73 + }
  74 +
  75 + uinfo.WatchAddsTime --
  76 + err = SaveUserInfo(uinfo)
  77 + if err != nil {
  78 + logger.Error("redis failed err=%v", err)
  79 + resp.Message = "ERROR_SRV_ERROR"
  80 + resp.Code = ERROR_SRV_ERROR
  81 + break
  82 + }
  83 +
  84 + resp.Code = ERROR_OK
  85 + break
  86 +
  87 + }
  88 +
  89 + //回包
  90 + respstr, _ := json.Marshal(&resp)
  91 + fmt.Fprint(w, string(respstr))
  92 +}
  93 +
  94 +
  95 +
  96 +func HandlerDrawguangold(w http.ResponseWriter, data string, uuid int) {
  97 + SetHeader(w)
  98 + var resp DrawguangoldResp
  99 + resp.Code = 0
  100 + var rdata DrawguangoldReq
  101 + err := json.Unmarshal([]byte(data), &rdata)
  102 + for {
  103 + if err != nil {
  104 + logger.Info("json decode HandlerDrawguangold data failed:%v,for:%v", err, data)
  105 + resp.Message = "json unmarshal failed"
  106 + resp.Code = ERROR_JSONUNMASH_ERROR
  107 + break
  108 + }
  109 +
  110 + uinfo,err := GetUserInfo(uuid)
  111 + if err != nil || uinfo == nil{
  112 + logger.Error("redis failed err=%v", err)
  113 + resp.Message = "ERROR_SRV_ERROR"
  114 + resp.Code = ERROR_SRV_ERROR
  115 + break
  116 + }
  117 + //首先判断一下存钱罐的金币是否足够
  118 + if rdata.Goldnum < uinfo.GuanGold {
  119 + logger.Error("guangold not enough ")
  120 + resp.Message = "ERROR_SRV_ERROR"
  121 + resp.Code = ERROR_GUANGOLD_NOTENOUGH
  122 + break
  123 + }
  124 +
  125 + //判断一下是否满足提取条件
  126 + totalcnt := uinfo.CalcTotalCnt()
  127 + if totalcnt <= uinfo.GetFromGuanCnt {
  128 + logger.Error("guangold not enough ")
  129 + resp.Message = "ERROR_SRV_ERROR"
  130 + resp.Code = ERROR_GUANGOLD_NOTENOUGH
  131 + break
  132 + }
  133 +
  134 + uinfo.GetFromGuanCnt ++
  135 + //
  136 + mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv)
  137 + if mcfg==nil {
  138 + logger.Error("GetMoneyBoxCfg failed err=%v", err)
  139 + resp.Message = "ERROR_GETCONFIG_FAILED"
  140 + resp.Code = ERROR_GETCONFIG_FAILED
  141 + break
  142 + }
  143 +
  144 + if rdata.Goldnum < mcfg.Min || rdata.Goldnum > mcfg.Max {
  145 + logger.Error("GetMoneyBoxCfg failed err=%v", err)
  146 + resp.Message = "ERROR_DRAWGUAN_FAILED"
  147 + resp.Code = ERROR_DRAWGUAN_FAILED
  148 + break
  149 + }
  150 +
  151 + uinfo.GuanGold = uinfo.GuanGold - rdata.Goldnum
  152 + uinfo.RealGold += rdata.Goldnum
  153 +
  154 + //todo 此处需要调佣SDK接口通知金币变化
  155 +
  156 +
  157 + resp.Guangold = uinfo.GuanGold
  158 + resp.Wallgold = uinfo.RealGold
  159 +
  160 + SaveUserInfo(uinfo)
  161 +
  162 + resp.Code = ERROR_OK
  163 + break
  164 + }
  165 +
  166 + //回包
  167 + respstr, _ := json.Marshal(&resp)
  168 + fmt.Fprint(w, string(respstr))
  169 +
  170 +}
  171 +
  172 +
  173 +func HandlerGetguangold(w http.ResponseWriter, data string, uuid int) {
  174 + SetHeader(w)
  175 + var resp GetguangoldResp
  176 + resp.Code = 0
  177 + var rdata GetguangoldReq
  178 + err := json.Unmarshal([]byte(data), &rdata)
  179 + for {
  180 + if err != nil {
  181 + logger.Info("json decode HandlerGetguangold data failed:%v,for:%v", err, data)
  182 + resp.Message = "json unmarshal failed"
  183 + resp.Code = ERROR_JSONUNMASH_ERROR
  184 + break
  185 + }
  186 +
  187 + uinfo,err := GetUserInfo(uuid)
  188 + if err != nil || uinfo == nil{
  189 + logger.Error("redis failed err=%v", err)
  190 + resp.Message = "ERROR_SRV_ERROR"
  191 + resp.Code = ERROR_SRV_ERROR
  192 + break
  193 + }
  194 +
  195 +
  196 + mcfg := jsonconf.GetMoneyBoxCfg(uinfo.Lv)
  197 + if mcfg==nil {
  198 + logger.Error("GetMoneyBoxCfg failed err=%v", err)
  199 + resp.Message = "ERROR_GETCONFIG_FAILED"
  200 + resp.Code = ERROR_GETCONFIG_FAILED
  201 + break
  202 + }
  203 +
  204 + uinfo.GuanGold += rdata.Goldnum
  205 + if uinfo.GuanGold >= mcfg.Max {
  206 + uinfo.GuanGold = mcfg.Max
  207 + }
  208 +
  209 + resp.Guangold = uinfo.GuanGold
  210 +
  211 + SaveUserInfo(uinfo)
  212 +
  213 + resp.Code = ERROR_OK
  214 + break
  215 + }
  216 +
  217 + //回包
  218 + respstr, _ := json.Marshal(&resp)
  219 + fmt.Fprint(w, string(respstr))
  220 +
  221 +}
  222 +
13 func HandlerLogin(w http.ResponseWriter, data string, uuid int) { 223 func HandlerLogin(w http.ResponseWriter, data string, uuid int) {
14 SetHeader(w) 224 SetHeader(w)
15 var resp UserLoginResp 225 var resp UserLoginResp
@@ -21,6 +231,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { @@ -21,6 +231,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) {
21 logger.Info("json decode HandlerLogin data failed:%v,for:%v", err, data) 231 logger.Info("json decode HandlerLogin data failed:%v,for:%v", err, data)
22 resp.Message = "json unmarshal failed" 232 resp.Message = "json unmarshal failed"
23 resp.Code = ERROR_JSONUNMASH_ERROR 233 resp.Code = ERROR_JSONUNMASH_ERROR
  234 + break
24 } 235 }
25 236
26 //先判断一下是否是新用户 237 //先判断一下是否是新用户
@@ -29,6 +240,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { @@ -29,6 +240,7 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) {
29 logger.Error("redis failed err=%v", err) 240 logger.Error("redis failed err=%v", err)
30 resp.Message = "ERROR_SRV_ERROR" 241 resp.Message = "ERROR_SRV_ERROR"
31 resp.Code = ERROR_SRV_ERROR 242 resp.Code = ERROR_SRV_ERROR
  243 + break
32 } 244 }
33 245
34 if !isexist { 246 if !isexist {
@@ -44,7 +256,11 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { @@ -44,7 +256,11 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) {
44 logger.Error("redis failed err=%v", err) 256 logger.Error("redis failed err=%v", err)
45 resp.Message = "ERROR_SRV_ERROR" 257 resp.Message = "ERROR_SRV_ERROR"
46 resp.Code = ERROR_SRV_ERROR 258 resp.Code = ERROR_SRV_ERROR
  259 + break
47 } 260 }
  261 +
  262 + resp.Code = ERROR_OK
  263 + break
48 } 264 }
49 265
50 //回包 266 //回包