package internal

import (
	"server/MergeConst"
	GoUtil "server/game_util"
	"sync"

	"server/pkg/github.com/name5566/leaf/gate"
)

var Agents = sync.Map{}

func init() {
	skeleton.RegisterChanRPC("NewAgent", rpcNewAgent)
	skeleton.RegisterChanRPC("CloseAgent", rpcCloseAgent)
}

func rpcNewAgent(args []interface{}) {
	a := args[0].(gate.Agent)
	Agents.Store(a, struct{}{})
	GoUtil.CallEvent(MergeConst.Notify_Socket_Connect, []interface{}{a})
}

func rpcCloseAgent(args []interface{}) {
	a := args[0].(gate.Agent)
	p, ok := Agents.Load(a)
	if ok {
		GoUtil.CallEvent(MergeConst.Notify_Socket_Closed, []interface{}{p})
		Agents.Delete(a)
	}
}

func AsignPlayerToAgents(a gate.Agent, player interface{}) {
	Agents.Store(a, player)
}
