From 5abe7f75647cba4072e9ad9e1da6d10391a41f03 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Thu, 16 Nov 2023 10:30:01 +0100 Subject: [PATCH] refactor: make HandleAsync async --- .../PacketHandlers/ChannelSelectionHandler.cs | 18 ++++++++---------- .../CharacterCreationHandler..cs | 9 ++++----- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Server/PacketHandlers/ChannelSelectionHandler.cs b/Server/PacketHandlers/ChannelSelectionHandler.cs index 96f3129..3f64d74 100644 --- a/Server/PacketHandlers/ChannelSelectionHandler.cs +++ b/Server/PacketHandlers/ChannelSelectionHandler.cs @@ -29,22 +29,22 @@ public class ChannelSelectionHandler : IPacketHandler { } - public Task HandleAsync(ChannelSelectionPacket packet, TcpSession session) + public async Task HandleAsync(ChannelSelectionPacket packet, TcpSession session) { var authSession = (AuthSession)session; ChannelSelectionResponsePacket responsePacket; var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty() }; - var hasCharacters = this._wonderkingContext.Accounts.Include(account => account.Characters) - .FirstOrDefault(a => a.Id == authSession.AccountId)?.Characters.Count > 0; - if (hasCharacters) + var account = await this._wonderkingContext.Accounts.Include(account => account.Characters) + .FirstOrDefaultAsync(a => a.Id == authSession.AccountId).ConfigureAwait(true); + if (account != null && account.Characters.Count > 0) { responsePacket = new ChannelSelectionResponsePacket { ChannelIsFullFlag = 0, Endpoint = "127.0.0.1", Port = 12345, - Characters = this._wonderkingContext.Characters.AsNoTracking() + Characters = await _wonderkingContext.Characters.AsNoTracking() .Where(c => c.Account.Id == authSession.AccountId) .Select(c => new CharacterData @@ -67,12 +67,12 @@ public class ChannelSelectionHandler : IPacketHandler .Select(item => item.ItemId) .ToArray() }) - .ToArray(), + .ToArrayAsync().ConfigureAwait(true), }; - guildNameResponsePacket.GuildNames = this._wonderkingContext.Characters + guildNameResponsePacket.GuildNames = await _wonderkingContext.Characters .Where(c => c.Account.Id == authSession.AccountId) - .Select(character => character.Guild.Name).ToArray(); + .Select(character => character.Guild.Name).ToArrayAsync().ConfigureAwait(true); } else { @@ -87,7 +87,5 @@ public class ChannelSelectionHandler : IPacketHandler authSession.Send(responsePacket); authSession.Send(guildNameResponsePacket); - - return Task.CompletedTask; } } diff --git a/Server/PacketHandlers/CharacterCreationHandler..cs b/Server/PacketHandlers/CharacterCreationHandler..cs index 5ee5af6..52bb1b3 100644 --- a/Server/PacketHandlers/CharacterCreationHandler..cs +++ b/Server/PacketHandlers/CharacterCreationHandler..cs @@ -21,7 +21,7 @@ public class CharacterCreationHandler : IPacketHandler _itemObjectPoolService = itemObjectPoolService; } - public Task HandleAsync(CharacterCreationPacket packet, TcpSession session) + public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session) { var authSession = session as AuthSession; var account = @@ -59,9 +59,9 @@ public class CharacterCreationHandler : IPacketHandler Health = 250, Mana = 250, }); - _wonderkingContext.SaveChanges(); + await _wonderkingContext.SaveChangesAsync().ConfigureAwait(false); - var character = this._wonderkingContext.Characters.AsNoTracking() + var character = await _wonderkingContext.Characters.AsNoTracking() .Where(c => authSession != null && c.Account.Id == authSession.AccountId && c.Name == packet.Name) .Select(c => new CharacterData @@ -82,8 +82,7 @@ public class CharacterCreationHandler : IPacketHandler .Where(item => item.InventoryTab == InventoryTab.WornCashEquipment) .Select(item => item.ItemId) .ToArray(), - }).FirstOrDefault(); + }).FirstOrDefaultAsync().ConfigureAwait(true); authSession?.Send(new CharacterCreationResponsePacket { Character = character }); - return Task.CompletedTask; } }