Commit ffc151b5cea437875b067dfe52ef79c36daadf29
1 parent
f0df1f48
Exists in
master
and in
1 other branch
feat:配置代码优化
Showing
4 changed files
with
62 additions
and
67 deletions
Show diff stats
service/ht-cardholder/config-registry.go
| ... | ... | @@ -5,124 +5,119 @@ import ( |
| 5 | 5 | "apigame/service/constd" |
| 6 | 6 | "encoding/json" |
| 7 | 7 | "errors" |
| 8 | + "fmt" | |
| 8 | 9 | ) |
| 9 | 10 | |
| 10 | 11 | var ( |
| 11 | - Registry RegistryConfigs // 卡牌活动配置 | |
| 12 | -) | |
| 13 | - | |
| 14 | -// RegistryConfigs 卡牌活动配置 | |
| 15 | -type RegistryConfigs struct { | |
| 16 | 12 | ConfigRaws CardActivityConfigRaw // 活动配置 原始数据 |
| 17 | 13 | Config CardActivityConfig // 活动配置 分析后数据 |
| 18 | -} | |
| 14 | +) | |
| 19 | 15 | |
| 20 | -func NewRegistryConfigs() { | |
| 21 | - Registry = RegistryConfigs{} | |
| 16 | +func NewConfigs() { | |
| 17 | + ConfigRaws = CardActivityConfigRaw{} | |
| 18 | + Config = CardActivityConfig{} | |
| 22 | 19 | } |
| 23 | 20 | |
| 24 | -// Decode 解析配置原始数据 | |
| 25 | -func (r *RegistryConfigs) Decode(confRaw CardActivityConfigRaw) { | |
| 26 | - conf := CardActivityConfig{ | |
| 27 | - Raw: confRaw, | |
| 28 | - Id: confRaw.Id, | |
| 21 | +// ConfigDecode 解析配置原始数据 | |
| 22 | +func ConfigDecode() { | |
| 23 | + Config = CardActivityConfig{ | |
| 24 | + Raw: ConfigRaws, | |
| 25 | + Id: ConfigRaws.Id, | |
| 29 | 26 | Awards: make(map[string]string), |
| 30 | 27 | AlbumConfig: make(map[string]AlbumConfig), |
| 31 | 28 | CardConfig: make(map[int]CardConfig), |
| 32 | 29 | CardholderConfig: make(map[string]CardholderConfig), |
| 33 | - NormalCardStarSequence: make([]NormalCardStarSequence, 0), | |
| 34 | - CardSequenceConfig: make([]CardSequenceConfig, 0), | |
| 30 | + NormalCardStarSequence: make(map[string]NormalCardStarSequence), | |
| 31 | + CardSequenceConfig: make(map[string]CardSequenceConfig), | |
| 35 | 32 | } |
| 36 | 33 | // 解析奖励 |
| 37 | 34 | { |
| 38 | - err := json.Unmarshal([]byte(confRaw.Awards), &conf.Awards) | |
| 35 | + err := json.Unmarshal([]byte(ConfigRaws.Awards), &Config.Awards) | |
| 39 | 36 | if err != nil { |
| 40 | - lxalilog.Errors(err, confRaw.Awards, constd.GAME_ID_HT, confRaw.Id) | |
| 37 | + lxalilog.Errors(err, ConfigRaws.Awards, constd.GAME_ID_HT, ConfigRaws.Id) | |
| 41 | 38 | return |
| 42 | 39 | } |
| 43 | 40 | } |
| 44 | 41 | // 卡组配置 |
| 45 | 42 | { |
| 46 | 43 | configs := make([]AlbumConfig, 0) |
| 47 | - err := json.Unmarshal([]byte(confRaw.AlbumConfig), &configs) | |
| 44 | + err := json.Unmarshal([]byte(ConfigRaws.AlbumConfig), &configs) | |
| 48 | 45 | if err != nil { |
| 49 | - lxalilog.Errors(err, confRaw.AlbumConfig, constd.GAME_ID_HT, confRaw.Id) | |
| 46 | + lxalilog.Errors(err, ConfigRaws.AlbumConfig, constd.GAME_ID_HT, ConfigRaws.Id) | |
| 50 | 47 | return |
| 51 | 48 | } |
| 52 | 49 | for _, i2 := range configs { |
| 53 | - conf.AlbumConfig[i2.SetId] = i2 | |
| 50 | + Config.AlbumConfig[i2.SetId] = i2 | |
| 54 | 51 | } |
| 55 | 52 | } |
| 56 | 53 | // 卡牌配置 |
| 57 | 54 | { |
| 58 | 55 | configs := make([]CardConfig, 0) |
| 59 | - err := json.Unmarshal([]byte(confRaw.CardConfig), &configs) | |
| 56 | + err := json.Unmarshal([]byte(ConfigRaws.CardConfig), &configs) | |
| 60 | 57 | if err != nil { |
| 61 | - lxalilog.Errors(err, confRaw.CardConfig, constd.GAME_ID_HT, confRaw.Id) | |
| 58 | + lxalilog.Errors(err, ConfigRaws.CardConfig, constd.GAME_ID_HT, ConfigRaws.Id) | |
| 62 | 59 | return |
| 63 | 60 | } |
| 64 | 61 | for _, i2 := range configs { |
| 65 | - conf.CardConfig[i2.Id] = i2 | |
| 62 | + Config.CardConfig[i2.Id] = i2 | |
| 66 | 63 | } |
| 67 | 64 | } |
| 68 | 65 | // 卡包开卡规则 |
| 69 | 66 | { |
| 70 | 67 | configs := make([]CardholderConfig, 0) |
| 71 | - err := json.Unmarshal([]byte(confRaw.CardHolderConfig), &configs) | |
| 68 | + err := json.Unmarshal([]byte(ConfigRaws.CardHolderConfig), &configs) | |
| 72 | 69 | if err != nil { |
| 73 | - lxalilog.Errors(err, confRaw.CardHolderConfig, constd.GAME_ID_HT, confRaw.Id) | |
| 70 | + lxalilog.Errors(err, ConfigRaws.CardHolderConfig, constd.GAME_ID_HT, ConfigRaws.Id) | |
| 74 | 71 | return |
| 75 | 72 | } |
| 76 | 73 | for _, i2 := range configs { |
| 77 | - conf.CardholderConfig[i2.Id] = i2 | |
| 74 | + Config.CardholderConfig[i2.Id] = i2 | |
| 78 | 75 | } |
| 79 | 76 | } |
| 80 | 77 | // 卡片星级配置 |
| 81 | 78 | { |
| 82 | 79 | configs := make([]NormalCardStarSequence, 0) |
| 83 | - err := json.Unmarshal([]byte(confRaw.NormalCardStarSequence), &configs) | |
| 80 | + err := json.Unmarshal([]byte(ConfigRaws.NormalCardStarSequence), &configs) | |
| 84 | 81 | if err != nil { |
| 85 | - lxalilog.Errors(err, confRaw.NormalCardStarSequence, constd.GAME_ID_HT, confRaw.Id) | |
| 82 | + lxalilog.Errors(err, ConfigRaws.NormalCardStarSequence, constd.GAME_ID_HT, ConfigRaws.Id) | |
| 86 | 83 | return |
| 87 | 84 | } |
| 88 | 85 | for _, i2 := range configs { |
| 89 | - conf.NormalCardStarSequence = append(conf.NormalCardStarSequence, i2) | |
| 86 | + combineId := CombineIdSequenceIdCohort(i2.Id, i2.SequenceId, i2.Cohort) | |
| 87 | + Config.NormalCardStarSequence[combineId] = i2 | |
| 90 | 88 | } |
| 91 | 89 | } |
| 92 | 90 | // 卡片星级对应卡牌配置 |
| 93 | 91 | { |
| 94 | 92 | configs := make([]CardSequenceConfig, 0) |
| 95 | - err := json.Unmarshal([]byte(confRaw.CardSequenceConfig), &configs) | |
| 93 | + err := json.Unmarshal([]byte(ConfigRaws.CardSequenceConfig), &configs) | |
| 96 | 94 | if err != nil { |
| 97 | - lxalilog.Errors(err, confRaw.CardSequenceConfig, constd.GAME_ID_HT, confRaw.Id) | |
| 95 | + lxalilog.Errors(err, ConfigRaws.CardSequenceConfig, constd.GAME_ID_HT, ConfigRaws.Id) | |
| 98 | 96 | return |
| 99 | 97 | } |
| 100 | 98 | for _, i2 := range configs { |
| 101 | - conf.CardSequenceConfig = append(conf.CardSequenceConfig, i2) | |
| 99 | + combineId := CombineIdSequenceIdCohort(i2.Id, i2.SequenceId, i2.Cohort) | |
| 100 | + Config.CardSequenceConfig[combineId] = i2 | |
| 102 | 101 | } |
| 103 | 102 | } |
| 104 | 103 | |
| 105 | - r.Config = conf | |
| 106 | - //logs.Debug(conf.Awards) | |
| 107 | - //logs.Debug(conf.AlbumConfig) | |
| 108 | - //logs.Debug(conf.CardConfig) | |
| 109 | - //logs.Debug(conf.CardholderConfig) | |
| 110 | - //logs.Debug("🐸", conf.NormalCardStarSequence) | |
| 111 | - //logs.Debug("🐸", conf.CardSequenceConfig) | |
| 104 | + //logs.Debug(Config.Awards) | |
| 105 | + //logs.Debug(Config.AlbumConfig) | |
| 106 | + //logs.Debug(Config.CardConfig) | |
| 107 | + //logs.Debug(Config.CardholderConfig) | |
| 108 | + //logs.Debug("🐸", Config.NormalCardStarSequence) | |
| 109 | + //logs.Debug("🐸", Config.CardSequenceConfig) | |
| 110 | +} | |
| 111 | + | |
| 112 | +// CombineIdSequenceIdCohort 组合ID k=ID_用户序列_用户分组 | |
| 113 | +func CombineIdSequenceIdCohort(id, sequenceId, cohort string) string { | |
| 114 | + return fmt.Sprintf("%s_%s_%s", id, sequenceId, cohort) | |
| 112 | 115 | } |
| 113 | 116 | |
| 114 | 117 | // FindCardSequenceConfig 查找配置 星级ID对应的卡片 |
| 115 | 118 | func FindCardSequenceConfig(id, sequenceId, cohort string) (conf CardSequenceConfig, has bool) { |
| 116 | - conf = CardSequenceConfig{} | |
| 117 | - has = false | |
| 118 | - for _, c := range Registry.Config.CardSequenceConfig { | |
| 119 | - if c.Id == id && | |
| 120 | - c.SequenceId == sequenceId && | |
| 121 | - c.Cohort == cohort { | |
| 122 | - has = true | |
| 123 | - return | |
| 124 | - } | |
| 125 | - } | |
| 119 | + combineId := CombineIdSequenceIdCohort(id, sequenceId, cohort) | |
| 120 | + conf, has = Config.CardSequenceConfig[combineId] | |
| 126 | 121 | if !has { |
| 127 | 122 | lxalilog.Errors(errors.New("ht_cardholder CardSequenceConfig error"), " LoadData Error", id, sequenceId, cohort) |
| 128 | 123 | } | ... | ... |
service/ht-cardholder/config.go
| ... | ... | @@ -12,13 +12,13 @@ type CardActivityUpdateConfig struct { |
| 12 | 12 | // CardActivityConfig 卡牌活动配置 分析后数据 |
| 13 | 13 | type CardActivityConfig struct { |
| 14 | 14 | Raw CardActivityConfigRaw |
| 15 | - Id int64 // ID | |
| 16 | - Awards map[string]string // 奖励配置 | |
| 17 | - AlbumConfig map[string]AlbumConfig // 卡组配置 | |
| 18 | - CardConfig map[int]CardConfig // 卡牌配置 | |
| 19 | - CardholderConfig map[string]CardholderConfig // 卡包开卡规则 | |
| 20 | - NormalCardStarSequence []NormalCardStarSequence // 卡片星级配置 | |
| 21 | - CardSequenceConfig []CardSequenceConfig // 卡片星级对应卡牌配置 | |
| 15 | + Id int64 // ID | |
| 16 | + Awards map[string]string // 奖励配置 | |
| 17 | + AlbumConfig map[string]AlbumConfig // 卡组配置 | |
| 18 | + CardConfig map[int]CardConfig // 卡牌配置 | |
| 19 | + CardholderConfig map[string]CardholderConfig // 卡包开卡规则 | |
| 20 | + NormalCardStarSequence map[string]NormalCardStarSequence // k=ID_用户序列_用户分组 卡片星级配置 | |
| 21 | + CardSequenceConfig map[string]CardSequenceConfig // k=ID_用户序列_用户分组 卡片星级对应卡牌配置 | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | func (c *CardActivityConfig) TableName() string { | ... | ... |
service/ht-cardholder/configs.go
| ... | ... | @@ -11,7 +11,7 @@ import ( |
| 11 | 11 | // Init 初始化 |
| 12 | 12 | func Init() { |
| 13 | 13 | |
| 14 | - NewRegistryConfigs() | |
| 14 | + NewConfigs() | |
| 15 | 15 | |
| 16 | 16 | TryUpdateConfigs() |
| 17 | 17 | |
| ... | ... | @@ -49,7 +49,7 @@ func LoadConfigs() { |
| 49 | 49 | return |
| 50 | 50 | } |
| 51 | 51 | // 更新数据 |
| 52 | - if configOpen.Id != Registry.Config.Raw.Id || configOpen.UpdateTime != Registry.Config.Raw.UpdateTime { | |
| 52 | + if configOpen.Id != Config.Raw.Id || configOpen.UpdateTime != Config.Raw.UpdateTime { | |
| 53 | 53 | logs.Debug("__________________尝试更新活动条目ID:", configOpen.Id) |
| 54 | 54 | confNew := CardActivityConfigRaw{} |
| 55 | 55 | sql := fmt.Sprintf("select * from %s where id=%d limit 1", configTableName, configOpen.Id) |
| ... | ... | @@ -59,8 +59,8 @@ func LoadConfigs() { |
| 59 | 59 | return |
| 60 | 60 | } |
| 61 | 61 | logs.Debug("__________________更新活动条目ID:", confNew.Id) |
| 62 | - Registry.ConfigRaws = confNew | |
| 63 | - Registry.Decode(confNew) | |
| 62 | + ConfigRaws = confNew | |
| 63 | + ConfigDecode() | |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | ... | ... |
service/ht-cardholder/handle.go
| ... | ... | @@ -40,7 +40,7 @@ func HandleGetNew(req *dto.ReqHtCardHolderGetNew) (code string, rsp dto.RspHtCar |
| 40 | 40 | return |
| 41 | 41 | } |
| 42 | 42 | // 检查卡包ID |
| 43 | - _, okCardholderConfig := Registry.Config.CardholderConfig[strconv.Itoa(req.Id)] | |
| 43 | + _, okCardholderConfig := Config.CardholderConfig[strconv.Itoa(req.Id)] | |
| 44 | 44 | if !okCardholderConfig { |
| 45 | 45 | code = constd.RECODE_MERGE_CARDHOLDER_CARDHOLDERID_ERROR |
| 46 | 46 | return |
| ... | ... | @@ -141,9 +141,9 @@ func _LoadData(uid int64) (d *models.HtCardHolderData) { |
| 141 | 141 | func LoadData(uid int64) (d *models.HtCardHolderData) { |
| 142 | 142 | d = _LoadData(uid) |
| 143 | 143 | // 如果当前有上线活动(活动ID不为0),且活动ID和玩家数据不同,说明活动已切换 需更新 |
| 144 | - if Registry.Config.Id != 0 && | |
| 145 | - Registry.Config.Id != d.ActivityId { | |
| 146 | - d.ActivityId = Registry.Config.Id | |
| 144 | + if Config.Id != 0 && | |
| 145 | + Config.Id != d.ActivityId { | |
| 146 | + d.ActivityId = Config.Id | |
| 147 | 147 | d.DetailsData = models.NewHtCardHolderDataDetails() |
| 148 | 148 | SaveData(d) |
| 149 | 149 | } |
| ... | ... | @@ -153,13 +153,13 @@ func LoadData(uid int64) (d *models.HtCardHolderData) { |
| 153 | 153 | // CheckStatus 判断活动是否开启 |
| 154 | 154 | func CheckStatus() string { |
| 155 | 155 | sec := lxtime.NowUninx() |
| 156 | - if Registry.Config.Id == 0 { | |
| 156 | + if Config.Id == 0 { | |
| 157 | 157 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN1_ERROR |
| 158 | 158 | } |
| 159 | - if sec < Registry.Config.Raw.StartTime { | |
| 159 | + if sec < Config.Raw.StartTime { | |
| 160 | 160 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN1_ERROR |
| 161 | 161 | } |
| 162 | - if sec > Registry.Config.Raw.EndTime { | |
| 162 | + if sec > Config.Raw.EndTime { | |
| 163 | 163 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN2_ERROR |
| 164 | 164 | } |
| 165 | 165 | return constd.RECODE_OK | ... | ... |