feat(db): add gacha-point balance + received tables

This commit is contained in:
gamer147
2026-05-28 22:50:22 -04:00
parent 261ce67cee
commit 21adc68e28
7 changed files with 3968 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,81 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace SVSim.Database.Migrations
{
/// <inheritdoc />
public partial class AddGachaPointExchange : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ViewerGachaPointBalance",
columns: table => new
{
ViewerId = table.Column<long>(type: "bigint", nullable: false),
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
PackId = table.Column<int>(type: "integer", nullable: false),
Points = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ViewerGachaPointBalance", x => new { x.ViewerId, x.Id });
table.ForeignKey(
name: "FK_ViewerGachaPointBalance_Viewers_ViewerId",
column: x => x.ViewerId,
principalTable: "Viewers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ViewerGachaPointReceived",
columns: table => new
{
ViewerId = table.Column<long>(type: "bigint", nullable: false),
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
PackId = table.Column<int>(type: "integer", nullable: false),
CardId = table.Column<long>(type: "bigint", nullable: false),
ReceivedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ViewerGachaPointReceived", x => new { x.ViewerId, x.Id });
table.ForeignKey(
name: "FK_ViewerGachaPointReceived_Viewers_ViewerId",
column: x => x.ViewerId,
principalTable: "Viewers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ViewerGachaPointBalance_ViewerId_PackId",
table: "ViewerGachaPointBalance",
columns: new[] { "ViewerId", "PackId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ViewerGachaPointReceived_ViewerId_PackId_CardId",
table: "ViewerGachaPointReceived",
columns: new[] { "ViewerId", "PackId", "CardId" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ViewerGachaPointBalance");
migrationBuilder.DropTable(
name: "ViewerGachaPointReceived");
}
}
}

View File

@@ -3476,6 +3476,65 @@ namespace SVSim.Database.Migrations
.HasForeignKey("ViewerId");
});
b.OwnsMany("SVSim.Database.Models.ViewerGachaPointBalance", "GachaPointBalances", b1 =>
{
b1.Property<long>("ViewerId")
.HasColumnType("bigint");
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b1.Property<int>("Id"));
b1.Property<int>("PackId")
.HasColumnType("integer");
b1.Property<int>("Points")
.HasColumnType("integer");
b1.HasKey("ViewerId", "Id");
b1.HasIndex("ViewerId", "PackId")
.IsUnique();
b1.ToTable("ViewerGachaPointBalance");
b1.WithOwner()
.HasForeignKey("ViewerId");
});
b.OwnsMany("SVSim.Database.Models.ViewerGachaPointReceived", "GachaPointReceived", b1 =>
{
b1.Property<long>("ViewerId")
.HasColumnType("bigint");
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b1.Property<int>("Id"));
b1.Property<long>("CardId")
.HasColumnType("bigint");
b1.Property<int>("PackId")
.HasColumnType("integer");
b1.Property<DateTime>("ReceivedAt")
.HasColumnType("timestamp with time zone");
b1.HasKey("ViewerId", "Id");
b1.HasIndex("ViewerId", "PackId", "CardId")
.IsUnique();
b1.ToTable("ViewerGachaPointReceived");
b1.WithOwner()
.HasForeignKey("ViewerId");
});
b.OwnsOne("SVSim.Database.Models.ViewerInfo", "Info", b1 =>
{
b1.Property<long>("ViewerId")
@@ -3593,6 +3652,10 @@ namespace SVSim.Database.Migrations
b.Navigation("Currency")
.IsRequired();
b.Navigation("GachaPointBalances");
b.Navigation("GachaPointReceived");
b.Navigation("Info")
.IsRequired();

View File

@@ -65,6 +65,10 @@ public class Viewer : BaseEntity<long>
public List<ViewerPackOpenCount> PackOpenCounts { get; set; } = new List<ViewerPackOpenCount>();
public List<ViewerGachaPointBalance> GachaPointBalances { get; set; } = new List<ViewerGachaPointBalance>();
public List<ViewerGachaPointReceived> GachaPointReceived { get; set; } = new List<ViewerGachaPointReceived>();
public List<ViewerBuildDeckProductPurchase> BuildDeckPurchases { get; set; } = new List<ViewerBuildDeckProductPurchase>();
public List<ViewerMission> Missions { get; set; } = new List<ViewerMission>();

View File

@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
namespace SVSim.Database.Models;
/// <summary>
/// Per-viewer, per-pack gacha-point balance. Owned collection on <see cref="Viewer"/>.
/// <c>PackId</c> = parent_gacha_id. <c>Points</c> accumulates one per pack opened (or
/// <c>PackChildGachaEntry.OverrideIncreaseGachaPoint</c> when set on the child) and is
/// decremented by <see cref="PackGachaPointConfig.ExchangeablePoint"/> per exchange.
/// Unique index on (ViewerId, PackId) per feedback_owned_collection_unique_index.
/// </summary>
[Owned]
public class ViewerGachaPointBalance
{
public int PackId { get; set; }
public int Points { get; set; }
}

View File

@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
namespace SVSim.Database.Models;
/// <summary>
/// Marker row recording that a viewer has already redeemed <c>CardId</c> from <c>PackId</c>'s
/// gacha-point exchange. Drives the per-entry <c>is_received</c> flag in
/// <c>/pack/get_gacha_point_rewards</c>. Owned collection on <see cref="Viewer"/>.
/// Unique index on (ViewerId, PackId, CardId) per feedback_owned_collection_unique_index.
/// </summary>
[Owned]
public class ViewerGachaPointReceived
{
public int PackId { get; set; }
public long CardId { get; set; }
public DateTime ReceivedAt { get; set; }
}

View File

@@ -167,6 +167,16 @@ public class SVSimDbContext : DbContext
b.HasIndex("ViewerId", "ProductId").IsUnique();
});
modelBuilder.Entity<Viewer>().OwnsMany(v => v.GachaPointBalances, b =>
{
b.HasIndex("ViewerId", "PackId").IsUnique();
});
modelBuilder.Entity<Viewer>().OwnsMany(v => v.GachaPointReceived, b =>
{
b.HasIndex("ViewerId", "PackId", "CardId").IsUnique();
});
// A given social account links to exactly one viewer — two viewers cannot share the same
// Steam (or Facebook, etc.) account. This is the dedup backstop the auth handler's find-
// or-link path (SteamSessionAuthenticationHandler) relies on: two concurrent first-Steam-