refactor: CompiledQueries to partial class

This commit is contained in:
Timothy Schenk 2023-11-22 14:51:46 +01:00
parent 9bd75cd6c4
commit b3de52c6d8
2 changed files with 59 additions and 47 deletions

View file

@ -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<WonderkingContext, Guid, Task<bool>> _accountExists = EF.CompileAsyncQuery(
(WonderkingContext context, Guid accountId) =>
context.Accounts.AsNoTracking().Any(a => a.Id == accountId));
private static readonly Func<WonderkingContext, Guid, Task<int>> _getAmountOfCharacters =
EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) =>
context.Characters.AsNoTracking()
.Where(c => c.Account.Id == accountId).Take(3)
.Count());
private static readonly Func<WonderkingContext, Guid, IAsyncEnumerable<string>> _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<WonderkingContext, Guid, IAsyncEnumerable<CharacterDataProjection>>
_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<InventoryItemProjection> InventoryItems);
}

View file

@ -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<ChannelSelectionPacket>
public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
{
private readonly WonderkingContext _wonderkingContext;
@ -110,48 +108,4 @@ public class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
}
}
private static readonly Func<WonderkingContext, Guid, Task<bool>> _accountExists = EF.CompileAsyncQuery(
(WonderkingContext context, Guid accountId) =>
context.Accounts.AsNoTracking().Any(a => a.Id == accountId));
private static readonly Func<WonderkingContext, Guid, Task<int>> _getAmountOfCharacters =
EF.CompileAsyncQuery((WonderkingContext context, Guid accountId) =>
context.Characters.AsNoTracking()
.Where(c => c.Account.Id == accountId).Take(3)
.Count());
private static readonly Func<WonderkingContext, Guid, IAsyncEnumerable<string>> _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<WonderkingContext, Guid, IAsyncEnumerable<CharacterDataProjection>> _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<InventoryItemProjection> InventoryItems);
}