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) new CharacterData
.Select(c => {
new CharacterData Name = toBeAddedCharacter.Name,
{ Job = toBeAddedCharacter.JobData,
Name = c.Name, Gender = toBeAddedCharacter.Gender,
Job = c.JobData, Level = toBeAddedCharacter.Level,
Gender = c.Gender, Experience = toBeAddedCharacter.Experience,
Level = c.Level, Stats = toBeAddedCharacter.BaseStats,
Experience = c.Experience, Health = toBeAddedCharacter.Health,
Stats = c.BaseStats, Mana = toBeAddedCharacter.Mana,
Health = c.Health, EquippedItems =
Mana = c.Mana, GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems
EquippedItems = .Where(item => item.InventoryTab == InventoryTab.WornEquipment)
GetItemIDsByInventoryTab(c.InventoryItems .Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()),
.Where(item => item.InventoryTab == InventoryTab.WornEquipment) EquippedCashItems = GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()), .Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
EquippedCashItems = GetItemIDsByInventoryTab(c.InventoryItems .Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable())
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment) };
.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,