Commit a17aa6cab25fafff7232ecad89812e13465a6688
1 parent
7ffab05c
Exists in
master
and in
1 other branch
feat✨:游戏功能配置
Showing
4 changed files
with
17 additions
and
47 deletions
Show diff stats
configs/confapi/config.go
| @@ -3,8 +3,6 @@ package confapi | @@ -3,8 +3,6 @@ package confapi | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/configs/confbase" | 4 | "apigame/configs/confbase" |
| 5 | "apigame/service-common/svconst" | 5 | "apigame/service-common/svconst" |
| 6 | - "apigame/service-common/svmysql" | ||
| 7 | - "apigame/service-common/svredis" | ||
| 8 | "fmt" | 6 | "fmt" |
| 9 | ) | 7 | ) |
| 10 | 8 | ||
| @@ -24,22 +22,6 @@ func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo { | @@ -24,22 +22,6 @@ func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo { | ||
| 24 | } | 22 | } |
| 25 | } | 23 | } |
| 26 | 24 | ||
| 27 | -func (c *ApiGameConfig) RedisInfo(suffix string) *svredis.RedisInfo { | ||
| 28 | - tableName := "s_game_config" | ||
| 29 | - return &svredis.RedisInfo{ | ||
| 30 | - CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix), | ||
| 31 | - CacheTime: 300, | ||
| 32 | - } | ||
| 33 | -} | ||
| 34 | - | ||
| 35 | -func (c *ApiGameConfig) MysqlInfo(suffix string) *svmysql.MysqlInfo { | ||
| 36 | - tableName := "s_game_config" | ||
| 37 | - return &svmysql.MysqlInfo{ | ||
| 38 | - DbMysql: svconst.DbApi.Where("gameid = ?", suffix), | ||
| 39 | - TableName: tableName, | ||
| 40 | - } | ||
| 41 | -} | ||
| 42 | - | ||
| 43 | // Raw 配置原始数据 | 25 | // Raw 配置原始数据 |
| 44 | type Raw struct { | 26 | type Raw struct { |
| 45 | AppId string `gorm:"column:appid"` | 27 | AppId string `gorm:"column:appid"` |
| @@ -48,3 +30,9 @@ type Raw struct { | @@ -48,3 +30,9 @@ type Raw struct { | ||
| 48 | AppKey string `gorm:"column:appkey"` | 30 | AppKey string `gorm:"column:appkey"` |
| 49 | Name string `gorm:"column:name"` | 31 | Name string `gorm:"column:name"` |
| 50 | } | 32 | } |
| 33 | + | ||
| 34 | +// Decode 解析配置原始数据 | ||
| 35 | +func (c *ApiGameConfig) Decode(gameId string, rawData any) { | ||
| 36 | + raw := rawData.(*Raw) | ||
| 37 | + c.Raw = raw | ||
| 38 | +} |
configs/confapi/get.go
| 1 | package confapi | 1 | package confapi |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | - "apigame/service-common/svmysql" | ||
| 5 | - "apigame/service-common/svredis" | ||
| 6 | - "apigame/util/util-lx/lxalilog" | ||
| 7 | - "fmt" | 4 | + "apigame/configs/confbase" |
| 5 | + "errors" | ||
| 8 | ) | 6 | ) |
| 9 | 7 | ||
| 10 | // GetConfig 获取 api游戏配置 | 8 | // GetConfig 获取 api游戏配置 |
| 11 | func GetConfig(gameId string) (conf *ApiGameConfig, err error) { | 9 | func GetConfig(gameId string) (conf *ApiGameConfig, err error) { |
| 10 | + | ||
| 12 | conf = new(ApiGameConfig) | 11 | conf = new(ApiGameConfig) |
| 13 | - has := svredis.LoadData(gameId, conf) | ||
| 14 | - if has { | ||
| 15 | - fmt.Println("dwjw confapi.GetConfig use cache") | ||
| 16 | - return | ||
| 17 | - } | ||
| 18 | - has, err = svmysql.First(conf, gameId) | ||
| 19 | - if err != nil { | ||
| 20 | - lxalilog.Errors(err, "confapi.GetConfig error", gameId) | ||
| 21 | - return | ||
| 22 | - } | 12 | + has := confbase.GetConfig[*ApiGameConfig, Raw](gameId, gameId, conf) |
| 23 | if !has { | 13 | if !has { |
| 14 | + err = errors.New("confapi.GetConfig error") | ||
| 24 | return | 15 | return |
| 25 | } | 16 | } |
| 26 | - fmt.Println("dwjw confapi.GetConfig save cache") | ||
| 27 | - svredis.SaveData(gameId, conf) | ||
| 28 | 17 | ||
| 29 | return | 18 | return |
| 30 | } | 19 | } |
configs/confbase/index.go
| @@ -9,7 +9,7 @@ import ( | @@ -9,7 +9,7 @@ import ( | ||
| 9 | "fmt" | 9 | "fmt" |
| 10 | ) | 10 | ) |
| 11 | 11 | ||
| 12 | -func SaveCache(gameId string, obj IConfData) { | 12 | +func SaveCache[T IConfData](gameId string, obj T) { |
| 13 | info := obj.ConfInfo(gameId) | 13 | info := obj.ConfInfo(gameId) |
| 14 | _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) | 14 | _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) |
| 15 | } | 15 | } |
| @@ -61,21 +61,14 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { | @@ -61,21 +61,14 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { | ||
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | func GetConfig[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) { |
| 64 | - confRaw := new(T2) | ||
| 65 | - info := obj.ConfInfo(gameId) | ||
| 66 | - db := info.DbMysql | ||
| 67 | - result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(confRaw) | ||
| 68 | - | ||
| 69 | - has = result.RowsAffected != 0 | ||
| 70 | - err := result.Error | ||
| 71 | - if err != nil { | ||
| 72 | - lxalilog.Errors(err, "confbase.LoadData error", gameId) | 64 | + has = LoadCache(gameId, obj) |
| 65 | + if has { | ||
| 73 | return | 66 | return |
| 74 | } | 67 | } |
| 68 | + has = LoadData[T1, T2](gameId, id, obj) | ||
| 75 | if !has { | 69 | if !has { |
| 76 | return | 70 | return |
| 77 | } | 71 | } |
| 78 | - | ||
| 79 | - obj.Decode(gameId, confRaw) | 72 | + SaveCache(gameId, obj) |
| 80 | return | 73 | return |
| 81 | } | 74 | } |
middleware/sign/index.go
| @@ -232,7 +232,7 @@ func CheckSign(data interface{}, checkToken bool) (code string, gameconfig *conf | @@ -232,7 +232,7 @@ func CheckSign(data interface{}, checkToken bool) (code string, gameconfig *conf | ||
| 232 | return | 232 | return |
| 233 | } | 233 | } |
| 234 | 234 | ||
| 235 | - appkey := gameconfig.AppKey | 235 | + appkey := gameconfig.Raw.AppKey |
| 236 | 236 | ||
| 237 | if appkey == "" { | 237 | if appkey == "" { |
| 238 | code = code_msg.RECODE_SIGNERROR | 238 | code = code_msg.RECODE_SIGNERROR |