diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 83e73ed9d..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "go.toolsEnvVars": { - "GOROOT": "E:\\golang\\go" - } -} \ No newline at end of file diff --git a/common/cool/global.go b/common/cool/global.go index 261bd6e80..5857451c9 100644 --- a/common/cool/global.go +++ b/common/cool/global.go @@ -1,8 +1,10 @@ package cool import ( + "blazing/common/data/entity" + "blazing/common/utils" "context" - "sync" + "reflect" "github.com/gogf/gf/v2/os/glog" "github.com/gogf/gf/v2/util/gconv" @@ -10,8 +12,8 @@ import ( ) var ( - Mainplayer sync.Map //玩家数据 - CmdCache sync.Map //命令缓存 + Mainplayer = &utils.SyncMap[uint32, *entity.Player]{} //玩家数据 + CmdCache = &utils.SyncMap[uint32, reflect.Value]{} //命令缓存 ) func init() { @@ -29,3 +31,13 @@ func init() { glog.Debug(context.Background(), "初始化雪花算法", newId) } +func ConutPlayer() int { + + count := 0 + Mainplayer.Range(func(uint32, *entity.Player) bool { + count++ + return true // 继续遍历 + }) + return count + //fmt.Println("元素数量:", count) // 输出: 3 +} diff --git a/common/data/entity/player.go b/common/data/entity/player.go index 36c0a6b9a..7900231b0 100644 --- a/common/data/entity/player.go +++ b/common/data/entity/player.go @@ -1,7 +1,7 @@ package entity import ( - "blazing/cool" + "context" "fmt" "sync" @@ -54,16 +54,7 @@ func (p *Player) SendPack(b []byte) error { return err } -func ConutPlayer() int { - count := 0 - cool.Mainplayer.Range(func(key, value interface{}) bool { - count++ - return true // 继续遍历 - }) - return count - //fmt.Println("元素数量:", count) // 输出: 3 -} // IsLoggedIn 检查是否已登录 func (lw *Player) IsLoggedIn() bool { diff --git a/common/utils/syncmap.go b/common/utils/syncmap.go index 3c7c7619b..52979be32 100644 --- a/common/utils/syncmap.go +++ b/common/utils/syncmap.go @@ -1,64 +1,42 @@ package utils import ( - "fmt" - "sync" + "sync" ) // 定义一个泛型的 SyncMap,支持任意类型的键和值 type SyncMap[K comparable, V any] struct { - m sync.Map + m sync.Map } // 设置键值对 func (sm *SyncMap[K, V]) Store(key K, value V) { - sm.m.Store(key, value) + sm.m.Store(key, value) +} +func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) { + val, ok := sm.m.LoadOrStore(key, value) + return val.(V), ok + } // 获取键对应的值 func (sm *SyncMap[K, V]) Load(key K) (V, bool) { - val, ok := sm.m.Load(key) - if ok { - return val.(V), true - } - var zeroValue V - return zeroValue, false + val, ok := sm.m.Load(key) + if ok { + return val.(V), true + } + var zeroValue V + return zeroValue, false } // 删除键值对 func (sm *SyncMap[K, V]) Delete(key K) { - sm.m.Delete(key) + sm.m.Delete(key) } // 遍历所有键值对 func (sm *SyncMap[K, V]) Range(f func(key K, value V) bool) { - sm.m.Range(func(key, value any) bool { - return f(key.(K), value.(V)) - }) + sm.m.Range(func(key, value any) bool { + return f(key.(K), value.(V)) + }) } - -func main() { - // 使用 SyncMap 存储字符串 -> int 类型的键值对 - sm1 := &SyncMap[string, int]{} - sm1.Store("apple", 5) - sm1.Store("banana", 10) - - // 加载并打印值 - if value, ok := sm1.Load("apple"); ok { - fmt.Println("apple:", value) - } - if value, ok := sm1.Load("banana"); ok { - fmt.Println("banana:", value) - } - - // 使用 SyncMap 存储 int -> string 类型的键值对 - sm2 := &SyncMap[int, string]{} - sm2.Store(1, "one") - sm2.Store(2, "two") - - // 遍历所有键值对 - sm2.Range(func(key int, value string) bool { - fmt.Printf("%d: %s\n", key, value) - return true - }) -} \ No newline at end of file diff --git a/common/utils/test_test.go b/common/utils/test_test.go index 6ea684ae8..ebec41a22 100644 --- a/common/utils/test_test.go +++ b/common/utils/test_test.go @@ -40,6 +40,31 @@ func TestThree(t *testing.T) { } fmt.Println(b.Bytes()) } +func TestMap(t *testing.T) { + // 使用 SyncMap 存储字符串 -> int 类型的键值对 + sm1 := &SyncMap[string, int]{} + sm1.Store("apple", 5) + sm1.Store("banana", 10) + + // 加载并打印值 + if value, ok := sm1.Load("apple"); ok { + fmt.Println("apple:", value) + } + if value, ok := sm1.Load("banana"); ok { + fmt.Println("banana:", value) + } + + // 使用 SyncMap 存储 int -> string 类型的键值对 + sm2 := &SyncMap[int, string]{} + sm2.Store(1, "one") + sm2.Store(2, "two") + + // 遍历所有键值对 + sm2.Range(func(key int, value string) bool { + fmt.Printf("%d: %s\n", key, value) + return true + }) +} func TestInit(t *testing.T) { table := termtables.CreateTable() diff --git a/logic/controller/controller.go b/logic/controller/controller.go index e973a873b..91610634e 100644 --- a/logic/controller/controller.go +++ b/logic/controller/controller.go @@ -46,9 +46,9 @@ func (h *Controller) QuitSelf(a int) error { for { //entity.ConutPlayer() - fmt.Println("当前在线人数", entity.ConutPlayer()) + fmt.Println("当前在线人数", cool.ConutPlayer()) - if entity.ConutPlayer() <= 0 { + if cool.ConutPlayer() <= 0 { //执行退出逻辑 os.Exit(1) } @@ -165,13 +165,13 @@ func getcmd(t reflect.Type) uint32 { // 遍历结构体方法并执行RECV_cmd func Recv(c gnet.Conn, data handler.TomeeHeader) { - tt, ok := cool.CmdCache.Load(data.CMD) + cmdlister, ok := cool.CmdCache.Load(data.CMD) if !ok { glog.Error(context.Background(), data.CMD, "cmd未注册") return //TODO 待实现cmd未注册 } - cmdlister := tt.(reflect.Value) + // fmt.Println(cmdlister) params := []reflect.Value{} diff --git a/logic/service/service.go b/logic/service/service.go index 0a4ad757d..2a494673a 100644 --- a/logic/service/service.go +++ b/logic/service/service.go @@ -19,8 +19,8 @@ func GetPlayer(c gnet.Conn, userid uint32) *entity.Player { //TODO 这里待优 } var player *entity.Player if player1, ok := cool.Mainplayer.Load((userid)); ok { - player = player1.(*entity.Player) //取成功,否则创建 - clientdata.SetPlayer(player) + + clientdata.SetPlayer(player1) } return player @@ -30,14 +30,14 @@ func KickPlayer(userid uint32) { //踢出玩家 //TODO 返回错误码 //var player *entity.Player if player1, ok := cool.Mainplayer.Load((userid)); ok { - player := player1.(*entity.Player) //取成功,否则创建 + //取成功,否则创建 head := handler.NewTomeeHeader() head.Result = uint32(errorcode.ErrorCodes.ErrAlreadyLoggedIn) head.UserID = userid head.CMD = 1001 - player.SendPack(head.Pack(nil)) - player.MainConn.MainConn.Close() + player1.SendPack(head.Pack(nil)) + player1.MainConn.MainConn.Close() // clientdata.Player = player }