⚗️ Try things out
continuous-integration/drone/push Build is passing Details

pull/110/head
Alexis Drai 2 years ago
parent fe131e00a9
commit 6c03fc4586

@ -1,4 +1,5 @@
using Data;
using Data.EF;
using Data.EF.Players;
using Model.Dice;
using Model.Dice.Faces;
@ -16,39 +17,7 @@ namespace App
{
static void Main(string[] args)
{
/*
* the DB stuff and the Model stuff are completely separate here
*
* that will change
*/
// DB stuff
// if you've run the 'dotnet' 'ef' commands, you should have a DB with 1 table, and 4 players in it
using PlayerDBManager playerDBManager = new();
// we'll add a 5th player from the App
PlayerEntity playerEntity = new Player("Ernesto").ToEntity();
try
{
playerDBManager.Add(playerEntity);
} // what if there's already a player with that name? Exception (see PlayerEntity's annotations)
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); }
try
{
IEnumerable<PlayerEntity> allPlayersFromDB = playerDBManager.GetAll();
foreach (PlayerEntity entity in allPlayersFromDB)
{
Debug.WriteLine(entity);
}
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); }
// Model stuff
// MODEL stuff
ILoader loader = new Stub();
GameRunner gameRunner;
try
@ -62,6 +31,24 @@ namespace App
gameRunner = new(new PlayerManager(), new DieManager(), null);
}
// DB stuff when the app opens
// Later, we'll use a GameDBRunner
using (PlayerDBManager playerDBManager = new(new DiceAppDbContext()))
{
// get all the players from the DB
IEnumerable<PlayerEntity> entities = playerDBManager.GetAll();
foreach (PlayerEntity entity in entities)
{
try // to persist them
{ // as models !
gameRunner.GlobalPlayerManager.Add(entity.ToModel());
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); }
}
}
string menuChoice = "nothing";
while (menuChoice != "q")
@ -159,6 +146,23 @@ namespace App
}
}
// DB stuff when the app closes
using (PlayerDBManager playerDBManager = new(new DiceAppDbContext()))
{
// get all the players from the app's memory
IEnumerable<Player> models = gameRunner.GlobalPlayerManager.GetAll();
foreach (Player model in models)
{
try // to persist them
{ // as entities !
playerDBManager.Add(model.ToEntity());
}
// what if there's already a player with that name? Exception (see PlayerEntity's annotations)
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); }
}
}
}
private static void Play(GameRunner gameRunner, string name)
@ -325,7 +329,12 @@ namespace App
gameRunner.GlobalPlayerManager.Add(player);
}
// almost no checks, this is temporary
result.Add(player);
try
{
result.Add(player);
}
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}

@ -5,7 +5,11 @@ namespace Data.EF.Players
{
public sealed class PlayerDBManager : IManager<PlayerEntity>, IDisposable
{
private readonly DiceAppDbContext db = new DiceAppDbContextWithStub();
private readonly DiceAppDbContext db;
public PlayerDBManager(DiceAppDbContext db)
{
this.db = db;
}
public void Dispose()
{

@ -12,7 +12,7 @@ namespace Data
{
GameRunner gr = new(new PlayerManager(), new DieManager());
Player player1 = new("Alice"), player2 = new("Bob"), player3 = new("Clyde");
Player player1 = new("Alice(Old Stub)"), player2 = new("Bob(Old Stub)"), player3 = new("Clyde(Old Stub)");
gr.GlobalPlayerManager.Add(player1);
gr.GlobalPlayerManager.Add(player2);

Loading…
Cancel
Save