将消息路由到房间
This commit is contained in:
parent
ac1c6ce04b
commit
890a893344
@ -2,6 +2,7 @@ package baseroom
|
||||
|
||||
import (
|
||||
"game/common/proto/pb"
|
||||
"github.com/fox/fox/service"
|
||||
)
|
||||
|
||||
type ServiceStatus int
|
||||
@ -71,7 +72,7 @@ func (s *RoomMgr) makeRoomId() int {
|
||||
return s.no
|
||||
}
|
||||
|
||||
func (s *RoomMgr) CreateRoom(roomType int) (room IRoom, code pb.ErrCode) {
|
||||
func (s *RoomMgr) CreateRoom(roomType int, srv service.IService) (room IRoom, code pb.ErrCode) {
|
||||
if s.status != SsWorking {
|
||||
return nil, pb.ErrCode_Maintain
|
||||
}
|
||||
@ -79,7 +80,7 @@ func (s *RoomMgr) CreateRoom(roomType int) (room IRoom, code pb.ErrCode) {
|
||||
if roomId < 0 {
|
||||
return nil, pb.ErrCode_SystemErr
|
||||
}
|
||||
room, code = s.createRoom.CreateRoom(roomId, roomType)
|
||||
room, code = s.createRoom.CreateRoom(roomId, roomType, srv)
|
||||
if room != nil {
|
||||
s.Add(room)
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"game/common/baseroom"
|
||||
"game/common/proto/pb"
|
||||
"github.com/fox/fox/log"
|
||||
"github.com/fox/fox/processor"
|
||||
"github.com/fox/fox/service"
|
||||
)
|
||||
|
||||
@ -25,5 +26,8 @@ func newColorRoom(id, roomType int, srv service.IService) (baseroom.IRoom, pb.Er
|
||||
}
|
||||
|
||||
func (rm *ColorRoom) OnInit() {
|
||||
rm.RegisterMessages(processor.RegisterMetas{
|
||||
pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, rm.OnEnterRoom},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
11
server/colorgame/room/onMessage.go
Normal file
11
server/colorgame/room/onMessage.go
Normal file
@ -0,0 +1,11 @@
|
||||
package room
|
||||
|
||||
import (
|
||||
"game/common/proto/pb"
|
||||
"github.com/fox/fox/ipb"
|
||||
)
|
||||
|
||||
func (rm *ColorRoom) OnEnterRoom(user *ColorPlayer, iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
|
||||
|
||||
return
|
||||
}
|
@ -1,14 +1,13 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"game/common/model/user"
|
||||
"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"
|
||||
"github.com/fox/fox/service"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -17,46 +16,24 @@ const (
|
||||
|
||||
func (s *ColorService) initProcessor() {
|
||||
s.processor.RegisterMessages(processor.RegisterMetas{
|
||||
pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, s.onLoginOrRegister},
|
||||
pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, s.onEnterRoom},
|
||||
})
|
||||
}
|
||||
|
||||
// 登录或注册
|
||||
func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
|
||||
ksync.GoSafe(func() {
|
||||
account, code, node := s.checkLoginOrRegister(req)
|
||||
userId := int64(0)
|
||||
rsp := &pb.S2CUserLogin{Code: code}
|
||||
if account != nil && code == pb.ErrCode_OK {
|
||||
// 拉取用户数据
|
||||
us := &user.User{}
|
||||
us, code = s.getUser(userId, req.Username)
|
||||
if code == pb.ErrCode_OK {
|
||||
rsp.UserId = us.ID
|
||||
rsp.Token, _ = generateToken(account.ID, account.Username)
|
||||
userId = rsp.UserId
|
||||
}
|
||||
}
|
||||
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, userId, int32(pb.MsgId_S2CUserLoginId), rsp)
|
||||
|
||||
if account != nil && account.ID > 0 {
|
||||
loginLog := &user.UserLoginLog{
|
||||
AccountID: account.ID,
|
||||
LoginIP: req.Ip,
|
||||
LoginTime: time.Now(),
|
||||
DeviceInfo: req.DeviceId,
|
||||
LoginResult: code == pb.ErrCode_OK,
|
||||
FailReason: code.String(),
|
||||
}
|
||||
switch code {
|
||||
case pb.ErrCode_LoginUserOrPwdErr, pb.ErrCode_OK:
|
||||
rpcMsg := ipb.MakeRpcMsg[user.UserLoginLog](rpc.LogUserAccountLogin, 0, loginLog)
|
||||
ksync.GoSafe(func() {
|
||||
_, _ = s.Call(service.RpcTopicEx(node.Name), timeout, rpcMsg)
|
||||
}, nil)
|
||||
|
||||
}
|
||||
us, code := rpc.RpcGetGameUser(s.bindService, s, iMsg.UserId)
|
||||
if code != pb.ErrCode_OK {
|
||||
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId,
|
||||
int32(pb.MsgId_S2CMatchRoomId), &pb.S2CMatchRoom{Code: pb.ErrCode_SystemErr})
|
||||
}
|
||||
// 切回服务协程处理业务逻辑
|
||||
s.RunOnce(func() {
|
||||
colorUser := room.NewColorPlayer(service.TopicEx(iMsg.ServiceName), s.room.Id(), &us.User, &us.UserResources)
|
||||
s.playerMgr.Add(colorUser)
|
||||
_ = s.room.Dispatch(colorUser, iMsg.MsgId, iMsg, req)
|
||||
})
|
||||
|
||||
}, nil)
|
||||
}
|
||||
|
@ -23,8 +23,10 @@ type ColorService struct {
|
||||
processor *processor.Processor
|
||||
bindService *userBindService.UserBindService
|
||||
|
||||
roomMgr *baseroom.RoomMgr
|
||||
//roomMgr *baseroom.RoomMgr
|
||||
playerMgr *baseroom.PlayerMgr
|
||||
room baseroom.IRoom
|
||||
status baseroom.ServiceStatus
|
||||
}
|
||||
|
||||
func Init() {
|
||||
@ -50,7 +52,11 @@ func Stop() {
|
||||
func newColorService(serviceId int) service.IService {
|
||||
var err error
|
||||
s := new(ColorService)
|
||||
s.roomMgr = baseroom.NewRoomMgr(&room.RoomFactory{})
|
||||
s.playerMgr = baseroom.NewPlayerMgr(&room.PlayerFactory{})
|
||||
factory := &room.RoomFactory{}
|
||||
s.room, _ = factory.CreateRoom(int(pb.ServiceTypeId_STI_ColorGame), 0, s)
|
||||
s.status = baseroom.SsWorking
|
||||
//s.roomMgr = baseroom.NewRoomMgr(&room.RoomFactory{})
|
||||
|
||||
sName := fmt.Sprintf("%v-%d", serviceName.ColorGame, serviceId)
|
||||
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
|
||||
@ -84,12 +90,13 @@ func (s *ColorService) OnInit() {
|
||||
}
|
||||
|
||||
func (s *ColorService) NotifyStop() {
|
||||
s.roomMgr.SetStatus(baseroom.SsWaitStop)
|
||||
//s.roomMgr.SetStatus(baseroom.SsWaitStop)
|
||||
s.status = baseroom.SsWaitStop
|
||||
s.BaseService.NotifyStop()
|
||||
}
|
||||
|
||||
func (s *ColorService) CanStop() bool {
|
||||
switch s.roomMgr.Status() {
|
||||
switch s.status {
|
||||
case baseroom.SsWorking:
|
||||
return false
|
||||
case baseroom.SsWaitStop, baseroom.SsStopped:
|
||||
@ -103,17 +110,19 @@ func (s *ColorService) CanStop() bool {
|
||||
}
|
||||
|
||||
func (s *ColorService) OnStop() {
|
||||
s.status = baseroom.SsStopped
|
||||
s.NatsService.OnStop()
|
||||
log.Debug("OnStop")
|
||||
}
|
||||
|
||||
func (s *ColorService) findRoom(uid int64) (baseroom.IPlayer, baseroom.IRoom, error) {
|
||||
func (s *ColorService) findRoom(uid int64) (*room.ColorPlayer, baseroom.IRoom, error) {
|
||||
if user := s.playerMgr.Find(uid); user != nil {
|
||||
if rm := s.roomMgr.Find(user.RoomId()); rm != nil {
|
||||
return user, rm, nil
|
||||
} else {
|
||||
return nil, nil, fmt.Errorf("user:%v not found room %d", uid, user.RoomId())
|
||||
}
|
||||
return room.GetPlayer(user), s.room, nil
|
||||
//if rm := s.roomMgr.Find(user.RoomId()); rm != nil {
|
||||
// return user, rm, nil
|
||||
//} else {
|
||||
// return nil, nil, fmt.Errorf("user:%v not found room %d", uid, user.RoomId())
|
||||
//}
|
||||
} else {
|
||||
return nil, nil, fmt.Errorf("user:%v not exist", uid)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user