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,124 +5,119 @@ import ( | ||
| 5 | "apigame/service/constd" | 5 | "apigame/service/constd" |
| 6 | "encoding/json" | 6 | "encoding/json" |
| 7 | "errors" | 7 | "errors" |
| 8 | + "fmt" | ||
| 8 | ) | 9 | ) |
| 9 | 10 | ||
| 10 | var ( | 11 | var ( |
| 11 | - Registry RegistryConfigs // 卡牌活动配置 | ||
| 12 | -) | ||
| 13 | - | ||
| 14 | -// RegistryConfigs 卡牌活动配置 | ||
| 15 | -type RegistryConfigs struct { | ||
| 16 | ConfigRaws CardActivityConfigRaw // 活动配置 原始数据 | 12 | ConfigRaws CardActivityConfigRaw // 活动配置 原始数据 |
| 17 | Config CardActivityConfig // 活动配置 分析后数据 | 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 | Awards: make(map[string]string), | 26 | Awards: make(map[string]string), |
| 30 | AlbumConfig: make(map[string]AlbumConfig), | 27 | AlbumConfig: make(map[string]AlbumConfig), |
| 31 | CardConfig: make(map[int]CardConfig), | 28 | CardConfig: make(map[int]CardConfig), |
| 32 | CardholderConfig: make(map[string]CardholderConfig), | 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 | if err != nil { | 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 | return | 38 | return |
| 42 | } | 39 | } |
| 43 | } | 40 | } |
| 44 | // 卡组配置 | 41 | // 卡组配置 |
| 45 | { | 42 | { |
| 46 | configs := make([]AlbumConfig, 0) | 43 | configs := make([]AlbumConfig, 0) |
| 47 | - err := json.Unmarshal([]byte(confRaw.AlbumConfig), &configs) | 44 | + err := json.Unmarshal([]byte(ConfigRaws.AlbumConfig), &configs) |
| 48 | if err != nil { | 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 | return | 47 | return |
| 51 | } | 48 | } |
| 52 | for _, i2 := range configs { | 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 | configs := make([]CardConfig, 0) | 55 | configs := make([]CardConfig, 0) |
| 59 | - err := json.Unmarshal([]byte(confRaw.CardConfig), &configs) | 56 | + err := json.Unmarshal([]byte(ConfigRaws.CardConfig), &configs) |
| 60 | if err != nil { | 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 | return | 59 | return |
| 63 | } | 60 | } |
| 64 | for _, i2 := range configs { | 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 | configs := make([]CardholderConfig, 0) | 67 | configs := make([]CardholderConfig, 0) |
| 71 | - err := json.Unmarshal([]byte(confRaw.CardHolderConfig), &configs) | 68 | + err := json.Unmarshal([]byte(ConfigRaws.CardHolderConfig), &configs) |
| 72 | if err != nil { | 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 | return | 71 | return |
| 75 | } | 72 | } |
| 76 | for _, i2 := range configs { | 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 | configs := make([]NormalCardStarSequence, 0) | 79 | configs := make([]NormalCardStarSequence, 0) |
| 83 | - err := json.Unmarshal([]byte(confRaw.NormalCardStarSequence), &configs) | 80 | + err := json.Unmarshal([]byte(ConfigRaws.NormalCardStarSequence), &configs) |
| 84 | if err != nil { | 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 | return | 83 | return |
| 87 | } | 84 | } |
| 88 | for _, i2 := range configs { | 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 | configs := make([]CardSequenceConfig, 0) | 92 | configs := make([]CardSequenceConfig, 0) |
| 95 | - err := json.Unmarshal([]byte(confRaw.CardSequenceConfig), &configs) | 93 | + err := json.Unmarshal([]byte(ConfigRaws.CardSequenceConfig), &configs) |
| 96 | if err != nil { | 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 | return | 96 | return |
| 99 | } | 97 | } |
| 100 | for _, i2 := range configs { | 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 | // FindCardSequenceConfig 查找配置 星级ID对应的卡片 | 117 | // FindCardSequenceConfig 查找配置 星级ID对应的卡片 |
| 115 | func FindCardSequenceConfig(id, sequenceId, cohort string) (conf CardSequenceConfig, has bool) { | 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 | if !has { | 121 | if !has { |
| 127 | lxalilog.Errors(errors.New("ht_cardholder CardSequenceConfig error"), " LoadData Error", id, sequenceId, cohort) | 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,13 +12,13 @@ type CardActivityUpdateConfig struct { | ||
| 12 | // CardActivityConfig 卡牌活动配置 分析后数据 | 12 | // CardActivityConfig 卡牌活动配置 分析后数据 |
| 13 | type CardActivityConfig struct { | 13 | type CardActivityConfig struct { |
| 14 | Raw CardActivityConfigRaw | 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 | func (c *CardActivityConfig) TableName() string { | 24 | func (c *CardActivityConfig) TableName() string { |
service/ht-cardholder/configs.go
| @@ -11,7 +11,7 @@ import ( | @@ -11,7 +11,7 @@ import ( | ||
| 11 | // Init 初始化 | 11 | // Init 初始化 |
| 12 | func Init() { | 12 | func Init() { |
| 13 | 13 | ||
| 14 | - NewRegistryConfigs() | 14 | + NewConfigs() |
| 15 | 15 | ||
| 16 | TryUpdateConfigs() | 16 | TryUpdateConfigs() |
| 17 | 17 | ||
| @@ -49,7 +49,7 @@ func LoadConfigs() { | @@ -49,7 +49,7 @@ func LoadConfigs() { | ||
| 49 | return | 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 | logs.Debug("__________________尝试更新活动条目ID:", configOpen.Id) | 53 | logs.Debug("__________________尝试更新活动条目ID:", configOpen.Id) |
| 54 | confNew := CardActivityConfigRaw{} | 54 | confNew := CardActivityConfigRaw{} |
| 55 | sql := fmt.Sprintf("select * from %s where id=%d limit 1", configTableName, configOpen.Id) | 55 | sql := fmt.Sprintf("select * from %s where id=%d limit 1", configTableName, configOpen.Id) |
| @@ -59,8 +59,8 @@ func LoadConfigs() { | @@ -59,8 +59,8 @@ func LoadConfigs() { | ||
| 59 | return | 59 | return |
| 60 | } | 60 | } |
| 61 | logs.Debug("__________________更新活动条目ID:", confNew.Id) | 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,7 +40,7 @@ func HandleGetNew(req *dto.ReqHtCardHolderGetNew) (code string, rsp dto.RspHtCar | ||
| 40 | return | 40 | return |
| 41 | } | 41 | } |
| 42 | // 检查卡包ID | 42 | // 检查卡包ID |
| 43 | - _, okCardholderConfig := Registry.Config.CardholderConfig[strconv.Itoa(req.Id)] | 43 | + _, okCardholderConfig := Config.CardholderConfig[strconv.Itoa(req.Id)] |
| 44 | if !okCardholderConfig { | 44 | if !okCardholderConfig { |
| 45 | code = constd.RECODE_MERGE_CARDHOLDER_CARDHOLDERID_ERROR | 45 | code = constd.RECODE_MERGE_CARDHOLDER_CARDHOLDERID_ERROR |
| 46 | return | 46 | return |
| @@ -141,9 +141,9 @@ func _LoadData(uid int64) (d *models.HtCardHolderData) { | @@ -141,9 +141,9 @@ func _LoadData(uid int64) (d *models.HtCardHolderData) { | ||
| 141 | func LoadData(uid int64) (d *models.HtCardHolderData) { | 141 | func LoadData(uid int64) (d *models.HtCardHolderData) { |
| 142 | d = _LoadData(uid) | 142 | d = _LoadData(uid) |
| 143 | // 如果当前有上线活动(活动ID不为0),且活动ID和玩家数据不同,说明活动已切换 需更新 | 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 | d.DetailsData = models.NewHtCardHolderDataDetails() | 147 | d.DetailsData = models.NewHtCardHolderDataDetails() |
| 148 | SaveData(d) | 148 | SaveData(d) |
| 149 | } | 149 | } |
| @@ -153,13 +153,13 @@ func LoadData(uid int64) (d *models.HtCardHolderData) { | @@ -153,13 +153,13 @@ func LoadData(uid int64) (d *models.HtCardHolderData) { | ||
| 153 | // CheckStatus 判断活动是否开启 | 153 | // CheckStatus 判断活动是否开启 |
| 154 | func CheckStatus() string { | 154 | func CheckStatus() string { |
| 155 | sec := lxtime.NowUninx() | 155 | sec := lxtime.NowUninx() |
| 156 | - if Registry.Config.Id == 0 { | 156 | + if Config.Id == 0 { |
| 157 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN1_ERROR | 157 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN1_ERROR |
| 158 | } | 158 | } |
| 159 | - if sec < Registry.Config.Raw.StartTime { | 159 | + if sec < Config.Raw.StartTime { |
| 160 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN1_ERROR | 160 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN1_ERROR |
| 161 | } | 161 | } |
| 162 | - if sec > Registry.Config.Raw.EndTime { | 162 | + if sec > Config.Raw.EndTime { |
| 163 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN2_ERROR | 163 | return constd.RECODE_MERGE_CARDHOLDER_NOTOPEN2_ERROR |
| 164 | } | 164 | } |
| 165 | return constd.RECODE_OK | 165 | return constd.RECODE_OK |