using System; using System.Collections.Generic; using System.Linq; namespace ET.Server { [FriendOf(typeof (SessionPlayerComponent))] public static class SessionPlayerComponentSystem { public class SessionPlayerComponentAwakeSystem: AwakeSystem { protected override void Awake(SessionPlayerComponent self, PlayerInfo playerInfo) { Log.Info($"创建session玩家绑定组件..."); self.PlayerInfo = playerInfo; self.PlayerId = playerInfo.Id; } } public class SessionPlayerComponentDestroySystem: DestroySystem { protected override void Destroy(SessionPlayerComponent self) { WNPlayer player = self.GetMyPlayer(); if (player == null) { return; } Log.Info($"处理玩家下线逻辑, playerId={self.PlayerId}, name={player.GetName()}, mapId={player.Map.MapId}"); // todo 玩家各组件下线相关处理 Map map = player.Map; if (map != null) { try { // 玩家离开 if (map.Actors.TryGetValue(player.GetId(), out GameStruct.Actor actor)) { actor.Ready = false; } // 记录玩家历史 map.SyncPlayerHistoryData(player); } catch (Exception e) { Log.Error($"处理玩家下线逻辑, 出错: catch 1 - {e}"); } finally { try { map.OnPlayerLogout(player); } catch (Exception e) { Log.Error($"处理玩家下线逻辑, 出错: catch 2 - {e}"); } } } player.Dispose(); } } /// /// 绑定玩家id /// /// /// public static void BindPlayerId(this SessionPlayerComponent self, WNPlayer player) { Log.Debug($"session玩家绑定组件 绑定玩家id={player.GetId()}"); self.PlayerId = player.GetId(); // 添加本地玩家数据 self.DomainScene().GetComponent().Add(player.GetId(), player); } /// /// 获取当前玩家实体 /// /// /// public static WNPlayer GetMyPlayer(this SessionPlayerComponent self) { return self.DomainScene().GetComponent().Get(self.PlayerId); } } }