2025-06-26 23:20:11 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"blazing/common/data/entity"
|
2025-07-06 19:31:30 +08:00
|
|
|
|
"blazing/common/socket/errorcode"
|
|
|
|
|
|
"blazing/common/socket/handler"
|
2025-07-15 12:14:17 +08:00
|
|
|
|
"blazing/cool"
|
2025-06-26 23:20:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-07-26 02:14:54 +00:00
|
|
|
|
func GetPlayer(c *entity.Conn, userid uint32) *entity.Player { //TODO 这里待优化,可能存在内存泄漏问题
|
2025-06-26 23:20:11 +08:00
|
|
|
|
|
|
|
|
|
|
//检查player初始化,是否为conn初始后取map,防止二次连接后存在两个player
|
|
|
|
|
|
|
2025-07-26 02:14:54 +00:00
|
|
|
|
clientdata := c.MainConn.Context().(*entity.ClientData)
|
2025-07-06 22:58:39 +08:00
|
|
|
|
if clientdata.GetPlayer() != nil {
|
|
|
|
|
|
return clientdata.GetPlayer()
|
2025-06-26 23:20:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
var player *entity.Player
|
2025-07-15 12:14:17 +08:00
|
|
|
|
if player1, ok := cool.Mainplayer.Load((userid)); ok {
|
2025-07-25 13:08:57 +00:00
|
|
|
|
|
2025-07-25 06:22:16 +00:00
|
|
|
|
clientdata.SetPlayer(player1)
|
2025-06-26 23:20:11 +08:00
|
|
|
|
}
|
2025-06-27 22:40:49 +08:00
|
|
|
|
|
|
|
|
|
|
return player
|
|
|
|
|
|
// return nil
|
|
|
|
|
|
}
|
2025-07-06 19:31:30 +08:00
|
|
|
|
func KickPlayer(userid uint32) { //踢出玩家
|
|
|
|
|
|
//TODO 返回错误码
|
|
|
|
|
|
//var player *entity.Player
|
2025-07-15 12:14:17 +08:00
|
|
|
|
if player1, ok := cool.Mainplayer.Load((userid)); ok {
|
2025-07-25 13:08:57 +00:00
|
|
|
|
//取成功,否则创建
|
2025-08-24 17:33:19 +08:00
|
|
|
|
head := handler.NewTomeeHeader(1001, userid)
|
2025-07-31 08:04:23 +00:00
|
|
|
|
head.Result = uint32(errorcode.ErrorCodes.ErrAccountLoggedInElsewhere)
|
2025-07-06 19:31:30 +08:00
|
|
|
|
|
2025-07-25 06:22:16 +00:00
|
|
|
|
player1.SendPack(head.Pack(nil))
|
|
|
|
|
|
player1.MainConn.MainConn.Close()
|
2025-07-06 19:31:30 +08:00
|
|
|
|
// clientdata.Player = player
|
|
|
|
|
|
}
|
2025-06-27 22:40:49 +08:00
|
|
|
|
|
2025-07-06 19:31:30 +08:00
|
|
|
|
//return player
|
|
|
|
|
|
// return nil
|
|
|
|
|
|
}
|
2025-07-26 02:14:54 +00:00
|
|
|
|
func SetPlayer(c *entity.Conn, userid uint32) *entity.Player { //TODO 这里待优化,
|
2025-07-06 23:17:19 +08:00
|
|
|
|
|
2025-07-26 02:14:54 +00:00
|
|
|
|
clientdata := c.MainConn.Context().(*entity.ClientData)
|
2025-07-06 23:17:19 +08:00
|
|
|
|
|
2025-06-27 22:40:49 +08:00
|
|
|
|
player := entity.NewPlayer(
|
|
|
|
|
|
entity.WithUserID(userid), //注入ID
|
2025-07-31 08:04:23 +00:00
|
|
|
|
entity.WithConn(*c), //注入conn
|
2025-06-27 22:40:49 +08:00
|
|
|
|
)
|
2025-07-15 12:14:17 +08:00
|
|
|
|
cool.Mainplayer.Store(userid, player)
|
2025-06-27 22:40:49 +08:00
|
|
|
|
|
2025-07-06 22:58:39 +08:00
|
|
|
|
clientdata.SetPlayer(player) //= player
|
2025-07-06 23:17:19 +08:00
|
|
|
|
|
2025-06-26 23:20:11 +08:00
|
|
|
|
return player
|
|
|
|
|
|
// return nil
|
|
|
|
|
|
}
|
2025-07-25 13:08:57 +00:00
|
|
|
|
|
2025-07-31 07:31:25 +00:00
|
|
|
|
// /**
|
|
|
|
|
|
// * @var type OutboundInf
|
|
|
|
|
|
// * @global
|
|
|
|
|
|
// */
|
|
|
|
|
|
// type OutInfo interface {
|
|
|
|
|
|
// error //实现错误接口
|
|
|
|
|
|
// Code() errorcode.ErrorCode //返回错误码,如果error不等于nil就返回这个实现对前台传错误码
|
|
|
|
|
|
// }
|