Add collection service — deck management + card inventory

SOR.Collection project (new):
- 8 message handlers over WebSocket: get_cards, get_decks, get_deck,
  create_deck, rename_deck, delete_deck, add_card, remove_card
- Deck construction rules (DeckRules.cs): min 20 / max 40 cards,
  max 4 copies per definition, max 20 decks per player
- add_card validates ownership (player must own enough physical copies),
  copy limit, deck size limit, and rejects ArchSpirits from the card list
- RefreshValidityAsync recalculates Deck.IsValid after every mutation
- Cross-context lookups (CollectionDbContext + CatalogDbContext) with
  batch queries — never N+1

Admin portal pages (new):
- /collection — overview stats (total cards, total decks, valid decks,
  unique players) + account ID lookup
- /collection/player/{accountId} — full player collection viewer:
  paginated card grid with name/type/affinity/rarity/foil/serial filters,
  deck list with expanded card tables, Grant Card modal (search + foil
  toggle, grants 1–100 copies), revoke individual card instances

Wiring:
- SOR.slnx + SOR.Host.csproj: added SOR.Collection project reference
- Program.cs: uncommented collection service registration
- NavMenu: added Collection item

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-02-25 12:28:17 -05:00
co-authored by Claude Happy
parent 925b2b1b3b
commit 0198aec9e5
19 changed files with 1594 additions and 3 deletions
@@ -0,0 +1,87 @@
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;
/// <summary>
/// collection.remove_card — removes copies of a card from a deck.
///
/// If Quantity is null or ≥ current quantity, the card is removed entirely.
/// If Quantity is a positive int, that many copies are decremented.
/// </summary>
internal sealed class CollectionRemoveCardHandler : CollectionHandlerBase, IMessageHandler
{
private readonly IMessageSerializer _serializer;
private readonly CollectionDbContext _collectionDb;
private readonly CatalogDbContext _catalogDb;
public CollectionRemoveCardHandler(
IMessageSerializer serializer,
CollectionDbContext collectionDb,
CatalogDbContext catalogDb)
{
_serializer = serializer;
_collectionDb = collectionDb;
_catalogDb = catalogDb;
}
public string MessageType => "collection.remove_card";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken cancellationToken)
{
var req = _serializer.DeserializePayload<RemoveCardRequest>(message.Payload);
var accountId = Guid.Parse(connection.UserId);
var deck = await _collectionDb.Decks
.Include(d => d.Cards)
.FirstOrDefaultAsync(
d => d.Id == req.DeckId && d.AccountId == accountId,
cancellationToken);
if (deck == null)
return ServerMessage.Error(message.Seq, "DECK_NOT_FOUND", "Deck not found.");
var entry = deck.Cards.FirstOrDefault(c => c.CardDefinitionId == req.CardDefinitionId);
if (entry == null)
return ServerMessage.Error(message.Seq, "CARD_NOT_IN_DECK",
"That card is not in this deck.");
int toRemove = req.Quantity is null or <= 0 ? entry.Quantity : req.Quantity.Value;
if (toRemove >= entry.Quantity)
{
// Remove the row entirely.
_collectionDb.DeckCards.Remove(entry);
deck.Cards.Remove(entry);
}
else
{
entry.Quantity -= toRemove;
}
await RefreshValidityAsync(_collectionDb, deck, cancellationToken);
var archName = await _catalogDb.CardDefinitions
.Where(cd => cd.Id == deck.ArchSpiritCardDefinitionId)
.Select(cd => cd.Name)
.FirstOrDefaultAsync(cancellationToken);
var summary = new DeckSummaryDto(
Id: deck.Id,
Name: deck.Name,
ArchSpiritCardDefinitionId: deck.ArchSpiritCardDefinitionId,
ArchSpiritName: archName,
CardCount: deck.Cards.Sum(c => c.Quantity),
IsValid: deck.IsValid,
CreatedAt: deck.CreatedAt,
UpdatedAt: deck.UpdatedAt);
return ServerMessage.Reply("collection.remove_card.ack", message.Seq,
new DeckMutationResponse(summary));
}
}