Add rules-aware lookup dice rolling

This commit is contained in:
2026-03-15 02:47:10 +01:00
parent 74613724bc
commit cada74c7ac
14 changed files with 540 additions and 8 deletions

View File

@@ -1,3 +1,4 @@
using System;
using Microsoft.EntityFrameworkCore;
namespace RolemasterDb.App.Data;
@@ -6,6 +7,8 @@ public static class RolemasterDbSchemaUpgrader
{
public static async Task EnsureLatestAsync(RolemasterDbContext dbContext, CancellationToken cancellationToken = default)
{
await EnsureAttackTableFumbleColumnsAsync(dbContext, cancellationToken);
await dbContext.Database.ExecuteSqlRawAsync(
"""
CREATE TABLE IF NOT EXISTS "CriticalBranches" (
@@ -87,4 +90,65 @@ public static class RolemasterDbSchemaUpgrader
""",
cancellationToken);
}
private static async Task EnsureAttackTableFumbleColumnsAsync(RolemasterDbContext dbContext, CancellationToken cancellationToken)
{
if (!await ColumnExistsAsync(dbContext, "AttackTables", "FumbleMinRoll", cancellationToken))
{
await dbContext.Database.ExecuteSqlRawAsync(
"""
ALTER TABLE "AttackTables"
ADD COLUMN "FumbleMinRoll" INTEGER NULL;
""",
cancellationToken);
}
if (!await ColumnExistsAsync(dbContext, "AttackTables", "FumbleMaxRoll", cancellationToken))
{
await dbContext.Database.ExecuteSqlRawAsync(
"""
ALTER TABLE "AttackTables"
ADD COLUMN "FumbleMaxRoll" INTEGER NULL;
""",
cancellationToken);
}
}
private static async Task<bool> ColumnExistsAsync(
RolemasterDbContext dbContext,
string tableName,
string columnName,
CancellationToken cancellationToken)
{
var connection = dbContext.Database.GetDbConnection();
var shouldClose = connection.State != System.Data.ConnectionState.Open;
if (shouldClose)
{
await connection.OpenAsync(cancellationToken);
}
try
{
await using var command = connection.CreateCommand();
command.CommandText = $"PRAGMA table_info(\"{tableName}\");";
await using var reader = await command.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
if (string.Equals(reader["name"]?.ToString(), columnName, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
finally
{
if (shouldClose)
{
await connection.CloseAsync();
}
}
}
}