Commit 94b2940a88e6f0deab7412397dc8f924fb80a84e

Authored by 陆恒
1 parent bfc50df5
Exists in master

提交接口

src/HttpServer/jsonconf/jsonconf.go
@@ -17,14 +17,14 @@ type SignConfig struct { @@ -17,14 +17,14 @@ type SignConfig struct {
17 } 17 }
18 18
19 type GoldGunsConfig struct { 19 type GoldGunsConfig struct {
20 - Level int `json:"level"`  
21 - Speed int `json:"speed"` 20 + Level int `json:"level"`
  21 + Speed int64 `json:"speed"`
22 } 22 }
23 23
24 type GoldChestConfig struct { 24 type GoldChestConfig struct {
25 - Boxid int `json:"boxid"`  
26 - Score int `json:"score"`  
27 - Reward int `json:"reward"` 25 + Boxid int `json:"boxid"`
  26 + Score int64 `json:"score"`
  27 + Reward int `json:"reward"`
28 } 28 }
29 29
30 type GameConfig struct { 30 type GameConfig struct {
@@ -63,9 +63,13 @@ func GetGoldGunsConfig(level int) *GoldGunsConfig { @@ -63,9 +63,13 @@ func GetGoldGunsConfig(level int) *GoldGunsConfig {
63 63
64 func GetGoldChestConfig(boxid int) *GoldChestConfig { 64 func GetGoldChestConfig(boxid int) *GoldChestConfig {
65 65
  66 + finalid := boxid
  67 + if finalid > len(g_jsonconf.BoxConfig) {
  68 + finalid = g_jsonconf.BoxConfig[len(g_jsonconf.BoxConfig)-1].Boxid
  69 + }
66 var rt *GoldChestConfig 70 var rt *GoldChestConfig
67 for _, val := range g_jsonconf.BoxConfig { 71 for _, val := range g_jsonconf.BoxConfig {
68 - if val.Boxid == boxid { 72 + if val.Boxid == finalid {
69 rt = &val 73 rt = &val
70 break 74 break
71 } 75 }
src/HttpServer/logic/constdef.go
1 package logic 1 package logic
  2 +
  3 +const (
  4 + DAILY_FETCH_CNT = 5 //每日领取宝箱次数
  5 +)
src/HttpServer/logic/datadef.go
1 package logic 1 package logic
  2 +
  3 +//
  4 +
  5 +type GetcurpropertyReq struct {
  6 + Openid string `json:"openid"`
  7 +}
  8 +
  9 +type GetcurpropertyData struct {
  10 + Protery int64 `json:"protery"`
  11 + Speed int64 `json:"speed"`
  12 + Curlevle int64 `json:"curlevle"`
  13 + Leftcnt int `json:"leftcnt"`
  14 +}
  15 +
  16 +type GetcurpropertyResp struct {
  17 + ErrNum int `json:"errNum"`
  18 + RetMsg string `json:"retMsg"`
  19 + RetData GetcurpropertyData `json:"retData"`
  20 +}
  21 +
  22 +//------------------------------------------------------------------------------------------------------
  23 +
  24 +type UserData struct {
  25 + Openid string
  26 + LastGetTime int //上一次请求物资时间戳,如果超过三次时间间隔则不计算收益
  27 + LeftCnt int //当天剩余领取宝箱次数
  28 + MaxLevel int //当前最高枪等级
  29 + Property int64 //当前物资
  30 + TotalFetchCnt int //累计领取次数
  31 + SignRound int //签到轮数
  32 + SingDay int //签到天数
  33 + IsSign int //当天是否已经签到
  34 + TodaySec int //当天零点时间戳,用于判断跨天
  35 +}
src/HttpServer/logic/errordef.go
1 package logic 1 package logic
2 2
3 const ( 3 const (
4 - ERROR_OK = 0 //没有错误 4 + ERROR_OK = 0 //没有错误
  5 + ERROR_UNMASH_JSONFAILED = 1 //json解析失败
  6 + ERROR_SERVER_FAILED = 2 //j服务器错误
5 7
6 ) 8 )
src/HttpServer/logic/function.go
1 package logic 1 package logic
2 2
3 -import "net/http" 3 +import (
  4 + "HttpServer/jsonconf"
  5 + "HttpServer/redishandler"
  6 + "common/logger"
  7 + "common/redis"
  8 + "encoding/json"
  9 + "net/http"
  10 + "time"
  11 +)
4 12
5 func SetHeader(w http.ResponseWriter) { 13 func SetHeader(w http.ResponseWriter) {
6 w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域 14 w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
7 w.Header().Set("Content-Type", "application/json") 15 w.Header().Set("Content-Type", "application/json")
8 w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid") 16 w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Uuid")
9 } 17 }
  18 +
  19 +func SaveUserInfo(data *UserData) error {
  20 +
  21 + savestr, err := json.Marshal(data)
  22 + if err != nil {
  23 + return err
  24 + }
  25 +
  26 + err = redishandler.GetRedisClient().HSet(redis.USER_DATA_KEY, data.Openid, string(savestr))
  27 +
  28 + return err
  29 +}
  30 +
  31 +func GetUserInfo(openid string) (*UserData, error) {
  32 +
  33 + //先判断用户是否存在 不存在创建新的用户
  34 + isexist, err := redishandler.GetRedisClient().HExists(redis.USER_DATA_KEY, openid)
  35 + if err != nil {
  36 + logger.Error("redis failed err=%v", err)
  37 + return nil, err
  38 + }
  39 +
  40 + tmp := new(UserData)
  41 + if !isexist {
  42 + //不存在
  43 + //属于新登录的玩家数据
  44 + err = tmp.InitUserInfo(openid)
  45 + } else {
  46 + //已经登陆过了 需要获取玩家数据
  47 + data, err := redishandler.GetRedisClient().HGet(redis.USER_DATA_KEY, openid)
  48 + if err != nil {
  49 + return nil, err
  50 + }
  51 +
  52 + err = json.Unmarshal([]byte(data), tmp)
  53 + if err != nil {
  54 + return nil, err
  55 + }
  56 + }
  57 +
  58 + return tmp, nil
  59 +}
  60 +
  61 +func (u *UserData) InitUserInfo(openid string) error {
  62 + nowtime := time.Now()
  63 + u.Openid = openid
  64 + u.SignRound = 1
  65 + u.IsSign = 0
  66 + u.LastGetTime = 0
  67 + u.LeftCnt = DAILY_FETCH_CNT
  68 + u.MaxLevel = 1
  69 + u.Property = 0
  70 + u.SingDay = 0
  71 + u.TodaySec = int(time.Date(nowtime.Year(), nowtime.Month(), nowtime.Day(), 0, 0, 0, 0, nowtime.Location()).Unix())
  72 + u.TotalFetchCnt = 0 //用于计算当前领取次数
  73 +
  74 + err := SaveUserInfo(u)
  75 + if err != nil {
  76 + logger.Error("InitUserInfo err=%v", err)
  77 + }
  78 + return err
  79 +}
  80 +
  81 +//计算当前领取宝箱需要的数值
  82 +func (u *UserData) CalcCurLe() int64 {
  83 + cfg := jsonconf.GetGoldChestConfig(u.TotalFetchCnt + 1)
  84 + if cfg == nil {
  85 + logger.Error("CalcCurLe failed u=%v", u)
  86 + return 0
  87 + }
  88 +
  89 + return cfg.Score
  90 +}
  91 +
  92 +//计算速率
  93 +func (u *UserData) CalcSpeed() int64 {
  94 + cfg := jsonconf.GetGoldGunsConfig(u.MaxLevel)
  95 + if cfg == nil {
  96 + logger.Error("CalcSpeed failed u=%v", u)
  97 + return 0
  98 + }
  99 + return cfg.Speed
  100 +}
  101 +
  102 +//计算物资
  103 +func (u *UserData) CalcProperty() {
  104 + nowtime := int(time.Now().Unix())
  105 + if nowtime < u.LastGetTime {
  106 + logger.Error("CalcProperty err n=%v,l=%v", nowtime, u.LastGetTime)
  107 + } else if nowtime >= u.LastGetTime+60*3 {
  108 + //超过三次请求了 不再计算离线收益
  109 +
  110 + } else {
  111 + //计算一下离线收益
  112 + mult := nowtime - u.LastGetTime
  113 + speed := u.CalcSpeed()
  114 + u.Property += int64(mult) * speed
  115 + }
  116 + u.LastGetTime = nowtime
  117 +
  118 + //判断跨天
  119 + if nowtime < u.TodaySec {
  120 + logger.Error("CalcProperty err2")
  121 + nowtt := time.Now()
  122 + u.TodaySec = int(time.Date(nowtt.Year(), nowtt.Month(), nowtt.Day(), 0, 0, 0, 0, nowtt.Location()).Unix())
  123 + } else {
  124 + if nowtime > u.TodaySec+86400 {
  125 + //跨天了
  126 + nowtt := time.Now()
  127 + u.TodaySec = int(time.Date(nowtt.Year(), nowtt.Month(), nowtt.Day(), 0, 0, 0, 0, nowtt.Location()).Unix())
  128 + u.LeftCnt = DAILY_FETCH_CNT
  129 + u.IsSign = 0
  130 + if u.SingDay == 7 {
  131 + u.SingDay = 0
  132 + u.SignRound++
  133 + }
  134 + }
  135 + }
  136 + //保存
  137 + err := SaveUserInfo(u)
  138 + if err != nil {
  139 + logger.Error("CalcProperty err=%v", err)
  140 + }
  141 +}
src/HttpServer/logic/httpserver.go
@@ -43,7 +43,7 @@ func startServerHttpServe() { @@ -43,7 +43,7 @@ func startServerHttpServe() {
43 http.HandleFunc("/eliminatestar/test", Testapi) //测试接口 43 http.HandleFunc("/eliminatestar/test", Testapi) //测试接口
44 http.HandleFunc("/eliminatestar/clear", ClearData) //清除账号 44 http.HandleFunc("/eliminatestar/clear", ClearData) //清除账号
45 //---------------------------------------------------------------------------------------- 45 //----------------------------------------------------------------------------------------
46 - 46 + http.HandleFunc("/daycs/getcurproperty", Getcurproperty) //请求当前物资
47 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil) 47 err := http.ListenAndServe(conf.GetServerHttpAddrConf(), nil)
48 CheckErr(err) 48 CheckErr(err)
49 } 49 }
@@ -67,9 +67,6 @@ func ClearData(w http.ResponseWriter, r *http.Request) { @@ -67,9 +67,6 @@ func ClearData(w http.ResponseWriter, r *http.Request) {
67 logger.Info("ClearData , body:%v,uuid=%v", s, Uuid) 67 logger.Info("ClearData , body:%v,uuid=%v", s, Uuid)
68 68
69 redishandler.GetRedisClient().HDel(redis.USER_DATA_KEY, strconv.Itoa(Uuid)) 69 redishandler.GetRedisClient().HDel(redis.USER_DATA_KEY, strconv.Itoa(Uuid))
70 - redishandler.GetRedisClient().HDel(redis.USER_TASKINFO_LIST, strconv.Itoa(Uuid))  
71 - redishandler.GetRedisClient().HDel(redis.USER_ACHIEVEMENTINFO_LIST, strconv.Itoa(Uuid))  
72 - redishandler.GetRedisClient().HDel(redis.USER_WITHDRAW_RECORDLIST, strconv.Itoa(Uuid))  
73 70
74 fmt.Fprint(w, "success!") 71 fmt.Fprint(w, "success!")
75 } 72 }
@@ -79,6 +76,16 @@ func Testapi(w http.ResponseWriter, r *http.Request) { @@ -79,6 +76,16 @@ func Testapi(w http.ResponseWriter, r *http.Request) {
79 fmt.Fprint(w, "success") 76 fmt.Fprint(w, "success")
80 } 77 }
81 78
  79 +func Getcurproperty(w http.ResponseWriter, r *http.Request) {
  80 + result, _ := ioutil.ReadAll(r.Body)
  81 + r.Body.Close()
  82 +
  83 + s := string(result)
  84 + logger.Info("Getcurproperty , body:%v", s)
  85 +
  86 + HandlerGetcurproperty(w, s)
  87 +}
  88 +
82 /*func Getnewlevelreward(w http.ResponseWriter, r *http.Request) { 89 /*func Getnewlevelreward(w http.ResponseWriter, r *http.Request) {
83 90
84 Uuid := 0 91 Uuid := 0
src/HttpServer/logic/logic.go
1 package logic 1 package logic
  2 +
  3 +import (
  4 + "common/logger"
  5 + "encoding/json"
  6 + "fmt"
  7 + "net/http"
  8 +)
  9 +
  10 +func HandlerGetcurproperty(w http.ResponseWriter, data string) {
  11 + SetHeader(w)
  12 + var resp GetcurpropertyResp
  13 + resp.ErrNum = 0
  14 + var rdata GetcurpropertyReq
  15 + err := json.Unmarshal([]byte(data), &rdata)
  16 + for {
  17 + if err != nil {
  18 + logger.Info("json decode HandlerGetcurproperty data failed:%v,for:%v", err, data)
  19 + resp.RetMsg = "json解析失败"
  20 + resp.ErrNum = ERROR_UNMASH_JSONFAILED
  21 + break
  22 + }
  23 +
  24 + uinfo, err := GetUserInfo(rdata.Openid)
  25 + if err != nil || uinfo == nil {
  26 + logger.Info(" HandlerGetcurproperty getdata failed:%v,for:%v", err, data)
  27 + resp.RetMsg = "服务器读取数据失败"
  28 + resp.ErrNum = ERROR_SERVER_FAILED
  29 + break
  30 + }
  31 +
  32 + //需要处理一下物资计算
  33 + uinfo.CalcProperty()
  34 +
  35 + //返回
  36 + resp.RetData.Leftcnt = uinfo.LeftCnt
  37 + resp.RetData.Curlevle = uinfo.CalcCurLe()
  38 + resp.RetData.Protery = uinfo.Property
  39 + resp.RetData.Speed = uinfo.CalcSpeed()
  40 +
  41 + break
  42 + }
  43 + //回包
  44 + respstr, _ := json.Marshal(&resp)
  45 + fmt.Fprint(w, string(respstr))
  46 +}
src/common/redis/def.go
1 package redis 1 package redis
2 2
3 const ( 3 const (
4 - USER_DATA_KEY = "STARSTAR_USER_DATA_KEY" //玩家数据  
5 - USER_WITHDRAW_RECORDLIST = "STARSTAR_USER_WITHDRAW_RECORDLIST" //玩家提现记录  
6 - USER_TASKINFO_LIST = "STARSTAR_USER_TASKINFO_LIST" //任务列表数据缓存  
7 - USER_ACHIEVEMENTINFO_LIST = "STARSTAR_USER_ACHIEVEMENTINFO_LIST" //成就列表数据缓存 4 + USER_DATA_KEY = "DAYCS_USER_DATA_KEY" //玩家数据
8 5
9 ) 6 )