Commit c7e75a14d9e14167529d0f95d40f9f959a437a5f
1 parent
a17aa6ca
Exists in
master
and in
1 other branch
feat✨:游戏功能配置
Showing
6 changed files
with
79 additions
and
56 deletions
Show diff stats
configs/confapi/config.go
| ... | ... | @@ -13,12 +13,14 @@ type ApiGameConfig struct { |
| 13 | 13 | |
| 14 | 14 | func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo { |
| 15 | 15 | tableName := "s_game_config" |
| 16 | + cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) | |
| 16 | 17 | return &confbase.ConfInfo{ |
| 17 | - DbMysql: svconst.DbApi, | |
| 18 | - TableName: tableName, | |
| 19 | - KeyName: "gameid", | |
| 20 | - CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix), | |
| 21 | - CacheTime: 300, | |
| 18 | + DbMysql: svconst.DbApi, | |
| 19 | + TableName: tableName, | |
| 20 | + KeyName: "gameid", | |
| 21 | + CacheKey: cacheKey, | |
| 22 | + CacheCurrent: cacheKey + ":current", | |
| 23 | + CacheTime: 300, | |
| 22 | 24 | } |
| 23 | 25 | } |
| 24 | 26 | ... | ... |
configs/confbase/index.go
| ... | ... | @@ -31,11 +31,11 @@ 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) { | |
| 34 | +func LoadData[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) (has bool) { | |
| 35 | 35 | confRaw := new(T2) |
| 36 | 36 | info := obj.ConfInfo(gameId) |
| 37 | 37 | db := info.DbMysql |
| 38 | - result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(confRaw) | |
| 38 | + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), confId).First(confRaw) | |
| 39 | 39 | |
| 40 | 40 | has = result.RowsAffected != 0 |
| 41 | 41 | err := result.Error |
| ... | ... | @@ -60,12 +60,12 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { |
| 60 | 60 | return |
| 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, confId any, obj T1) (has bool) { | |
| 64 | 64 | has = LoadCache(gameId, obj) |
| 65 | 65 | if has { |
| 66 | 66 | return |
| 67 | 67 | } |
| 68 | - has = LoadData[T1, T2](gameId, id, obj) | |
| 68 | + has = LoadData[T1, T2](gameId, confId, obj) | |
| 69 | 69 | if !has { |
| 70 | 70 | return |
| 71 | 71 | } | ... | ... |
configs/confbase/interface.go
| ... | ... | @@ -4,11 +4,12 @@ import "gorm.io/gorm" |
| 4 | 4 | |
| 5 | 5 | // ConfInfo 配置对象信息 |
| 6 | 6 | type ConfInfo struct { |
| 7 | - DbMysql *gorm.DB | |
| 8 | - TableName string | |
| 9 | - KeyName string | |
| 10 | - CacheKey string | |
| 11 | - CacheTime int | |
| 7 | + DbMysql *gorm.DB | |
| 8 | + TableName string | |
| 9 | + KeyName string | |
| 10 | + CacheKey string | |
| 11 | + CacheCurrent string | |
| 12 | + CacheTime int | |
| 12 | 13 | } |
| 13 | 14 | |
| 14 | 15 | // IConfData 配置对象 | ... | ... |
configs/confcardholder/config.go
| 1 | 1 | package confcardholder |
| 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" |
| ... | ... | @@ -31,6 +32,19 @@ type ActivityConfig struct { |
| 31 | 32 | GameId string // 所属游戏ID |
| 32 | 33 | } |
| 33 | 34 | |
| 35 | +func (c *ActivityConfig) ConfInfo(suffix string) *confbase.ConfInfo { | |
| 36 | + tableName := svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG | |
| 37 | + cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) | |
| 38 | + return &confbase.ConfInfo{ | |
| 39 | + DbMysql: svconst.DbConfig, | |
| 40 | + TableName: fmt.Sprintf("%s_%s", tableName, suffix), | |
| 41 | + KeyName: "gameid", | |
| 42 | + CacheKey: fmt.Sprintf("%s:%d", cacheKey, c.Id), | |
| 43 | + CacheCurrent: cacheKey + ":current", | |
| 44 | + CacheTime: 300, | |
| 45 | + } | |
| 46 | +} | |
| 47 | + | |
| 34 | 48 | func (c *ActivityConfig) RedisInfo(suffix string) *svredis.RedisInfo { |
| 35 | 49 | tableName := svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG |
| 36 | 50 | return &svredis.RedisInfo{ | ... | ... |
configs/confcardholder/decode.go
| ... | ... | @@ -9,17 +9,18 @@ import ( |
| 9 | 9 | ) |
| 10 | 10 | |
| 11 | 11 | // Decode 解析配置原始数据 |
| 12 | -func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { | |
| 12 | +func (c *ActivityConfig) Decode(gameId string, rawData any) { | |
| 13 | + raw := rawData.(*ActivityConfigRaw) | |
| 13 | 14 | c.GameId = gameId |
| 14 | - c.Raw = configRaw | |
| 15 | + c.Raw = raw | |
| 15 | 16 | |
| 16 | - c.Id = configRaw.Id | |
| 17 | - c.OpenLevel = configRaw.OpenLevel | |
| 18 | - c.PreviewTime = configRaw.PreviewTime | |
| 19 | - c.StartTime = configRaw.StartTime | |
| 20 | - c.EndTime = configRaw.EndTime | |
| 21 | - c.Round = configRaw.Round | |
| 22 | - c.IconPath = configRaw.IconPath | |
| 17 | + c.Id = raw.Id | |
| 18 | + c.OpenLevel = raw.OpenLevel | |
| 19 | + c.PreviewTime = raw.PreviewTime | |
| 20 | + c.StartTime = raw.StartTime | |
| 21 | + c.EndTime = raw.EndTime | |
| 22 | + c.Round = raw.Round | |
| 23 | + c.IconPath = raw.IconPath | |
| 23 | 24 | |
| 24 | 25 | c.Awards = make(map[string]string) |
| 25 | 26 | c.AlbumConfig = make(map[int]AlbumConfig) |
| ... | ... | @@ -30,18 +31,18 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { |
| 30 | 31 | c.StarShopConfig = make(map[int]StarShopConfig) |
| 31 | 32 | // 解析奖励 |
| 32 | 33 | { |
| 33 | - err := json.Unmarshal([]byte(configRaw.Awards), &c.Awards) | |
| 34 | + err := json.Unmarshal([]byte(raw.Awards), &c.Awards) | |
| 34 | 35 | if err != nil { |
| 35 | - lxalilog.Errors(err, configRaw.Awards, gameId, configRaw.Id) | |
| 36 | + lxalilog.Errors(err, raw.Awards, gameId, raw.Id) | |
| 36 | 37 | return |
| 37 | 38 | } |
| 38 | 39 | } |
| 39 | 40 | // 卡组配置 |
| 40 | 41 | { |
| 41 | 42 | configs := make([]AlbumConfig, 0) |
| 42 | - err := json.Unmarshal([]byte(configRaw.AlbumConfig), &configs) | |
| 43 | + err := json.Unmarshal([]byte(raw.AlbumConfig), &configs) | |
| 43 | 44 | if err != nil { |
| 44 | - lxalilog.Errors(err, configRaw.AlbumConfig, gameId, configRaw.Id) | |
| 45 | + lxalilog.Errors(err, raw.AlbumConfig, gameId, raw.Id) | |
| 45 | 46 | return |
| 46 | 47 | } |
| 47 | 48 | for _, i2 := range configs { |
| ... | ... | @@ -51,9 +52,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { |
| 51 | 52 | // 卡牌配置 |
| 52 | 53 | { |
| 53 | 54 | configs := make([]CardConfig, 0) |
| 54 | - err := json.Unmarshal([]byte(configRaw.CardConfig), &configs) | |
| 55 | + err := json.Unmarshal([]byte(raw.CardConfig), &configs) | |
| 55 | 56 | if err != nil { |
| 56 | - lxalilog.Errors(err, configRaw.CardConfig, gameId, configRaw.Id) | |
| 57 | + lxalilog.Errors(err, raw.CardConfig, gameId, raw.Id) | |
| 57 | 58 | return |
| 58 | 59 | } |
| 59 | 60 | for _, i2 := range configs { |
| ... | ... | @@ -63,9 +64,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { |
| 63 | 64 | // 卡包开卡规则 |
| 64 | 65 | { |
| 65 | 66 | configs := make([]OpenCardholderConfig, 0) |
| 66 | - err := json.Unmarshal([]byte(configRaw.CardHolderConfig), &configs) | |
| 67 | + err := json.Unmarshal([]byte(raw.CardHolderConfig), &configs) | |
| 67 | 68 | if err != nil { |
| 68 | - lxalilog.Errors(err, configRaw.CardHolderConfig, gameId, configRaw.Id) | |
| 69 | + lxalilog.Errors(err, raw.CardHolderConfig, gameId, raw.Id) | |
| 69 | 70 | return |
| 70 | 71 | } |
| 71 | 72 | for _, i2 := range configs { |
| ... | ... | @@ -75,9 +76,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { |
| 75 | 76 | // 卡片星级配置 |
| 76 | 77 | { |
| 77 | 78 | configs := make([]NormalCardStarConfig, 0) |
| 78 | - err := json.Unmarshal([]byte(configRaw.NormalCardStarSequence), &configs) | |
| 79 | + err := json.Unmarshal([]byte(raw.NormalCardStarSequence), &configs) | |
| 79 | 80 | if err != nil { |
| 80 | - lxalilog.Errors(err, configRaw.NormalCardStarSequence, gameId, configRaw.Id) | |
| 81 | + lxalilog.Errors(err, raw.NormalCardStarSequence, gameId, raw.Id) | |
| 81 | 82 | return |
| 82 | 83 | } |
| 83 | 84 | for _, i2 := range configs { |
| ... | ... | @@ -89,9 +90,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { |
| 89 | 90 | // 卡片星级对应卡牌配置 |
| 90 | 91 | { |
| 91 | 92 | configs := make([]CardSequenceConfig, 0) |
| 92 | - err := json.Unmarshal([]byte(configRaw.CardSequenceConfig), &configs) | |
| 93 | + err := json.Unmarshal([]byte(raw.CardSequenceConfig), &configs) | |
| 93 | 94 | if err != nil { |
| 94 | - lxalilog.Errors(err, configRaw.CardSequenceConfig, gameId, configRaw.Id) | |
| 95 | + lxalilog.Errors(err, raw.CardSequenceConfig, gameId, raw.Id) | |
| 95 | 96 | return |
| 96 | 97 | } |
| 97 | 98 | for _, i2 := range configs { |
| ... | ... | @@ -103,9 +104,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { |
| 103 | 104 | // 星星商店配置 |
| 104 | 105 | { |
| 105 | 106 | configs := make([]StarShopConfig, 0) |
| 106 | - err := json.Unmarshal([]byte(configRaw.StarShopConfig), &configs) | |
| 107 | + err := json.Unmarshal([]byte(raw.StarShopConfig), &configs) | |
| 107 | 108 | if err != nil { |
| 108 | - lxalilog.Errors(err, configRaw.StarShopConfig, gameId, configRaw.Id) | |
| 109 | + lxalilog.Errors(err, raw.StarShopConfig, gameId, raw.Id) | |
| 109 | 110 | return |
| 110 | 111 | } |
| 111 | 112 | for _, i2 := range configs { | ... | ... |
configs/confcardholder/get.go
| ... | ... | @@ -3,9 +3,7 @@ package confcardholder |
| 3 | 3 | import ( |
| 4 | 4 | "apigame/configs/confbase" |
| 5 | 5 | "apigame/service-common/svconst" |
| 6 | - "apigame/service-common/svmysql" | |
| 7 | 6 | "apigame/service-common/svredis" |
| 8 | - "apigame/util/util-lx/lxalilog" | |
| 9 | 7 | "apigame/util/util-lx/lxtime" |
| 10 | 8 | "apigame/util/utstring" |
| 11 | 9 | "apigame/util/zredis" |
| ... | ... | @@ -47,27 +45,34 @@ func GetCurrent(gameId string) (conf *ActivityConfig, has bool) { |
| 47 | 45 | |
| 48 | 46 | // GetConfig 获取 配置根据Id |
| 49 | 47 | func GetConfig(gameId string, confId int64) (conf *ActivityConfig, has bool) { |
| 50 | - var err error | |
| 51 | - conf = &ActivityConfig{Id: confId} | |
| 52 | - has = svredis.LoadData(gameId, conf) | |
| 53 | - if has { | |
| 54 | - fmt.Println("dwjw confcardholder.GetConfig use cache") | |
| 55 | - return | |
| 56 | - } | |
| 57 | - confRaw := new(ActivityConfigRaw) | |
| 58 | - has, err = svmysql.First(confRaw, gameId) | |
| 59 | - if err != nil { | |
| 60 | - lxalilog.Errors(err, "confcardholder.GetConfig error", gameId) | |
| 61 | - return | |
| 62 | - } | |
| 48 | + conf = new(ActivityConfig) | |
| 49 | + has := confbase.GetConfig[*ActivityConfig, ActivityConfigRaw](gameId, gameId, conf) | |
| 63 | 50 | if !has { |
| 51 | + err = errors.New("confapi.GetConfig error") | |
| 64 | 52 | return |
| 65 | 53 | } |
| 66 | 54 | |
| 67 | - conf.Decode(gameId, confRaw) | |
| 68 | - | |
| 69 | - fmt.Println("dwjw confcardholder.GetConfig save cache") | |
| 70 | - svredis.SaveData(gameId, conf) | |
| 55 | + //var err error | |
| 56 | + //conf = &ActivityConfig{Id: confId} | |
| 57 | + //has = svredis.LoadData(gameId, conf) | |
| 58 | + //if has { | |
| 59 | + // fmt.Println("dwjw confcardholder.GetConfig use cache") | |
| 60 | + // return | |
| 61 | + //} | |
| 62 | + //confRaw := new(ActivityConfigRaw) | |
| 63 | + //has, err = svmysql.First(confRaw, gameId) | |
| 64 | + //if err != nil { | |
| 65 | + // lxalilog.Errors(err, "confcardholder.GetConfig error", gameId) | |
| 66 | + // return | |
| 67 | + //} | |
| 68 | + //if !has { | |
| 69 | + // return | |
| 70 | + //} | |
| 71 | + // | |
| 72 | + //conf.Decode(gameId, confRaw) | |
| 73 | + // | |
| 74 | + //fmt.Println("dwjw confcardholder.GetConfig save cache") | |
| 75 | + //svredis.SaveData(gameId, conf) | |
| 71 | 76 | |
| 72 | 77 | return |
| 73 | 78 | } | ... | ... |