"refactor(player): 添加错误检查方法并迁移任务测试代码到独立文件"

This commit is contained in:
1
2025-08-05 23:49:31 +00:00
parent 0f6c8d9329
commit a9dfe6aac5
4 changed files with 100 additions and 78 deletions

View File

@@ -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 {

View File

@@ -0,0 +1 @@
package entity

View File

@@ -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}
}

91
common/utils/task_test.go Normal file
View File

@@ -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
}