namespace SOR.Collection.Handlers;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
///
/// collection.get_owned — returns the full owned collection as { definitionId → ownedCount }.
///
/// No pagination: the map is small (one entry per unique card definition owned)
/// and returning it all at once lets the client filter and sort without round-trips.
///
internal sealed class CollectionGetOwnedHandler : CollectionHandlerBase, IMessageHandler
{
private readonly CollectionDbContext _collectionDb;
public CollectionGetOwnedHandler(CollectionDbContext collectionDb)
=> _collectionDb = collectionDb;
public string MessageType => "collection.get_owned";
public async Task HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken cancellationToken)
{
var accountId = Guid.Parse(connection.UserId);
var owned = await _collectionDb.PlayerCards
.Where(pc => pc.AccountId == accountId)
.GroupBy(pc => pc.CardDefinitionId)
.Select(g => new { DefId = g.Key, Count = g.Count() })
.ToDictionaryAsync(x => x.DefId, x => x.Count, cancellationToken);
return ServerMessage.Reply(
"collection.get_owned.ack",
message.Seq,
new GetOwnedResponse(owned));
}
}