package base

import (
	"mongo.games.com/game/common"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"
	"mongo.games.com/goserver/srvlib"
	"mongo.games.com/goserver/srvlib/protocol"
)

var SrvSessMgrSington = &SrvSessMgr{}

type SrvSessMgr struct {
}

// 注册事件
func (this *SrvSessMgr) OnRegiste(s *netlib.Session) {
	attr := s.GetAttribute(srvlib.SessionAttributeServerInfo)
	if attr != nil {
		if srvInfo, ok := attr.(*protocol.SSSrvRegiste); ok && srvInfo != nil {
			if srvInfo.GetType() == srvlib.WorldServerType {
				logger.Logger.Warn("(this *SrvSessMgr) OnRegiste (WorldSrv):", s)
			} else if srvInfo.GetType() == srvlib.GateServerType {
				logger.Logger.Warn("(this *SrvSessMgr) OnRegiste (GateSrv):", s)
			} else if srvInfo.GetType() == srvlib.RankServerType {
				logger.Logger.Warn("(this *SrvSessMgr) OnRegiste (RankSrv):", s)
			} else if srvInfo.GetType() == int32(common.RobotServerType) {
				logger.Logger.Warn("(this *SrvSessMgr) OnRegiste (RobotSrv):", s)
				NpcServerAgentSingleton.OnConnected()
			}
		}
	}
}

// 注销事件
func (this *SrvSessMgr) OnUnregiste(s *netlib.Session) {
	attr := s.GetAttribute(srvlib.SessionAttributeServerInfo)
	if attr != nil {
		if srvInfo, ok := attr.(*protocol.SSSrvRegiste); ok && srvInfo != nil {
			if srvInfo.GetType() == srvlib.WorldServerType {
				logger.Logger.Warn("(this *SrvSessMgr) OnUnregiste (WorldSrv):", s)
			} else if srvInfo.GetType() == srvlib.GateServerType {
				logger.Logger.Warn("(this *SrvSessMgr) OnUnregiste (GateSrv):", s)
			} else if srvInfo.GetType() == srvlib.RankServerType {
				logger.Logger.Warn("(this *SrvSessMgr) OnUnregiste (GateSrv):", s)
			} else if srvInfo.GetType() == int32(common.RobotServerType) {
				logger.Logger.Warn("(this *SrvSessMgr) OnUnregiste (RobotSrv):", s)
				NpcServerAgentSingleton.OnDisconnected()
			}
		}
	}
}

func init() {
	srvlib.ServerSessionMgrSington.AddListener(SrvSessMgrSington)
}
