diff --git a/Server/PacketHandlers/ChannelSelectionHandler.cs b/Server/PacketHandlers/ChannelSelectionHandler.cs index 6ad66cb..31ccfb7 100644 --- a/Server/PacketHandlers/ChannelSelectionHandler.cs +++ b/Server/PacketHandlers/ChannelSelectionHandler.cs @@ -27,27 +27,27 @@ public class ChannelSelectionHandler : IPacketHandler ChannelSelectionResponsePacket responsePacket; var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty() }; - var account = await _wonderkingContext.Accounts - .FirstOrDefaultAsync(a => a.Id == authSession.AccountId); + var accountExists = await _accountExists(_wonderkingContext, authSession.AccountId); - var amountOfCharacter = await _wonderkingContext.Characters - .Where(c => c.Account.Id == authSession.AccountId) - .CountAsync(); + var amountOfCharacter = await _getAmountOfCharacters(_wonderkingContext, authSession.AccountId); - if (account != null && amountOfCharacter > 0) + if (!accountExists) + { + return; + } + + if (amountOfCharacter > 0) { responsePacket = new ChannelSelectionResponsePacket { ChannelIsFullFlag = 0, Endpoint = "127.0.0.1", Port = 2000, - Characters = await GetCharacterDataAsync(account.Id).ToArrayAsync() + Characters = await GetCharacterDataAsync(authSession.AccountId).ToArrayAsync() }; - guildNameResponsePacket.GuildNames = await _wonderkingContext.Characters - .Where(c => c.Account.Id == authSession.AccountId) - .Where(c => c.Guild != null) - .Select(character => character.Guild.Name).ToArrayAsync(); + guildNameResponsePacket.GuildNames = + await _getGuildNames(_wonderkingContext, authSession.AccountId).ToArrayAsync(); } else { @@ -110,6 +110,22 @@ public class ChannelSelectionHandler : IPacketHandler } } + private static readonly Func> _accountExists = EF.CompileAsyncQuery( + (WonderkingContext context, Guid accountId) => + context.Accounts.AsNoTracking().Any(a => a.Id == accountId)); + + private static readonly Func> _getAmountOfCharacters = + EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => + context.Characters.AsNoTracking() + .Where(c => c.Account.Id == accountId).Take(3) + .Count()); + + private static readonly Func> _getGuildNames = + EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => + context.Characters.AsNoTracking() + .Where(c => c.Account.Id == accountId && c.Guild != null) + .Select(c => c.Guild.Name).Take(3)); + private static readonly Func> _getCharacters = EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => context.Characters.AsNoTracking().AsSplitQuery()