// WingsEmu // // Developed by NosWings Team using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using Master.Datas; using Master.Extensions; using Master.Proxies; using Master.Services; using WingsAPI.Communication.ServerApi.Protocol; using WingsEmu.Core.Extensions; using WingsEmu.Game; namespace Master.Managers { public class WorldServerManager { private readonly ConcurrentDictionary _worldServerById = new(); private readonly ConcurrentDictionary> _worldServersByWorldGroup = new(); public IEnumerable GetWorlds() { return _worldServerById.Select(keyValuePair => keyValuePair.Value); } public WorldServer GetWorldById(int channelId) => _worldServerById.TryGetValue(channelId, out WorldServer server) ? server : null; public IEnumerable GetWorldsByWorldGroup(string requestWorldGroup) => _worldServersByWorldGroup.TryGetValue(requestWorldGroup, out List servers) ? servers : null; public bool UnregisterWorld(int channelId) { if (!_worldServerById.Remove(channelId, out WorldServer value)) { return false; } if (!_worldServersByWorldGroup.TryGetValue(value.WorldGroup, out List servers)) { return false; } servers.RemoveAll(s => s.ChannelId == channelId); return true; } public void RegisterWorldServer(SerializableGameServer serialized) { var worldServer = serialized.ToWorldServer(); worldServer.LastPulse = DateTime.UtcNow; _worldServerById[worldServer.ChannelId] = worldServer; _worldServersByWorldGroup.GetOrSetDefault(worldServer.WorldGroup, new List()).Add(worldServer); } } }