sa/Stars Assistant/Services/Game.cs

75 lines
2.4 KiB
C#
Raw Normal View History

using Splat;
2024-09-17 18:20:42 +00:00
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();
}
2024-09-17 18:20:42 +00:00
/// <summary>
/// The base path in which all game files reside.
/// </summary>
public string GamePath { get; set; } = String.Empty;
2024-09-17 18:20:42 +00:00
/// <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;
2024-09-17 18:20:42 +00:00
/// <summary>
/// Combine into the DatabaseName
/// </summary>
public string DatabaseFileName => Path.Combine(GamePath, $"{BaseName}.sqlite");
/// <summary>
/// Search for Planet files using this pattern, will give you all pxx files.
/// </summary>
public string PlanetFileSearchPattern => $"{BaseName}.p*";
/// <summary>
/// Get the name of a planet file for a given race.
/// </summary>
/// <param name="r">The race to load.</param>
/// <returns>Fully qualified file path.</returns>
public string PlanetFileForRace (Model.Race r) => Path.Combine(GamePath, $"{BaseName}.p{r.PlayerFileId}");
}