74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package player
|
||
|
||
import (
|
||
"github.com/badu/bus"
|
||
"github.com/samber/lo"
|
||
|
||
"blazing/modules/blazing/model"
|
||
)
|
||
|
||
type Done struct {
|
||
*Player //对玩家进行操作
|
||
*bus.Topic[*model.MilestoneEX]
|
||
}
|
||
|
||
func NewDone(P *Player) Done {
|
||
return Done{
|
||
Player: P,
|
||
Topic: bus.NewTopic[*model.MilestoneEX](),
|
||
}
|
||
|
||
}
|
||
|
||
// 注册地图BOSS完成事件
|
||
// MAPID 地图ID
|
||
// BOSSID 地图BOSSID
|
||
// 注册胜利次数
|
||
func (d *Done) SPT(mapid, bossid, count uint32, fn func()) {
|
||
d.Topic.Sub(func(v *model.MilestoneEX) {
|
||
|
||
if v.DoneType == model.MilestoneMode.BOSS && EqualBasicSlice(v.Args, []uint32{mapid, bossid}) && v.Count == count {
|
||
|
||
_, ok := lo.Find(v.Args, func(v1 uint32) bool { //寻找是否触发过
|
||
|
||
return v1 == count
|
||
})
|
||
if !ok { //说明没有触发过
|
||
v.Results = append(v.Results, count) //把本次的记录添加
|
||
fn()
|
||
}
|
||
|
||
}
|
||
|
||
})
|
||
|
||
}
|
||
|
||
// 分发事件 ,指定事件+1 并触发是否完成
|
||
func (d *Done) Done(Donetype model.EnumMilestone, id []uint32) {
|
||
|
||
d.Service.Done.Exec(Donetype, id, func(t *model.MilestoneEX) bool {
|
||
|
||
d.Topic.Pub(t) //异步发送,然后给事件+1
|
||
|
||
t.Count++
|
||
// d.Topic.PubAsyncCallBack(s, func() { //如果没执行完,说明奖励没发完,直接掉线
|
||
// d.Service.Done.Exec(s) //给计数器加1
|
||
// }) //提交触发里程碑奖励
|
||
return true
|
||
}) //给计数器加1
|
||
}
|
||
|
||
// 方法1:手动遍历(性能最优)
|
||
func EqualBasicSlice[T int | string | bool | uint32](a, b []T) bool {
|
||
if len(a) != len(b) {
|
||
return false
|
||
}
|
||
for i := range a {
|
||
if a[i] != b[i] {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|