Commit df149dfa8b8f1038852027efed911fee3c88d106
1 parent
dfed6b47
Exists in
master
and in
1 other branch
feat✨:游戏功能配置
Showing
5 changed files
with
129 additions
and
0 deletions
Show diff stats
configs/confbase/index.go
| @@ -2,9 +2,35 @@ package confbase | @@ -2,9 +2,35 @@ package confbase | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/service-common/svmysql" | 4 | "apigame/service-common/svmysql" |
| 5 | + "apigame/util/util-lx/lxalilog" | ||
| 5 | "apigame/util/util-lx/lxtime" | 6 | "apigame/util/util-lx/lxtime" |
| 7 | + "apigame/util/zjson" | ||
| 8 | + "apigame/util/zredis" | ||
| 9 | + "fmt" | ||
| 6 | ) | 10 | ) |
| 7 | 11 | ||
| 12 | +func SaveCache(gameId string, obj IConfData) { | ||
| 13 | + info := obj.ConfInfo(gameId) | ||
| 14 | + _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +func LoadCache[T IConfData](gameId string, obj T) (has bool) { | ||
| 18 | + has = true | ||
| 19 | + info := obj.ConfInfo(gameId) | ||
| 20 | + text, err := zredis.Get(zredis.GetConn(), info.CacheKey) | ||
| 21 | + if err != nil { | ||
| 22 | + has = false | ||
| 23 | + return | ||
| 24 | + } | ||
| 25 | + err = zjson.Obj(text, &obj) | ||
| 26 | + if err != nil { | ||
| 27 | + fmt.Println(err) | ||
| 28 | + has = false | ||
| 29 | + return | ||
| 30 | + } | ||
| 31 | + return | ||
| 32 | +} | ||
| 33 | + | ||
| 8 | func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { | 34 | func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { |
| 9 | info := obj.MysqlInfo(gameId) | 35 | info := obj.MysqlInfo(gameId) |
| 10 | db := info.DbMysql | 36 | db := info.DbMysql |
| @@ -13,3 +39,23 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { | @@ -13,3 +39,23 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { | ||
| 13 | has = result.RowsAffected != 0 | 39 | has = result.RowsAffected != 0 |
| 14 | return | 40 | return |
| 15 | } | 41 | } |
| 42 | + | ||
| 43 | +func LoadData[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) { | ||
| 44 | + confRaw := new(T2) | ||
| 45 | + info := obj.ConfInfo(gameId) | ||
| 46 | + db := info.DbMysql | ||
| 47 | + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(obj) | ||
| 48 | + | ||
| 49 | + has = result.RowsAffected != 0 | ||
| 50 | + err := result.Error | ||
| 51 | + if err != nil { | ||
| 52 | + lxalilog.Errors(err, "confbase.LoadData error", gameId) | ||
| 53 | + return | ||
| 54 | + } | ||
| 55 | + if !has { | ||
| 56 | + return | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + obj.Decode(gameId, confRaw) | ||
| 60 | + return | ||
| 61 | +} |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +package confbase | ||
| 2 | + | ||
| 3 | +import "gorm.io/gorm" | ||
| 4 | + | ||
| 5 | +// ConfInfo 配置对象信息 | ||
| 6 | +type ConfInfo struct { | ||
| 7 | + DbMysql *gorm.DB | ||
| 8 | + TableName string | ||
| 9 | + KeyName string | ||
| 10 | + CacheKey string | ||
| 11 | + CacheTime int | ||
| 12 | +} | ||
| 13 | + | ||
| 14 | +// IConfData 配置对象 | ||
| 15 | +type IConfData interface { | ||
| 16 | + // ConfInfo redis存储信息 | ||
| 17 | + ConfInfo(suffix string) *ConfInfo | ||
| 18 | + // Decode 解码 | ||
| 19 | + Decode(gameId string, rawData any) | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +// IConfRawData mysql存储对象 | ||
| 23 | +type IConfRawData interface { | ||
| 24 | +} |
| @@ -0,0 +1,42 @@ | @@ -0,0 +1,42 @@ | ||
| 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: tableName + suffix, | ||
| 22 | + KeyName: "id", | ||
| 23 | + CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix), | ||
| 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 | +} |
controllers/demo.go
| @@ -2,6 +2,8 @@ package controllers | @@ -2,6 +2,8 @@ package controllers | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/configs" | 4 | "apigame/configs" |
| 5 | + "apigame/configs/confbase" | ||
| 6 | + "apigame/configs/confdemo" | ||
| 5 | "apigame/models" | 7 | "apigame/models" |
| 6 | "apigame/service/code-msg" | 8 | "apigame/service/code-msg" |
| 7 | "apigame/util/zjson" | 9 | "apigame/util/zjson" |
| @@ -67,5 +69,13 @@ func (c *DemoController) Demo() { | @@ -67,5 +69,13 @@ func (c *DemoController) Demo() { | ||
| 67 | fmt.Println(zjson.Str(list)) | 69 | fmt.Println(zjson.Str(list)) |
| 68 | } | 70 | } |
| 69 | 71 | ||
| 72 | + { | ||
| 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) | ||
| 78 | + } | ||
| 79 | + | ||
| 70 | c.RetRspCodeData(code_msg.RECODE_OK, rsp) | 80 | c.RetRspCodeData(code_msg.RECODE_OK, rsp) |
| 71 | } | 81 | } |