fix: 修复空提交问题

This commit is contained in:
1
2025-11-16 20:48:12 +00:00
parent 82841491d0
commit baaec283e6
3 changed files with 31 additions and 40 deletions

View File

@@ -9,25 +9,24 @@ import (
type Done struct {
*Player //对玩家进行操作
*bus.Topic[*model.MilestoneData]
*bus.Topic[*model.MilestoneEX]
}
// 注册地图BOSS完成事件
// MAPID 地图ID
// BOSSID 地图BOSSID
// 注册胜利次数
//
func (d *Done) SPT(mapid, bossid, count uint32, fn func()) {
d.Topic.Sub(func(v *model.MilestoneData) {
d.Topic.Sub(func(v *model.MilestoneEX) {
if v.DoneType == model.MilestoneMode.BOSS && EqualBasicSlice(v.IDs, []uint32{mapid, bossid}) && v.Count == count {
if v.DoneType == model.MilestoneMode.BOSS && EqualBasicSlice(v.Args, []uint32{mapid, bossid}) && v.Count == count {
_, ok := lo.Find(v.IDs, func(v1 uint32) bool { //寻找是否触发过
_, ok := lo.Find(v.Args, func(v1 uint32) bool { //寻找是否触发过
return v1 == count
})
if !ok { //说明没有触发过
v.IDs = append(v.IDs, count) //把本次的记录添加
v.Results = append(v.Results, count) //把本次的记录添加
fn()
}
@@ -40,19 +39,16 @@ func (d *Done) SPT(mapid, bossid, count uint32, fn func()) {
// 分发事件 ,指定事件+1 并触发是否完成
func (d *Done) Pub(Donetype model.EnumMilestone, id []uint32) {
t := d.Service.Done.Get(Donetype, id)
s := model.MilestoneData{
d.Service.Done.Exec(Donetype, id, func(t *model.MilestoneEX) bool {
DoneType: Donetype,
IDs: id,
Args: t.Args,
Count: t.Count + 1,
}
d.Topic.Pub(&s) //异步发送,然后给事件+1
// d.Topic.PubAsyncCallBack(s, func() { //如果没执行完,说明奖励没发完,直接掉线
// d.Service.Done.Exec(s) //给计数器加1
// }) //提交触发里程碑奖励
d.Service.Done.Exec(s) //给计数器加1
d.Topic.Pub(t) //异步发送,然后给事件+1
t.Count++
// d.Topic.PubAsyncCallBack(s, func() { //如果没执行完,说明奖励没发完,直接掉线
// d.Service.Done.Exec(s) //给计数器加1
// }) //提交触发里程碑奖励
return true
}) //给计数器加1
}
// 方法1手动遍历性能最优

View File

@@ -14,22 +14,22 @@ var MilestoneMode = enum.New[struct {
}]()
// 里程碑数据结构与DoneEvent对应记录单条里程碑的详细信息
type MilestoneData struct {
DoneType EnumMilestone `json:"done_type"` // 里程碑类型0-地图解锁1-BOSS击杀2-物品收集等)
Args []uint32 `json:"args"` // 关联ID列表如地图ID、BOSSID、物品ID等
Results []uint32 `json:"results"` // 完成情况参数(如:击杀次数、收集数量阈值等)
Count uint32 `json:"count"` // 累计完成次数某BOSS累计击杀3次
//IsCompleted bool `json:"is_completed"` // 是否完全达成(用于区分阶段性里程碑)
}
// type MilestoneData struct {
// DoneType EnumMilestone `json:"done_type"` // 里程碑类型0-地图解锁1-BOSS击杀2-物品收集等)
// Args []uint32 `json:"args"` // 关联ID列表如地图ID、BOSSID、物品ID等
// Results []uint32 `json:"results"` // 完成情况参数(如:击杀次数、收集数量阈值等)
// Count uint32 `json:"count"` // 累计完成次数某BOSS累计击杀3次
// //IsCompleted bool `json:"is_completed"` // 是否完全达成(用于区分阶段性里程碑)
// }
const TableNameMilestone = "milestone"
// Milestone 数据库存储结构体映射milestone表
type Milestone struct {
*cool.Model
PlayerID uint64 `gorm:"not null;index:idx_milestone_by_player_id;comment:'所属玩家ID'" json:"player_id"`
DoneType uint32 `gorm:"not null;comment:'里程碑类型'" json:"done_type"`
Args string `gorm:"type:jsonb;not null;comment:'里程碑ID'" json:"args"`
PlayerID uint64 `gorm:"not null;index:idx_milestone_by_player_id;comment:'所属玩家ID'" json:"player_id"`
DoneType EnumMilestone `gorm:"not null;comment:'里程碑类型'" json:"done_type"`
Args string `gorm:"type:jsonb;not null;comment:'里程碑ID'" json:"args"`
// 注:不单独设置"里程碑ID",通过 PlayerID + DoneType + IDs 组合唯一标识一个里程碑(更灵活)
Results string `gorm:"type:jsonb;not null;comment:'里程碑参数'" json:"results"`
Count uint32 `gorm:"not null;comment:'里程碑完成次数'" json:"count"`
@@ -38,8 +38,8 @@ type Milestone struct {
// MilestoneEX 里程碑扩展结构体,用于业务层解析后的数据操作
type MilestoneEX struct {
Milestone
IDs []uint32 `json:"args"` // 解析后的里程碑详细数据
Args []uint32 `json:"results"` // 解析后的里程碑详细数据
Args []uint32 `json:"args"` // 解析后的里程碑详细数据
Results []uint32 `json:"results"` // 解析后的里程碑详细数据
}
// TableName 返回表名

View File

@@ -9,20 +9,15 @@ type DoneService struct {
BaseService
}
func (s *DoneService) Get(data model.EnumMilestone, id []uint32) model.MilestoneEX {
func (s *DoneService) Exec(data model.EnumMilestone, id []uint32, fn func(*model.MilestoneEX) bool) {
m := s.GModel(s.Model).Where("done_type", data).Where("args", id)
var tt model.MilestoneEX
m.Scan(&tt)
return tt
}
func (s *DoneService) Exec(data model.MilestoneData) {
m := s.GModel(s.Model).Where("done_type", data.DoneType).Where("args", data.Args)
var tt model.MilestoneEX
m.Scan(&tt)
tt.Args = data.Args
ook := fn(&tt)
if !ook { //不需要保存
return
}
_, err := m.Save(tt)
if err != nil {
panic(err)