feat: compiled queries

This commit is contained in:
Timothy Schenk 2023-11-24 20:04:42 +01:00
parent aa09d0b755
commit ecd86d890c
2 changed files with 37 additions and 25 deletions

View file

@ -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<WonderkingContext, Guid, Task<Account>> _getAccount =
EF.CompileQuery((WonderkingContext context, Guid accountId) =>
context.Accounts.FirstOrDefaultAsync(a => a.Id == accountId, default));
}

View file

@ -1,6 +1,5 @@
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. // Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore;
using NetCoreServer; using NetCoreServer;
using Server.DB; using Server.DB;
using Server.DB.Documents; using Server.DB.Documents;
@ -13,7 +12,7 @@ using Wonderking.Packets.Outgoing.Data;
namespace Server.PacketHandlers; namespace Server.PacketHandlers;
public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket> public partial class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
{ {
private readonly CharacterStatsMappingConfiguration _characterStatsMapping; private readonly CharacterStatsMappingConfiguration _characterStatsMapping;
private readonly ItemObjectPoolService _itemObjectPoolService; private readonly ItemObjectPoolService _itemObjectPoolService;
@ -35,8 +34,8 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
return; return;
} }
var account = var account = await
_wonderkingContext.Accounts.FirstOrDefault(a => a.Id == authSession.AccountId); _getAccount(_wonderkingContext, authSession.AccountId);
var firstJobConfig = SelectFirstJobConfig(packet.FirstJob); var firstJobConfig = SelectFirstJobConfig(packet.FirstJob);
var items = CreateDefaultItems(packet, firstJobConfig); var items = CreateDefaultItems(packet, firstJobConfig);
@ -45,27 +44,26 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
account?.Characters.Add(toBeAddedCharacter); account?.Characters.Add(toBeAddedCharacter);
await _wonderkingContext.SaveChangesAsync(); await _wonderkingContext.SaveChangesAsync();
var character = await _wonderkingContext.Characters.AsNoTrackingWithIdentityResolution().AsSplitQuery() var character =
.Where(c => c.Account.Id == authSession.AccountId && c.Name == packet.Name)
.Select(c =>
new CharacterData new CharacterData
{ {
Name = c.Name, Name = toBeAddedCharacter.Name,
Job = c.JobData, Job = toBeAddedCharacter.JobData,
Gender = c.Gender, Gender = toBeAddedCharacter.Gender,
Level = c.Level, Level = toBeAddedCharacter.Level,
Experience = c.Experience, Experience = toBeAddedCharacter.Experience,
Stats = c.BaseStats, Stats = toBeAddedCharacter.BaseStats,
Health = c.Health, Health = toBeAddedCharacter.Health,
Mana = c.Mana, Mana = toBeAddedCharacter.Mana,
EquippedItems = EquippedItems =
GetItemIDsByInventoryTab(c.InventoryItems GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems
.Where(item => item.InventoryTab == InventoryTab.WornEquipment) .Where(item => item.InventoryTab == InventoryTab.WornEquipment)
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()), .Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()),
EquippedCashItems = GetItemIDsByInventoryTab(c.InventoryItems EquippedCashItems = GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment) .Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()) .Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable())
}).FirstAsync(); };
await authSession.SendAsync(new CharacterCreationResponsePacket await authSession.SendAsync(new CharacterCreationResponsePacket
{ {
Character = character, Character = character,