From b3de52c6d84ae9945736653125d0b9d52a51b71d Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Wed, 22 Nov 2023 14:51:46 +0100 Subject: [PATCH] refactor: CompiledQueries to partial class --- ...ChannelSelectionHandler.CompiledQueries.cs | 58 +++++++++++++++++++ .../PacketHandlers/ChannelSelectionHandler.cs | 48 +-------------- 2 files changed, 59 insertions(+), 47 deletions(-) create mode 100644 Server/PacketHandlers/ChannelSelectionHandler.CompiledQueries.cs diff --git a/Server/PacketHandlers/ChannelSelectionHandler.CompiledQueries.cs b/Server/PacketHandlers/ChannelSelectionHandler.CompiledQueries.cs new file mode 100644 index 0000000..955f6a3 --- /dev/null +++ b/Server/PacketHandlers/ChannelSelectionHandler.CompiledQueries.cs @@ -0,0 +1,58 @@ +// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. + +using Microsoft.EntityFrameworkCore; +using Server.DB; +using Server.DB.Documents; +using Wonderking.Game.Data.Character; +using Wonderking.Packets.Outgoing.Data; + +namespace Server.PacketHandlers; + +public partial class ChannelSelectionHandler +{ + private static readonly Func> _accountExists = EF.CompileAsyncQuery( + (WonderkingContext context, Guid accountId) => + context.Accounts.AsNoTracking().Any(a => a.Id == accountId)); + + private static readonly Func> _getAmountOfCharacters = + EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => + context.Characters.AsNoTracking() + .Where(c => c.Account.Id == accountId).Take(3) + .Count()); + + private static readonly Func> _getGuildNames = + EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => + context.Characters.AsNoTracking() + .Where(c => c.Account.Id == accountId && c.Guild != null) + .Select(c => c.Guild.Name).Take(3)); + + private static readonly Func> + _getCharacters = + EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => + context.Characters.AsNoTracking().AsSplitQuery() + .Where(c => c.Account.Id == accountId) + .Select(c => new CharacterDataProjection( + c.Name, + c.JobData, + c.Gender, + c.Level, + c.Experience, + c.BaseStats, + c.Health, + c.Mana, + c.InventoryItems.Select(i => new InventoryItemProjection(i.ItemId, i.Slot, i.InventoryTab)) + )).Take(3)); + + private sealed record InventoryItemProjection(ushort ItemId, byte Slot, InventoryTab InventoryTab); + + private sealed record CharacterDataProjection( + string Name, + JobData JobData, + Gender Gender, + ushort Level, + long Experience, + BaseStats BaseStats, + int Health, + int Mana, + IEnumerable InventoryItems); +} diff --git a/Server/PacketHandlers/ChannelSelectionHandler.cs b/Server/PacketHandlers/ChannelSelectionHandler.cs index 31ccfb7..056f27c 100644 --- a/Server/PacketHandlers/ChannelSelectionHandler.cs +++ b/Server/PacketHandlers/ChannelSelectionHandler.cs @@ -1,18 +1,16 @@ // Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. using DotNext.Collections.Generic; -using Microsoft.EntityFrameworkCore; using NetCoreServer; using Server.DB; using Server.DB.Documents; -using Wonderking.Game.Data.Character; using Wonderking.Packets.Incoming; using Wonderking.Packets.Outgoing; using Wonderking.Packets.Outgoing.Data; namespace Server.PacketHandlers; -public class ChannelSelectionHandler : IPacketHandler +public partial class ChannelSelectionHandler : IPacketHandler { private readonly WonderkingContext _wonderkingContext; @@ -110,48 +108,4 @@ public class ChannelSelectionHandler : IPacketHandler } } - private static readonly Func> _accountExists = EF.CompileAsyncQuery( - (WonderkingContext context, Guid accountId) => - context.Accounts.AsNoTracking().Any(a => a.Id == accountId)); - - private static readonly Func> _getAmountOfCharacters = - EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => - context.Characters.AsNoTracking() - .Where(c => c.Account.Id == accountId).Take(3) - .Count()); - - private static readonly Func> _getGuildNames = - EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => - context.Characters.AsNoTracking() - .Where(c => c.Account.Id == accountId && c.Guild != null) - .Select(c => c.Guild.Name).Take(3)); - - private static readonly Func> _getCharacters = - EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) => - context.Characters.AsNoTracking().AsSplitQuery() - .Where(c => c.Account.Id == accountId) - .Select(c => new CharacterDataProjection( - c.Name, - c.JobData, - c.Gender, - c.Level, - c.Experience, - c.BaseStats, - c.Health, - c.Mana, - c.InventoryItems.Select(i => new InventoryItemProjection(i.ItemId, i.Slot, i.InventoryTab)) - )).Take(3)); - - private sealed record InventoryItemProjection(ushort ItemId, byte Slot, InventoryTab InventoryTab); - - private sealed record CharacterDataProjection( - string Name, - JobData JobData, - Gender Gender, - ushort Level, - long Experience, - BaseStats BaseStats, - int Health, - int Mana, - IEnumerable InventoryItems); }