123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- 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<Map, JObject>
- {
- 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.UnitPlayers = new Dictionary<string, int>();
- self.UnitObjIds = new Dictionary<string, int>();
- self.UnitPlayerLikes = new Dictionary<string, int>();
- // 战斗服事件组件
- self.AddComponent<MapEventComponent>();
- // 场景复活组件
- self.AddComponent<MapReliveTimeComponent>();
- // 场景排行榜组件
- self.AddComponent<MapRankComponent>();
- }
- }
- public class MapDestroySystem: DestroySystem<Map>
- {
- protected override void Destroy(Map self)
- {
- }
- }
- 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;
- }
- 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<GetPlayerData>(result);
- }
- /// <summary>
- /// 从战斗服同步角色数据
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void SyncPlayerHistoryData(this Map self, WNPlayer player)
- {
- GetPlayerData result = self.GetPlayerData(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="playerId"></param>
- /// <returns></returns>
- public static WNPlayer GetPlayer(this Map self, long playerId)
- {
- return self.Players.TryGetValue(playerId, out WNPlayer player) ? player : null;
- }
- /// <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>
- /// <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}");
- WNPlayer actorPlayer = self.GetPlayer(player.GetId());
- if (actorPlayer != null)
- {
- self.Players.Remove(player.GetId());
- }
- // 重置进入场景标识
- player.GetComponent<PlayerTempDataComponent>().MapData.ready = false;
- }
- /// <summary>
- /// 玩家进入场景请求
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void PlayerEnterRequest(this Map self, WNPlayer player)
- {
- bool ready = player.GetComponent<PlayerTempDataComponent>().MapData.ready;
- if (ready)
- {
- Log.Info($"PlayerEnterRequest: playerId={player.GetId()}, ready={ready}");
- return;
- }
- try
- {
- string enterData = player.ToJSON4EnterScene(self);
- Log.Debug($"{player.GetName()}, enterSceneData:{enterData}");
- self.GetZoneManager().playerEnterRequest(player.GetId().ToString().Trim(), self.Id.ToString().Trim(), enterData);
- }
- catch (Exception e)
- {
- Log.Warning(
- $"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>
- 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;
- }
- /// <summary>
- /// 玩家进场景后推的消息
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void PlayerReady(this Map self, WNPlayer player)
- {
- }
- /// <summary>
- /// 角色成功进入场景
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void PlayerEntered(this Map self, WNPlayer player)
- {
- }
- /// <summary>
- /// 玩家登录事件
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void PlayerLogin(this Map self, WNPlayer player)
- {
- }
- /// <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().Trim(), self.InstanceId.ToString().Trim(), keepObject);
- }
- catch (Exception e)
- {
- Log.Warning($"playerLeaveRequest: catch - {e}");
- }
- Log.Debug($"playerLeaveRequest--------------------{player.GetName()} - {self.InstanceId} - {self.Prop.Name}");
- }
- /// <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>
- public 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().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
- Log.Info($"addUnits needReturn : mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, data={data}, add units result={addUnitsResult}");
- } else
- {
- int objId = self.GetXmdsManager().addUnits(self.Id.ToString().Trim(), JsonConvert.SerializeObject(data, Formatting.Indented));
- Log.Info($"addUnits: mapId={self.MapId}, instanceId={self.Id.ToString().Trim()}, objId={objId}");
- }
- await ETTask.CompletedTask;
- return addUnitsResult;
- }
- public 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().Trim(), objectId);
- await ETTask.CompletedTask;
- }
- /// <summary>
- /// 场景复活数据
- /// </summary>
- /// <param name="self"></param>
- /// <param name="type">1:原地复活;2:出生点复活;3:复活点复活. 4:技能复活.</param>
- 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);
- }
- /// <summary>
- /// 是否结束
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static bool IsGameOver(this Map self)
- {
- return self.IsGameOver;
- }
- /// <summary>
- /// 增加单位玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="openId"></param>
- /// <param name="objId"></param>
- public static void AddUnitObjId(this Map self, string openId, int objId)
- {
- if (objId <= 0 || self.IsGameOver())
- {
- return;
- }
- if (!self.UnitObjIds.TryAdd(openId, objId))
- {
- self.UnitObjIds[openId] = objId;
- }
- }
- /// <summary>
- /// 获取单位玩家objId
- /// </summary>
- /// <param name="self"></param>
- /// <param name="openId"></param>
- public static int GetUnitObjId(this Map self, string openId)
- {
- if (self.UnitObjIds.TryGetValue(openId, out int objId))
- {
- return objId;
- }
- return 0;
- }
- /// <summary>
- /// 添加单位玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="openId"></param>
- /// <param name="templateId"></param>
- public static void AddUnitPlayer(this Map self, string openId, int templateId)
- {
- if (templateId <= 0 || self.IsGameOver())
- {
- return;
- }
- if (!self.UnitPlayers.TryAdd(openId, templateId))
- {
- self.UnitPlayers[openId] = templateId;
- }
- }
- /// <summary>
- /// 获取单位玩家模板id
- /// </summary>
- /// <param name="self"></param>
- /// <param name="openId"></param>
- /// <returns></returns>
- public static int GetUnitTemplateId(this Map self, string openId)
- {
- if (self.UnitPlayers.TryGetValue(openId, out int templateId))
- {
- return templateId;
- }
- return 0;
- }
- /// <summary>
- /// 获取当前坐标xy
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static string GetCurXY(this Map self)
- {
- // return self.DeadUnits.Count switch
- // {
- // // 1塔位置
- // 0 => "230;85",
- // // 1塔挂了,2塔位置
- // 1 when self.DeadUnits.Contains(1001) => "150;85",
- // // 1塔2塔挂了,3塔位置
- // 2 when self.DeadUnits.Contains(1001) && self.DeadUnits.Contains(1002) => "70;85",
- // _ => "230;85"
- // };
- // todo 默认塔1位置,后面调整为路点
- return "218;78";
- }
- }
- }
|