123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- 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.RoomId = player.GetRoomId();
- 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.Player = player;
- self.UnitPlayers = new Dictionary<string, Struct.UnitPlayerData>();
- self.DeadUnits = new List<int>();
- self.DeadUnitPlayer = new List<int>();
- // 战斗服事件组件
- self.AddComponent<MapEventComponent>();
- // 场景排行榜组件
- self.AddComponent<MapRankComponent>();
- // token为空过滤一下抖音相关组件
- if (self.TokenIsNull())
- {
- return;
- }
- // 抖音直播评论任务组件
- self.AddComponent<MapDouyinLiveCommentComponent>();
- // 抖音直播礼物任务组件
- self.AddComponent<MapDouyinLiveGiftComponent>();
- // 抖音直播点赞任务组件
- self.AddComponent<MapDouyinLiveLikeComponent>();
- // 抖音直播礼物置顶
- self.DomainScene().GetComponent<GameDouyinComponent>().TopGifts(self.RoomId);
- }
- }
- 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;
- }
- 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.Player;
- }
- /// <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}");
- player.Map = self;
- self.SetForce(player);
- self.Player = player;
- }
- /// <summary>
- /// 玩家进场景后推的消息
- /// </summary>
- /// <param name="self"></param>
- /// <param name="player"></param>
- public static void PlayerReady(this Map self, WNPlayer player)
- {
- self.CurBattleIndex = 0;
- }
- /// <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="templateId"></param>
- /// <param name="objId"></param>
- /// <param name="likes"></param>
- /// <param name="name"></param>
- /// <param name="url"></param>
- public static Struct.UnitPlayerData AddUnitPlayer(this Map self, string openId, int templateId, int objId, long likes, string name, string url)
- {
- if (string.IsNullOrEmpty(openId) || templateId <= 0 || objId <= 0 || self.IsGameOver())
- {
- return null;
- }
- Struct.UnitPlayerData unitPlayerData = null;
- if (self.UnitPlayers.ContainsKey(openId))
- {
- unitPlayerData = self.UnitPlayers[openId];
- unitPlayerData.TemplateId = templateId;
- unitPlayerData.ObjId = objId;
- unitPlayerData.Name = name;
- unitPlayerData.Url = url;
- unitPlayerData.Map = self;
- self.UnitPlayers[openId] = unitPlayerData;
- }
- else
- {
- unitPlayerData = new Struct.UnitPlayerData();
- unitPlayerData.OpenId = openId;
- unitPlayerData.TemplateId = templateId;
- unitPlayerData.ObjId = objId;
- unitPlayerData.Name = name;
- unitPlayerData.Url = url;
- unitPlayerData.Level = 1;
- unitPlayerData.Likes = 0;
- unitPlayerData.ReliveTime = 0;
- unitPlayerData.x = 0;
- unitPlayerData.y = 0;
- unitPlayerData.Map = self;
- self.UnitPlayers.TryAdd(openId, unitPlayerData);
- }
- return unitPlayerData;
- }
- /// <summary>
- /// 获取单位玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="openId"></param>
- /// <returns></returns>
- public static Struct.UnitPlayerData GetUnitPlayerByOpenId(this Map self, string openId)
- {
- if (self.UnitPlayers.TryGetValue(openId, out Struct.UnitPlayerData unitPlayer))
- {
- return unitPlayer;
- }
- return null;
- }
- /// <summary>
- /// 获取单位玩家
- /// </summary>
- /// <param name="self"></param>
- /// <param name="objId"></param>
- /// <returns></returns>
- public static Struct.UnitPlayerData GetUnitPlayerByObjId(this Map self, int objId)
- {
- return self.UnitPlayers.Values.FirstOrDefault(unitPlayer => unitPlayer != null && unitPlayer.ObjId == objId);
- }
- /// <summary>
- /// 玩家增加贡献值
- /// </summary>
- /// <param name="self"></param>
- /// <param name="openId"></param>
- /// <param name="addValue"></param>
- public static void AddContributeValue(this Map self, string openId, long addValue)
- {
- Struct.UnitPlayerData unitPlayerData = self.GetUnitPlayerByOpenId(openId);
- if (unitPlayerData == null)
- {
- return;
- }
- if (self.IsGameOver())
- {
- return;
- }
- long value = unitPlayerData.ContributeValue + addValue;
- if (value >= long.MaxValue)
- {
- value = long.MaxValue;
- }
- unitPlayerData.ContributeValue = value;
- }
- /// <summary>
- /// 获取抖音直播接口调用凭证
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static string GetDouyinAccessToken(this Map self)
- {
- return self.DomainScene().GetComponent<GameDouyinComponent>().AccessToken;
- }
- /// <summary>
- /// Token 是否为空
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static bool TokenIsNull(this Map self)
- {
- return self.DomainScene().GetComponent<GameDouyinComponent>().TokenIsNull;
- }
- /// <summary>
- /// 获得当前战场(当前塔位置),随机位置
- /// </summary>
- /// <param name="self"></param>
- /// <returns></returns>
- public static Vector2 GetRandomPlayerPos(this Map self)
- {
- Vector2[] TowerPos = { new Vector2() { X = 103, Y = 197 }, new Vector2() { X = 190, Y = 133 }, new Vector2() { X = 104, Y = 69 } };
- int index = self.CurBattleIndex;
- Vector2 tower = TowerPos[index];
- Random rand = new Random();
- float r;
- double ang;
- if (index == 0)
- {
- r = (float)Math.Sqrt(rand.Next(2500)) + 10;
- ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 78f / 180f + Math.PI;
- }
- else if (index == 1)
- {
- r = (float)Math.Sqrt(rand.Next(3136)) + 10;
- ang = rand.Next(20) / 180.0f * Math.PI + Math.PI * 76f / 180f;
- }
- else
- {
- r = (float)Math.Sqrt(rand.Next(2500)) + 10;
- ang = rand.Next(22) / 180.0f * Math.PI + Math.PI * 150f / 180f;
- }
- return new Vector2(tower.X + (float)(r * Math.Cos(ang)), tower.Y - (float)(r * Math.Sin(ang)));
- }
- public static void TransferUnitsToNewTower(this Map self)
- {
- foreach(Struct.UnitPlayerData player in self.UnitPlayers.Values)
- {
- Vector2 pos = self.GetRandomPlayerPos();
- self.GetXmdsManager().transferUnit(self.Id.ToString(), player.ObjId, pos.X, pos.Y);
- }
- Log.Debug($"transfer unit: {self.UnitPlayers.Count}");
- }
- }
- }
|