diff --git a/common/data/entity/player.go b/common/data/entity/player.go index 3497f5a8e..1b6e09772 100644 --- a/common/data/entity/player.go +++ b/common/data/entity/player.go @@ -5,6 +5,8 @@ import ( "fmt" "sync" "time" + + "github.com/gogf/gf/v2/frame/g" ) type Player struct { @@ -50,6 +52,12 @@ func (p *Player) SendPack(b []byte) error { err := p.MainConn.SendPack(b) return err } +func (p *Player) Cheak(b error) { + if b != nil { + g.Log().Error(context.Background(), "出现错误", p.UserID, b.Error()) + } + +} // IsLoggedIn 检查是否已登录 func (lw *Player) IsLoggedIn() bool { diff --git a/common/data/entity/task.go b/common/data/entity/task.go new file mode 100644 index 000000000..9356433d1 --- /dev/null +++ b/common/data/entity/task.go @@ -0,0 +1 @@ +package entity diff --git a/common/utils/sqrt_test.go b/common/utils/sqrt_test.go index 315b33a16..95d0b55e2 100644 --- a/common/utils/sqrt_test.go +++ b/common/utils/sqrt_test.go @@ -4,8 +4,6 @@ import ( "fmt" "math" "testing" - - "github.com/badu/bus" ) func Test_fastSqrt(t *testing.T) { @@ -91,79 +89,3 @@ func BenchmarkFastSqrtInt(b *testing.B) { } } } - -func Test_event(b *testing.T) { - - topic := bus.NewTopic[*Task]() //直接注册到用户 - for i := 0; i < 1; i++ { - isdone := false - t := topic.Sub(func(v *Task) { //用户初始化时注册里程碑 - if v.done { //如果任务完成 - isdone = true - fmt.Println(v.id, "任务完成") - return - } - - if v.cfunc != nil { - v.cont = v.cfunc(v.cont) //增加计数 - } - - if v.cont > 0 { //如果计数达到,标记任务完成 - v.done = true - } - fmt.Println(v.id, "当前任务计数", v.cont, "是否完成", v.done) - - }) - if isdone { //如果任务完成,取消注册 - t.Cancel() - } - - } - task1 := &Task{id: 1, cfunc: func(t uint32) uint32 { //满足任务1后推定 - - fmt.Println(1, "当前任务计数", t) - //实现自定义逻辑 - //增加用户计数 - //增加服务器总计数 - return t + 1 - - }} - topic.Pub(task1) - topic.Pub(&Task{id: 2, done: true}) - - topic.Pub(task1) //发送事件 - -} - -// Various event types -const EventA = 0x01 - -type Task struct { - id uint32 - cfunc func(uint32) uint32 - cont uint32 - done bool -} - -// Event type for testing purposes -type Event struct { - Data string - type1 uint32 -} - -// Type returns the event type -func (ev Event) Type() uint32 { - return ev.type1 -} - -// newEventA creates a new instance of an event -func newEventA(data string) Event { - return Event{Data: data, type1: EventA} -} - -// Various event types -const EventB1 = 0x02 - -func newEventB(data string) Event { - return Event{Data: data, type1: EventB1} -} diff --git a/common/utils/task_test.go b/common/utils/task_test.go new file mode 100644 index 000000000..5a0017a65 --- /dev/null +++ b/common/utils/task_test.go @@ -0,0 +1,91 @@ +package utils + +import ( + "fmt" + "sync" + "testing" + + "github.com/badu/bus" +) + +func Test_event(b *testing.T) { + + topic := bus.NewTopic[*Task]() //直接注册到用户 + for i := 0; i < 1; i++ { + isdone := false + t := topic.Sub(func(v *Task) { //用户初始化时注册里程碑 + if v.done { //如果任务完成 + isdone = true + fmt.Println(v.id, "任务完成") + return + } + + if v.cfunc != nil { + v.cont = v.cfunc(v.cont) //增加计数 + } + + if v.cont > 5 { //如果计数达到,标记任务完成 + v.done = true + } + fmt.Println(v.id, "当前任务计数", v.cont, "是否完成", v.done) + + }) + if isdone { //如果任务完成,取消注册 + t.Cancel() + } + + } + task1 := &Task{id: 1, cfunc: func(t uint32) uint32 { //满足任务1后推定 + + //fmt.Println(1, "当前任务计数", t) + //实现自定义逻辑 + //增加用户计数 + //增加服务器总计数 + return t + 1 + + }} + topic.Pub(task1) + topic.Pub(task1) + topic.Pub(&Task{id: 2, done: true}) + + topic.Pub(task1) //发送事件 + task1.Complete() + + fmt.Println("当前任务状态", task1.IsDone()) +} + +type TaskTree struct { + AllTasks []*Task //任务集合 + mu sync.Mutex +} + +// Various event types +const EventA = 0x01 + +type Task struct { + id uint32 + cfunc func(uint32) uint32 + cont uint32 + done bool +} + +func (e *Task) IsDone() bool { + return e.done +} +func (e *Task) Complete() { + e.done = true +} +func (e *Task) EventID() string { + return "YourInterestingEventName" +} + +// Event type for testing purposes +type Event struct { + Data string + type1 uint32 +} + +// Type returns the event type +func (ev Event) Type() uint32 { + return ev.type1 +}