using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; namespace World { public static class WorldGlobalDataUtils { public static string GetDataKey(int dataType, int globalId,int dataVer) { return dataType.ToString() + "|" + globalId + "|" + dataVer; } public static bool UpdateWorldGlobalData(ref SSWorldGlobalDataDBOpReq req) { WorldGlobalData gd = WorldServerUtils.GetWorldGlobalData(); var AllData = gd.allData; string key = GetDataKey((int)req.DataType, req.GlobalId,req.DataVer); if (!AllData.ContainsKey(key)) { //出错了,数据还没加载出来 TraceLog.Error("WorldGlobalDataUtils.UpdateWorldGlobalData not load data key {0} form db",key); return false; } AllData[key].needSaveDB = true; AllData[key].needBroadcast = true; return true; } public static WorldGlobalDataOne GetWorldGlobalData(int dataType, int globalId, int dataVer) { WorldGlobalData gd = WorldServerUtils.GetWorldGlobalData(); var AllData = gd.allData; string key = GetDataKey(dataType, globalId, dataVer); if (AllData.ContainsKey(key)) { return AllData[key]; } return null; } //停止时判断数据是否都保存了 public static bool IsAllDataSave() { WorldGlobalData gd = WorldServerUtils.GetWorldGlobalData(); foreach (var item in gd.allData.Values) { if(item.needSaveDB) { return false; } } return true; } //查询数据 public static void QueryWorldGlobalData(int dataType, int globalId, int dataVer) { SSWorldGlobalDataDBOpReq req = new SSWorldGlobalDataDBOpReq(); req.OpType = WorldGlobalDataDBOpType.Query; req.DataType = (WorldGlobalDataType)dataType; req.GlobalId = globalId; req.DataVer = dataVer; uint dbServerID = DBServerIDUtils.GetGameDBServerID(0); WorldServerUtils.GetPacketSender().SendToServerByID(dbServerID, (int)SSGameMsgID.WorldglobaldataDbOpReq, ref req,0); } //保存数据 public static void SaveAllWorldGlobalData() { uint dbServerID = DBServerIDUtils.GetGameDBServerID(0); SSWorldGlobalDataDBOpReq req = new SSWorldGlobalDataDBOpReq(); req.OpType = WorldGlobalDataDBOpType.Update; WorldGlobalData gd = WorldServerUtils.GetWorldGlobalData(); foreach (var item in gd.allData.Values) { if (item.needSaveDB) { req.DataType = item.dataType; req.GlobalId = item.globalId; req.DataVer = item.dataVer; bool needSend = ByteArryToStruct(ref req.Data, ToByteArray(req.DataType,ref item.protoData)); //存储数据 if (needSend) { WorldServerUtils.GetPacketSender().SendToServerByID(dbServerID, (int)SSGameMsgID.WorldglobaldataDbOpReq, ref req, 0); } } } } public static void BroadcastAllWorldGlobalData(bool onlyGame = false) { CSWorldGlobalDataNotify notify = new CSWorldGlobalDataNotify(); WorldGlobalData gd = WorldServerUtils.GetWorldGlobalData(); foreach (var item in gd.allData.Values) { if (item.needBroadcast) { if(onlyGame) { continue; } item.needBroadcast = false; notify.DataType = item.dataType; notify.GlobalId = item.globalId; notify.DataVer = item.dataVer; bool needSend = CopyToNotify(ref notify,ref item.protoData); //广播数据 if (needSend) { WorldServerUtils.GetPacketSender().Broadcast((int)ServerType.Game, (int)CSGameMsgID.WorldglobaldataNotify, ref notify, 0,0); } } } } //struct 和 byte 转换 private static bool ByteArryToStruct(ref RepeatedByte_1024 data,byte[] buffer) { if (buffer == null) { return false; } for (int i = 0; i < buffer.Length && i < data.GetMaxCount(); i++) { data.Add(buffer[i]); } return true; } public static byte[] StructToByteArry(ref RepeatedByte_1024 data) { byte[] buffer = new byte[data.Count]; for (int i = 0; i < data.Count; i++) { buffer[i] = data[i]; } return buffer; } //协议 数据 之间的转换 public static bool BytesArryEquals(byte[] b1, RepeatedByte_1024 b2) { if (b1 == null) return false; if (b1.Length != b2.Count) return false; for (int i = 0; i < b1.Length; i++) if (b1[i] != b2[i]) return false; return true; } public static bool ParseFrom(WorldGlobalDataType dataType, ref IStructMessage protoData,byte[] buffer) { if (buffer == null) { return false; } switch (dataType) { default: return false; } return true; } public static byte[] ToByteArray(WorldGlobalDataType dataType, ref IStructMessage protoData) { switch (dataType) { default: return null; } } public static bool CopyToNotify(ref CSWorldGlobalDataNotify notify, ref IStructMessage protoData) { switch (notify.DataType) { default: TraceLog.Trace("WorldGlobalDataUtils.CopyToNotify type {0} error", notify.DataType); return false; } } } }