Commit f259cd564015a8f315e6288f06c97d20a215a419

Authored by 王家文
1 parent f0922e3f
Exists in master and in 1 other branch dev-wjw

feat✨:游戏功能配置

configs/confapi/config.go
... ... @@ -11,6 +11,14 @@ type ApiGameConfig struct {
11 11 Raw *Raw
12 12 }
13 13  
  14 +func (c *ApiGameConfig) GetUid() string {
  15 + return c.Raw.GameId
  16 +}
  17 +
  18 +func (c *ApiGameConfig) CheckCurrent() bool {
  19 + return true
  20 +}
  21 +
14 22 func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo {
15 23 tableName := "s_game_config"
16 24 cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix)
... ...
configs/confbase/index.go
1 1 package confbase
2 2  
3 3 import (
4   - "apigame/service-common/svmysql"
5 4 "apigame/util/util-lx/lxalilog"
6   - "apigame/util/util-lx/lxtime"
7 5 "apigame/util/zjson"
8 6 "apigame/util/zredis"
9 7 "fmt"
... ... @@ -53,11 +51,10 @@ func LoadData[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1)
53 51 return
54 52 }
55 53  
56   -func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) {
57   - info := obj.MysqlInfo(gameId)
  54 +func FindDuringTime[T1 IConfData, T2 IConfRawData](obj T1, raw T2, gameId string) (has bool) {
  55 + info := obj.ConfInfo(gameId)
58 56 db := info.DbMysql
59   - timeNow := lxtime.NowUninx()
60   - result := db.Table(info.TableName).Where("start_time <= ? AND end_time >= ?", timeNow, timeNow).First(obj)
  57 + result := db.Table(info.TableName).Where(info.CurrentQuery, info.CurrentArgs).First(raw)
61 58 has = result.RowsAffected != 0
62 59 return
63 60 }
... ... @@ -74,3 +71,33 @@ func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1)
74 71 SaveCache(gameId, obj)
75 72 return
76 73 }
  74 +
  75 +// GetCurrent 获取 当前配置
  76 +func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool {
  77 + has := false
  78 + info := obj.ConfInfo(gameId)
  79 + currentKey := info.CacheCurrent
  80 + currentId := zredis.GetString(zredis.GetConn(), currentKey)
  81 + confRaw := *new(T2)
  82 + hasFind := false
  83 + if currentId == "" {
  84 + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)
  85 + } else {
  86 + has = GetConfig[T1, T2](gameId, currentId, obj)
  87 + if has {
  88 + if obj.CheckCurrent() {
  89 + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)
  90 + }
  91 + } else {
  92 + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)
  93 + }
  94 + }
  95 + if hasFind {
  96 + obj.Decode(gameId, confRaw)
  97 + SaveCache(gameId, obj)
  98 + has = true
  99 + currentId = obj.GetUid()
  100 + _ = zredis.Set(zredis.GetConn(), currentKey, currentId)
  101 + }
  102 + return has
  103 +}
