using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; namespace World { public static class WorldGlobalDataHandler { //临时数据 static Dictionary> playerUid_map = new Dictionary>(); //来自gameserver public static void OnWorldglobaldataDbOpReq(uint remoteAppID, StructPacket packet) { // playeruid == packet.ObjectID PlayerInfoWorld player = WorldServerUtils.GetPlayerTableOp().GetPlayerInfo(packet.ObjectID); if (player == null) { TraceLog.Error("WorldGlobalDataHandler.OnWorldglobaldataDbOpReq no player uid {0}", packet.ObjectID); return; } ref SSWorldGlobalDataDBOpReq req = ref packet.GetMessage(); switch (req.OpType) { case WorldGlobalDataDBOpType.Query: PlayerQueryWorldglobaldata(player,ref req); break; case WorldGlobalDataDBOpType.Update: PlayerUpdateWorldglobaldata(player, ref req); break; } } public static void OnWorldglobaldataDbOpNewReq(uint remoteAppID, StructPacket packet) { ref SSWorldGlobalDataDBOpNewReq req = ref packet.GetMessage(); switch (req.OpType) { case WorldGlobalDataDBOpType.Query: //PlayerQueryWorldglobaldataNew(ref req); break; case WorldGlobalDataDBOpType.Update: //PlayerUpdateWorldglobaldata(player, ref req); break; } } //来自gamedbserver public static void OnWorldglobaldataDbOpRes(uint remoteAppID, StructPacket packet) { ref SSWorldGlobalDataDBOpRes res = ref packet.GetMessage(); if(res.Ret != CSErrCode.None) { return;//操作失败 } WorldGlobalDataOne one = WorldGlobalDataUtils.GetWorldGlobalData((int)res.DataType, res.GlobalId, res.DataVer); string key = WorldGlobalDataUtils.GetDataKey((int)res.DataType, res.GlobalId, res.DataVer); if (one == null) { one = new WorldGlobalDataOne(); one.dataType = res.DataType; one.globalId = res.GlobalId; one.dataVer = res.DataVer; one.needSaveDB = true; var succ = WorldGlobalDataUtils.ParseFrom(res.DataType,ref one.protoData, WorldGlobalDataUtils.StructToByteArry(ref res.Data)); if(!succ) { return; } WorldGlobalData gd = WorldServerUtils.GetWorldGlobalData(); gd.allData.Add(key, one); } switch (res.OpType) { case WorldGlobalDataDBOpType.Query: //通知玩家 if (playerUid_map.ContainsKey(key)) { CSWorldGlobalDataNotify notify = new CSWorldGlobalDataNotify(); notify.DataType = res.DataType; notify.GlobalId = res.GlobalId; notify.DataVer = res.DataVer; WorldGlobalDataUtils.CopyToNotify(ref notify, ref one.protoData); foreach (var playerUid in playerUid_map[key]) { PlayerInfoWorld player = WorldServerUtils.GetPlayerTableOp().GetPlayerInfo(playerUid); if (player != null) { WorldServerUtils.GetPacketSender().SendToServerByID(player.GameServerID, (int)CSGameMsgID.WorldglobaldataNotify, ref notify, playerUid); } } playerUid_map[key].Clear(); } break; case WorldGlobalDataDBOpType.Update: //更新数据 byte[] bty = WorldGlobalDataUtils.ToByteArray(res.DataType, ref one.protoData); if (WorldGlobalDataUtils.BytesArryEquals(bty, res.Data)) { one.needSaveDB = false; } break; } } private static void PlayerQueryWorldglobaldata(PlayerInfoWorld player, ref SSWorldGlobalDataDBOpReq req) { WorldGlobalDataOne one = WorldGlobalDataUtils.GetWorldGlobalData((int)req.DataType, req.GlobalId,req.DataVer); if (one == null) { string key = WorldGlobalDataUtils.GetDataKey((int)req.DataType, req.GlobalId, req.DataVer); if (!playerUid_map.ContainsKey(key)) { playerUid_map.Add(key, new List()); } //没有数据,直接查询(如果超过10个还没返回结果,继续查询) if (playerUid_map[key].Count == 0 || playerUid_map[key].Count > 10) { WorldGlobalDataUtils.QueryWorldGlobalData((int)req.DataType, req.GlobalId, req.DataVer); } if(!playerUid_map[key].Contains(player.UserID)) { playerUid_map[key].Add(player.UserID); } } else { //已经有数据了,直接通知玩家 CSWorldGlobalDataNotify res = new CSWorldGlobalDataNotify(); res.DataType = req.DataType; res.GlobalId = req.GlobalId; res.DataVer = req.DataVer; WorldGlobalDataUtils.CopyToNotify(ref res, ref one.protoData); WorldServerUtils.GetPacketSender().SendToServerByID(player.GameServerID, (int)CSGameMsgID.WorldglobaldataNotify, ref res, player.UserID); } } private static void PlayerQueryWorldglobaldataNew(ref SSWorldGlobalDataDBOpReq req) { } private static void PlayerUpdateWorldglobaldata(PlayerInfoWorld player, ref SSWorldGlobalDataDBOpReq req) { WorldGlobalDataUtils.UpdateWorldGlobalData(ref req); } } }