package server

import (
	"game/common/proto/pb"
	"game/common/rpc"
	"game/server/colorgame/room"
	"github.com/fox/fox/ipb"
	"github.com/fox/fox/ksync"
	"github.com/fox/fox/processor"
)

const (
// timeout = time.Second * 30
)

func (s *ColorService) initProcessor() {
	s.Processor().RegisterMessages(processor.RegisterMetas{
		pb.MsgId_ReqEnterRoomId: {pb.ReqEnterRoom{}, s.onEnterRoom},
	})
}

// 进房间
func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.ReqEnterRoom) {
	ksync.GoSafe(func() {
		us, code := rpc.RpcGetGameUser(s, iMsg.UserId)
		if code != pb.ErrCode_OK {
			s.SendServiceMsg(pb.ServiceTypeId_STI_Gate, iMsg.UserId,
				int32(pb.MsgId_RspEnterRoomId), &pb.RspEnterRoom{Code: pb.ErrCode_SystemErr})
			return
		}
		// 切回服务协程处理业务逻辑
		s.RunOnce(func() {
			colorUser := room.NewColorPlayer(s.room.Id(), &us.User, &us.UserResources)
			s.playerMgr.Add(colorUser)
			_ = s.room.Dispatch(colorUser, iMsg.MsgId, iMsg, req)
		})

	}, nil)
}
