Add SOR.Bazaar service — store, P2P marketplace, auction house, cosmetics
New project: SOR.Bazaar (12 message handlers)
Store: get_store, buy_booster_pack, buy_prebuilt_deck, buy_cosmetic, get_my_balance
Marketplace: get_listings, get_my_listings, list_card, cancel_listing,
buy_listing (shared-tx atomicity), place_bid (shared-tx + refund),
get_transactions
New DB entities:
BazaarDb: Cosmetic, PurchaseRecord, BoosterPackCardGrant, AuctionBid
CollectionDb: PlayerCosmetic
IdentityDb: Account.OriginPoints
BoosterPack gets CardSetId for set-scoped card pool draws.
BazaarEnums: PurchaseProductType, CosmeticType.
3 EF migrations applied to pg-forge-01:
AddOriginPoints, AddBazaarCosmetics, AddPlayerCosmetics
Admin pages: CosmeticsPage, CosmeticEditor, PurchaseRecordsPage (with
expandable booster pack card grant rows), NavMenu Bazaar submenu updates.
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:
@@ -0,0 +1,201 @@
|
||||
namespace SOR.Bazaar;
|
||||
|
||||
// ── Requests ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Returns all active store products (packs, decks, cosmetics).</summary>
|
||||
public sealed record GetStoreRequest;
|
||||
|
||||
/// <summary>Open a booster pack. Deducts OriginPoints and draws cards.</summary>
|
||||
public sealed record BuyBoosterPackRequest(Guid PackId);
|
||||
|
||||
/// <summary>Purchase a prebuilt deck. Deducts OriginPoints, grants cards, creates a deck.</summary>
|
||||
public sealed record BuyPrebuiltDeckRequest(Guid DeckId);
|
||||
|
||||
/// <summary>Purchase a cosmetic. Deducts OriginPoints, grants PlayerCosmetic.</summary>
|
||||
public sealed record BuyCosmeticRequest(Guid CosmeticId);
|
||||
|
||||
/// <summary>Browse active P2P listings with optional filters.</summary>
|
||||
public sealed record GetListingsRequest(
|
||||
int Page,
|
||||
int PageSize,
|
||||
Guid? CardDefinitionId,
|
||||
decimal? MinPrice,
|
||||
decimal? MaxPrice,
|
||||
string? Rarity,
|
||||
string? ListingType);
|
||||
|
||||
/// <summary>Returns the caller's own listings, optionally filtered by status.</summary>
|
||||
public sealed record GetMyListingsRequest(string? StatusFilter);
|
||||
|
||||
/// <summary>
|
||||
/// List a card for sale on the P2P marketplace.
|
||||
/// Card must be IsTradeable = true and not already in an active listing.
|
||||
/// </summary>
|
||||
public sealed record ListCardRequest(
|
||||
Guid PlayerCardId,
|
||||
decimal Price,
|
||||
string ListingType,
|
||||
bool IsMasked,
|
||||
DateTimeOffset? ExpiresAt);
|
||||
|
||||
/// <summary>Cancel an active listing owned by the caller.</summary>
|
||||
public sealed record CancelListingRequest(Guid ListingId);
|
||||
|
||||
/// <summary>Buy a Fixed-price listing. Transfers card ownership and OriginPoints atomically.</summary>
|
||||
public sealed record BuyListingRequest(Guid ListingId);
|
||||
|
||||
/// <summary>Place a bid on an Auction listing. Amount must exceed the current highest bid.</summary>
|
||||
public sealed record PlaceBidRequest(Guid ListingId, decimal Amount);
|
||||
|
||||
/// <summary>Returns the caller's buy and sell transaction history.</summary>
|
||||
public sealed record GetTransactionsRequest(int Page, int PageSize);
|
||||
|
||||
/// <summary>Returns the caller's current OriginPoints balance.</summary>
|
||||
public sealed record GetMyBalanceRequest;
|
||||
|
||||
// ── Responses ────────────────────────────────────────────────────────────────
|
||||
|
||||
public sealed record GetStoreResponse(
|
||||
IEnumerable<StorePackDto> Packs,
|
||||
IEnumerable<StoreDeckDto> Decks,
|
||||
IEnumerable<CosmeticDto> Cosmetics);
|
||||
|
||||
public sealed record BuyBoosterPackResponse(
|
||||
Guid PurchaseId,
|
||||
IEnumerable<OpenedCardDto> CardsOpened,
|
||||
int NewOriginPointBalance);
|
||||
|
||||
public sealed record BuyPrebuiltDeckResponse(
|
||||
Guid PurchaseId,
|
||||
Guid DeckId,
|
||||
string DeckName,
|
||||
int TotalCards,
|
||||
int NewOriginPointBalance);
|
||||
|
||||
public sealed record BuyCosmeticResponse(
|
||||
Guid PurchaseId,
|
||||
CosmeticDto Cosmetic,
|
||||
int NewOriginPointBalance);
|
||||
|
||||
public sealed record GetListingsResponse(
|
||||
IEnumerable<ListingDto> Listings,
|
||||
int TotalCount,
|
||||
int Page,
|
||||
int PageSize);
|
||||
|
||||
public sealed record GetMyListingsResponse(IEnumerable<ListingDto> Listings);
|
||||
|
||||
public sealed record ListCardResponse(ListingDto Listing);
|
||||
|
||||
public sealed record CancelListingResponse(Guid ListingId);
|
||||
|
||||
public sealed record BuyListingResponse(
|
||||
TransactionDto Transaction,
|
||||
int NewOriginPointBalance);
|
||||
|
||||
public sealed record PlaceBidResponse(
|
||||
BidDto Bid,
|
||||
bool WasPreviousBidRefunded,
|
||||
int NewOriginPointBalance);
|
||||
|
||||
public sealed record GetTransactionsResponse(
|
||||
IEnumerable<TransactionDto> Transactions,
|
||||
int TotalCount,
|
||||
int Page,
|
||||
int PageSize);
|
||||
|
||||
public sealed record GetMyBalanceResponse(int OriginPoints);
|
||||
|
||||
// ── DTOs ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
public sealed record StorePackDto(
|
||||
Guid Id,
|
||||
string Name,
|
||||
string? Description,
|
||||
string? ImageCdnUrl,
|
||||
int OriginPointPrice,
|
||||
decimal? UsdPrice,
|
||||
Guid? CardSetId,
|
||||
int TotalSlots,
|
||||
DateTimeOffset? AvailableFrom,
|
||||
DateTimeOffset? AvailableUntil);
|
||||
|
||||
public sealed record StoreDeckDto(
|
||||
Guid Id,
|
||||
string Name,
|
||||
string? Description,
|
||||
string? ImageCdnUrl,
|
||||
int OriginPointPrice,
|
||||
decimal? UsdPrice,
|
||||
int TotalCards,
|
||||
string? ArchSpiritName,
|
||||
bool IsStarterDeck,
|
||||
DateTimeOffset? AvailableFrom,
|
||||
DateTimeOffset? AvailableUntil);
|
||||
|
||||
public sealed record CosmeticDto(
|
||||
Guid Id,
|
||||
string Name,
|
||||
string? Description,
|
||||
string CosmeticType,
|
||||
Guid? CardDefinitionId,
|
||||
string? PreviewImageCdnUrl,
|
||||
string AssetCdnUrl,
|
||||
int OriginPointPrice,
|
||||
decimal? UsdPrice);
|
||||
|
||||
public sealed record OpenedCardDto(
|
||||
Guid PlayerCardId,
|
||||
Guid CardDefinitionId,
|
||||
string Name,
|
||||
string CardType,
|
||||
string Affinity,
|
||||
string Rarity,
|
||||
bool IsFoil,
|
||||
int? SerialNumber,
|
||||
string? ArtUrl);
|
||||
|
||||
public sealed record ListingDto(
|
||||
Guid Id,
|
||||
Guid SellerAccountId,
|
||||
Guid PlayerCardId,
|
||||
Guid? CardDefinitionId,
|
||||
string? CardName,
|
||||
string? CardType,
|
||||
string? Rarity,
|
||||
string ListingType,
|
||||
decimal Price,
|
||||
string Currency,
|
||||
DateTimeOffset ListedAt,
|
||||
DateTimeOffset? ExpiresAt,
|
||||
bool IsMasked,
|
||||
decimal? CurrentBid,
|
||||
int BidCount);
|
||||
|
||||
public sealed record TransactionDto(
|
||||
Guid Id,
|
||||
Guid ListingId,
|
||||
Guid BuyerAccountId,
|
||||
Guid SellerAccountId,
|
||||
Guid PlayerCardId,
|
||||
decimal Amount,
|
||||
string Currency,
|
||||
DateTimeOffset TransactedAt);
|
||||
|
||||
public sealed record BidDto(
|
||||
Guid Id,
|
||||
Guid ListingId,
|
||||
Guid BidderAccountId,
|
||||
decimal Amount,
|
||||
string Currency,
|
||||
DateTimeOffset PlacedAt,
|
||||
bool IsWinning);
|
||||
|
||||
public sealed record PlayerCosmeticDto(
|
||||
Guid Id,
|
||||
Guid CosmeticId,
|
||||
string CosmeticType,
|
||||
Guid? CardDefinitionId,
|
||||
string? PreviewImageCdnUrl,
|
||||
string AssetCdnUrl,
|
||||
DateTimeOffset AcquiredAt);
|
||||
@@ -0,0 +1,178 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Bazaar;
|
||||
using SOR.Database.Entities.Collection;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarBuyBoosterPackHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly CollectionDbContext _collectionDb;
|
||||
private readonly CatalogDbContext _catalogDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.buy_booster_pack";
|
||||
|
||||
public BazaarBuyBoosterPackHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb,
|
||||
CatalogDbContext catalogDb,
|
||||
SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_collectionDb = collectionDb;
|
||||
_catalogDb = catalogDb;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<BuyBoosterPackRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
// ── Load pack ──────────────────────────────────────────────────────────
|
||||
var pack = await _bazaarDb.BoosterPacks
|
||||
.FirstOrDefaultAsync(p => p.Id == req.PackId && p.IsActive
|
||||
&& (p.AvailableFrom == null || p.AvailableFrom <= now)
|
||||
&& (p.AvailableUntil == null || p.AvailableUntil >= now), ct);
|
||||
|
||||
if (pack is null)
|
||||
return ServerMessage.Error(message.Seq, "PACK_NOT_FOUND",
|
||||
"Booster pack not found or not currently available.");
|
||||
|
||||
if (string.IsNullOrEmpty(pack.SlotsJson))
|
||||
return ServerMessage.Error(message.Seq, "PACK_NOT_CONFIGURED",
|
||||
"This pack has no card slots configured.");
|
||||
|
||||
// ── Debit OriginPoints ────────────────────────────────────────────────
|
||||
var debited = await TryDebitOriginPointsAsync(_identityDb, accountId, pack.OriginPointPrice, ct);
|
||||
if (!debited)
|
||||
return ServerMessage.Error(message.Seq, "INSUFFICIENT_FUNDS",
|
||||
"You do not have enough Origin Points to purchase this pack.");
|
||||
|
||||
// ── Draw cards ────────────────────────────────────────────────────────
|
||||
List<SlotEntry> slots;
|
||||
try
|
||||
{
|
||||
slots = System.Text.Json.JsonSerializer.Deserialize<List<SlotEntry>>(pack.SlotsJson)
|
||||
?? new List<SlotEntry>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return ServerMessage.Error(message.Seq, "PACK_NOT_CONFIGURED",
|
||||
"This pack has an invalid slot configuration.");
|
||||
}
|
||||
|
||||
var grantedCards = new List<(PlayerCard card, string rarity, string name)>();
|
||||
|
||||
foreach (var slot in slots)
|
||||
{
|
||||
if (!Enum.TryParse<CardRarity>(slot.Rarity, true, out var rarity))
|
||||
continue;
|
||||
|
||||
// Build pool query: rarity match + published + optional set scope
|
||||
var poolQuery = _catalogDb.CardDefinitions
|
||||
.Where(cd => cd.Rarity == rarity && cd.IsPublished);
|
||||
|
||||
if (pack.CardSetId.HasValue)
|
||||
poolQuery = poolQuery.Where(cd => cd.SetId == pack.CardSetId.Value);
|
||||
|
||||
// Random draw: pull all eligible IDs, then randomly pick server-side.
|
||||
// For large pools this can be optimised with ORDER BY RANDOM() LIMIT n in raw SQL,
|
||||
// but this approach is simple and correct for typical pool sizes.
|
||||
var pool = await poolQuery
|
||||
.Select(cd => new { cd.Id, cd.Name, cd.Rarity })
|
||||
.ToListAsync(ct);
|
||||
|
||||
if (pool.Count == 0) continue;
|
||||
|
||||
for (int i = 0; i < slot.Count; i++)
|
||||
{
|
||||
var pick = pool[Random.Shared.Next(pool.Count)];
|
||||
|
||||
var playerCard = new PlayerCard
|
||||
{
|
||||
AccountId = accountId,
|
||||
CardDefinitionId = pick.Id,
|
||||
AcquiredAt = now,
|
||||
IsFoil = Random.Shared.NextDouble() < 0.05, // 5% foil chance
|
||||
IsTradeable = true,
|
||||
};
|
||||
_collectionDb.PlayerCards.Add(playerCard);
|
||||
grantedCards.Add((playerCard, pick.Rarity.ToString(), pick.Name));
|
||||
}
|
||||
}
|
||||
|
||||
await _collectionDb.SaveChangesAsync(ct);
|
||||
|
||||
// ── Create PurchaseRecord + BoosterPackCardGrants ─────────────────────
|
||||
var purchase = new PurchaseRecord
|
||||
{
|
||||
AccountId = accountId,
|
||||
ProductType = PurchaseProductType.BoosterPack,
|
||||
ProductId = pack.Id,
|
||||
OriginPointsSpent = pack.OriginPointPrice,
|
||||
QuantityGranted = grantedCards.Count,
|
||||
PurchasedAt = now,
|
||||
};
|
||||
_bazaarDb.PurchaseRecords.Add(purchase);
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
foreach (var (card, rarity, cardName) in grantedCards)
|
||||
{
|
||||
_bazaarDb.BoosterPackCardGrants.Add(new BoosterPackCardGrant
|
||||
{
|
||||
PurchaseRecordId = purchase.Id,
|
||||
PlayerCardId = card.Id,
|
||||
CardDefinitionId = card.CardDefinitionId,
|
||||
CardName = cardName,
|
||||
Rarity = rarity,
|
||||
IsFoil = card.IsFoil,
|
||||
});
|
||||
}
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
var newBalance = await GetOriginPointsAsync(_identityDb, accountId, ct);
|
||||
|
||||
// ── Build response ────────────────────────────────────────────────────
|
||||
var defIds = grantedCards.Select(g => g.card.CardDefinitionId).Distinct().ToList();
|
||||
var defs = await _catalogDb.CardDefinitions
|
||||
.Include(cd => cd.Assets)
|
||||
.Where(cd => defIds.Contains(cd.Id))
|
||||
.ToDictionaryAsync(cd => cd.Id, ct);
|
||||
|
||||
var opened = grantedCards.Select(g =>
|
||||
{
|
||||
defs.TryGetValue(g.card.CardDefinitionId, out var def);
|
||||
return new OpenedCardDto(
|
||||
g.card.Id,
|
||||
g.card.CardDefinitionId,
|
||||
def?.Name ?? string.Empty,
|
||||
def?.CardType.ToString() ?? string.Empty,
|
||||
def?.Affinity.ToString() ?? string.Empty,
|
||||
g.rarity,
|
||||
g.card.IsFoil,
|
||||
g.card.SerialNumber,
|
||||
GetArtUrl(def));
|
||||
}).ToList();
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.buy_booster_pack.ack",
|
||||
message.Seq,
|
||||
new BuyBoosterPackResponse(purchase.Id, opened, newBalance));
|
||||
}
|
||||
|
||||
private sealed record SlotEntry(string Rarity, int Count);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Bazaar;
|
||||
using SOR.Database.Entities.Collection;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarBuyCosmeticHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly CollectionDbContext _collectionDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.buy_cosmetic";
|
||||
|
||||
public BazaarBuyCosmeticHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb,
|
||||
SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_collectionDb = collectionDb;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<BuyCosmeticRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var cosmetic = await _bazaarDb.Cosmetics
|
||||
.FirstOrDefaultAsync(c => c.Id == req.CosmeticId && c.IsActive, ct);
|
||||
|
||||
if (cosmetic is null)
|
||||
return ServerMessage.Error(message.Seq, "COSMETIC_NOT_FOUND",
|
||||
"Cosmetic not found or not currently available.");
|
||||
|
||||
// Guard: already owned
|
||||
var alreadyOwned = await _collectionDb.PlayerCosmetics
|
||||
.AnyAsync(pc => pc.AccountId == accountId && pc.CosmeticId == req.CosmeticId, ct);
|
||||
|
||||
if (alreadyOwned)
|
||||
return ServerMessage.Error(message.Seq, "ALREADY_OWNED",
|
||||
"You already own this cosmetic.");
|
||||
|
||||
// ── Debit ─────────────────────────────────────────────────────────────
|
||||
var debited = await TryDebitOriginPointsAsync(_identityDb, accountId, cosmetic.OriginPointPrice, ct);
|
||||
if (!debited)
|
||||
return ServerMessage.Error(message.Seq, "INSUFFICIENT_FUNDS",
|
||||
"You do not have enough Origin Points to purchase this cosmetic.");
|
||||
|
||||
// ── Grant cosmetic ────────────────────────────────────────────────────
|
||||
var playerCosmetic = new PlayerCosmetic
|
||||
{
|
||||
AccountId = accountId,
|
||||
CosmeticId = cosmetic.Id,
|
||||
AcquiredAt = now,
|
||||
};
|
||||
_collectionDb.PlayerCosmetics.Add(playerCosmetic);
|
||||
await _collectionDb.SaveChangesAsync(ct);
|
||||
|
||||
// ── PurchaseRecord ────────────────────────────────────────────────────
|
||||
var purchase = new PurchaseRecord
|
||||
{
|
||||
AccountId = accountId,
|
||||
ProductType = PurchaseProductType.Cosmetic,
|
||||
ProductId = cosmetic.Id,
|
||||
OriginPointsSpent = cosmetic.OriginPointPrice,
|
||||
QuantityGranted = 1,
|
||||
PurchasedAt = now,
|
||||
};
|
||||
_bazaarDb.PurchaseRecords.Add(purchase);
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
var newBalance = await GetOriginPointsAsync(_identityDb, accountId, ct);
|
||||
|
||||
var cosmeticDto = new CosmeticDto(
|
||||
cosmetic.Id,
|
||||
cosmetic.Name,
|
||||
cosmetic.Description,
|
||||
cosmetic.CosmeticType.ToString(),
|
||||
cosmetic.CardDefinitionId,
|
||||
cosmetic.PreviewImageCdnUrl,
|
||||
cosmetic.AssetCdnUrl,
|
||||
cosmetic.OriginPointPrice,
|
||||
cosmetic.UsdPrice);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.buy_cosmetic.ack",
|
||||
message.Seq,
|
||||
new BuyCosmeticResponse(purchase.Id, cosmeticDto, newBalance));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Bazaar;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// Purchases a Fixed-price listing.
|
||||
///
|
||||
/// Atomicity: a single NpgsqlConnection transaction is shared across BazaarDbContext,
|
||||
/// CollectionDbContext, and SorIdentityDbContext so that the listing claim, card transfer,
|
||||
/// and OriginPoint debit/credit are all committed or rolled back together. This is safe
|
||||
/// on multiple application servers because step 2 uses a conditional SQL UPDATE that acts
|
||||
/// as a row-level optimistic lock — only one buyer can claim the listing.
|
||||
/// </summary>
|
||||
internal sealed class BazaarBuyListingHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly CollectionDbContext _collectionDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.buy_listing";
|
||||
|
||||
public BazaarBuyListingHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb,
|
||||
SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_collectionDb = collectionDb;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<BuyListingRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
await using var tx = await OpenSharedTransactionAsync(_bazaarDb, _collectionDb, _identityDb, ct);
|
||||
try
|
||||
{
|
||||
// ── Step 1: Atomic claim — flip Active → Sold (optimistic lock) ──
|
||||
var claimed = await _bazaarDb.Database.ExecuteSqlRawAsync(
|
||||
"""
|
||||
UPDATE bazaar.listings
|
||||
SET status = 'Sold'
|
||||
WHERE id = {0}
|
||||
AND status = 'Active'
|
||||
AND listing_type = 'Fixed'
|
||||
AND seller_account_id <> {1}
|
||||
""",
|
||||
req.ListingId, accountId);
|
||||
|
||||
if (claimed == 0)
|
||||
return ServerMessage.Error(message.Seq, "LISTING_ALREADY_SOLD",
|
||||
"This listing is no longer available.");
|
||||
|
||||
// ── Step 2: Load listing details ──────────────────────────────────
|
||||
var listing = await _bazaarDb.Listings
|
||||
.FirstAsync(l => l.Id == req.ListingId, ct);
|
||||
|
||||
var price = (int)listing.Price; // OriginPoints are integer
|
||||
var sellerAccId = listing.SellerAccountId;
|
||||
|
||||
// ── Step 3: Debit buyer ───────────────────────────────────────────
|
||||
var debited = await TryDebitOriginPointsAsync(_identityDb, accountId, price, ct);
|
||||
if (!debited)
|
||||
{
|
||||
await tx.RollbackAsync(ct);
|
||||
return ServerMessage.Error(message.Seq, "INSUFFICIENT_FUNDS",
|
||||
"You do not have enough Origin Points for this purchase.");
|
||||
}
|
||||
|
||||
// ── Step 4: Credit seller ─────────────────────────────────────────
|
||||
await CreditOriginPointsAsync(_identityDb, sellerAccId, price, ct);
|
||||
|
||||
// ── Step 5: Transfer card ownership ──────────────────────────────
|
||||
var card = await _collectionDb.PlayerCards
|
||||
.FirstAsync(pc => pc.Id == listing.PlayerCardId, ct);
|
||||
card.AccountId = accountId;
|
||||
await _collectionDb.SaveChangesAsync(ct);
|
||||
|
||||
// ── Step 6: Record transaction + purchase record ──────────────────
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var txnRecord = new BazaarTransaction
|
||||
{
|
||||
ListingId = listing.Id,
|
||||
BuyerAccountId = accountId,
|
||||
SellerAccountId = sellerAccId,
|
||||
PlayerCardId = listing.PlayerCardId,
|
||||
Amount = listing.Price,
|
||||
Currency = listing.Currency,
|
||||
TransactedAt = now,
|
||||
};
|
||||
_bazaarDb.Transactions.Add(txnRecord);
|
||||
|
||||
var purchase = new PurchaseRecord
|
||||
{
|
||||
AccountId = accountId,
|
||||
ProductType = PurchaseProductType.BoosterPack, // reuse enum? no — use a distinct concept
|
||||
ProductId = listing.Id,
|
||||
OriginPointsSpent = price,
|
||||
QuantityGranted = 1,
|
||||
PurchasedAt = now,
|
||||
};
|
||||
// Note: P2P buys are tracked via BazaarTransaction; PurchaseRecord is for store purchases.
|
||||
// We do NOT create a PurchaseRecord here — BazaarTransaction already serves as the audit trail.
|
||||
_bazaarDb.Transactions.Add(txnRecord);
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
// ── Step 7: Commit ────────────────────────────────────────────────
|
||||
await tx.CommitAsync(ct);
|
||||
|
||||
var newBalance = await GetOriginPointsAsync(_identityDb, accountId, ct);
|
||||
|
||||
var txnDto = new TransactionDto(
|
||||
txnRecord.Id,
|
||||
txnRecord.ListingId,
|
||||
txnRecord.BuyerAccountId,
|
||||
txnRecord.SellerAccountId,
|
||||
txnRecord.PlayerCardId,
|
||||
txnRecord.Amount,
|
||||
txnRecord.Currency.ToString(),
|
||||
txnRecord.TransactedAt);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.buy_listing.ack",
|
||||
message.Seq,
|
||||
new BuyListingResponse(txnDto, newBalance));
|
||||
}
|
||||
catch
|
||||
{
|
||||
await tx.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Bazaar;
|
||||
using SOR.Database.Entities.Collection;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarBuyPrebuiltDeckHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly CollectionDbContext _collectionDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.buy_prebuilt_deck";
|
||||
|
||||
public BazaarBuyPrebuiltDeckHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb,
|
||||
SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_collectionDb = collectionDb;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<BuyPrebuiltDeckRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var deck = await _bazaarDb.PrebuiltDecks
|
||||
.Include(d => d.Cards)
|
||||
.FirstOrDefaultAsync(d => d.Id == req.DeckId && d.IsActive
|
||||
&& (d.AvailableFrom == null || d.AvailableFrom <= now)
|
||||
&& (d.AvailableUntil == null || d.AvailableUntil >= now), ct);
|
||||
|
||||
if (deck is null)
|
||||
return ServerMessage.Error(message.Seq, "DECK_NOT_FOUND",
|
||||
"Prebuilt deck not found or not currently available.");
|
||||
|
||||
// Starter decks are handled by collection.select_starter_deck; not purchasable here.
|
||||
if (deck.IsStarterDeck)
|
||||
return ServerMessage.Error(message.Seq, "STARTER_DECK",
|
||||
"Starter decks must be selected during onboarding, not purchased.");
|
||||
|
||||
// ── Debit ─────────────────────────────────────────────────────────────
|
||||
var debited = await TryDebitOriginPointsAsync(_identityDb, accountId, deck.OriginPointPrice, ct);
|
||||
if (!debited)
|
||||
return ServerMessage.Error(message.Seq, "INSUFFICIENT_FUNDS",
|
||||
"You do not have enough Origin Points to purchase this deck.");
|
||||
|
||||
// ── Grant cards ───────────────────────────────────────────────────────
|
||||
var grantedCards = new List<PlayerCard>();
|
||||
foreach (var deckCard in deck.Cards)
|
||||
{
|
||||
for (int i = 0; i < deckCard.Quantity; i++)
|
||||
{
|
||||
var pc = new PlayerCard
|
||||
{
|
||||
AccountId = accountId,
|
||||
CardDefinitionId = deckCard.CardDefinitionId,
|
||||
AcquiredAt = now,
|
||||
IsTradeable = true, // purchased deck cards are tradeable
|
||||
};
|
||||
_collectionDb.PlayerCards.Add(pc);
|
||||
grantedCards.Add(pc);
|
||||
}
|
||||
}
|
||||
|
||||
// Grant ArchSpirit card if specified
|
||||
if (deck.ArchSpiritCardDefinitionId.HasValue)
|
||||
{
|
||||
var archCard = new PlayerCard
|
||||
{
|
||||
AccountId = accountId,
|
||||
CardDefinitionId = deck.ArchSpiritCardDefinitionId.Value,
|
||||
AcquiredAt = now,
|
||||
IsTradeable = true,
|
||||
};
|
||||
_collectionDb.PlayerCards.Add(archCard);
|
||||
grantedCards.Add(archCard);
|
||||
}
|
||||
await _collectionDb.SaveChangesAsync(ct);
|
||||
|
||||
// ── Create Deck ───────────────────────────────────────────────────────
|
||||
var newDeck = new Deck
|
||||
{
|
||||
AccountId = accountId,
|
||||
Name = deck.Name,
|
||||
Description = deck.Description,
|
||||
ArchSpiritCardDefinitionId = deck.ArchSpiritCardDefinitionId ?? Guid.Empty,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now,
|
||||
IsValid = false, // will be validated on next use
|
||||
};
|
||||
_collectionDb.Decks.Add(newDeck);
|
||||
await _collectionDb.SaveChangesAsync(ct);
|
||||
|
||||
foreach (var deckCard in deck.Cards)
|
||||
{
|
||||
_collectionDb.DeckCards.Add(new DeckCard
|
||||
{
|
||||
DeckId = newDeck.Id,
|
||||
CardDefinitionId = deckCard.CardDefinitionId,
|
||||
Quantity = deckCard.Quantity,
|
||||
});
|
||||
}
|
||||
await _collectionDb.SaveChangesAsync(ct);
|
||||
|
||||
// ── PurchaseRecord ────────────────────────────────────────────────────
|
||||
var purchase = new PurchaseRecord
|
||||
{
|
||||
AccountId = accountId,
|
||||
ProductType = PurchaseProductType.PrebuiltDeck,
|
||||
ProductId = deck.Id,
|
||||
OriginPointsSpent = deck.OriginPointPrice,
|
||||
QuantityGranted = 1,
|
||||
PurchasedAt = now,
|
||||
};
|
||||
_bazaarDb.PurchaseRecords.Add(purchase);
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
var newBalance = await GetOriginPointsAsync(_identityDb, accountId, ct);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.buy_prebuilt_deck.ack",
|
||||
message.Seq,
|
||||
new BuyPrebuiltDeckResponse(
|
||||
purchase.Id,
|
||||
newDeck.Id,
|
||||
newDeck.Name,
|
||||
grantedCards.Count,
|
||||
newBalance));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarCancelListingHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.cancel_listing";
|
||||
|
||||
public BazaarCancelListingHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<CancelListingRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
var listing = await _bazaarDb.Listings
|
||||
.FirstOrDefaultAsync(l => l.Id == req.ListingId, ct);
|
||||
|
||||
if (listing is null)
|
||||
return ServerMessage.Error(message.Seq, "LISTING_NOT_FOUND", "Listing not found.");
|
||||
|
||||
if (listing.SellerAccountId != accountId)
|
||||
return ServerMessage.Error(message.Seq, "FORBIDDEN",
|
||||
"You can only cancel your own listings.");
|
||||
|
||||
if (listing.Status != BazaarListingStatus.Active)
|
||||
return ServerMessage.Error(message.Seq, "LISTING_NOT_ACTIVE",
|
||||
"Only active listings can be cancelled.");
|
||||
|
||||
// For auction listings with an active winning bid, refund the winning bidder.
|
||||
if (listing.ListingType == BazaarListingType.Auction)
|
||||
{
|
||||
var winningBid = await _bazaarDb.AuctionBids
|
||||
.FirstOrDefaultAsync(b => b.ListingId == listing.Id && b.IsWinning, ct);
|
||||
|
||||
if (winningBid is not null)
|
||||
{
|
||||
await CreditOriginPointsAsync(_identityDb, winningBid.BidderAccountId,
|
||||
(int)winningBid.Amount, ct);
|
||||
winningBid.IsWinning = false;
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
|
||||
listing.Status = BazaarListingStatus.Cancelled;
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.cancel_listing.ack",
|
||||
message.Seq,
|
||||
new CancelListingResponse(listing.Id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarGetListingsHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
|
||||
public string MessageType => "bazaar.get_listings";
|
||||
|
||||
public BazaarGetListingsHandler(IMessageSerializer serializer, BazaarDbContext bazaarDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GetListingsRequest>(message.Payload);
|
||||
|
||||
var query = _bazaarDb.Listings
|
||||
.Where(l => l.Status == BazaarListingStatus.Active);
|
||||
|
||||
if (req.CardDefinitionId.HasValue)
|
||||
query = query.Where(l => l.CardDefinitionId == req.CardDefinitionId.Value);
|
||||
|
||||
if (req.MinPrice.HasValue)
|
||||
query = query.Where(l => l.Price >= req.MinPrice.Value);
|
||||
|
||||
if (req.MaxPrice.HasValue)
|
||||
query = query.Where(l => l.Price <= req.MaxPrice.Value);
|
||||
|
||||
if (!string.IsNullOrEmpty(req.ListingType) &&
|
||||
Enum.TryParse<BazaarListingType>(req.ListingType, true, out var lt))
|
||||
query = query.Where(l => l.ListingType == lt);
|
||||
|
||||
var total = await query.CountAsync(ct);
|
||||
|
||||
var page = Math.Max(1, req.Page);
|
||||
var pageSize = Math.Clamp(req.PageSize, 1, 100);
|
||||
|
||||
var listings = await query
|
||||
.OrderBy(l => l.ListedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.Select(l => new
|
||||
{
|
||||
l.Id,
|
||||
l.SellerAccountId,
|
||||
l.PlayerCardId,
|
||||
l.CardDefinitionId,
|
||||
l.ListingType,
|
||||
l.Price,
|
||||
l.Currency,
|
||||
l.ListedAt,
|
||||
l.ExpiresAt,
|
||||
l.IsMasked,
|
||||
CurrentBid = l.ListingType == BazaarListingType.Auction
|
||||
? _bazaarDb.AuctionBids
|
||||
.Where(b => b.ListingId == l.Id && b.IsWinning)
|
||||
.Select(b => (decimal?)b.Amount)
|
||||
.FirstOrDefault()
|
||||
: (decimal?)null,
|
||||
BidCount = l.ListingType == BazaarListingType.Auction
|
||||
? _bazaarDb.AuctionBids.Count(b => b.ListingId == l.Id)
|
||||
: 0,
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
|
||||
var dtos = listings.Select(l => new ListingDto(
|
||||
l.Id,
|
||||
l.SellerAccountId,
|
||||
l.PlayerCardId,
|
||||
l.IsMasked ? null : l.CardDefinitionId,
|
||||
null, // CardName — not denormalized on BazaarListing; clients resolve via catalog
|
||||
null, // CardType
|
||||
null, // Rarity
|
||||
l.ListingType.ToString(),
|
||||
l.Price,
|
||||
l.Currency.ToString(),
|
||||
l.ListedAt,
|
||||
l.ExpiresAt,
|
||||
l.IsMasked,
|
||||
l.CurrentBid,
|
||||
l.BidCount)).ToList();
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.get_listings.ack",
|
||||
message.Seq,
|
||||
new GetListingsResponse(dtos, total, page, pageSize));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Identity;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarGetMyBalanceHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.get_my_balance";
|
||||
|
||||
public BazaarGetMyBalanceHandler(IMessageSerializer serializer, SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
var balance = await _identityDb.Set<Account>()
|
||||
.Where(a => a.Id == accountId)
|
||||
.Select(a => a.OriginPoints)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.get_my_balance.ack",
|
||||
message.Seq,
|
||||
new GetMyBalanceResponse(balance));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarGetMyListingsHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
|
||||
public string MessageType => "bazaar.get_my_listings";
|
||||
|
||||
public BazaarGetMyListingsHandler(IMessageSerializer serializer, BazaarDbContext bazaarDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GetMyListingsRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
var query = _bazaarDb.Listings
|
||||
.Where(l => l.SellerAccountId == accountId);
|
||||
|
||||
if (!string.IsNullOrEmpty(req.StatusFilter) &&
|
||||
Enum.TryParse<BazaarListingStatus>(req.StatusFilter, true, out var status))
|
||||
query = query.Where(l => l.Status == status);
|
||||
|
||||
var listings = await query
|
||||
.OrderByDescending(l => l.ListedAt)
|
||||
.Select(l => new
|
||||
{
|
||||
l.Id,
|
||||
l.SellerAccountId,
|
||||
l.PlayerCardId,
|
||||
l.CardDefinitionId,
|
||||
l.ListingType,
|
||||
l.Price,
|
||||
l.Currency,
|
||||
l.ListedAt,
|
||||
l.ExpiresAt,
|
||||
l.IsMasked,
|
||||
CurrentBid = l.ListingType == BazaarListingType.Auction
|
||||
? _bazaarDb.AuctionBids
|
||||
.Where(b => b.ListingId == l.Id && b.IsWinning)
|
||||
.Select(b => (decimal?)b.Amount)
|
||||
.FirstOrDefault()
|
||||
: (decimal?)null,
|
||||
BidCount = l.ListingType == BazaarListingType.Auction
|
||||
? _bazaarDb.AuctionBids.Count(b => b.ListingId == l.Id)
|
||||
: 0,
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
|
||||
var dtos = listings.Select(l => new ListingDto(
|
||||
l.Id,
|
||||
l.SellerAccountId,
|
||||
l.PlayerCardId,
|
||||
l.CardDefinitionId,
|
||||
null, null, null,
|
||||
l.ListingType.ToString(),
|
||||
l.Price,
|
||||
l.Currency.ToString(),
|
||||
l.ListedAt,
|
||||
l.ExpiresAt,
|
||||
l.IsMasked,
|
||||
l.CurrentBid,
|
||||
l.BidCount)).ToList();
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.get_my_listings.ack",
|
||||
message.Seq,
|
||||
new GetMyListingsResponse(dtos));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarGetStoreHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
|
||||
public string MessageType => "bazaar.get_store";
|
||||
|
||||
public BazaarGetStoreHandler(IMessageSerializer serializer, BazaarDbContext bazaarDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var packs = await _bazaarDb.BoosterPacks
|
||||
.Where(p => p.IsActive
|
||||
&& (p.AvailableFrom == null || p.AvailableFrom <= now)
|
||||
&& (p.AvailableUntil == null || p.AvailableUntil >= now))
|
||||
.OrderBy(p => p.Name)
|
||||
.Select(p => new StorePackDto(
|
||||
p.Id,
|
||||
p.Name,
|
||||
p.Description,
|
||||
p.ImageCdnUrl,
|
||||
p.OriginPointPrice,
|
||||
p.UsdPrice,
|
||||
p.CardSetId,
|
||||
p.SlotsJson == null ? 0 : CountSlots(p.SlotsJson),
|
||||
p.AvailableFrom,
|
||||
p.AvailableUntil))
|
||||
.ToListAsync(ct);
|
||||
|
||||
var decks = await _bazaarDb.PrebuiltDecks
|
||||
.Where(d => d.IsActive
|
||||
&& (d.AvailableFrom == null || d.AvailableFrom <= now)
|
||||
&& (d.AvailableUntil == null || d.AvailableUntil >= now))
|
||||
.OrderBy(d => d.Name)
|
||||
.Select(d => new StoreDeckDto(
|
||||
d.Id,
|
||||
d.Name,
|
||||
d.Description,
|
||||
d.ImageCdnUrl,
|
||||
d.OriginPointPrice,
|
||||
d.UsdPrice,
|
||||
d.Cards.Sum(c => c.Quantity),
|
||||
d.ArchSpiritName,
|
||||
d.IsStarterDeck,
|
||||
d.AvailableFrom,
|
||||
d.AvailableUntil))
|
||||
.ToListAsync(ct);
|
||||
|
||||
var cosmetics = await _bazaarDb.Cosmetics
|
||||
.Where(c => c.IsActive)
|
||||
.OrderBy(c => c.CosmeticType)
|
||||
.ThenBy(c => c.Name)
|
||||
.Select(c => new CosmeticDto(
|
||||
c.Id,
|
||||
c.Name,
|
||||
c.Description,
|
||||
c.CosmeticType.ToString(),
|
||||
c.CardDefinitionId,
|
||||
c.PreviewImageCdnUrl,
|
||||
c.AssetCdnUrl,
|
||||
c.OriginPointPrice,
|
||||
c.UsdPrice))
|
||||
.ToListAsync(ct);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.get_store.ack",
|
||||
message.Seq,
|
||||
new GetStoreResponse(packs, decks, cosmetics));
|
||||
}
|
||||
|
||||
// SlotsJson is a JSONB string — count total cards across all slots without
|
||||
// full deserialisation (just count "count" values via a quick parse).
|
||||
private static int CountSlots(string slotsJson)
|
||||
{
|
||||
try
|
||||
{
|
||||
var slots = System.Text.Json.JsonSerializer
|
||||
.Deserialize<SlotEntry[]>(slotsJson);
|
||||
return slots?.Sum(s => s.Count) ?? 0;
|
||||
}
|
||||
catch { return 0; }
|
||||
}
|
||||
|
||||
private sealed record SlotEntry(string Rarity, int Count);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarGetTransactionsHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
|
||||
public string MessageType => "bazaar.get_transactions";
|
||||
|
||||
public BazaarGetTransactionsHandler(IMessageSerializer serializer, BazaarDbContext bazaarDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GetTransactionsRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
var page = Math.Max(1, req.Page);
|
||||
var pageSize = Math.Clamp(req.PageSize, 1, 100);
|
||||
|
||||
var query = _bazaarDb.Transactions
|
||||
.Where(t => t.BuyerAccountId == accountId || t.SellerAccountId == accountId);
|
||||
|
||||
var total = await query.CountAsync(ct);
|
||||
|
||||
var txns = await query
|
||||
.OrderByDescending(t => t.TransactedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.Select(t => new TransactionDto(
|
||||
t.Id,
|
||||
t.ListingId,
|
||||
t.BuyerAccountId,
|
||||
t.SellerAccountId,
|
||||
t.PlayerCardId,
|
||||
t.Amount,
|
||||
t.Currency.ToString(),
|
||||
t.TransactedAt))
|
||||
.ToListAsync(ct);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.get_transactions.ack",
|
||||
message.Seq,
|
||||
new GetTransactionsResponse(txns, total, page, pageSize));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Npgsql;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Catalog;
|
||||
using SOR.Database.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Shared helpers for all bazaar message handlers.
|
||||
/// </summary>
|
||||
internal abstract class BazaarHandlerBase
|
||||
{
|
||||
// ── Origin Points ─────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Atomically deducts <paramref name="amount"/> from the account's OriginPoints
|
||||
/// using a conditional UPDATE (origin_points >= amount).
|
||||
/// Returns true when the deduction succeeded; false when the balance was insufficient.
|
||||
/// Must be called within an open transaction when cross-context atomicity is required.
|
||||
/// </summary>
|
||||
protected static async Task<bool> TryDebitOriginPointsAsync(
|
||||
SorIdentityDbContext identityDb,
|
||||
Guid accountId,
|
||||
int amount,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var rows = await identityDb.Database.ExecuteSqlRawAsync(
|
||||
"""
|
||||
UPDATE identity.accounts
|
||||
SET origin_points = origin_points - {0}
|
||||
WHERE "Id" = {1}
|
||||
AND origin_points >= {0}
|
||||
""",
|
||||
amount, accountId, ct);
|
||||
|
||||
return rows == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Credits <paramref name="amount"/> OriginPoints to the account.
|
||||
/// Does not require a minimum balance check.
|
||||
/// </summary>
|
||||
protected static async Task CreditOriginPointsAsync(
|
||||
SorIdentityDbContext identityDb,
|
||||
Guid accountId,
|
||||
int amount,
|
||||
CancellationToken ct)
|
||||
{
|
||||
await identityDb.Database.ExecuteSqlRawAsync(
|
||||
"""
|
||||
UPDATE identity.accounts
|
||||
SET origin_points = origin_points + {0}
|
||||
WHERE "Id" = {1}
|
||||
""",
|
||||
amount, accountId, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the account's current OriginPoints balance after a debit/credit.
|
||||
/// </summary>
|
||||
protected static async Task<int> GetOriginPointsAsync(
|
||||
SorIdentityDbContext identityDb,
|
||||
Guid accountId,
|
||||
CancellationToken ct)
|
||||
{
|
||||
return await identityDb.Database
|
||||
.SqlQuery<int>($"""
|
||||
SELECT origin_points AS "Value"
|
||||
FROM identity.accounts
|
||||
WHERE "Id" = {accountId}
|
||||
""")
|
||||
.FirstOrDefaultAsync(ct);
|
||||
}
|
||||
|
||||
// ── Shared Npgsql transaction ─────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Opens a single <see cref="NpgsqlConnection"/> and begins a transaction on it,
|
||||
/// then shares that same underlying transaction across all supplied DbContexts.
|
||||
///
|
||||
/// All three contexts must point to the same Postgres instance (which they do —
|
||||
/// they all share the same connection string). This approach is safe across
|
||||
/// multiple application servers because Postgres row-level locking serialises
|
||||
/// concurrent claims; no distributed coordination is needed.
|
||||
///
|
||||
/// Caller is responsible for CommitAsync / RollbackAsync and disposing the returned
|
||||
/// transaction.
|
||||
/// </summary>
|
||||
protected static async Task<IDbContextTransaction> OpenSharedTransactionAsync(
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb,
|
||||
SorIdentityDbContext identityDb,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var conn = (NpgsqlConnection)bazaarDb.Database.GetDbConnection();
|
||||
if (conn.State != System.Data.ConnectionState.Open)
|
||||
await conn.OpenAsync(ct);
|
||||
|
||||
var tx = await conn.BeginTransactionAsync(
|
||||
System.Data.IsolationLevel.ReadCommitted, ct);
|
||||
|
||||
await bazaarDb.Database.UseTransactionAsync(tx, ct);
|
||||
await collectionDb.Database.UseTransactionAsync(tx, ct);
|
||||
await identityDb.Database.UseTransactionAsync(tx, ct);
|
||||
|
||||
// Wrap so callers use EF's IDbContextTransaction interface for Commit/Rollback.
|
||||
return bazaarDb.Database.CurrentTransaction!;
|
||||
}
|
||||
|
||||
// ── Card art helper ───────────────────────────────────────────────────────
|
||||
|
||||
protected static string? GetArtUrl(CardDefinition? def)
|
||||
=> def?.Assets
|
||||
.FirstOrDefault(a => a.AssetType == CardAssetType.Art2D)
|
||||
?.CdnUrl;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Bazaar;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
internal sealed class BazaarListCardHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly CollectionDbContext _collectionDb;
|
||||
|
||||
public string MessageType => "bazaar.list_card";
|
||||
|
||||
public BazaarListCardHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_collectionDb = collectionDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<ListCardRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
// Validate listing type
|
||||
if (!Enum.TryParse<BazaarListingType>(req.ListingType, true, out var listingType))
|
||||
return ServerMessage.Error(message.Seq, "INVALID_LISTING_TYPE",
|
||||
$"ListingType must be Fixed or Auction.");
|
||||
|
||||
// Validate expiry for Auction
|
||||
if (listingType == BazaarListingType.Auction && req.ExpiresAt == null)
|
||||
return ServerMessage.Error(message.Seq, "EXPIRY_REQUIRED",
|
||||
"Auction listings require an ExpiresAt date.");
|
||||
|
||||
if (req.ExpiresAt.HasValue && req.ExpiresAt.Value <= DateTimeOffset.UtcNow)
|
||||
return ServerMessage.Error(message.Seq, "INVALID_EXPIRY",
|
||||
"ExpiresAt must be in the future.");
|
||||
|
||||
// Validate price
|
||||
if (req.Price <= 0)
|
||||
return ServerMessage.Error(message.Seq, "INVALID_PRICE", "Price must be greater than zero.");
|
||||
|
||||
// Load the card — must be owned by this account
|
||||
var card = await _collectionDb.PlayerCards
|
||||
.FirstOrDefaultAsync(pc => pc.Id == req.PlayerCardId && pc.AccountId == accountId, ct);
|
||||
|
||||
if (card is null)
|
||||
return ServerMessage.Error(message.Seq, "CARD_NOT_FOUND",
|
||||
"Card not found in your collection.");
|
||||
|
||||
if (!card.IsTradeable)
|
||||
return ServerMessage.Error(message.Seq, "CARD_NOT_TRADEABLE",
|
||||
"This card cannot be listed on the bazaar (starter deck card).");
|
||||
|
||||
// Check not already in an active listing
|
||||
var alreadyListed = await _bazaarDb.Listings
|
||||
.AnyAsync(l => l.PlayerCardId == req.PlayerCardId
|
||||
&& l.Status == BazaarListingStatus.Active, ct);
|
||||
|
||||
if (alreadyListed)
|
||||
return ServerMessage.Error(message.Seq, "ALREADY_LISTED",
|
||||
"This card already has an active listing.");
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var listing = new BazaarListing
|
||||
{
|
||||
SellerAccountId = accountId,
|
||||
PlayerCardId = req.PlayerCardId,
|
||||
CardDefinitionId = card.CardDefinitionId,
|
||||
ListingType = listingType,
|
||||
Price = req.Price,
|
||||
Currency = BazaarCurrency.Gold,
|
||||
ListedAt = now,
|
||||
ExpiresAt = req.ExpiresAt,
|
||||
Status = BazaarListingStatus.Active,
|
||||
IsMasked = req.IsMasked,
|
||||
};
|
||||
|
||||
_bazaarDb.Listings.Add(listing);
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
var dto = new ListingDto(
|
||||
listing.Id,
|
||||
listing.SellerAccountId,
|
||||
listing.PlayerCardId,
|
||||
listing.CardDefinitionId,
|
||||
null, null, null,
|
||||
listing.ListingType.ToString(),
|
||||
listing.Price,
|
||||
listing.Currency.ToString(),
|
||||
listing.ListedAt,
|
||||
listing.ExpiresAt,
|
||||
listing.IsMasked,
|
||||
null,
|
||||
0);
|
||||
|
||||
return ServerMessage.Reply("bazaar.list_card.ack", message.Seq, new ListCardResponse(dto));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
namespace SOR.Bazaar.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Bazaar;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// Places a bid on an Auction listing.
|
||||
///
|
||||
/// Atomicity: shared NpgsqlConnection transaction across Bazaar and Identity contexts.
|
||||
/// The previous winning bidder's OriginPoints are refunded and their IsWinning flag cleared
|
||||
/// in the same transaction as the new bid being recorded, so no bid is ever "lost".
|
||||
/// </summary>
|
||||
internal sealed class BazaarPlaceBidHandler : BazaarHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly BazaarDbContext _bazaarDb;
|
||||
private readonly CollectionDbContext _collectionDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
|
||||
public string MessageType => "bazaar.place_bid";
|
||||
|
||||
public BazaarPlaceBidHandler(
|
||||
IMessageSerializer serializer,
|
||||
BazaarDbContext bazaarDb,
|
||||
CollectionDbContext collectionDb,
|
||||
SorIdentityDbContext identityDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_bazaarDb = bazaarDb;
|
||||
_collectionDb = collectionDb;
|
||||
_identityDb = identityDb;
|
||||
}
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<PlaceBidRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
if (req.Amount <= 0)
|
||||
return ServerMessage.Error(message.Seq, "INVALID_BID", "Bid must be greater than zero.");
|
||||
|
||||
await using var tx = await OpenSharedTransactionAsync(_bazaarDb, _collectionDb, _identityDb, ct);
|
||||
try
|
||||
{
|
||||
// ── Load and validate listing ──────────────────────────────────────
|
||||
var listing = await _bazaarDb.Listings
|
||||
.FirstOrDefaultAsync(l => l.Id == req.ListingId, ct);
|
||||
|
||||
if (listing is null)
|
||||
return ServerMessage.Error(message.Seq, "LISTING_NOT_FOUND", "Listing not found.");
|
||||
|
||||
if (listing.ListingType != BazaarListingType.Auction)
|
||||
return ServerMessage.Error(message.Seq, "NOT_AUCTION",
|
||||
"This listing is not an auction.");
|
||||
|
||||
if (listing.Status != BazaarListingStatus.Active)
|
||||
return ServerMessage.Error(message.Seq, "LISTING_NOT_ACTIVE",
|
||||
"This auction is no longer active.");
|
||||
|
||||
if (listing.ExpiresAt.HasValue && listing.ExpiresAt.Value <= DateTimeOffset.UtcNow)
|
||||
return ServerMessage.Error(message.Seq, "AUCTION_EXPIRED",
|
||||
"This auction has already expired.");
|
||||
|
||||
if (listing.SellerAccountId == accountId)
|
||||
return ServerMessage.Error(message.Seq, "CANNOT_BID_OWN",
|
||||
"You cannot bid on your own listing.");
|
||||
|
||||
// ── Check bid beats current highest ───────────────────────────────
|
||||
var prevWinner = await _bazaarDb.AuctionBids
|
||||
.FirstOrDefaultAsync(b => b.ListingId == req.ListingId && b.IsWinning, ct);
|
||||
|
||||
decimal minBid = prevWinner?.Amount ?? listing.Price;
|
||||
if (req.Amount <= minBid)
|
||||
return ServerMessage.Error(message.Seq, "BID_TOO_LOW",
|
||||
$"Bid must exceed the current highest bid of {minBid}.");
|
||||
|
||||
// ── Debit new bidder ───────────────────────────────────────────────
|
||||
var debited = await TryDebitOriginPointsAsync(_identityDb, accountId, (int)req.Amount, ct);
|
||||
if (!debited)
|
||||
{
|
||||
await tx.RollbackAsync(ct);
|
||||
return ServerMessage.Error(message.Seq, "INSUFFICIENT_FUNDS",
|
||||
"You do not have enough Origin Points to place this bid.");
|
||||
}
|
||||
|
||||
// ── Refund previous winner ────────────────────────────────────────
|
||||
bool refunded = false;
|
||||
if (prevWinner is not null)
|
||||
{
|
||||
await CreditOriginPointsAsync(_identityDb, prevWinner.BidderAccountId,
|
||||
(int)prevWinner.Amount, ct);
|
||||
prevWinner.IsWinning = false;
|
||||
refunded = true;
|
||||
}
|
||||
|
||||
// ── Record new winning bid ────────────────────────────────────────
|
||||
var bid = new AuctionBid
|
||||
{
|
||||
ListingId = req.ListingId,
|
||||
BidderAccountId = accountId,
|
||||
Amount = req.Amount,
|
||||
Currency = BazaarCurrency.Gold,
|
||||
PlacedAt = DateTimeOffset.UtcNow,
|
||||
IsWinning = true,
|
||||
};
|
||||
_bazaarDb.AuctionBids.Add(bid);
|
||||
await _bazaarDb.SaveChangesAsync(ct);
|
||||
|
||||
await tx.CommitAsync(ct);
|
||||
|
||||
var newBalance = await GetOriginPointsAsync(_identityDb, accountId, ct);
|
||||
|
||||
var bidDto = new BidDto(
|
||||
bid.Id,
|
||||
bid.ListingId,
|
||||
bid.BidderAccountId,
|
||||
bid.Amount,
|
||||
bid.Currency.ToString(),
|
||||
bid.PlacedAt,
|
||||
bid.IsWinning);
|
||||
|
||||
return ServerMessage.Reply(
|
||||
"bazaar.place_bid.ack",
|
||||
message.Seq,
|
||||
new PlaceBidResponse(bidDto, refunded, newBalance));
|
||||
}
|
||||
catch
|
||||
{
|
||||
await tx.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<AssemblyName>SOR.Bazaar</AssemblyName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SOR.Database\SOR.Database.csproj" />
|
||||
<ProjectReference Include="..\SOR.Networking.Abstractions\SOR.Networking.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\SOR.Shared\SOR.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace SOR.Bazaar;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SOR.Bazaar.Handlers;
|
||||
using SOR.Database;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Registers all bazaar service components into the DI container.
|
||||
/// Called from SOR.Host/Program.cs when ENABLED_SERVICES includes "bazaar".
|
||||
/// </summary>
|
||||
public static class ServiceRegistration
|
||||
{
|
||||
public static IServiceCollection AddBazaarService(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
// All four DbContexts needed across the bazaar handlers.
|
||||
services.AddBazaarDatabase(configuration);
|
||||
services.AddCollectionDatabase(configuration);
|
||||
services.AddCatalogDatabase(configuration);
|
||||
services.AddIdentityDatabase(configuration);
|
||||
|
||||
// Store — read-only browsing
|
||||
services.AddTransient<IMessageHandler, BazaarGetStoreHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarGetMyBalanceHandler>();
|
||||
|
||||
// Store — purchases
|
||||
services.AddTransient<IMessageHandler, BazaarBuyBoosterPackHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarBuyPrebuiltDeckHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarBuyCosmeticHandler>();
|
||||
|
||||
// P2P marketplace — browsing
|
||||
services.AddTransient<IMessageHandler, BazaarGetListingsHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarGetMyListingsHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarGetTransactionsHandler>();
|
||||
|
||||
// P2P marketplace — mutations
|
||||
services.AddTransient<IMessageHandler, BazaarListCardHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarCancelListingHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarBuyListingHandler>();
|
||||
services.AddTransient<IMessageHandler, BazaarPlaceBidHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user