From ec155ecab16b904ecc56afe4f4ded481e6de511f Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Sat, 25 Nov 2023 14:21:55 +0100 Subject: [PATCH] refactor: early return + itemids as span --- .../PacketHandlers/CharacterCreationHandler.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Server/PacketHandlers/CharacterCreationHandler.cs b/Server/PacketHandlers/CharacterCreationHandler.cs index fd8bc94..76c23bc 100644 --- a/Server/PacketHandlers/CharacterCreationHandler.cs +++ b/Server/PacketHandlers/CharacterCreationHandler.cs @@ -29,19 +29,23 @@ public class CharacterCreationHandler : IPacketHandler public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session) { - var authSession = session as AuthSession; - if (authSession is null) + if (session is not AuthSession authSession) { return; } var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == authSession.AccountId); + if (account is null) + { + return; + } + var firstJobConfig = SelectFirstJobConfig(packet.FirstJob); var items = CreateDefaultItems(packet, firstJobConfig); var toBeAddedCharacter = CreateDefaultCharacter(packet, account, items, firstJobConfig); - account?.Characters.Add(toBeAddedCharacter); + account.Characters.Add(toBeAddedCharacter); await _wonderkingContext.SaveChangesAsync(); var character = @@ -92,7 +96,7 @@ public class CharacterCreationHandler : IPacketHandler private static Character CreateDefaultCharacter(CharacterCreationPacket packet, Account account, InventoryItem[] items, JobSpecificMapping firstJobConfig) { - return new Character + return new Character() { Account = account, MapId = 300, @@ -154,8 +158,8 @@ public class CharacterCreationHandler : IPacketHandler private static ushort[] GetItemIDsByInventoryTab(IEnumerable> items) { - var ids = new ushort[20]; - ids.AsSpan().Clear(); + Span ids = stackalloc ushort[20]; + ids.Clear(); foreach (var item in items) { @@ -167,6 +171,6 @@ public class CharacterCreationHandler : IPacketHandler ids[item.Item2] = item.Item1; } - return ids; + return ids.ToArray(); } }