将消息路由到房间
This commit is contained in:
parent
ac1c6ce04b
commit
890a893344
@ -2,6 +2,7 @@ package baseroom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"game/common/proto/pb"
|
"game/common/proto/pb"
|
||||||
|
"github.com/fox/fox/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceStatus int
|
type ServiceStatus int
|
||||||
@ -71,7 +72,7 @@ func (s *RoomMgr) makeRoomId() int {
|
|||||||
return s.no
|
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 {
|
if s.status != SsWorking {
|
||||||
return nil, pb.ErrCode_Maintain
|
return nil, pb.ErrCode_Maintain
|
||||||
}
|
}
|
||||||
@ -79,7 +80,7 @@ func (s *RoomMgr) CreateRoom(roomType int) (room IRoom, code pb.ErrCode) {
|
|||||||
if roomId < 0 {
|
if roomId < 0 {
|
||||||
return nil, pb.ErrCode_SystemErr
|
return nil, pb.ErrCode_SystemErr
|
||||||
}
|
}
|
||||||
room, code = s.createRoom.CreateRoom(roomId, roomType)
|
room, code = s.createRoom.CreateRoom(roomId, roomType, srv)
|
||||||
if room != nil {
|
if room != nil {
|
||||||
s.Add(room)
|
s.Add(room)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"game/common/baseroom"
|
"game/common/baseroom"
|
||||||
"game/common/proto/pb"
|
"game/common/proto/pb"
|
||||||
"github.com/fox/fox/log"
|
"github.com/fox/fox/log"
|
||||||
|
"github.com/fox/fox/processor"
|
||||||
"github.com/fox/fox/service"
|
"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() {
|
func (rm *ColorRoom) OnInit() {
|
||||||
|
rm.RegisterMessages(processor.RegisterMetas{
|
||||||
|
pb.MsgId_C2SMatchRoomId: {pb.C2SMatchRoom{}, rm.OnEnterRoom},
|
||||||
|
})
|
||||||
return
|
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
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"game/common/model/user"
|
|
||||||
"game/common/proto/pb"
|
"game/common/proto/pb"
|
||||||
"game/common/rpc"
|
"game/common/rpc"
|
||||||
|
"game/server/colorgame/room"
|
||||||
"github.com/fox/fox/ipb"
|
"github.com/fox/fox/ipb"
|
||||||
"github.com/fox/fox/ksync"
|
"github.com/fox/fox/ksync"
|
||||||
"github.com/fox/fox/processor"
|
"github.com/fox/fox/processor"
|
||||||
"github.com/fox/fox/service"
|
"github.com/fox/fox/service"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -17,46 +16,24 @@ const (
|
|||||||
|
|
||||||
func (s *ColorService) initProcessor() {
|
func (s *ColorService) initProcessor() {
|
||||||
s.processor.RegisterMessages(processor.RegisterMetas{
|
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) {
|
func (s *ColorService) onEnterRoom(iMsg *ipb.InternalMsg, req *pb.C2SMatchRoom) {
|
||||||
ksync.GoSafe(func() {
|
ksync.GoSafe(func() {
|
||||||
account, code, node := s.checkLoginOrRegister(req)
|
us, code := rpc.RpcGetGameUser(s.bindService, s, iMsg.UserId)
|
||||||
userId := int64(0)
|
if code != pb.ErrCode_OK {
|
||||||
rsp := &pb.S2CUserLogin{Code: code}
|
s.SendServiceMsg(service.TopicEx(iMsg.ServiceName), iMsg.ConnId, iMsg.UserId,
|
||||||
if account != nil && code == pb.ErrCode_OK {
|
int32(pb.MsgId_S2CMatchRoomId), &pb.S2CMatchRoom{Code: pb.ErrCode_SystemErr})
|
||||||
// 拉取用户数据
|
|
||||||
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)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// 切回服务协程处理业务逻辑
|
||||||
|
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)
|
}, nil)
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,10 @@ type ColorService struct {
|
|||||||
processor *processor.Processor
|
processor *processor.Processor
|
||||||
bindService *userBindService.UserBindService
|
bindService *userBindService.UserBindService
|
||||||
|
|
||||||
roomMgr *baseroom.RoomMgr
|
//roomMgr *baseroom.RoomMgr
|
||||||
playerMgr *baseroom.PlayerMgr
|
playerMgr *baseroom.PlayerMgr
|
||||||
|
room baseroom.IRoom
|
||||||
|
status baseroom.ServiceStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
@ -50,7 +52,11 @@ func Stop() {
|
|||||||
func newColorService(serviceId int) service.IService {
|
func newColorService(serviceId int) service.IService {
|
||||||
var err error
|
var err error
|
||||||
s := new(ColorService)
|
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)
|
sName := fmt.Sprintf("%v-%d", serviceName.ColorGame, serviceId)
|
||||||
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
|
if s.NatsService, err = service.NewNatsService(&service.InitNatsServiceParams{
|
||||||
@ -84,12 +90,13 @@ func (s *ColorService) OnInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ColorService) NotifyStop() {
|
func (s *ColorService) NotifyStop() {
|
||||||
s.roomMgr.SetStatus(baseroom.SsWaitStop)
|
//s.roomMgr.SetStatus(baseroom.SsWaitStop)
|
||||||
|
s.status = baseroom.SsWaitStop
|
||||||
s.BaseService.NotifyStop()
|
s.BaseService.NotifyStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ColorService) CanStop() bool {
|
func (s *ColorService) CanStop() bool {
|
||||||
switch s.roomMgr.Status() {
|
switch s.status {
|
||||||
case baseroom.SsWorking:
|
case baseroom.SsWorking:
|
||||||
return false
|
return false
|
||||||
case baseroom.SsWaitStop, baseroom.SsStopped:
|
case baseroom.SsWaitStop, baseroom.SsStopped:
|
||||||
@ -103,17 +110,19 @@ func (s *ColorService) CanStop() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ColorService) OnStop() {
|
func (s *ColorService) OnStop() {
|
||||||
|
s.status = baseroom.SsStopped
|
||||||
s.NatsService.OnStop()
|
s.NatsService.OnStop()
|
||||||
log.Debug("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 user := s.playerMgr.Find(uid); user != nil {
|
||||||
if rm := s.roomMgr.Find(user.RoomId()); rm != nil {
|
return room.GetPlayer(user), s.room, nil
|
||||||
return user, rm, nil
|
//if rm := s.roomMgr.Find(user.RoomId()); rm != nil {
|
||||||
} else {
|
// return user, rm, nil
|
||||||
return nil, nil, fmt.Errorf("user:%v not found room %d", uid, user.RoomId())
|
//} else {
|
||||||
}
|
// return nil, nil, fmt.Errorf("user:%v not found room %d", uid, user.RoomId())
|
||||||
|
//}
|
||||||
} else {
|
} else {
|
||||||
return nil, nil, fmt.Errorf("user:%v not exist", uid)
|
return nil, nil, fmt.Errorf("user:%v not exist", uid)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user