... ...
configs/confbase/interface.go
... ... @@ -7,6 +7,8 @@ type ConfInfo struct {
7 7 DbMysql *gorm.DB
8 8 TableName string
9 9 KeyName string
  10 + CurrentQuery any
  11 + CurrentArgs []any
10 12 CacheKey string
11 13 CacheCurrent string
12 14 CacheTime int
... ... @@ -18,6 +20,10 @@ type IConfData interface {
18 20 ConfInfo(suffix string) *ConfInfo
19 21 // Decode 解码
20 22 Decode(gameId string, rawData any)
  23 + // GetUid 获取keyID
  24 + GetUid() string
  25 + // CheckCurrent 判断当前开放
  26 + CheckCurrent() bool
21 27 }
22 28  
23 29 // IConfRawData mysql存储对象
... ...
configs/confcardholder/config.go
... ... @@ -5,6 +5,8 @@ import (
5 5 "apigame/service-common/svconst"
6 6 "apigame/service-common/svmysql"
7 7 "apigame/service-common/svredis"
  8 + "apigame/util/util-lx/lxtime"
  9 + "apigame/util/utstring"
8 10 "fmt"
9 11 )
10 12  
... ... @@ -32,13 +34,25 @@ type ActivityConfig struct {
32 34 GameId string // 所属游戏ID
33 35 }
34 36  
  37 +func (c *ActivityConfig) GetUid() string {
  38 + return utstring.Int64ToString(c.Id)
  39 +}
  40 +
  41 +func (c *ActivityConfig) CheckCurrent() bool {
  42 + timeNow := lxtime.NowUninx()
  43 + return timeNow >= c.StartTime && timeNow <= c.EndTime && c.Raw.Status == 1
  44 +}
  45 +
35 46 func (c *ActivityConfig) ConfInfo(suffix string) *confbase.ConfInfo {
36 47 tableName := svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG
37 48 cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix)
  49 + timeNow := lxtime.NowUninx()
38 50 return &confbase.ConfInfo{
39 51 DbMysql: svconst.DbConfig,
40 52 TableName: fmt.Sprintf("%s_%s", tableName, suffix),
41 53 KeyName: "id",
  54 + CurrentQuery: "start_time <= ? AND end_time >= ? AND status = ?",
  55 + CurrentArgs: []any{timeNow, timeNow, 1},
42 56 CacheKey: fmt.Sprintf("%s:%d", cacheKey, c.Id),
43 57 CacheCurrent: cacheKey + ":current",
44 58 CacheTime: 300,
... ...
configs/confcardholder/get.go
... ... @@ -2,44 +2,40 @@ package confcardholder
2 2  
3 3 import (
4 4 "apigame/configs/confbase"
5   - "apigame/service-common/svconst"
6   - "apigame/service-common/svredis"
7   - "apigame/util/util-lx/lxtime"
8   - "apigame/util/utstring"
9   - "apigame/util/zredis"
10   - "fmt"
11 5 )
12 6  
13 7 // GetCurrent 获取 当前配置
14 8 func GetCurrent(gameId string) (conf *ActivityConfig, has bool) {
15   - currentKey := fmt.Sprintf("%s:%s:%s:current",
16   - svconst.REDIS_CACHEP_REFIX,
17   - svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG,
18   - gameId)
19   - currentId := zredis.GetInt64(zredis.GetConn(), currentKey)
20   - timeNow := lxtime.NowUninx()
21   - confRaw := new(ActivityConfigRaw)
22   - hasFind := false
23   - if currentId == 0 {
24   - hasFind = confbase.FindDuringTime(confRaw, gameId)
25   - } else {
26   - conf, has = GetConfig(gameId, currentId)
27   - if has {
28   - if timeNow < conf.StartTime || timeNow > conf.EndTime {
29   - hasFind = confbase.FindDuringTime(confRaw, gameId)
30   - }
31   - } else {
32   - hasFind = confbase.FindDuringTime(confRaw, gameId)
33   - }
34   - }
35   - if hasFind {
36   - conf = new(ActivityConfig)
37   - conf.Decode(gameId, confRaw)
38   - svredis.SaveData(gameId, conf)
39   - has = true
40   - currentId = conf.Id
41   - _ = zredis.Set(zredis.GetConn(), currentKey, utstring.Int64ToString(currentId))
42   - }
  9 + //currentKey := fmt.Sprintf("%s:%s:%s:current",
  10 + // svconst.REDIS_CACHEP_REFIX,
  11 + // svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG,
  12 + // gameId)
  13 + //currentId := zredis.GetInt64(zredis.GetConn(), currentKey)
  14 + //timeNow := lxtime.NowUninx()
  15 + //confRaw := new(ActivityConfigRaw)
  16 + //hasFind := false
  17 + //if currentId == 0 {
  18 + // hasFind = confbase.FindDuringTime(confRaw, gameId)
  19 + //} else {
  20 + // conf, has = GetConfig(gameId, currentId)
  21 + // if has {
  22 + // if timeNow < conf.StartTime || timeNow > conf.EndTime {
  23 + // hasFind = confbase.FindDuringTime(confRaw, gameId)
  24 + // }
  25 + // } else {
  26 + // hasFind = confbase.FindDuringTime(confRaw, gameId)
  27 + // }
  28 + //}
  29 + //if hasFind {
  30 + // conf = new(ActivityConfig)
  31 + // conf.Decode(gameId, confRaw)
  32 + // svredis.SaveData(gameId, conf)
  33 + // has = true
  34 + // currentId = conf.Id
  35 + // _ = zredis.Set(zredis.GetConn(), currentKey, utstring.Int64ToString(currentId))
  36 + //}
  37 + conf = new(ActivityConfig)
  38 + has = confbase.GetCurrent[*ActivityConfig, ActivityConfigRaw](gameId, conf)
43 39 return
44 40 }
45 41  
... ... @@ -48,27 +44,5 @@ func GetConfig(gameId string, confId int64) (conf *ActivityConfig, has bool) {
48 44 conf = new(ActivityConfig)
49 45 has = confbase.GetConfig[*ActivityConfig, ActivityConfigRaw](gameId, confId, conf)
50 46  
51   - //var err error
52   - //conf = &ActivityConfig{Id: confId}
53   - //has = svredis.LoadData(gameId, conf)
54   - //if has {
55   - // fmt.Println("dwjw confcardholder.GetConfig use cache")
56   - // return
57   - //}
58   - //confRaw := new(ActivityConfigRaw)
59   - //has, err = svmysql.First(confRaw, gameId)
60   - //if err != nil {
61   - // lxalilog.Errors(err, "confcardholder.GetConfig error", gameId)
62   - // return
63   - //}
64   - //if !has {
65   - // return
66   - //}
67   - //
68   - //conf.Decode(gameId, confRaw)
69   - //
70   - //fmt.Println("dwjw confcardholder.GetConfig save cache")
71   - //svredis.SaveData(gameId, conf)
72   -
73 47 return
74 48 }
... ...
configs/confdemo/1.go
... ... @@ -1,42 +0,0 @@
1   -package confdemo
2   -
3   -import (
4   - "apigame/configs/confbase"
5   - "apigame/service-common/svconst"
6   - "fmt"
7   -)
8   -
9   -// DemoConfig 房间排行活动配置 分析后数据
10   -type DemoConfig struct {
11   - Raw *DemoConfigRaw `json:"-"`
12   -
13   - Id int64 // ID
14   - OpenLevel int // 开启等级
15   -}
16   -
17   -func (c *DemoConfig) ConfInfo(suffix string) *confbase.ConfInfo {
18   - tableName := "s_demo"
19   - return &confbase.ConfInfo{
20   - DbMysql: svconst.DbConfig,
21   - TableName: fmt.Sprintf("%s_%s", tableName, suffix),
22   - KeyName: "id",
23   - CacheKey: fmt.Sprintf("%s:%s:%s:%d", svconst.REDIS_CACHEP_REFIX, tableName, suffix, c.Id),
24   - CacheTime: 300,
25   - }
26   -}
27   -
28   -// DemoConfigRaw 房间排行活动配置 原始数据
29   -type DemoConfigRaw struct {
30   - Id int64 `gorm:"column:id;primaryKey"` // ID
31   - OpenLevel int // 开启等级
32   - PreviewTime int64 // 预告时间
33   - StartTime int64 // 开始时间
34   - EndTime int64 // 结束时间
35   -
36   - Robot string // 机器人配置
37   - Room string // 房间配置
38   -
39   - Ver string // 版本号
40   - Status int // 状态 0=关闭 1=开启
41   - UpdateTime int64 // 修改时间戳
42   -}
configs/confdemo/decode.go
... ... @@ -1,10 +0,0 @@
1   -package confdemo
2   -
3   -// Decode 解析配置原始数据
4   -func (c *DemoConfig) Decode(gameId string, rawData any) {
5   - raw := rawData.(*DemoConfigRaw)
6   - c.Raw = raw
7   -
8   - c.Id = raw.Id
9   - c.OpenLevel = raw.OpenLevel
10   -}
controllers/demo.go
... ... @@ -2,8 +2,6 @@ package controllers
2 2  
3 3 import (
4 4 "apigame/configs"
5   - "apigame/configs/confbase"
6   - "apigame/configs/confdemo"
7 5 "apigame/models"
8 6 "apigame/service/code-msg"
9 7 "apigame/util/zjson"
... ... @@ -69,23 +67,23 @@ func (c *DemoController) Demo() {
69 67 fmt.Println(zjson.Str(list))
70 68 }
71 69  
72   - {
73   - gameId := "10149"
74   - {
75   - conf := new(confdemo.DemoConfig)
76   - has := confbase.LoadData[*confdemo.DemoConfig, confdemo.DemoConfigRaw](gameId, 1, conf)
77   - fmt.Println(has)
78   - fmt.Println(conf)
79   -
80   - confbase.SaveCache(gameId, conf)
81   - }
82   - {
83   - conf := &confdemo.DemoConfig{Id: 1}
84   - has := confbase.LoadCache(gameId, conf)
85   - fmt.Println(has)
86   - fmt.Println(conf)
87   - }
88   - }
  70 + //{
  71 + // gameId := "10149"
  72 + // {
  73 + // conf := new(confdemo.DemoConfig)
  74 + // has := confbase.LoadData[*confdemo.DemoConfig, confdemo.DemoConfigRaw](gameId, 1, conf)
  75 + // fmt.Println(has)
  76 + // fmt.Println(conf)
  77 + //
  78 + // confbase.SaveCache(gameId, conf)
  79 + // }
  80 + // {
  81 + // conf := &confdemo.DemoConfig{Id: 1}
  82 + // has := confbase.LoadCache(gameId, conf)
  83 + // fmt.Println(has)
  84 + // fmt.Println(conf)
  85 + // }
  86 + //}
89 87  
90 88 c.RetRspCodeData(code_msg.RECODE_OK, rsp)
91 89 }
... ...
util/zredis/kv.go
... ... @@ -18,7 +18,7 @@ func Increment(key string) int64 {
18 18 return 0
19 19 }
20 20  
21   -func Set(conn redis.Conn, key, value string) (err error) {
  21 +func Set(conn redis.Conn, key, value any) (err error) {
22 22 _, err = conn.Do("Set", key, value)
23 23 return
24 24 }
... ... @@ -33,6 +33,14 @@ func SetEx(conn redis.Conn, key, value string, exTime int) (err error) {
33 33 return
34 34 }
35 35  
  36 +func GetString(conn redis.Conn, key string) string {
  37 + value, err := redis.String(conn.Do("Get", key))
  38 + if err == nil {
  39 + return value
  40 + }
  41 + return ""
  42 +}
  43 +
36 44 func GetInt64(conn redis.Conn, key string) int64 {
37 45 value, err := redis.Int64(conn.Do("Get", key))
38 46 if err == nil {
... ...