implement startup database loading (path still hardcoded)

This commit is contained in:
2024-09-20 19:01:09 +02:00
parent 275e6de228
commit 63761001fa
4 changed files with 94 additions and 16 deletions

View File

@ -1,17 +1,58 @@
using Splat;
namespace StarsAssistant.Services;
public class Game
{
/// <summary>
/// Default constructor, initialize required fields manually, targeted
/// for Initial DB creation.
/// </summary>
public Game() {}
/// <summary>
/// Init game state from database, targeted for CLI arg.
/// </summary>
/// <param name="dbGame">Record loaded from DB</param>
public Game(Model.Game dbGame)
{
GamePath = dbGame.GamePath;
BaseName = dbGame.BaseName;
}
/// <summary>
/// Save this record in the database. Uses the service locator to access the
/// database unless you specify an instance. This is needed during initial
/// game creation, where the services are not yet established.
/// </summary>
/// <param name="db">Optional DB instance if services are yet unavailable.</param>
public void SaveToDatabase(Model.StarsDatabase? db = null)
{
db ??= Locator.Current.GetService<Model.StarsDatabase>()!;
Model.Game? dbGame = db.Game.FirstOrDefault();
if (dbGame == null)
{
dbGame = new Model.Game();
db.Add(dbGame);
db.SaveChanges();
}
dbGame.GamePath = GamePath;
dbGame.BaseName = BaseName;
db.Update(dbGame);
db.SaveChanges();
}
/// <summary>
/// The base path in which all game files reside.
/// </summary>
public required string GamePath { get; set; }
public string GamePath { get; set; } = String.Empty;
/// <summary>
/// The base name without extensions of your game, inside the GamePath folder.
/// All dependant files are resolved using this name.
/// </summary>
public required string BaseName { get; set; }
public string BaseName { get; set; } = String.Empty;
/// <summary>
/// Combine into the DatabaseName