From ecd86d890cfaf941edf3ebe7e1285f525390795c Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Fri, 24 Nov 2023 20:04:42 +0100 Subject: [PATCH] feat: compiled queries --- ...haracterCreationHandler.CompiledQueries.cs | 14 ++++++ .../CharacterCreationHandler.cs | 48 +++++++++---------- 2 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 Server/PacketHandlers/CharacterCreationHandler.CompiledQueries.cs diff --git a/Server/PacketHandlers/CharacterCreationHandler.CompiledQueries.cs b/Server/PacketHandlers/CharacterCreationHandler.CompiledQueries.cs new file mode 100644 index 0000000..70a99c2 --- /dev/null +++ b/Server/PacketHandlers/CharacterCreationHandler.CompiledQueries.cs @@ -0,0 +1,14 @@ +// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. + +using Microsoft.EntityFrameworkCore; +using Server.DB; +using Server.DB.Documents; + +namespace Server.PacketHandlers; + +public partial class CharacterCreationHandler +{ + private static readonly Func> _getAccount = + EF.CompileQuery((WonderkingContext context, Guid accountId) => + context.Accounts.FirstOrDefaultAsync(a => a.Id == accountId, default)); +} diff --git a/Server/PacketHandlers/CharacterCreationHandler.cs b/Server/PacketHandlers/CharacterCreationHandler.cs index 7a64c02..2d0bf8a 100644 --- a/Server/PacketHandlers/CharacterCreationHandler.cs +++ b/Server/PacketHandlers/CharacterCreationHandler.cs @@ -1,6 +1,5 @@ // Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. -using Microsoft.EntityFrameworkCore; using NetCoreServer; using Server.DB; using Server.DB.Documents; @@ -13,7 +12,7 @@ using Wonderking.Packets.Outgoing.Data; namespace Server.PacketHandlers; -public class CharacterCreationHandler : IPacketHandler +public partial class CharacterCreationHandler : IPacketHandler { private readonly CharacterStatsMappingConfiguration _characterStatsMapping; private readonly ItemObjectPoolService _itemObjectPoolService; @@ -35,8 +34,8 @@ public class CharacterCreationHandler : IPacketHandler return; } - var account = - _wonderkingContext.Accounts.FirstOrDefault(a => a.Id == authSession.AccountId); + var account = await + _getAccount(_wonderkingContext, authSession.AccountId); var firstJobConfig = SelectFirstJobConfig(packet.FirstJob); var items = CreateDefaultItems(packet, firstJobConfig); @@ -45,27 +44,26 @@ public class CharacterCreationHandler : IPacketHandler account?.Characters.Add(toBeAddedCharacter); await _wonderkingContext.SaveChangesAsync(); - var character = await _wonderkingContext.Characters.AsNoTrackingWithIdentityResolution().AsSplitQuery() - .Where(c => c.Account.Id == authSession.AccountId && c.Name == packet.Name) - .Select(c => - new CharacterData - { - Name = c.Name, - Job = c.JobData, - Gender = c.Gender, - Level = c.Level, - Experience = c.Experience, - Stats = c.BaseStats, - Health = c.Health, - Mana = c.Mana, - EquippedItems = - GetItemIDsByInventoryTab(c.InventoryItems - .Where(item => item.InventoryTab == InventoryTab.WornEquipment) - .Select(item => new Tuple(item.ItemId, item.Slot)).AsEnumerable()), - EquippedCashItems = GetItemIDsByInventoryTab(c.InventoryItems - .Where(item => item.InventoryTab == InventoryTab.WornCashEquipment) - .Select(item => new Tuple(item.ItemId, item.Slot)).AsEnumerable()) - }).FirstAsync(); + var character = + new CharacterData + { + Name = toBeAddedCharacter.Name, + Job = toBeAddedCharacter.JobData, + Gender = toBeAddedCharacter.Gender, + Level = toBeAddedCharacter.Level, + Experience = toBeAddedCharacter.Experience, + Stats = toBeAddedCharacter.BaseStats, + Health = toBeAddedCharacter.Health, + Mana = toBeAddedCharacter.Mana, + EquippedItems = + GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems + .Where(item => item.InventoryTab == InventoryTab.WornEquipment) + .Select(item => new Tuple(item.ItemId, item.Slot)).AsEnumerable()), + EquippedCashItems = GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems + .Where(item => item.InventoryTab == InventoryTab.WornCashEquipment) + .Select(item => new Tuple(item.ItemId, item.Slot)).AsEnumerable()) + }; + await authSession.SendAsync(new CharacterCreationResponsePacket { Character = character,