添加了节点服务器,各个服务器之间支持多对多

This commit is contained in:
flswld
2022-12-24 04:14:33 +08:00
parent 16dd9c1e87
commit 7e86669628
92 changed files with 1429 additions and 287 deletions

View File

@@ -1,7 +1,7 @@
package game
import (
"hk4e/gs/constant"
"hk4e/common/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
)

View File

@@ -47,7 +47,8 @@ func NewCommandManager() *CommandManager {
r := new(CommandManager)
// 创建AI世界
GAME_MANAGER.OnRegOk(false, &proto.SetPlayerBornDataReq{AvatarId: 10000007, NickName: "System"}, 1, 0)
GAME_MANAGER.OnRegOk(false, &proto.SetPlayerBornDataReq{AvatarId: 10000007, NickName: "System"}, 1, 0, "")
GAME_MANAGER.FightServerSelectNotify(1, "")
r.system = USER_MANAGER.GetOnlineUser(1)
r.system.DbState = model.DbNormal
r.system.SceneLoadState = model.SceneEnterDone

View File

@@ -134,17 +134,47 @@ func (g *GameManager) Stop() {
for _, player := range userList {
g.DisconnectPlayer(player.PlayerID, kcp.EnetServerShutdown)
}
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 3)
// 保存玩家数据
LOCAL_EVENT_MANAGER.localEventChan <- &LocalEvent{
EventId: RunUserCopyAndSave,
}
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 3)
}
func (g *GameManager) SendMsgEx(cmdId uint16, userId uint32, clientSeq uint32, gateAppId string, payloadMsg pb.Message) {
if userId < 100000000 {
return
}
if payloadMsg == nil {
logger.Error("payload msg is nil")
return
}
gameMsg := &mq.GameMsg{
UserId: userId,
CmdId: cmdId,
ClientSeq: clientSeq,
PayloadMessage: payloadMsg,
}
g.messageQueue.SendToGate(gateAppId, &mq.NetMsg{
MsgType: mq.MsgTypeGame,
EventId: mq.NormalMsg,
GameMsg: gameMsg,
})
}
// SendMsg 发送消息给客户端
func (g *GameManager) SendMsg(cmdId uint16, userId uint32, clientSeq uint32, payloadMsg pb.Message) {
if userId < 100000000 || payloadMsg == nil {
if userId < 100000000 {
return
}
if payloadMsg == nil {
logger.Error("payload msg is nil")
return
}
player := USER_MANAGER.GetOnlineUser(userId)
if player == nil {
logger.Error("player not exist, uid: %v", userId)
return
}
gameMsg := new(mq.GameMsg)
@@ -158,7 +188,7 @@ func (g *GameManager) SendMsg(cmdId uint16, userId uint32, clientSeq uint32, pay
return
}
gameMsg.PayloadMessageData = payloadMessageData
g.messageQueue.SendToGate("1", &mq.NetMsg{
g.messageQueue.SendToGate(player.GateAppId, &mq.NetMsg{
MsgType: mq.MsgTypeGame,
EventId: mq.NormalMsg,
GameMsg: gameMsg,
@@ -222,7 +252,11 @@ func (g *GameManager) ReconnectPlayer(userId uint32) {
}
func (g *GameManager) DisconnectPlayer(userId uint32, reason uint32) {
g.messageQueue.SendToGate("1", &mq.NetMsg{
player := USER_MANAGER.GetOnlineUser(userId)
if player == nil {
return
}
g.messageQueue.SendToGate(player.GateAppId, &mq.NetMsg{
MsgType: mq.MsgTypeConnCtrl,
EventId: mq.KickPlayerNotify,
ConnCtrlMsg: &mq.ConnCtrlMsg{

View File

@@ -38,10 +38,10 @@ func (l *LocalEventManager) LocalEventHandle(localEvent *LocalEvent) {
if playerLoginInfo.Player != nil {
USER_MANAGER.playerMap[playerLoginInfo.Player.PlayerID] = playerLoginInfo.Player
}
GAME_MANAGER.OnLoginOk(playerLoginInfo.UserId, playerLoginInfo.Player, playerLoginInfo.ClientSeq)
GAME_MANAGER.OnLoginOk(playerLoginInfo.UserId, playerLoginInfo.Player, playerLoginInfo.ClientSeq, playerLoginInfo.GateAppId)
case CheckUserExistOnRegFromDbFinish:
playerRegInfo := localEvent.Msg.(*PlayerRegInfo)
GAME_MANAGER.OnRegOk(playerRegInfo.Exist, playerRegInfo.Req, playerRegInfo.UserId, playerRegInfo.ClientSeq)
GAME_MANAGER.OnRegOk(playerRegInfo.Exist, playerRegInfo.Req, playerRegInfo.UserId, playerRegInfo.ClientSeq, playerRegInfo.GateAppId)
case RunUserCopyAndSave:
startTime := time.Now().UnixNano()
// 拷贝一份数据避免并发访问

View File

@@ -4,6 +4,7 @@ import (
"hk4e/common/mq"
"hk4e/gate/kcp"
"hk4e/gs/model"
"hk4e/node/api"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
@@ -120,15 +121,18 @@ func (r *RouteManager) InitRoute() {
func (r *RouteManager) RouteHandle(netMsg *mq.NetMsg) {
switch netMsg.MsgType {
case mq.MsgTypeGame:
if netMsg.OriginServerType != api.GATE {
return
}
gameMsg := netMsg.GameMsg
switch netMsg.EventId {
case mq.NormalMsg:
if gameMsg.CmdId == cmd.PlayerLoginReq {
GAME_MANAGER.PlayerLoginReq(gameMsg.UserId, gameMsg.ClientSeq, gameMsg.PayloadMessage)
GAME_MANAGER.PlayerLoginReq(gameMsg.UserId, gameMsg.ClientSeq, netMsg.OriginServerAppId, gameMsg.PayloadMessage)
return
}
if gameMsg.CmdId == cmd.SetPlayerBornDataReq {
GAME_MANAGER.SetPlayerBornDataReq(gameMsg.UserId, gameMsg.ClientSeq, gameMsg.PayloadMessage)
GAME_MANAGER.SetPlayerBornDataReq(gameMsg.UserId, gameMsg.ClientSeq, netMsg.OriginServerAppId, gameMsg.PayloadMessage)
return
}
r.doRoute(gameMsg.CmdId, gameMsg.UserId, gameMsg.ClientSeq, gameMsg.PayloadMessage)
@@ -136,12 +140,17 @@ func (r *RouteManager) RouteHandle(netMsg *mq.NetMsg) {
GAME_MANAGER.OnUserOffline(gameMsg.UserId)
}
case mq.MsgTypeConnCtrl:
if netMsg.OriginServerType != api.GATE {
return
}
connCtrlMsg := netMsg.ConnCtrlMsg
switch netMsg.EventId {
case mq.ClientRttNotify:
GAME_MANAGER.ClientRttNotify(connCtrlMsg.UserId, connCtrlMsg.ClientRtt)
case mq.ClientTimeNotify:
GAME_MANAGER.ClientTimeNotify(connCtrlMsg.UserId, connCtrlMsg.ClientTime)
case mq.FightServerSelectNotify:
GAME_MANAGER.FightServerSelectNotify(connCtrlMsg.UserId, connCtrlMsg.FightServerAppId)
}
}
}

View File

@@ -3,7 +3,7 @@ package game
import (
"time"
"hk4e/gs/constant"
"hk4e/common/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/random"

View File

@@ -1,8 +1,8 @@
package game
import (
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/object"

View File

@@ -3,6 +3,7 @@ package game
import (
"time"
"hk4e/common/mq"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
@@ -56,6 +57,16 @@ func (g *GameManager) EntityAiSyncNotify(player *model.Player, payloadMsg pb.Mes
g.SendMsg(cmd.EntityAiSyncNotify, player.PlayerID, player.ClientSeq, entityAiSyncNotify)
}
func (g *GameManager) ClientRttNotify(userId uint32, clientRtt uint32) {
player := USER_MANAGER.GetOnlineUser(userId)
if player == nil {
logger.Error("player is nil, uid: %v", userId)
return
}
logger.Debug("client rtt notify, uid: %v, rtt: %v", userId, clientRtt)
player.ClientRTT = clientRtt
}
func (g *GameManager) ClientTimeNotify(userId uint32, clientTime uint32) {
player := USER_MANAGER.GetOnlineUser(userId)
if player == nil {
@@ -66,14 +77,29 @@ func (g *GameManager) ClientTimeNotify(userId uint32, clientTime uint32) {
player.ClientTime = clientTime
}
func (g *GameManager) ClientRttNotify(userId uint32, clientRtt uint32) {
func (g *GameManager) FightServerSelectNotify(userId uint32, fightAppId string) {
player := USER_MANAGER.GetOnlineUser(userId)
if player == nil {
logger.Error("player is nil, uid: %v", userId)
return
}
logger.Debug("client rtt notify, uid: %v, rtt: %v", userId, clientRtt)
player.ClientRTT = clientRtt
logger.Debug("fight server select notify, uid: %v, fightAppId: %v", userId, fightAppId)
player.FightAppId = fightAppId
// 创建世界
world := WORLD_MANAGER.CreateWorld(player)
GAME_MANAGER.messageQueue.SendToFight(fightAppId, &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.AddFightRoutine,
FightMsg: &mq.FightMsg{
FightRoutineId: world.id,
GateServerAppId: player.GateAppId,
},
})
world.AddPlayer(player, player.SceneId)
player.WorldId = world.id
// 进入场景
player.SceneLoadState = model.SceneNone
g.SendMsg(cmd.PlayerEnterSceneNotify, userId, player.ClientSeq, g.PacketPlayerEnterSceneNotifyLogin(player, proto.EnterType_ENTER_TYPE_SELF))
}
func (g *GameManager) ServerAnnounceNotify(announceId uint32, announceMsg string) {

View File

@@ -1,8 +1,8 @@
package game
import (
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/constant"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"

View File

@@ -3,8 +3,8 @@ package game
import (
"time"
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/reflection"
@@ -14,36 +14,38 @@ import (
pb "google.golang.org/protobuf/proto"
)
func (g *GameManager) PlayerLoginReq(userId uint32, clientSeq uint32, payloadMsg pb.Message) {
logger.Info("user login req, uid: %v", userId)
func (g *GameManager) PlayerLoginReq(userId uint32, clientSeq uint32, gateAppId string, payloadMsg pb.Message) {
logger.Info("user login req, uid: %v, gateAppId: %v", userId, gateAppId)
req := payloadMsg.(*proto.PlayerLoginReq)
logger.Debug("login data: %v", req)
g.OnLogin(userId, clientSeq)
g.OnLogin(userId, clientSeq, gateAppId)
}
func (g *GameManager) SetPlayerBornDataReq(userId uint32, clientSeq uint32, payloadMsg pb.Message) {
logger.Info("user reg req, uid: %v", userId)
func (g *GameManager) SetPlayerBornDataReq(userId uint32, clientSeq uint32, gateAppId string, payloadMsg pb.Message) {
logger.Info("user reg req, uid: %v, gateAppId: %v", userId, gateAppId)
req := payloadMsg.(*proto.SetPlayerBornDataReq)
logger.Debug("reg data: %v", req)
g.OnReg(userId, clientSeq, req)
g.OnReg(userId, clientSeq, gateAppId, req)
}
func (g *GameManager) OnLogin(userId uint32, clientSeq uint32) {
func (g *GameManager) OnLogin(userId uint32, clientSeq uint32, gateAppId string) {
logger.Info("user login, uid: %v", userId)
player, asyncWait := USER_MANAGER.OnlineUser(userId, clientSeq)
player, asyncWait := USER_MANAGER.OnlineUser(userId, clientSeq, gateAppId)
if !asyncWait {
g.OnLoginOk(userId, player, clientSeq)
g.OnLoginOk(userId, player, clientSeq, gateAppId)
}
}
func (g *GameManager) OnLoginOk(userId uint32, player *model.Player, clientSeq uint32) {
func (g *GameManager) OnLoginOk(userId uint32, player *model.Player, clientSeq uint32, gateAppId string) {
if player == nil {
g.SendMsg(cmd.DoSetPlayerBornDataNotify, userId, clientSeq, new(proto.DoSetPlayerBornDataNotify))
g.SendMsgEx(cmd.DoSetPlayerBornDataNotify, userId, clientSeq, gateAppId, new(proto.DoSetPlayerBornDataNotify))
return
}
player.OnlineTime = uint32(time.Now().UnixMilli())
player.Online = true
player.GateAppId = gateAppId
// 初始化
player.InitAll()
// player.TeamConfig.UpdateTeam()
@@ -53,32 +55,24 @@ func (g *GameManager) OnLoginOk(userId uint32, player *model.Player, clientSeq u
player.Pos.Y = player.SafePos.Y
player.Pos.Z = player.SafePos.Z
// 创建世界
world := WORLD_MANAGER.CreateWorld(player)
world.AddPlayer(player, player.SceneId)
player.WorldId = world.id
player.CombatInvokeHandler = model.NewInvokeHandler[proto.CombatInvokeEntry]()
player.AbilityInvokeHandler = model.NewInvokeHandler[proto.AbilityInvokeEntry]()
g.LoginNotify(userId, player, clientSeq)
player.SceneLoadState = model.SceneNone
g.SendMsg(cmd.PlayerEnterSceneNotify, userId, clientSeq, g.PacketPlayerEnterSceneNotifyLogin(player, proto.EnterType_ENTER_TYPE_SELF))
}
func (g *GameManager) OnReg(userId uint32, clientSeq uint32, payloadMsg pb.Message) {
func (g *GameManager) OnReg(userId uint32, clientSeq uint32, gateAppId string, payloadMsg pb.Message) {
logger.Debug("user reg, uid: %v", userId)
req := payloadMsg.(*proto.SetPlayerBornDataReq)
logger.Debug("avatar id: %v, nickname: %v", req.AvatarId, req.NickName)
exist, asyncWait := USER_MANAGER.CheckUserExistOnReg(userId, req, clientSeq)
exist, asyncWait := USER_MANAGER.CheckUserExistOnReg(userId, req, clientSeq, gateAppId)
if !asyncWait {
g.OnRegOk(exist, req, userId, clientSeq)
g.OnRegOk(exist, req, userId, clientSeq, gateAppId)
}
}
func (g *GameManager) OnRegOk(exist bool, req *proto.SetPlayerBornDataReq, userId uint32, clientSeq uint32) {
func (g *GameManager) OnRegOk(exist bool, req *proto.SetPlayerBornDataReq, userId uint32, clientSeq uint32, gateAppId string) {
if exist {
logger.Error("recv reg req, but user is already exist, userId: %v", userId)
return
@@ -98,8 +92,8 @@ func (g *GameManager) OnRegOk(exist bool, req *proto.SetPlayerBornDataReq, userI
}
USER_MANAGER.AddUser(player)
g.SendMsg(cmd.SetPlayerBornDataRsp, userId, clientSeq, new(proto.SetPlayerBornDataRsp))
g.OnLogin(userId, clientSeq)
g.SendMsgEx(cmd.SetPlayerBornDataRsp, userId, clientSeq, gateAppId, new(proto.SetPlayerBornDataRsp))
g.OnLogin(userId, clientSeq, gateAppId)
}
func (g *GameManager) OnUserOffline(userId uint32) {

View File

@@ -66,9 +66,10 @@ type PlayerRegInfo struct {
Req *proto.SetPlayerBornDataReq
UserId uint32
ClientSeq uint32
GateAppId string
}
func (u *UserManager) CheckUserExistOnReg(userId uint32, req *proto.SetPlayerBornDataReq, clientSeq uint32) (exist bool, asyncWait bool) {
func (u *UserManager) CheckUserExistOnReg(userId uint32, req *proto.SetPlayerBornDataReq, clientSeq uint32, gateAppId string) (exist bool, asyncWait bool) {
_, exist = u.playerMap[userId]
if exist {
return true, false
@@ -86,6 +87,7 @@ func (u *UserManager) CheckUserExistOnReg(userId uint32, req *proto.SetPlayerBor
Req: req,
UserId: userId,
ClientSeq: clientSeq,
GateAppId: gateAppId,
},
}
}()
@@ -137,9 +139,10 @@ type PlayerLoginInfo struct {
UserId uint32
Player *model.Player
ClientSeq uint32
GateAppId string
}
func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32) (*model.Player, bool) {
func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32, gateAppId string) (*model.Player, bool) {
player, exist := u.playerMap[userId]
if exist {
u.ChangeUserDbState(player, model.DbNormal)
@@ -158,6 +161,7 @@ func (u *UserManager) OnlineUser(userId uint32, clientSeq uint32) (*model.Player
UserId: userId,
Player: player,
ClientSeq: clientSeq,
GateAppId: gateAppId,
},
}
}()

View File

@@ -3,8 +3,7 @@ package game
import (
"strconv"
"hk4e/gs/constant"
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/model"
"hk4e/pkg/logger"

View File

@@ -3,7 +3,8 @@ package game
import (
"time"
"hk4e/gs/constant"
"hk4e/common/constant"
"hk4e/common/mq"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/object"
@@ -320,6 +321,13 @@ func (g *GameManager) UserWorldRemovePlayer(world *World, player *model.Player)
if world.owner.PlayerID == player.PlayerID {
// 房主离开销毁世界
WORLD_MANAGER.DestroyWorld(world.id)
GAME_MANAGER.messageQueue.SendToFight(world.owner.FightAppId, &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.DelFightRoutine,
FightMsg: &mq.FightMsg{
FightRoutineId: world.id,
},
})
return
}
if world.multiplayer && world.GetWorldPlayerNum() > 0 {

View File

@@ -5,8 +5,8 @@ import (
"strconv"
"time"
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/object"

View File

@@ -3,7 +3,7 @@ package game
import (
"time"
"hk4e/gs/constant"
"hk4e/common/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"

View File

@@ -5,7 +5,7 @@ import (
"time"
"unicode/utf8"
"hk4e/gs/constant"
"hk4e/common/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/object"

View File

@@ -4,8 +4,8 @@ import (
"strings"
"time"
"hk4e/common/constant"
"hk4e/gdconf"
"hk4e/gs/constant"
"hk4e/gs/model"
"hk4e/pkg/endec"
"hk4e/pkg/logger"
@@ -462,7 +462,7 @@ func (g *GameManager) DrownBackHandler(player *model.Player) {
}
// 获取最近角色实体的锚点
// TODO 阻塞优化 16ms我感觉有点慢
//for _, entry := range gdc.CONF.ScenePointEntries {
// for _, entry := range gdc.CONF.ScenePointEntries {
// if entry.PointData == nil || entry.PointData.TranPos == nil {
// continue
// }
@@ -475,7 +475,7 @@ func (g *GameManager) DrownBackHandler(player *model.Player) {
// if player.SafePos.Distance(pointPos) < player.SafePos.Distance(pos) {
// pos = pointPos
// }
//}
// }
// 传送玩家至安全位置
g.TeleportPlayer(player, uint32(constant.EnterReasonConst.Revival), player.SceneId, pos)
}

View File

@@ -1,8 +1,8 @@
package game
import (
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/constant"
"hk4e/gs/model"
"hk4e/pkg/endec"
"hk4e/pkg/logger"

View File

@@ -1,8 +1,8 @@
package game
import (
"hk4e/common/constant"
gdc "hk4e/gs/config"
"hk4e/gs/constant"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"

View File

@@ -4,10 +4,10 @@ import (
"math"
"time"
"hk4e/common/constant"
"hk4e/protocol/cmd"
"hk4e/common/mq"
"hk4e/gs/constant"
"hk4e/gs/game/aoi"
"hk4e/gs/model"
"hk4e/pkg/alg"
@@ -71,13 +71,6 @@ func (w *WorldManager) CreateWorld(owner *model.Player) *World {
}
world.mpLevelEntityId = world.GetNextWorldEntityId(constant.EntityIdTypeConst.MPLEVEL)
w.worldMap[worldId] = world
GAME_MANAGER.messageQueue.SendToFight("1", &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.AddFightRoutine,
FightMsg: &mq.FightMsg{
FightRoutineId: world.id,
},
})
return world
}
@@ -88,13 +81,6 @@ func (w *WorldManager) DestroyWorld(worldId uint32) {
player.WorldId = 0
}
delete(w.worldMap, worldId)
GAME_MANAGER.messageQueue.SendToFight("1", &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.DelFightRoutine,
FightMsg: &mq.FightMsg{
FightRoutineId: world.id,
},
})
}
// GetBigWorld 获取本服务器的AI世界
@@ -715,7 +701,7 @@ func (s *Scene) CreateEntityAvatar(player *model.Player, avatarId uint32) uint32
if avatarId == s.world.GetPlayerActiveAvatarId(player) {
s.world.aoiManager.AddEntityIdToGridByPos(entity.id, float32(entity.pos.X), float32(entity.pos.Y), float32(entity.pos.Z))
}
GAME_MANAGER.messageQueue.SendToFight("1", &mq.NetMsg{
GAME_MANAGER.messageQueue.SendToFight(s.world.owner.FightAppId, &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.FightRoutineAddEntity,
FightMsg: &mq.FightMsg{
@@ -765,7 +751,7 @@ func (s *Scene) CreateEntityMonster(pos *model.Vector, level uint8, fightProp ma
}
s.entityMap[entity.id] = entity
s.world.aoiManager.AddEntityIdToGridByPos(entity.id, float32(entity.pos.X), float32(entity.pos.Y), float32(entity.pos.Z))
GAME_MANAGER.messageQueue.SendToFight("1", &mq.NetMsg{
GAME_MANAGER.messageQueue.SendToFight(s.world.owner.FightAppId, &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.FightRoutineAddEntity,
FightMsg: &mq.FightMsg{
@@ -915,7 +901,7 @@ func (s *Scene) DestroyEntity(entityId uint32) {
}
s.world.aoiManager.RemoveEntityIdFromGridByPos(entity.id, float32(entity.pos.X), float32(entity.pos.Y), float32(entity.pos.Z))
delete(s.entityMap, entityId)
GAME_MANAGER.messageQueue.SendToFight("1", &mq.NetMsg{
GAME_MANAGER.messageQueue.SendToFight(s.world.owner.FightAppId, &mq.NetMsg{
MsgType: mq.MsgTypeFight,
EventId: mq.FightRoutineDelEntity,
FightMsg: &mq.FightMsg{