123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- 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<Map, JObject, WNPlayer>
- {
- protected override void Awake(Map self, JObject opts, WNPlayer player)
- {
- Log.Debug($"创建场景实体...create area opts:{opts.ToString()}");
- self.CreateTime = TimeHelper.ServerNow();
- 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.HasPlayerEntered = false;
- self.Actors = new Dictionary<long, Struct.Actor>();
- self.DeadUnits = new List<int>();
- self.DeadUnitPlayer = new List<int>();
- self.IsLeaveKeepObject = false;
- // 战斗服事件组件
- self.AddComponent<MapEventComponent>();
- }
- }
- public class MapDestroySystem: DestroySystem<Map>
- {
- protected override void Destroy(Map self)
- {
- Log.Info($"销毁场景");
- }
- }
- public static ZoneManagerPrx GetZoneManager(this Map self)
- {
- return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceZoneManager;
- }
- public static XmdsManagerPrx GetXmdsManager(this Map self)
- {
- return self.DomainScene().GetComponent<BattleIceAgentComponent>().IceXmdsManager;
- }
- /// <summary>
- /// 获取战斗服角色数据
- /// </summary>
- /// <param name="self"></param>
- /// <param name="playerId"></param>
- /// <returns></returns>
- private static GetPlayerData GetBattleServerPlayerData(this Map self, long playerId)
- {
- string result = self.GetXmdsManager().getPlayerData(playerId.ToString(), true);
- return string.IsNullOrEmpty(result)? null : JsonConvert.DeserializeObject<GetPlayerData>(result);
- }
- /// <summary>
- /// 从战斗服同步角色数据
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
- {
- GetPlayerData result = self.GetBattleServerPlayerData(player.GetId());
- if (result == null)
- {
- return;
- }
- player.GetComponent<PlayerTempDataComponent>().SyncNowData(self.MapId, self.InstanceId, result);
- player.GetComponent<PlayerTempDataComponent>().SyncHistoryData(self.Prop, self.InstanceId, result);
- }
- /// <summary>
- /// 分配阵营
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void SetForce(this Map self, WNPlayer player)
- {
- player.Force = (int)AreaForce.FORCEA;
- }
- /// <summary>
- /// 场景添加角色
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- 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;
- if (!self.Actors.ContainsKey(player.GetId()))
- {
- self.HasPlayerEntered = true;
- self.Actors.Add(player.GetId(), new Struct.Actor());
- }
- }
- /// <summary>
- /// 玩家进场景后推的消息
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void OnReady(this Map self, WNPlayer player)
- {
- }
- /// <summary>
- /// 角色成功进入场景
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void OnPlayerEntered(this Map self, WNPlayer player)
- {
- }
- /// <summary>
- /// 玩家登录事件
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void OnPlayerLogin(this Map self, WNPlayer player)
- {
- }
- /// <summary>
- /// 移除角色,通用框架接口 切换场景/掉线会自动调用,尽量 不要手动调用
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- /// <param name="keepObject"></param>
- public static void RemovePlayer(this Map self, WNPlayer player, bool keepObject)
- {
- Log.Info($"RemovePlayer: playerId={player.GetId()}, mapId={self.MapId}, ip={player.Session.RemoteAddress}");
- // 重置进入场景标识
- // player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
- if (self.Actors.ContainsKey(player.GetId()))
- {
- self.PlayerLeaveRequest(player, keepObject);
- self.Actors.Remove(player.GetId());
- }
- }
- /// <summary>
- /// 玩家离线时触发
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void OnPlayerLogout(this Map self, WNPlayer player)
- {
- if (self.Actors.ContainsKey(player.GetId()))
- {
- // 本场景移除玩家
- self.RemovePlayer(player, self.IsLeaveKeepObject);
- }
- // 移除本地玩家数据
- self.DomainScene().GetComponent<GamePlayerComponent>().Remove(player.GetId());
- }
- /// <summary>
- /// 绑定战斗服
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- /// <param name="serverId"></param>
- public static void BindBattleServer(this Map self, WNPlayer player, string serverId)
- {
- self.BattleServerId = serverId;
- }
- /// <summary>
- /// 创建单位
- /// </summary>
- /// <param name="self"></param>
- /// <param name="data"></param>
- /// <param name="needReturn"></param>
- /// <returns></returns>
- private static async ETTask<int> AddUnits(this Map self, List<Struct.MonsterUnit> data, bool needReturn)
- {
- if (data.Count <= 0)
- {
- return 0;
- }
- int addUnitsResult = 0;
- if (needReturn) {
- addUnitsResult = self.GetXmdsManager().addUnits(self.Id.ToString(), JsonConvert.SerializeObject(data, Formatting.Indented));
- Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString()}, data={data}, add units result={addUnitsResult}");
- } else
- {
- int objId = self.GetXmdsManager().addUnits(self.Id.ToString(), JsonConvert.SerializeObject(data, Formatting.Indented));
- Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString()}, objId={objId}");
- }
- await ETTask.CompletedTask;
- return addUnitsResult;
- }
- private static async ETTask<int> AddUnits(this Map self, Struct.MonsterUnit data, bool needReturn)
- {
- List<Struct.MonsterUnit> listData = new List<Struct.MonsterUnit>();
- listData.Add(data);
- return await self.AddUnits(listData, needReturn);
- }
- /// <summary>
- /// 移除单位,战斗服创建的unit
- /// </summary>
- /// <param name="self"></param>
- /// <param name="objectId"></param>
- public static async ETTask RemovePointUnit(this Map self, int objectId)
- {
- self.GetXmdsManager().removePointUnit(self.Id.ToString(), objectId);
- await ETTask.CompletedTask;
- }
- /// <summary>
- /// 玩家进入场景请求
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void PlayerEnterRequest(this Map self, WNPlayer player)
- {
- self.Actors.TryGetValue(player.GetId(), out Struct.Actor actor);
- if (actor.Ready)
- {
- Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={actor.Ready}");
- return;
- }
- try
- {
- string enterData = player.ToJSON4EnterScene(self);
- Log.Debug($"{player.GetName()}, EnterSceneData:{enterData}");
- self.GetZoneManager().playerEnterRequest(player.GetId().ToString(), self.Id.ToString(), enterData);
- }
- catch (Exception e)
- {
- Log.Error($"PlayerEnterRequest: playerName={player.GetName()}, instanceId={self.Id}, mapName={self.Prop.Name}, serverID={self.BattleServerId}, e={e}");
- }
- }
- /// <summary>
- /// 玩家离开场景请求
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- /// <param name="keepObject"></param>
- public static void PlayerLeaveRequest(this Map self, WNPlayer player, bool keepObject)
- {
- try
- {
- self.GetZoneManager().playerLeaveRequest(player.GetId().ToString(), self.InstanceId.ToString(), keepObject);
- }
- catch (Exception e)
- {
- Log.Error($"PlayerLeaveRequest: 出错 - {e}");
- }
- Log.Debug($"PlayerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
- }
- /// <summary>
- /// 战斗服结束场景
- /// </summary>
- /// <param name="self"></param>
- public static void DestroyZoneRequest(this Map self)
- {
- self.GetZoneManager().destroyZoneRequest(self.Id.ToString());
- }
- }
- }
|