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.
 
 
 
 
 
 

169 lines
5.3 KiB

using System.Collections.Generic;
using System.Linq;
using ProtoCSStruct;
using Sog;
namespace Game
{
public static class PresetSvc
{
private static long m_lastTickTime;
private static string GetSaveFileName()
{
return "./GameServer_PresetDb_" + GameServerUtils.GetApp().ServerID.ToString() + ".fdb";
}
/// <summary>
/// 保存到数据库,存本地文件
/// </summary>
public static void SaveDataToFile()
{
ref var data = ref GameServerUtils.GetGameServerData().presetdata;
string filename = GetSaveFileName();
int iRet = Sog.IO.FileDBSave.SaveToFile(ref data, filename);
if (iRet != 0)
{
TraceLog.Error("PresetSvc.SaveDataToFile save file {0} error ,ret {1}", filename, iRet);
return;
}
TraceLog.Trace("PresetSvc.SaveDataToFile save file {0} success", filename);
}
public static void LoadDataFromFile()
{
string filename = GetSaveFileName();
TraceLog.Trace("PresetSvc.LoadDataFromFile load now file {0}", filename);
ref PresetData data = ref GameServerUtils.GetGameServerData().presetdata;
bool success = Sog.IO.FileDBSave.ReadFromFile(ref data, filename);
if (success)
{
TraceLog.Trace("PresetSvc.LoadDataFromFile load file {0} success, count {1}"
, filename, data.Data.Count);
}
else
{
RefreshSyncPresetReq();
}
}
public static void OnTick(long nowMs)
{
//5秒一次
if (nowMs - m_lastTickTime < 5000)
{
return;
}
m_lastTickTime = nowMs;
//RefreshSyncPresetReq();
}
public static void RefreshSyncPresetReq()
{
ref PresetData data = ref GameServerUtils.GetGameServerData().presetdata;
SSSyncPresetDataReq req = new SSSyncPresetDataReq();
req.ReqVersion = data.Req;
req.GId = (int)GameServerUtils.GetAppID();
GameServerUtils.GetPacketSender().SendToWorldServer((int)SSGameMsgID.SyncPresetReq, ref req, 0);
}
public static void RefreshSyncPresetDataRes(StructPacket packet)
{
var res = packet.GetMessage<SSSyncPresetDataRes>();
ref PresetData data = ref GameServerUtils.GetGameServerData().presetdata;
if (res.ResVersion == data.Req)
{
return;
}
UpdateGmUid(data.Settings.GmUid, res.PresetData.Settings.GmUid);
data = res.PresetData;
TraceLog.Trace("PresetSvc.RefreshSyncPresetDataRes sync data received. version={0}", data.Req);
SaveDataToFile();
}
private static void UpdateGmUid(RepeatedLong_64 oldUid, RepeatedLong_64 newUid)
{
var gmUidArray = GameServerUtils.GetServerConfig().gmUid;
var list = gmUidArray.ToList();
for (int i = 0; i < oldUid.Count; i++)
{
if (gmUidArray.Contains(oldUid[i]))
{
list.Remove(oldUid[i]);
}
}
for (int i = 0; i < newUid.Count; i++)
{
if (!list.Contains(newUid[i]))
{
list.Add(newUid[i]);
}
}
GameServerUtils.GetServerConfig().gmUid = list.ToArray();
}
public static List<TypeIDValue32> GetPresetData(int realmId)
{
long now = GameServerUtils.GetTimeSecond();
List<TypeIDValue32> presetItems = new List<TypeIDValue32>();
PresetData data = GameServerUtils.GetGameServerData().presetdata;
for (var i = 0; i < data.Data.Count; i++)
{
var realmIds = data.Data[i].ReamIds;
bool exist = true;
if (realmIds.Count > 0)
{
exist = false;
for (var j = 0; j < realmIds.Count; j++)
{
if (realmId != realmIds[j]) continue;
exist = true;
break;
}
}
if (!exist)
{
continue;
}
if (now < data.Data[i].Start_time || now > data.Data[i].End_time)
{
continue;
}
var items = data.Data[i].Items;
for (int j = 0; j < items.Count; j++)
{
presetItems.Add(items[j]);
}
}
return presetItems;
}
public static int GetDelayQuestion()
{
return GameServerUtils.GetGameServerData().presetdata.Settings.DelayQuestion;
}
public static int GetOldPlayerReamId()
{
var targetReamId = 801001;
var oldPlayerReamId = GameServerUtils.GetGameServerData().presetdata.Settings.OldPlayerReamId;
if (oldPlayerReamId > 0)
{
targetReamId = oldPlayerReamId;
}
return targetReamId;
}
}
}