refactor(item): 优化物品添加逻辑并移除冗余代码

- 修改 ItemAdd 方法签名,从可变参数改为两个独立参数 itemId 和 itemCnt
- 移除了对 model.ItemInfo 的依赖,简化调用方式
- 更新所有调用 ItemAdd 的地方以适配新接口
- 删除未使用的 imports 和注释掉的旧配置加载逻辑
- 修复购买物品时金币扣除与物品发放的一致性问题
- 增加玩家操作消耗塞尔豆的扣费逻辑(如宠物治疗、技能设置等)

此变更提升了代码简洁性和一致性,并增强了业务逻辑的准确性。
```
This commit is contained in:
2025-12-08 21:11:12 +08:00
parent 1436cc0117
commit 36ca75aa03
15 changed files with 170 additions and 138 deletions

View File

@@ -57,16 +57,16 @@ func (*Talk) GroupName() string {
// }
// }
// ResourceConfig 资源配置信息对应XML中的配置
type ResourceConfig struct {
Type uint32 `json:"type"` // 资源类型ID
MapID uint32 `json:"map_id"` // 所在地图ID
Name string `json:"name"` // 资源名称
CollectType string `json:"collect_type"` // 采集类型
MaxDailyCnt uint32 `json:"max_daily_cnt"` // 每日最大采集次数
Unit string `json:"unit"` // 单位
Dir uint32 `json:"dir"` // 方向
}
// // ResourceConfig 资源配置信息对应XML中的配置
// type ResourceConfig struct {
// Type uint32 `json:"type"` // 资源类型ID
// MapID uint32 `json:"map_id"` // 所在地图ID
// Name string `json:"name"` // 资源名称
// CollectType string `json:"collect_type"` // 采集类型
// MaxDailyCnt uint32 `json:"max_daily_cnt"` // 每日最大采集次数
// Unit string `json:"unit"` // 单位
// Dir uint32 `json:"dir"` // 方向
// }
// 初始化创建表
func init() {

View File

@@ -1,10 +1,8 @@
package service
import (
"blazing/common/utils"
"blazing/cool"
"blazing/modules/blazing/model"
dictmodel "blazing/modules/dict/model"
"blazing/modules/dict/service"
"context"
"strings"
@@ -38,11 +36,8 @@ func (s *PetFusionMaterialService) Data(Material1 [4]uint32) uint32 {
cacheKey := strings.Join(gconv.Strings(Material1[:]), ":")
println(cacheKey, "获取融合id")
fusions := service.DictInfoServiceS.DataRemark("fusion")
fusions := utils.ToMap(service.NewDictInfoService().DataOne("fusion"), func(t dictmodel.DictInfo) uint32 {
return gconv.Uint32(t.Remark) //物品id转id
})
for _, v := range Material1 {
_, ok := fusions[v]
if !ok {
@@ -70,15 +65,13 @@ func (s *PetFusionMaterialService) Data(Material1 [4]uint32) uint32 {
return effect, nil
}, 0)
effect2s := utils.ToMap(service.NewDictInfoService().DataOne("effect"), func(t dictmodel.DictInfo) uint32 {
return gconv.Uint32(t.ID)
})
effect2s := service.DictInfoServiceS.DataID("effect")
effect := ret.Interface().(*model.PetFusionMaterial)
if err != nil {
r := grand.Intn(len(service.NewDictInfoService().DataOne("effect")) - 1)
return gconv.Uint32(service.NewDictInfoService().DataOne("effect")[r].Remark)
for _, v := range effect2s {
return gconv.Uint32(v.Remark)
}
}
r := grand.Intn(4)

View File

@@ -3,6 +3,7 @@ package service
import (
"blazing/cool"
"blazing/modules/blazing/model"
"blazing/modules/dict/service"
)
type TalkService struct {
@@ -22,23 +23,34 @@ func NewTalkService(id uint32) *TalkService {
//实现挖矿次数确认
func (s *TalkService) Cheak(flag int) bool {
func (s *TalkService) Cheak(mapid uint32, flag int) (int, bool) {
m1 := s.GModel(s.Model)
var talks *model.Talk
m1.Where("talk_id", flag).Scan(&talks)
if talks == nil {
return true //如果表里没有记载数据,那么就可以直接挖矿
return 0, true //如果表里没有记载数据,那么就可以直接挖矿
}
//因为这个是挖一次更新一次,而且是实时更新的,如果更新日期是今天,那么就可以确认不用再重置,否则就需要重置挖矿记录
if !IsToday(talks.UpdateTime) {
talks.Count = 0
m1.Save(talks)
return true
return int(talks.Count), true
}
maps := service.DictInfoServiceS.DataRemark("mapid")
if uint32(maps[mapid].ID) != TalkConfigServiceS.GetCache(flag)[0].MapID {
return 0, false //没在地图
}
if talks.Count >= TalkConfigServiceS.GetCache(flag)[0].DailyCollectCount {
return 0, false
}
return int(talks.Count), true //int(config.MaxDailyCnt - talks.Count)
}
func (s *TalkService) Update(flag int) {

View File

@@ -11,6 +11,8 @@ type TalkConfigService struct {
*cool.Service
}
var TalkConfigServiceS = NewTalkConfigService()
func NewTalkConfigService() *TalkConfigService {
return &TalkConfigService{
@@ -19,3 +21,10 @@ func NewTalkConfigService() *TalkConfigService {
}
}
func (s *TalkConfigService) GetCache(flag int) []model.MineralCollectionConfig {
var config []model.MineralCollectionConfig
cool.DBM(s.Model).Where("type", flag).Scan(&config)
return config
}

View File

@@ -19,7 +19,7 @@ func init() {
&cool.Controller{
Prefix: "/admin/dict/info",
Api: []string{"Add", "Delete", "Update", "Info", "List", "Page"},
Service: service.NewDictInfoService(),
Service: service.DictInfoServiceS,
},
}
// 注册路由
@@ -34,7 +34,7 @@ type DictInfoDataReq struct {
// Data 方法 获得字典数据
func (c *DictInfoController) Data(ctx context.Context, req *DictInfoDataReq) (res *cool.BaseRes, err error) {
service := service.NewDictInfoService()
service := service.DictInfoServiceS
data, err := service.Data(ctx, req.Types)
res = cool.Ok(data)
return

View File

@@ -3,6 +3,7 @@ package service
import (
"context"
"blazing/common/utils"
"blazing/cool"
"blazing/modules/dict/model"
@@ -54,7 +55,7 @@ func (s *DictInfoService) Data(ctx context.Context, types []string) (data interf
}
return
}
func (s *DictInfoService) DataOne(types string) (data []model.DictInfo) {
func (s *DictInfoService) DataRemark(types string) (data map[uint32]model.DictInfo) {
var (
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
@@ -72,10 +73,42 @@ func (s *DictInfoService) DataOne(types string) (data []model.DictInfo) {
m := cool.DBM(dictInfoModel)
var ress []model.DictInfo
m.Where("typeId", ty.ID).Scan(&ress)
return ress, nil
fusions := utils.ToMap(ress, func(t model.DictInfo) uint32 {
return gconv.Uint32(t.Remark) //物品id转id
})
return fusions, nil
}, 0)
return ret.Interface().([]model.DictInfo)
return ret.Interface().(map[uint32]model.DictInfo)
}
func (s *DictInfoService) DataID(types string) (data map[uint32]model.DictInfo) {
var (
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
)
ret, _ := cacheDict.GetOrSetFunc(context.Background(), types, func(context.Context) (interface{}, error) {
// 如果typeData为空, 则返回空
var ty *model.DictType
mType := cool.DBM(dictTypeModel)
mType.Where("key in (?)", types).Scan(&ty)
// 如果typeData为空, 则返回空
if ty == nil {
return []model.DictInfo{}, nil
}
m := cool.DBM(dictInfoModel)
var ress []model.DictInfo
m.Where("typeId", ty.ID).Scan(&ress)
fusions := utils.ToMap(ress, func(t model.DictInfo) uint32 {
return gconv.Uint32(t.ID) //物品id转id
})
return fusions, nil
}, 0)
return ret.Interface().(map[uint32]model.DictInfo)
}
@@ -122,6 +155,8 @@ func delChildDict(id int64) error {
return err
}
var DictInfoServiceS = NewDictInfoService()
// NewDictInfoService 初始化 DictInfoService
func NewDictInfoService() *DictInfoService {
return &DictInfoService{