diff --git a/Migrations/20251105224215_InitialGameSchema.Designer.cs b/Migrations/20251105224215_InitialGameSchema.Designer.cs
new file mode 100644
index 0000000..95c82bf
--- /dev/null
+++ b/Migrations/20251105224215_InitialGameSchema.Designer.cs
@@ -0,0 +1,470 @@
+//
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Server.Data;
+
+#nullable disable
+
+namespace SystemRevivalServer.Migrations
+{
+ [DbContext(typeof(GameDbContext))]
+ [Migration("20251105224215_InitialGameSchema")]
+ partial class InitialGameSchema
+ {
+ ///
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "9.0.10")
+ .HasAnnotation("Relational:MaxIdentifierLength", 64);
+
+ MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
+
+ modelBuilder.Entity("Server.Data.Account", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("BannedUntil")
+ .HasColumnType("datetime(6)");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("Email")
+ .HasMaxLength(255)
+ .HasColumnType("varchar(255)");
+
+ b.Property("IsBanned")
+ .HasColumnType("tinyint(1)");
+
+ b.Property("PasswordHash")
+ .IsRequired()
+ .HasColumnType("varbinary(64)");
+
+ b.Property("PasswordSalt")
+ .IsRequired()
+ .HasColumnType("varbinary(32)");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.Property("Username")
+ .IsRequired()
+ .HasMaxLength(32)
+ .HasColumnType("varchar(32)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Email")
+ .IsUnique();
+
+ b.HasIndex("Username")
+ .IsUnique();
+
+ b.ToTable("accounts", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Character", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("AccountId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("AppearanceJson")
+ .HasColumnType("json");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("FacingDeg")
+ .HasColumnType("float");
+
+ b.Property("HpCurrent")
+ .HasColumnType("int unsigned");
+
+ b.Property("HpMax")
+ .HasColumnType("int unsigned");
+
+ b.Property("Level")
+ .HasColumnType("int unsigned");
+
+ b.Property("MpCurrent")
+ .HasColumnType("int unsigned");
+
+ b.Property("MpMax")
+ .HasColumnType("int unsigned");
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(32)
+ .HasColumnType("varchar(32)");
+
+ b.Property("PosX")
+ .HasColumnType("double");
+
+ b.Property("PosY")
+ .HasColumnType("double");
+
+ b.Property("PosZ")
+ .HasColumnType("double");
+
+ b.Property("StaminaCurrent")
+ .HasColumnType("int unsigned");
+
+ b.Property("StaminaMax")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatDexterity")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatEndurance")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatIntelligence")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatStrength")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatWisdom")
+ .HasColumnType("int unsigned");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.Property("Xp")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("ZoneId")
+ .HasColumnType("bigint unsigned");
+
+ b.HasKey("Id");
+
+ b.HasIndex("AccountId")
+ .IsUnique();
+
+ b.HasIndex("ZoneId");
+
+ b.ToTable("characters", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.EquipmentSlot", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CharacterId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ItemTemplateId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("SlotCode")
+ .HasColumnType("tinyint unsigned");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.HasKey("Id");
+
+ b.HasIndex("ItemTemplateId");
+
+ b.HasIndex("CharacterId", "SlotCode")
+ .IsUnique();
+
+ b.ToTable("equipment", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.InventoryItem", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CharacterId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ItemTemplateId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("Quantity")
+ .HasColumnType("int unsigned");
+
+ b.Property("SlotIndex")
+ .HasColumnType("int unsigned");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.HasKey("Id");
+
+ b.HasIndex("CharacterId");
+
+ b.HasIndex("ItemTemplateId");
+
+ b.HasIndex("CharacterId", "SlotIndex")
+ .IsUnique();
+
+ b.ToTable("inventories", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.ItemTemplate", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ItemType")
+ .HasColumnType("tinyint unsigned");
+
+ b.Property("MaxStack")
+ .HasColumnType("int unsigned");
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(64)
+ .HasColumnType("varchar(64)");
+
+ b.Property("Rarity")
+ .HasColumnType("tinyint unsigned");
+
+ b.Property("StatsJson")
+ .HasColumnType("json");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.Property("Weight")
+ .HasColumnType("int unsigned");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name")
+ .IsUnique();
+
+ b.ToTable("item_templates", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Session", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("AccountId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ExpiresAt")
+ .HasColumnType("datetime(6)");
+
+ b.Property("Token")
+ .IsRequired()
+ .HasMaxLength(64)
+ .HasColumnType("varchar(64)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("AccountId");
+
+ b.HasIndex("Token")
+ .IsUnique();
+
+ b.ToTable("sessions", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Zone", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("MaxX")
+ .HasColumnType("double");
+
+ b.Property("MaxY")
+ .HasColumnType("double");
+
+ b.Property("MaxZ")
+ .HasColumnType("double");
+
+ b.Property("MinX")
+ .HasColumnType("double");
+
+ b.Property("MinY")
+ .HasColumnType("double");
+
+ b.Property("MinZ")
+ .HasColumnType("double");
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(64)
+ .HasColumnType("varchar(64)");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name")
+ .IsUnique();
+
+ b.ToTable("zones", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Character", b =>
+ {
+ b.HasOne("Server.Data.Account", "Account")
+ .WithMany("Characters")
+ .HasForeignKey("AccountId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Server.Data.Zone", "Zone")
+ .WithMany()
+ .HasForeignKey("ZoneId")
+ .OnDelete(DeleteBehavior.Restrict)
+ .IsRequired();
+
+ b.Navigation("Account");
+
+ b.Navigation("Zone");
+ });
+
+ modelBuilder.Entity("Server.Data.EquipmentSlot", b =>
+ {
+ b.HasOne("Server.Data.Character", "Character")
+ .WithMany()
+ .HasForeignKey("CharacterId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Server.Data.ItemTemplate", "ItemTemplate")
+ .WithMany()
+ .HasForeignKey("ItemTemplateId")
+ .OnDelete(DeleteBehavior.Restrict)
+ .IsRequired();
+
+ b.Navigation("Character");
+
+ b.Navigation("ItemTemplate");
+ });
+
+ modelBuilder.Entity("Server.Data.InventoryItem", b =>
+ {
+ b.HasOne("Server.Data.Character", "Character")
+ .WithMany()
+ .HasForeignKey("CharacterId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Server.Data.ItemTemplate", "ItemTemplate")
+ .WithMany()
+ .HasForeignKey("ItemTemplateId")
+ .OnDelete(DeleteBehavior.Restrict)
+ .IsRequired();
+
+ b.Navigation("Character");
+
+ b.Navigation("ItemTemplate");
+ });
+
+ modelBuilder.Entity("Server.Data.Session", b =>
+ {
+ b.HasOne("Server.Data.Account", "Account")
+ .WithMany("Sessions")
+ .HasForeignKey("AccountId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Account");
+ });
+
+ modelBuilder.Entity("Server.Data.Account", b =>
+ {
+ b.Navigation("Characters");
+
+ b.Navigation("Sessions");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/Migrations/20251105224215_InitialGameSchema.cs b/Migrations/20251105224215_InitialGameSchema.cs
new file mode 100644
index 0000000..51cce05
--- /dev/null
+++ b/Migrations/20251105224215_InitialGameSchema.cs
@@ -0,0 +1,327 @@
+using System;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace SystemRevivalServer.Migrations
+{
+ ///
+ public partial class InitialGameSchema : Migration
+ {
+ ///
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.AlterDatabase()
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "accounts",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ Username = table.Column(type: "varchar(32)", maxLength: 32, nullable: false)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ Email = table.Column(type: "varchar(255)", maxLength: 255, nullable: true)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ PasswordHash = table.Column(type: "varbinary(64)", nullable: false),
+ PasswordSalt = table.Column(type: "varbinary(32)", nullable: false),
+ IsBanned = table.Column(type: "tinyint(1)", nullable: false),
+ BannedUntil = table.Column(type: "datetime(6)", nullable: true),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)"),
+ UpdatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.ComputedColumn)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_accounts", x => x.Id);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "item_templates",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ Name = table.Column(type: "varchar(64)", maxLength: 64, nullable: false)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ ItemType = table.Column(type: "tinyint unsigned", nullable: false),
+ Rarity = table.Column(type: "tinyint unsigned", nullable: false),
+ MaxStack = table.Column(type: "int unsigned", nullable: false),
+ Weight = table.Column(type: "int unsigned", nullable: false),
+ StatsJson = table.Column(type: "json", nullable: true)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)"),
+ UpdatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.ComputedColumn)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_item_templates", x => x.Id);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "zones",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ Name = table.Column(type: "varchar(64)", maxLength: 64, nullable: false)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ MinX = table.Column(type: "double", nullable: false),
+ MinY = table.Column(type: "double", nullable: false),
+ MinZ = table.Column(type: "double", nullable: false),
+ MaxX = table.Column(type: "double", nullable: false),
+ MaxY = table.Column(type: "double", nullable: false),
+ MaxZ = table.Column(type: "double", nullable: false),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)"),
+ UpdatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.ComputedColumn)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_zones", x => x.Id);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "sessions",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ AccountId = table.Column(type: "bigint unsigned", nullable: false),
+ Token = table.Column(type: "varchar(64)", maxLength: 64, nullable: false)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ ExpiresAt = table.Column(type: "datetime(6)", nullable: false),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_sessions", x => x.Id);
+ table.ForeignKey(
+ name: "FK_sessions_accounts_AccountId",
+ column: x => x.AccountId,
+ principalTable: "accounts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "characters",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ AccountId = table.Column(type: "bigint unsigned", nullable: false),
+ Name = table.Column(type: "varchar(32)", maxLength: 32, nullable: false)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ Level = table.Column(type: "int unsigned", nullable: false),
+ Xp = table.Column(type: "bigint unsigned", nullable: false),
+ StatStrength = table.Column(type: "int unsigned", nullable: false),
+ StatDexterity = table.Column(type: "int unsigned", nullable: false),
+ StatWisdom = table.Column(type: "int unsigned", nullable: false),
+ StatIntelligence = table.Column(type: "int unsigned", nullable: false),
+ StatEndurance = table.Column(type: "int unsigned", nullable: false),
+ HpCurrent = table.Column(type: "int unsigned", nullable: false),
+ HpMax = table.Column(type: "int unsigned", nullable: false),
+ MpCurrent = table.Column(type: "int unsigned", nullable: false),
+ MpMax = table.Column(type: "int unsigned", nullable: false),
+ StaminaCurrent = table.Column(type: "int unsigned", nullable: false),
+ StaminaMax = table.Column(type: "int unsigned", nullable: false),
+ ZoneId = table.Column(type: "bigint unsigned", nullable: false),
+ PosX = table.Column(type: "double", nullable: false),
+ PosY = table.Column(type: "double", nullable: false),
+ PosZ = table.Column(type: "double", nullable: false),
+ FacingDeg = table.Column(type: "float", nullable: false),
+ AppearanceJson = table.Column(type: "json", nullable: true)
+ .Annotation("MySql:CharSet", "utf8mb4"),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)"),
+ UpdatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.ComputedColumn)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_characters", x => x.Id);
+ table.ForeignKey(
+ name: "FK_characters_accounts_AccountId",
+ column: x => x.AccountId,
+ principalTable: "accounts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_characters_zones_ZoneId",
+ column: x => x.ZoneId,
+ principalTable: "zones",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "equipment",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ CharacterId = table.Column(type: "bigint unsigned", nullable: false),
+ SlotCode = table.Column(type: "tinyint unsigned", nullable: false),
+ ItemTemplateId = table.Column(type: "bigint unsigned", nullable: false),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)"),
+ UpdatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.ComputedColumn)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_equipment", x => x.Id);
+ table.ForeignKey(
+ name: "FK_equipment_characters_CharacterId",
+ column: x => x.CharacterId,
+ principalTable: "characters",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_equipment_item_templates_ItemTemplateId",
+ column: x => x.ItemTemplateId,
+ principalTable: "item_templates",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateTable(
+ name: "inventories",
+ columns: table => new
+ {
+ Id = table.Column(type: "bigint unsigned", nullable: false)
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
+ CharacterId = table.Column(type: "bigint unsigned", nullable: false),
+ SlotIndex = table.Column(type: "int unsigned", nullable: false),
+ ItemTemplateId = table.Column(type: "bigint unsigned", nullable: false),
+ Quantity = table.Column(type: "int unsigned", nullable: false),
+ CreatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)"),
+ UpdatedAt = table.Column(type: "datetime(6)", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
+ .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.ComputedColumn)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_inventories", x => x.Id);
+ table.ForeignKey(
+ name: "FK_inventories_characters_CharacterId",
+ column: x => x.CharacterId,
+ principalTable: "characters",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_inventories_item_templates_ItemTemplateId",
+ column: x => x.ItemTemplateId,
+ principalTable: "item_templates",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ })
+ .Annotation("MySql:CharSet", "utf8mb4");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_accounts_Email",
+ table: "accounts",
+ column: "Email",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_accounts_Username",
+ table: "accounts",
+ column: "Username",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_characters_AccountId",
+ table: "characters",
+ column: "AccountId",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_characters_ZoneId",
+ table: "characters",
+ column: "ZoneId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_equipment_CharacterId_SlotCode",
+ table: "equipment",
+ columns: new[] { "CharacterId", "SlotCode" },
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_equipment_ItemTemplateId",
+ table: "equipment",
+ column: "ItemTemplateId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_inventories_CharacterId",
+ table: "inventories",
+ column: "CharacterId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_inventories_CharacterId_SlotIndex",
+ table: "inventories",
+ columns: new[] { "CharacterId", "SlotIndex" },
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_inventories_ItemTemplateId",
+ table: "inventories",
+ column: "ItemTemplateId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_item_templates_Name",
+ table: "item_templates",
+ column: "Name",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_sessions_AccountId",
+ table: "sessions",
+ column: "AccountId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_sessions_Token",
+ table: "sessions",
+ column: "Token",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_zones_Name",
+ table: "zones",
+ column: "Name",
+ unique: true);
+ }
+
+ ///
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "equipment");
+
+ migrationBuilder.DropTable(
+ name: "inventories");
+
+ migrationBuilder.DropTable(
+ name: "sessions");
+
+ migrationBuilder.DropTable(
+ name: "characters");
+
+ migrationBuilder.DropTable(
+ name: "item_templates");
+
+ migrationBuilder.DropTable(
+ name: "accounts");
+
+ migrationBuilder.DropTable(
+ name: "zones");
+ }
+ }
+}
diff --git a/Migrations/GameDbContextModelSnapshot.cs b/Migrations/GameDbContextModelSnapshot.cs
new file mode 100644
index 0000000..ac14ff7
--- /dev/null
+++ b/Migrations/GameDbContextModelSnapshot.cs
@@ -0,0 +1,467 @@
+//
+using System;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Metadata;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Server.Data;
+
+#nullable disable
+
+namespace SystemRevivalServer.Migrations
+{
+ [DbContext(typeof(GameDbContext))]
+ partial class GameDbContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("ProductVersion", "9.0.10")
+ .HasAnnotation("Relational:MaxIdentifierLength", 64);
+
+ MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
+
+ modelBuilder.Entity("Server.Data.Account", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("BannedUntil")
+ .HasColumnType("datetime(6)");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("Email")
+ .HasMaxLength(255)
+ .HasColumnType("varchar(255)");
+
+ b.Property("IsBanned")
+ .HasColumnType("tinyint(1)");
+
+ b.Property("PasswordHash")
+ .IsRequired()
+ .HasColumnType("varbinary(64)");
+
+ b.Property("PasswordSalt")
+ .IsRequired()
+ .HasColumnType("varbinary(32)");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.Property("Username")
+ .IsRequired()
+ .HasMaxLength(32)
+ .HasColumnType("varchar(32)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Email")
+ .IsUnique();
+
+ b.HasIndex("Username")
+ .IsUnique();
+
+ b.ToTable("accounts", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Character", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("AccountId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("AppearanceJson")
+ .HasColumnType("json");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("FacingDeg")
+ .HasColumnType("float");
+
+ b.Property("HpCurrent")
+ .HasColumnType("int unsigned");
+
+ b.Property("HpMax")
+ .HasColumnType("int unsigned");
+
+ b.Property("Level")
+ .HasColumnType("int unsigned");
+
+ b.Property("MpCurrent")
+ .HasColumnType("int unsigned");
+
+ b.Property("MpMax")
+ .HasColumnType("int unsigned");
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(32)
+ .HasColumnType("varchar(32)");
+
+ b.Property("PosX")
+ .HasColumnType("double");
+
+ b.Property("PosY")
+ .HasColumnType("double");
+
+ b.Property("PosZ")
+ .HasColumnType("double");
+
+ b.Property("StaminaCurrent")
+ .HasColumnType("int unsigned");
+
+ b.Property("StaminaMax")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatDexterity")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatEndurance")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatIntelligence")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatStrength")
+ .HasColumnType("int unsigned");
+
+ b.Property("StatWisdom")
+ .HasColumnType("int unsigned");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.Property("Xp")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("ZoneId")
+ .HasColumnType("bigint unsigned");
+
+ b.HasKey("Id");
+
+ b.HasIndex("AccountId")
+ .IsUnique();
+
+ b.HasIndex("ZoneId");
+
+ b.ToTable("characters", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.EquipmentSlot", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CharacterId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ItemTemplateId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("SlotCode")
+ .HasColumnType("tinyint unsigned");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.HasKey("Id");
+
+ b.HasIndex("ItemTemplateId");
+
+ b.HasIndex("CharacterId", "SlotCode")
+ .IsUnique();
+
+ b.ToTable("equipment", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.InventoryItem", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CharacterId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ItemTemplateId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("Quantity")
+ .HasColumnType("int unsigned");
+
+ b.Property("SlotIndex")
+ .HasColumnType("int unsigned");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.HasKey("Id");
+
+ b.HasIndex("CharacterId");
+
+ b.HasIndex("ItemTemplateId");
+
+ b.HasIndex("CharacterId", "SlotIndex")
+ .IsUnique();
+
+ b.ToTable("inventories", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.ItemTemplate", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ItemType")
+ .HasColumnType("tinyint unsigned");
+
+ b.Property("MaxStack")
+ .HasColumnType("int unsigned");
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(64)
+ .HasColumnType("varchar(64)");
+
+ b.Property("Rarity")
+ .HasColumnType("tinyint unsigned");
+
+ b.Property("StatsJson")
+ .HasColumnType("json");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.Property("Weight")
+ .HasColumnType("int unsigned");
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name")
+ .IsUnique();
+
+ b.ToTable("item_templates", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Session", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("AccountId")
+ .HasColumnType("bigint unsigned");
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("ExpiresAt")
+ .HasColumnType("datetime(6)");
+
+ b.Property("Token")
+ .IsRequired()
+ .HasMaxLength(64)
+ .HasColumnType("varchar(64)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("AccountId");
+
+ b.HasIndex("Token")
+ .IsUnique();
+
+ b.ToTable("sessions", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Zone", b =>
+ {
+ b.Property("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("bigint unsigned");
+
+ MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property("Id"));
+
+ b.Property("CreatedAt")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ b.Property("MaxX")
+ .HasColumnType("double");
+
+ b.Property("MaxY")
+ .HasColumnType("double");
+
+ b.Property("MaxZ")
+ .HasColumnType("double");
+
+ b.Property("MinX")
+ .HasColumnType("double");
+
+ b.Property("MinY")
+ .HasColumnType("double");
+
+ b.Property("MinZ")
+ .HasColumnType("double");
+
+ b.Property("Name")
+ .IsRequired()
+ .HasMaxLength(64)
+ .HasColumnType("varchar(64)");
+
+ b.Property("UpdatedAt")
+ .ValueGeneratedOnAddOrUpdate()
+ .HasColumnType("datetime(6)")
+ .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
+
+ MySqlPropertyBuilderExtensions.UseMySqlComputedColumn(b.Property("UpdatedAt"));
+
+ b.HasKey("Id");
+
+ b.HasIndex("Name")
+ .IsUnique();
+
+ b.ToTable("zones", (string)null);
+ });
+
+ modelBuilder.Entity("Server.Data.Character", b =>
+ {
+ b.HasOne("Server.Data.Account", "Account")
+ .WithMany("Characters")
+ .HasForeignKey("AccountId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Server.Data.Zone", "Zone")
+ .WithMany()
+ .HasForeignKey("ZoneId")
+ .OnDelete(DeleteBehavior.Restrict)
+ .IsRequired();
+
+ b.Navigation("Account");
+
+ b.Navigation("Zone");
+ });
+
+ modelBuilder.Entity("Server.Data.EquipmentSlot", b =>
+ {
+ b.HasOne("Server.Data.Character", "Character")
+ .WithMany()
+ .HasForeignKey("CharacterId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Server.Data.ItemTemplate", "ItemTemplate")
+ .WithMany()
+ .HasForeignKey("ItemTemplateId")
+ .OnDelete(DeleteBehavior.Restrict)
+ .IsRequired();
+
+ b.Navigation("Character");
+
+ b.Navigation("ItemTemplate");
+ });
+
+ modelBuilder.Entity("Server.Data.InventoryItem", b =>
+ {
+ b.HasOne("Server.Data.Character", "Character")
+ .WithMany()
+ .HasForeignKey("CharacterId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("Server.Data.ItemTemplate", "ItemTemplate")
+ .WithMany()
+ .HasForeignKey("ItemTemplateId")
+ .OnDelete(DeleteBehavior.Restrict)
+ .IsRequired();
+
+ b.Navigation("Character");
+
+ b.Navigation("ItemTemplate");
+ });
+
+ modelBuilder.Entity("Server.Data.Session", b =>
+ {
+ b.HasOne("Server.Data.Account", "Account")
+ .WithMany("Sessions")
+ .HasForeignKey("AccountId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Account");
+ });
+
+ modelBuilder.Entity("Server.Data.Account", b =>
+ {
+ b.Navigation("Characters");
+
+ b.Navigation("Sessions");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/Program.cs b/Program.cs
index d88dee2..56060c0 100644
--- a/Program.cs
+++ b/Program.cs
@@ -1,11 +1,19 @@
using System.Net;
using System.Net.Sockets;
using Server.Networking;
+using Microsoft.EntityFrameworkCore;
+using Server.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton();
builder.Services.AddSingleton();
var app = builder.Build();
+var cs = builder.Configuration.GetConnectionString("PrimaryMySql")
+ ?? builder.Configuration.GetSection("Database")["ConnectionString"];
+var mysqlConnectionString = ConnectionStringFactory.BuildMySqlFromEnv();
+builder.Services.AddDbContext(options =>
+ options.UseMySql(cs, ServerVersion.AutoDetect(cs)));
+
app.UseWebSockets(new WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(20),
@@ -23,6 +31,20 @@ app.Map("/ws", async (HttpContext httpContext, NetworkManager networkManager) =>
var acceptedWebSocket = await httpContext.WebSockets.AcceptWebSocketAsync();
await networkManager.AcceptWebSocketAsync(httpContext, acceptedWebSocket);
});
+// EF Core
+builder.Services.AddDbContext(options =>
+ options.UseMySql(mysqlConnectionString, ServerVersion.AutoDetect(mysqlConnectionString)));
+
+builder.Services.AddOptions()
+ .Configure(o =>
+ {
+ o.ConnectionString = mysqlConnectionString;
+ o.CommandTimeoutSeconds = int.TryParse(Environment.GetEnvironmentVariable("DB_COMMAND_TIMEOUT_S"), out var t) ? t : 30;
+ o.MaxRetryCount = int.TryParse(Environment.GetEnvironmentVariable("DB_MAX_RETRY"), out var r) ? r : 3;
+ o.RetryBaseDelayMs = int.TryParse(Environment.GetEnvironmentVariable("DB_RETRY_BASE_MS"), out var d) ? d : 100;
+ })
+ .Validate(o => !string.IsNullOrWhiteSpace(o.ConnectionString), "DB connection string required")
+ .ValidateOnStart();
// Start TCP listener
int tcpPort = int.TryParse(Environment.GetEnvironmentVariable("TCP_PORT"), out var parsed) ? parsed : 40000;
diff --git a/SystemRevivalServer.csproj b/SystemRevivalServer.csproj
index 9ff70ae..156ed45 100644
--- a/SystemRevivalServer.csproj
+++ b/SystemRevivalServer.csproj
@@ -10,7 +10,16 @@
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+
+
+
diff --git a/appsettings.json b/appsettings.json
index 10f68b8..2a44ce7 100644
--- a/appsettings.json
+++ b/appsettings.json
@@ -1,9 +1,15 @@
{
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft.AspNetCore": "Warning"
+ "Logging": {
+ "LogLevel": {
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
+ }
+ },
+ "AllowedHosts": "*",
+ "Database": {
+ "ConnectionString": "Server=a2nlmysql45plsk.secureserver.net;Port=3306;Database=Theraborn;User ID=tbadmin;Password=8N10ml9x?;Pooling=true;Minimum Pool Size=0;Maximum Pool Size=50;",
+ "CommandTimeoutSeconds": 30,
+ "MaxRetryCount": 3,
+ "RetryBaseDelayMs": 100
}
- },
- "AllowedHosts": "*"
}
diff --git a/data/ConnectionStringFactory.cs b/data/ConnectionStringFactory.cs
new file mode 100644
index 0000000..5d410ec
--- /dev/null
+++ b/data/ConnectionStringFactory.cs
@@ -0,0 +1,44 @@
+using System;
+using MySqlConnector;
+
+namespace Server.Data;
+
+public static class ConnectionStringFactory
+{
+ public static string BuildMySqlFromEnv()
+ {
+ // Prefer a full connection string if present
+ var full = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
+ if (!string.IsNullOrWhiteSpace(full)) return full;
+
+ string host = Get("DB_HOST", required: true);
+ int port = int.TryParse(Environment.GetEnvironmentVariable("DB_PORT"), out var p) ? p : 3306;
+ string db = Get("DB_NAME", required: true);
+ string user = Get("DB_USER", required: true);
+ string pass = Get("DB_PASSWORD", required: true);
+
+ var b = new MySqlConnectionStringBuilder
+ {
+ Server = host,
+ Port = (uint)port,
+ Database = db,
+ UserID = user,
+ Password = pass,
+ Pooling = true,
+ MinimumPoolSize = 0,
+ MaximumPoolSize = (uint)(int.TryParse(Environment.GetEnvironmentVariable("DB_MAX_POOL"), out var max) ? Math.Max(1, max) : 50),
+ // Optional:
+ // DefaultCommandTimeout = (uint)(int.TryParse(Environment.GetEnvironmentVariable("DB_COMMAND_TIMEOUT_S"), out var t) ? t : 30)
+ };
+
+ return b.ConnectionString;
+
+ static string Get(string name, bool required)
+ {
+ var v = Environment.GetEnvironmentVariable(name);
+ if (required && string.IsNullOrWhiteSpace(v))
+ throw new InvalidOperationException($"Missing required environment variable: {name}");
+ return v ?? "";
+ }
+ }
+}
diff --git a/data/DatabaseHealthCheck.cs b/data/DatabaseHealthCheck.cs
new file mode 100644
index 0000000..6d51ca3
--- /dev/null
+++ b/data/DatabaseHealthCheck.cs
@@ -0,0 +1,31 @@
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Extensions.Diagnostics.HealthChecks;
+using MySqlConnector;
+
+namespace Server.Data;
+
+public sealed class DatabaseHealthCheck : IHealthCheck
+{
+ private readonly DatabaseOptions _options;
+
+ public DatabaseHealthCheck(Microsoft.Extensions.Options.IOptions options)
+ => _options = options.Value;
+
+ public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
+ {
+ try
+ {
+ await using var conn = new MySqlConnection(_options.ConnectionString);
+ await conn.OpenAsync(cancellationToken);
+
+ await using var cmd = new MySqlCommand("SELECT 1;", conn);
+ var result = await cmd.ExecuteScalarAsync(cancellationToken);
+ return result is not null ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy("SELECT 1 returned null");
+ }
+ catch (System.Exception ex)
+ {
+ return new HealthCheckResult(HealthStatus.Unhealthy, "DB connection failed", ex);
+ }
+ }
+}
diff --git a/data/DatabaseManager.cs b/data/DatabaseManager.cs
new file mode 100644
index 0000000..099367b
--- /dev/null
+++ b/data/DatabaseManager.cs
@@ -0,0 +1,176 @@
+using System;
+using System.Data;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using MySqlConnector;
+
+namespace Server.Data;
+
+public sealed class DatabaseManager : IDatabaseManager, IAsyncDisposable
+{
+ private readonly DatabaseOptions _options;
+ private readonly ILogger _logger;
+
+ public DatabaseManager(IOptions options, ILogger logger)
+ {
+ _options = options.Value;
+ _logger = logger;
+ }
+
+ // ---------------------------
+ // Public API
+ // ---------------------------
+
+ public async Task ExecuteAsync(
+ Func> operation,
+ CancellationToken cancellationToken = default)
+ {
+ return await RetryAsync(async ct =>
+ {
+ await using MySqlConnection mysqlConnection = await OpenNewConnectionAsync(ct);
+ return await operation(mysqlConnection);
+ }, cancellationToken);
+ }
+
+ public async Task ExecuteAsync(
+ Func operation,
+ CancellationToken cancellationToken = default)
+ {
+ await RetryAsync(async ct =>
+ {
+ await using MySqlConnection mysqlConnection = await OpenNewConnectionAsync(ct);
+ await operation(mysqlConnection);
+ return true; // dummy value for non-generic path
+ }, cancellationToken);
+ }
+
+ public async Task WithTransactionAsync(
+ Func> operation,
+ IsolationLevel isolationLevel = IsolationLevel.ReadCommitted,
+ CancellationToken cancellationToken = default)
+ {
+ return await RetryAsync(async ct =>
+ {
+ await using MySqlConnection mysqlConnection = await OpenNewConnectionAsync(ct);
+ var transaction = await mysqlConnection.BeginTransactionAsync(isolationLevel, ct);
+
+ try
+ {
+ T result = await operation(mysqlConnection, transaction);
+ await transaction.CommitAsync(ct);
+ await DisposeTransactionAsync(transaction);
+ return result;
+ }
+ catch
+ {
+ await SafeRollbackAsync(transaction, ct);
+ await DisposeTransactionAsync(transaction);
+ throw;
+ }
+ }, cancellationToken);
+ }
+
+ public async Task WithTransactionAsync(
+ Func