From 739f7e08bf7aa96094c45ef367c41d6fecae14d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=82ngelo=20Tadeucci?= Date: Thu, 27 Mar 2025 16:39:06 -0300 Subject: [PATCH] Simplify channel lookup logic --- .../Containers/ChannelClientLookup.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Maple2.Server.World/Containers/ChannelClientLookup.cs b/Maple2.Server.World/Containers/ChannelClientLookup.cs index 7c6e47a17..019e16ee9 100644 --- a/Maple2.Server.World/Containers/ChannelClientLookup.cs +++ b/Maple2.Server.World/Containers/ChannelClientLookup.cs @@ -62,14 +62,14 @@ public IEnumerable Keys { } public (ushort gamePort, int grpcPort, int channel) FindOrCreateChannelByIp(string gameIp, string grpcGameIp, bool instancedContent) { - for (int i = 0; i < channels.Count; i++) { - channels.TryGetValue(i, out Channel? activeChannel); - if (activeChannel is null) { - continue; - } - if (activeChannel.Endpoint.Address.ToString() == gameIp && activeChannel.Status is ChannelStatus.Inactive && activeChannel.InstancedContent == instancedContent) { - return (activeChannel.GamePort, activeChannel.GrpcPort, activeChannel.Id); - } + // find the first channel that matches the ip, status and instanced content + Channel? activeChannel = channels.Values.FirstOrDefault(channel => + channel.Endpoint.Address.ToString() == gameIp && + channel.Status is ChannelStatus.Inactive && + channel.InstancedContent == instancedContent); + + if (activeChannel is not null) { + return (activeChannel.GamePort, activeChannel.GrpcPort, activeChannel.Id); } return AddChannel(gameIp, grpcGameIp, instancedContent);