using Fantasy; using Fantasy.Async; using Fantasy.Entitas; using Fantasy.Network; using Fantasy.Platform.Net; using NB.Game; namespace NB.Gate; public static class GateUnitManageComponentSystem { public static GateUnit Add(this GateUnitManageComponent self, Session session, long accountId) { if (self.Units.TryGetValue(accountId, out var unit)) { unit.Session = session; return unit; } unit = Entity.Create(self.Scene, accountId, true, true); unit.AccountID = accountId; unit.Session = session; self.Units.Add(accountId, unit); return unit; } public static GateUnit? Get(this GateUnitManageComponent self, long accountId) { return self.Units.GetValueOrDefault(accountId); } public static bool TryGet(this GateUnitManageComponent self, long accountId, out GateUnit? unit) { return self.Units.TryGetValue(accountId, out unit); } public static async FTask Remove(this GateUnitManageComponent self, long accountId, bool isDispose = true) { if (!self.Units.TryGetValue(accountId, out var unit)) return; //通知其他服务器下线 var result = await GateLoginHelper.Offline(unit); if (result != 0) { Log.Error($"通知其他服务器下线失败,错误码:{result}"); return; } Log.Info($"accountId:{accountId} 下线成功"); self.Units.Remove(accountId); if (isDispose) { unit.Dispose(); } } /// /// 回话迭代器 /// /// /// public static IEnumerable ForEachUnitSession(this GateUnitManageComponent self) { foreach (var (_, gateUnit) in self.Units) { Session gateSession = gateUnit.Session; if (gateSession == null) continue; yield return gateSession; } } }