perf: various optimizations

This commit is contained in:
Timothy Schenk 2023-11-22 07:35:25 +01:00
parent d15b1f8045
commit ac976321f5

View file

@ -27,27 +27,27 @@ public class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
ChannelSelectionResponsePacket responsePacket; ChannelSelectionResponsePacket responsePacket;
var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty<string>() }; var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty<string>() };
var account = await _wonderkingContext.Accounts var accountExists = await _accountExists(_wonderkingContext, authSession.AccountId);
.FirstOrDefaultAsync(a => a.Id == authSession.AccountId);
var amountOfCharacter = await _wonderkingContext.Characters var amountOfCharacter = await _getAmountOfCharacters(_wonderkingContext, authSession.AccountId);
.Where(c => c.Account.Id == authSession.AccountId)
.CountAsync();
if (account != null && amountOfCharacter > 0) if (!accountExists)
{
return;
}
if (amountOfCharacter > 0)
{ {
responsePacket = new ChannelSelectionResponsePacket responsePacket = new ChannelSelectionResponsePacket
{ {
ChannelIsFullFlag = 0, ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1", Endpoint = "127.0.0.1",
Port = 2000, Port = 2000,
Characters = await GetCharacterDataAsync(account.Id).ToArrayAsync() Characters = await GetCharacterDataAsync(authSession.AccountId).ToArrayAsync()
}; };
guildNameResponsePacket.GuildNames = await _wonderkingContext.Characters guildNameResponsePacket.GuildNames =
.Where(c => c.Account.Id == authSession.AccountId) await _getGuildNames(_wonderkingContext, authSession.AccountId).ToArrayAsync();
.Where(c => c.Guild != null)
.Select(character => character.Guild.Name).ToArrayAsync();
} }
else else
{ {
@ -110,6 +110,22 @@ public class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
} }
} }
private static readonly Func<WonderkingContext, Guid, Task<bool>> _accountExists = EF.CompileAsyncQuery(
(WonderkingContext context, Guid accountId) =>
context.Accounts.AsNoTracking().Any(a => a.Id == accountId));
private static readonly Func<WonderkingContext, Guid, Task<int>> _getAmountOfCharacters =
EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) =>
context.Characters.AsNoTracking()
.Where(c => c.Account.Id == accountId).Take(3)
.Count());
private static readonly Func<WonderkingContext, Guid, IAsyncEnumerable<string>> _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<WonderkingContext, Guid, IAsyncEnumerable<CharacterDataProjection>> _getCharacters = private static readonly Func<WonderkingContext, Guid, IAsyncEnumerable<CharacterDataProjection>> _getCharacters =
EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) =>
context.Characters.AsNoTracking().AsSplitQuery() context.Characters.AsNoTracking().AsSplitQuery()