Commit b5dab26d96522dae2e15779d9a3f735d20cc05e3

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

feat✨:游戏功能配置

conf/local.conf
... ... @@ -12,7 +12,7 @@ env = local
12 12 host = 127.0.0.1
13 13 port = 6379
14 14 pwd = 123456
15   -db = 12
  15 +db = 0
16 16 prefix = merge::
17 17 tokendb = 13
18 18 cachedb = 14
... ...
configs/confapi/config.go
1 1 package confapi
2 2  
3 3 import (
  4 + "apigame/configs/confbase"
4 5 "apigame/service-common/svconst"
5 6 "apigame/service-common/svmysql"
6 7 "apigame/service-common/svredis"
... ... @@ -9,11 +10,18 @@ import (
9 10  
10 11 // ApiGameConfig api游戏配置
11 12 type ApiGameConfig struct {
12   - AppId string `gorm:"column:appid"`
13   - GameId string `gorm:"column:gameid"`
14   - Secret string `gorm:"column:secret"`
15   - AppKey string `gorm:"column:appkey"`
16   - Name string `gorm:"column:name"`
  13 + Raw *Raw
  14 +}
  15 +
  16 +func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo {
  17 + tableName := "s_game_config"
  18 + return &confbase.ConfInfo{
  19 + DbMysql: svconst.DbApi,
  20 + TableName: tableName,
  21 + KeyName: "gameid",
  22 + CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix),
  23 + CacheTime: 300,
  24 + }
17 25 }
18 26  
19 27 func (c *ApiGameConfig) RedisInfo(suffix string) *svredis.RedisInfo {
... ... @@ -31,3 +39,12 @@ func (c *ApiGameConfig) MysqlInfo(suffix string) *svmysql.MysqlInfo {
31 39 TableName: tableName,
32 40 }
33 41 }
  42 +
  43 +// Raw 配置原始数据
  44 +type Raw struct {
  45 + AppId string `gorm:"column:appid"`
  46 + GameId string `gorm:"column:gameid"`
  47 + Secret string `gorm:"column:secret"`
  48 + AppKey string `gorm:"column:appkey"`
  49 + Name string `gorm:"column:name"`
  50 +}
... ...
configs/confbase/index.go
... ... @@ -31,6 +31,26 @@ func LoadCache[T IConfData](gameId string, obj T) (has bool) {
31 31 return
32 32 }
33 33  
  34 +func LoadData[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) {
  35 + confRaw := new(T2)
  36 + info := obj.ConfInfo(gameId)
  37 + db := info.DbMysql
  38 + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(confRaw)
  39 +
  40 + has = result.RowsAffected != 0
  41 + err := result.Error
  42 + if err != nil {
  43 + lxalilog.Errors(err, "confbase.LoadData error", gameId)
  44 + return
  45 + }
  46 + if !has {
  47 + return
  48 + }
  49 +
  50 + obj.Decode(gameId, confRaw)
  51 + return
  52 +}
  53 +
34 54 func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) {
35 55 info := obj.MysqlInfo(gameId)
36 56 db := info.DbMysql
... ... @@ -40,11 +60,11 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) {
40 60 return
41 61 }
42 62  
43   -func LoadData[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) {
  63 +func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) {
44 64 confRaw := new(T2)
45 65 info := obj.ConfInfo(gameId)
46 66 db := info.DbMysql
47   - result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(obj)
  67 + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(confRaw)
48 68  
49 69 has = result.RowsAffected != 0
50 70 err := result.Error
... ...
configs/confdemo/1.go
... ... @@ -18,9 +18,9 @@ func (c *DemoConfig) ConfInfo(suffix string) *confbase.ConfInfo {
18 18 tableName := "s_demo"
19 19 return &confbase.ConfInfo{
20 20 DbMysql: svconst.DbConfig,
21   - TableName: tableName + suffix,
  21 + TableName: fmt.Sprintf("%s_%s", tableName, suffix),
22 22 KeyName: "id",
23   - CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix),
  23 + CacheKey: fmt.Sprintf("%s:%s:%s:%d", svconst.REDIS_CACHEP_REFIX, tableName, suffix, c.Id),
24 24 CacheTime: 300,
25 25 }
26 26 }
... ...
configs/confdemo/decode.go
... ... @@ -4,4 +4,7 @@ package confdemo
4 4 func (c *DemoConfig) Decode(gameId string, rawData any) {
5 5 raw := rawData.(*DemoConfigRaw)
6 6 c.Raw = raw
  7 +
  8 + c.Id = raw.Id
  9 + c.OpenLevel = raw.OpenLevel
7 10 }
... ...
controllers/demo.go
... ... @@ -71,10 +71,20 @@ func (c *DemoController) Demo() {
71 71  
72 72 {
73 73 gameId := "10149"
74   - conf := new(confdemo.DemoConfig)
75   - has := confbase.LoadData[*confdemo.DemoConfig, confdemo.DemoConfigRaw](gameId, 1, conf)
76   - fmt.Println(has)
77   - fmt.Println(conf)
  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 + }
78 88 }
79 89  
80 90 c.RetRspCodeData(code_msg.RECODE_OK, rsp)
... ...