Gitea/workflows #1
@@ -19,6 +19,16 @@ public sealed class ChessEngineOptions
|
|||||||
public const string SectionName = "ChessEngine";
|
public const string SectionName = "ChessEngine";
|
||||||
|
|
||||||
public ChessEngineKind Engine { get; set; } = ChessEngineKind.Stockfish;
|
public ChessEngineKind Engine { get; set; } = ChessEngineKind.Stockfish;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Absolute path to the learned-weights file. Leave null to default to
|
||||||
|
/// <c>{ContentRoot}/chess-data/learned-weights.txt</c> (fine for local dev). In
|
||||||
|
/// production set this to a stable, service-writable location OUTSIDE the deploy
|
||||||
|
/// directory (e.g. <c>/var/lib/joshheaps/chess-data/learned-weights.txt</c>) so the
|
||||||
|
/// trained weights survive deploys and avoid deploy-user vs service-user permission
|
||||||
|
/// clashes. Override via the <c>ChessEngine__WeightsPath</c> environment variable.
|
||||||
|
/// </summary>
|
||||||
|
public string? WeightsPath { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Creates the configured <see cref="IChessEngine"/> per game.</summary>
|
/// <summary>Creates the configured <see cref="IChessEngine"/> per game.</summary>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using JoshHeaps.Net.Models;
|
using JoshHeaps.Net.Models;
|
||||||
using JoshHeaps.Net.Services.Interfaces;
|
using JoshHeaps.Net.Services.Interfaces;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace JoshHeaps.Net.Services.Implementations;
|
namespace JoshHeaps.Net.Services.Implementations;
|
||||||
|
|
||||||
@@ -16,11 +17,30 @@ public sealed class LearnedWeightsStore : ILearnedWeightsStore
|
|||||||
|
|
||||||
public string WeightsFilePath { get; }
|
public string WeightsFilePath { get; }
|
||||||
|
|
||||||
public LearnedWeightsStore(IHostEnvironment env)
|
public LearnedWeightsStore(IHostEnvironment env, IOptions<ChessEngineOptions> options, ILogger<LearnedWeightsStore> logger)
|
||||||
{
|
{
|
||||||
WeightsFilePath = Path.Combine(env.ContentRootPath, "chess-data", "learned-weights.txt");
|
// Prefer the configured path (production points this outside the deploy dir); fall
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(WeightsFilePath)!);
|
// back to the content root for local dev.
|
||||||
CustomChessEngine.NativeMethods.learned_load(WeightsFilePath);
|
var configured = options.Value.WeightsPath;
|
||||||
|
WeightsFilePath = string.IsNullOrWhiteSpace(configured)
|
||||||
|
? Path.Combine(env.ContentRootPath, "chess-data", "learned-weights.txt")
|
||||||
|
: configured;
|
||||||
|
|
||||||
|
// A missing/unwritable/misconfigured path must not take down the whole app — the
|
||||||
|
// learned engine just plays from a neutral table and can't persist training.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(WeightsFilePath)!);
|
||||||
|
CustomChessEngine.NativeMethods.learned_load(WeightsFilePath);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex,
|
||||||
|
"Could not initialize the learned-weights store at {Path}. The learned engine will " +
|
||||||
|
"play from a neutral table and training will not persist. In production set " +
|
||||||
|
"ChessEngine:WeightsPath (env ChessEngine__WeightsPath) to a service-writable directory.",
|
||||||
|
WeightsFilePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public nint CreateTrainer() => CustomChessEngine.NativeMethods.trainer_create();
|
public nint CreateTrainer() => CustomChessEngine.NativeMethods.trainer_create();
|
||||||
|
|||||||
Reference in New Issue
Block a user