using System; using System.Collections.Generic; using System.Threading.Tasks; 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:{opts}"); 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; } } 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); player.Map = self; } /** 分配阵营 **/ 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}"); self.Players.TryGetValue(player.GetId(), out WNPlayer actorPlayer); if (actorPlayer != null) { self.Players.Remove(player.GetId()); } } /** 玩家进入场景请求 **/ 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 OnReady(this Map self, WNPlayer player) { } /** 玩家登录事件 **/ public static void OnPlayerLogin(this Map self, WNPlayer player) { } /** 角色成功进入场景 **/ public static void OnPlayerEntered(this Map self, WNPlayer player) { } /** 绑定战斗服 **/ 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.Trim(), data); } /** 创建单位 **/ public static async ETTask AddUnitsToMap(this Map self, Struct.MonsterUnit unit, bool needReturn) { List listData = new List(); listData.Add(unit); int addUnitsResult = 0; if (needReturn) { addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(listData, Formatting.Indented)); } else { int objId = await self.AddUnits(self.Id.ToString().Trim(), ""); Log.Info($"addUnitsToArea: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}"); } Log.Info($"addUnitsToArea: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={listData}, add units result={addUnitsResult}"); return addUnitsResult; } /** 移除单位 **/ public static async ETTask RemoveUnit(this Map self, int unitId) { self.GetXmdsManager().removeUnit(self.Id.ToString().Trim(), unitId); await ETTask.CompletedTask; } } }