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;
var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty<string>() };
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<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 =
EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) =>
context.Characters.AsNoTracking().AsSplitQuery()