2025-09-14 01:35:16 +08:00
|
|
|
|
package player
|
|
|
|
|
|
|
2025-10-14 03:07:55 +08:00
|
|
|
|
import (
|
|
|
|
|
|
"github.com/panjf2000/gnet/v2"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func GetPlayer(c gnet.Conn, userid uint32) *Player { //TODO 这里待优化,可能存在内存泄漏问题
|
2025-10-13 18:51:41 +08:00
|
|
|
|
|
2025-09-14 01:35:16 +08:00
|
|
|
|
//检查player初始化,是否为conn初始后取map,防止二次连接后存在两个player
|
2025-10-14 02:23:00 +08:00
|
|
|
|
|
2025-11-10 02:45:19 +00:00
|
|
|
|
clientdata, ok := c.Context().(*ClientData)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-10-10 02:57:20 +00:00
|
|
|
|
if clientdata.Player == nil {
|
|
|
|
|
|
|
2026-02-21 22:41:59 +08:00
|
|
|
|
clientdata.Player = NewPlayer(c)
|
2025-09-14 01:35:16 +08:00
|
|
|
|
|
2025-10-10 02:57:20 +00:00
|
|
|
|
}
|
2025-09-14 01:35:16 +08:00
|
|
|
|
|
|
|
|
|
|
// gff := socket.NewClientData()
|
|
|
|
|
|
|
|
|
|
|
|
// gff.Player = clientdata.Player
|
|
|
|
|
|
// c.MainConn.SetContext(gff)
|
|
|
|
|
|
Mainplayer.Store(userid, clientdata.Player)
|
|
|
|
|
|
|
|
|
|
|
|
return clientdata.Player
|
|
|
|
|
|
// return nil
|
|
|
|
|
|
}
|
2025-10-16 12:16:03 +08:00
|
|
|
|
func KickPlayer(userid uint32) error { //踢出玩家
|
2025-09-14 01:35:16 +08:00
|
|
|
|
//TODO 返回错误码
|
|
|
|
|
|
//var player *entity.Player
|
2025-10-03 20:20:17 +08:00
|
|
|
|
if player1, ok := Mainplayer.Load(userid); ok {
|
2026-02-07 00:18:14 +08:00
|
|
|
|
player1.Kick(false)
|
2025-10-10 03:06:23 +00:00
|
|
|
|
|
2025-09-14 01:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//return player
|
2025-10-16 12:16:03 +08:00
|
|
|
|
return nil
|
2025-09-14 01:35:16 +08:00
|
|
|
|
}
|