implement startup database loading (path still hardcoded)
This commit is contained in:
parent
275e6de228
commit
63761001fa
23
Stars Assistant/Model/Game.cs
Normal file
23
Stars Assistant/Model/Game.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace StarsAssistant.Model;
|
||||
|
||||
public class Game
|
||||
{
|
||||
/// <summary>
|
||||
/// Dummy primary key, normally not used, but makes working with EF easier.
|
||||
/// </summary>
|
||||
public int GameId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The base path in which all game files reside.
|
||||
/// </summary>
|
||||
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 string BaseName { get; set; } = String.Empty;
|
||||
}
|
@ -37,7 +37,7 @@ public class StarsDatabase(string DbPath) : DbContext
|
||||
/// <summary>
|
||||
/// The record with all game metadata, will only contain a single line at all times.
|
||||
/// </summary>
|
||||
// public DbSet<Game> Game { get; set; }
|
||||
public DbSet<Game> Game { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
@ -25,19 +25,37 @@ sealed class Program
|
||||
var logger = new ConsoleLogger() { Level = LogLevel.Debug };
|
||||
Locator.CurrentMutable.RegisterConstant(logger, typeof(ILogger));
|
||||
|
||||
Services.Game g = new()
|
||||
bool newGame = false;
|
||||
string dbPath = "/home/torben/Nextcloud/Documents/Stars!/Games/goingth/GOINGTH.sqlite";
|
||||
Services.Game gameSvc;
|
||||
|
||||
using (StarsDatabase starsDB = new(dbPath))
|
||||
{
|
||||
BaseName = "GOINGTH",
|
||||
GamePath = "/home/torben/goingth/"
|
||||
};
|
||||
if (Path.Exists(dbPath))
|
||||
{
|
||||
Model.Game dbGame = starsDB.Game.First();
|
||||
gameSvc = new Services.Game(dbGame);
|
||||
}
|
||||
else
|
||||
{
|
||||
starsDB.Database.EnsureCreated();
|
||||
gameSvc = new()
|
||||
{
|
||||
BaseName = "GOINGTH",
|
||||
GamePath = "/home/torben/Nextcloud/Documents/Stars!/Games/goingth/"
|
||||
};
|
||||
gameSvc.SaveToDatabase(starsDB);
|
||||
newGame = true;
|
||||
}
|
||||
}
|
||||
|
||||
Locator.CurrentMutable.RegisterConstant(g, typeof(Services.Game));
|
||||
Locator.CurrentMutable.RegisterConstant(new Services.CSVDataLoader(), typeof(Services.CSVDataLoader));
|
||||
Locator.CurrentMutable.Register(() => new StarsDatabase(g.DatabaseFileName), typeof(StarsDatabase));
|
||||
Locator.CurrentMutable.RegisterConstant(gameSvc, typeof(Services.Game));
|
||||
Locator.CurrentMutable.RegisterConstant(new Services.CSVDataLoader(), typeof(Services.CSVDataLoader));
|
||||
Locator.CurrentMutable.Register(() => new StarsDatabase(gameSvc.DatabaseFileName), typeof(StarsDatabase));
|
||||
|
||||
if (newGame)
|
||||
__createTestData();
|
||||
|
||||
__createTestData();
|
||||
|
||||
BuildAvaloniaApp()
|
||||
.StartWithClassicDesktopLifetime(args);
|
||||
}
|
||||
@ -53,12 +71,8 @@ sealed class Program
|
||||
|
||||
public static void __createTestData ()
|
||||
{
|
||||
Services.Game game = Locator.Current.GetService<Services.Game>()!;
|
||||
using var db = Locator.Current.GetService<StarsDatabase>()!;
|
||||
|
||||
db.Database.EnsureDeleted();
|
||||
db.Database.EnsureCreated();
|
||||
|
||||
// Note: This sample requires the database to be created before running.
|
||||
// Console.WriteLine($"Database path: {db.DbPath}.");
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user