package smallrocket

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

type CSPlayerOpPacketFactory struct {
}

type CSPlayerOpHandler struct {
}

func (f *CSPlayerOpPacketFactory) CreatePacket() interface{} {
	pack := &smallrocket.CSSmallRocketOp{}
	return pack
}

func (h *CSPlayerOpHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSPlayerOpHandler Process recv ", data)
	if msg, ok := data.(*smallrocket.CSSmallRocketOp); ok {
		p := base.PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warn("CSPlayerOpHandler p == nil")
			return nil
		}
		scene := p.GetScene()
		if scene == nil {
			logger.Logger.Warn("CSPlayerOpHandler p.scene == nil")
			return nil
		}

		if !common.IsSmallRocket(scene.GetGameId()) {
			logger.Logger.Error("CSPlayerOpHandler gameId Error ", scene.GameId)
			return nil
		}
		if !scene.HasPlayer(p) {
			return nil
		}
		sp := scene.GetScenePolicy()
		if sp != nil {
			sp.OnPlayerOp(scene, p, int(msg.GetOpCode()), msg.GetParams())
		}
		return nil
	}
	return nil
}

func init() {
	common.RegisterHandler(int(smallrocket.SmallRocketPacketID_PACKET_CS_SMALLROCKET_PLAYEROP), &CSPlayerOpHandler{})
	netlib.RegisterFactory(int(smallrocket.SmallRocketPacketID_PACKET_CS_SMALLROCKET_PLAYEROP), &CSPlayerOpPacketFactory{})
}
