using System; using System.Collections.Generic; using BattleIce; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ET.Server { [FriendOf(typeof (Map))] [FriendOf(typeof (BattleIceAgentComponent))] public static class MapSystem { public class MapAwakeSystem: AwakeSystem { /// /// 本地地图场景实体创建 /// /// /// protected override void Awake(Map self, JObject opts) { self.createTime = TimeHelper.ServerNow(); Log.Debug($"create area opts:{JsonConvert.SerializeObject(opts, Formatting.Indented)}"); self.LogicServerId = opts.SelectToken("logicServerId") != null? Convert.ToInt32(opts.SelectToken("logicServerId")) : 0; self.MapId = Convert.ToInt32(opts.SelectToken("areaId")); self.Prop = MapConfigCategory.Instance.Get(self.MapId); self.Type = self.Prop.Type; self.UnitObjIds = new List(); // 场景事件组件 self.AddComponent(); // 场景复活组件 self.AddComponent(); // 场景排行榜组件 self.AddComponent(); } } public class MapDestroySystem: DestroySystem { /// /// 本地地图场景实体销毁 /// /// protected override void Destroy(Map self) { } } public static ZoneManagerPrx GetZoneManager(this Map self) { return self.DomainScene().GetComponent().IceZoneManager; } public static XmdsManagerPrx GetXmdsManager(this Map self) { return self.DomainScene().GetComponent().IceXmdsManager; } public static GetPlayerData GetPlayerData(this Map self, long playerId) { string result = self.GetXmdsManager().getPlayerData(playerId.ToString().Trim(), true); return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject(result); } /** 从战斗服同步角色数据 **/ public static void SyncPlayerHistoryData(this Map self, WNPlayer player) { GetPlayerData result = self.GetPlayerData(player.GetId()); if (result == null) { return; } player.GetComponent().SyncNowData(self.MapId, self.InstanceId, result); player.GetComponent().SyncHistoryData(self.Prop, self.InstanceId, result); } /** 场景添加角色 **/ public static void AddPlayer(this Map self, WNPlayer player) { Log.Info($"addPlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}"); self.SetForce(player); self.Players.TryAdd(player.GetId(), player); player.Map = self; } /** 获取场景角色 **/ public static WNPlayer GetPlayer(this Map self, long playerId) { return self.Players.TryGetValue(playerId, out WNPlayer player) ? player : null; } /** 分配阵营 **/ public static void SetForce(this Map self, WNPlayer player) { player.Force = (int)AreaForce.FORCEA; } /** 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用 **/ public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject) { Log.Info($"removePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}"); WNPlayer actorPlayer = self.GetPlayer(player.GetId()); if (actorPlayer != null) { self.Players.Remove(player.GetId()); } // 重置进入场景标识 player.GetComponent().MapData.ready = false; } /** 玩家进入场景请求 **/ public static void PlayerEnterRequest(this Map self, WNPlayer player) { bool ready = player.GetComponent().MapData.ready; if (ready) { Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={ready}"); return; } try { self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), player.ToJSON4EnterScene(self)); } catch (Exception e) { Log.Warning( $"playerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}"); } } /** 玩家离开场景请求 **/ public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject) { try { self.GetZoneManager().playerLeaveRequest(player.GetId().ToString().Trim(), self.InstanceId.ToString().Trim(), keepObject); } catch (Exception e) { Log.Warning($"playerLeaveRequest: catch - {e}"); } Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}"); } /** 绑定战斗服 **/ public static void BindBattleServer(this Map self, WNPlayer player, string serverId) { self.BattleServerId = serverId; } /** 创建单位 **/ public static async ETTask AddUnits(this Map self, string instanceId, string data) { await ETTask.CompletedTask; return self.GetXmdsManager().addUnits(instanceId, data); } /** 创建单位 **/ public static async ETTask AddUnits(this Map self, Struct.MonsterUnit unit, bool needReturn) { List listData = new List(); listData.Add(unit); int addUnitsResult = 0; int objId = 0; if (needReturn) { addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(listData, Formatting.Indented)); Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={listData}, add units result={addUnitsResult}"); } else { objId = await self.AddUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(listData, Formatting.Indented)); Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}"); } return objId; } /** 移除单位 **/ public static async ETTask RemoveUnit(this Map self, int unitId) { self.GetXmdsManager().removeUnit(self.Id.ToString().Trim(), unitId); await ETTask.CompletedTask; } /// /// 场景复活数据 /// /// /// 1:原地复活;2:出生点复活;3:复活点复活. 4:技能复活. public static string ReliveData(this Map self, ReliveType type) { JObject jsonObject = new (); jsonObject.Add("type", (int)type); jsonObject.Add("qty", 0); jsonObject.Add("itemType", "diamond"); jsonObject.Add("hp", 8); jsonObject.Add("mp", 0); return JsonConvert.SerializeObject(jsonObject, Formatting.Indented); } /// /// 是否结束 /// /// /// public static bool IsGameOver(this Map self) { return self.IsGameOver; } } }