package player

import "blazing/common/socket/errorcode"

func GetPlayer(c *Conn, userid uint32) *Player { //TODO 这里待优化,可能存在内存泄漏问题
	c.Mu.Lock()
	defer c.Mu.Unlock()
	//检查player初始化，是否为conn初始后取map，防止二次连接后存在两个player

	clientdata := c.MainConn.Context().(*ClientData)
	if clientdata.Player != nil {
		return clientdata.Player
	}

	clientdata.Player = NewPlayer(

		WithConn(c), //注入conn
	)

	// gff := socket.NewClientData()

	// gff.Player = clientdata.Player
	// c.MainConn.SetContext(gff)
	Mainplayer.Store(userid, clientdata.Player)

	return clientdata.Player
	//	return nil
}
func KickPlayer(userid uint32) { //踢出玩家
	//TODO 返回错误码
	//var player *entity.Player
	if player1, ok := Mainplayer.Load((userid)); ok {
		//取成功,否则创建
		head := NewTomeeHeader(1001, userid)
		head.Result = uint32(errorcode.ErrorCodes.ErrAccountLoggedInElsewhere)

		player1.SendPack(head.Pack(nil))
		player1.MainConn.MainConn.Close()
		//	clientdata.Player = player
	}

	//return player
	//	return nil
}
