You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
4.3 KiB
109 lines
4.3 KiB
1 month ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using Sog;
|
||
|
using ProtoCSStruct;
|
||
|
using Sog.Log;
|
||
|
|
||
|
namespace Game
|
||
|
{
|
||
|
public static class BattleVersionSvc
|
||
|
{
|
||
|
private static long LastAskVersionTime = 0;
|
||
|
|
||
|
//每隔30s请求一次各战斗服务器的版本号
|
||
|
public static void OnTickSecond(long nowSec)
|
||
|
{
|
||
|
if(LastAskVersionTime + 30 > nowSec)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
LastAskVersionTime = nowSec;
|
||
|
//GetAllBattleServerVersion();
|
||
|
}
|
||
|
|
||
|
|
||
|
//获取所有战斗服务器的版本号
|
||
|
public static void GetAllBattleServerVersion()
|
||
|
{
|
||
|
SSBattleServerVersionReq req = new SSBattleServerVersionReq();
|
||
|
req.RemoteAppID = GameServerUtils.GetAppID();
|
||
|
//广播给所有battle
|
||
|
GameServerUtils.GetPacketSender().Broadcast((int)ServerType.Battle, (int)SSGameMsgID.BattleServerVersionReq, ref req, 0, 0);
|
||
|
}
|
||
|
|
||
|
public static void OnBattleServerVersionRes(uint serverID, StructPacket packet)
|
||
|
{
|
||
|
ref SSBattleServerVersionRes res = ref packet.GetMessage<SSBattleServerVersionRes>();
|
||
|
BattleServerVersionData versionData = GameServerUtils.GetBattleServerVersionData();
|
||
|
if(versionData.m_versionMap.TryGetValue(res.RemoteAppID, out int oldVersion))
|
||
|
{
|
||
|
if(oldVersion == res.BattleVersion)
|
||
|
{
|
||
|
//没发生变化,不用处理
|
||
|
return;
|
||
|
}
|
||
|
//移除现有的
|
||
|
versionData.m_versionMap.Remove(res.RemoteAppID);
|
||
|
if (versionData.m_appIdList.TryGetValue(oldVersion, out List<uint> appIdList))
|
||
|
{
|
||
|
appIdList.Remove(res.RemoteAppID);
|
||
|
}
|
||
|
}
|
||
|
versionData.m_versionMap.Add(res.RemoteAppID, res.BattleVersion);
|
||
|
if(versionData.m_appIdList.ContainsKey(res.BattleVersion) == false)
|
||
|
{
|
||
|
versionData.m_appIdList.Add(res.BattleVersion, new List<uint>());
|
||
|
}
|
||
|
var list = versionData.m_appIdList[res.BattleVersion];
|
||
|
list.Add(res.RemoteAppID);
|
||
|
list.Sort();
|
||
|
|
||
|
TraceLog.Debug("BattleVesionSvc.OnBattleServerVersionRes Update Server BattleVersion {0} version {1}, total count {2}",
|
||
|
ServerIDUtils.IDToString(res.RemoteAppID), res.BattleVersion, list.Count);
|
||
|
}
|
||
|
|
||
|
//根据玩家的战斗版本号获取战斗服务器id
|
||
|
public static uint GetBattleSvrID(PlayerOnGame player)
|
||
|
{
|
||
|
if(player.cacheBattleAppId == 0)
|
||
|
{
|
||
|
uint serverId;
|
||
|
BattleServerVersionData versionData = GameServerUtils.GetBattleServerVersionData();
|
||
|
if(versionData.m_appIdList.TryGetValue(player.battleVersion, out var serverList) && serverList.Count > 0)
|
||
|
{
|
||
|
serverId = serverList[0];
|
||
|
if (serverList.Count > 1)
|
||
|
{
|
||
|
int modIndex = (int)(player.UserID % serverList.Count);
|
||
|
if(modIndex == 0)
|
||
|
{
|
||
|
modIndex = serverList.Count;
|
||
|
}
|
||
|
serverId = serverList[modIndex-1];
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//对应版本没有服务器,就发到第一个服务器
|
||
|
serverId = GameServerUtils.GetPacketSender().GetBattleServerBaseID() + 1;
|
||
|
TraceLog.Error("BattleVesionSvc.GetBattleSvrID no server info {0} {1}", player.UserID, player.battleVersion);
|
||
|
}
|
||
|
player.cacheBattleAppId = serverId;
|
||
|
TraceLog.Trace("BattleVesionSvc.GetBattleSvrID role battleSvrID {0} {1}", player.UserID, serverId);
|
||
|
}
|
||
|
return player.cacheBattleAppId;
|
||
|
}
|
||
|
|
||
|
//设置客服端的战斗版本号
|
||
|
public static void SetPlayerBattleVersion(PlayerOnGame player, int version)
|
||
|
{
|
||
|
if (player.battleVersion != version)
|
||
|
{
|
||
|
//战斗版本号发生了变化 需要清除缓存的战斗服务器id
|
||
|
player.battleVersion = version;
|
||
|
player.cacheBattleAppId = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|