@ -7,15 +7,19 @@ using Model.Games;
using Model.Players ;
using Model.Players ;
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.Diagnostics ;
using System.Diagnostics ;
using System.Drawing ;
using System.Drawing ;
using System.Linq ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
namespace App
namespace App
{
{
internal static class Program
internal static class Program
{
{
private static NLog . Logger logger = NLog . LogManager . GetCurrentClassLogger ( ) ;
static async Task Main ( string [ ] args )
static async Task Main ( string [ ] args )
{
{
// MODEL stuff
// MODEL stuff
@ -27,9 +31,8 @@ namespace App
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
Console . WriteLine ( ex . Message ) ;
logger . Warn ( ex ) ;
Console . WriteLine ( ex . StackTrace ) ;
masterOfCeremonies = new ( new PlayerManager ( ) , new DiceGroupManager ( ) , new GameManager ( ) ) ;
masterOfCeremonies = new ( new PlayerManager ( ) , new DiceGroupManager ( ) , null ) ;
}
}
try
try
@ -40,9 +43,8 @@ namespace App
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// get all the players from the DB
// get all the players from the DB
IEnumerable < PlayerEntity > entities = db . Players ;
PlayerDbManager playerDbManager = new ( db ) ;
IEnumerable < PlayerEntity > entities = await playerDbManager . GetAll ( ) ;
Debug . WriteLine ( "Loading players" ) ;
foreach ( PlayerEntity entity in entities )
foreach ( PlayerEntity entity in entities )
{
{
@ -50,13 +52,12 @@ namespace App
{
{
// persist them as models !
// persist them as models !
await masterOfCeremonies . GlobalPlayerManager . Add ( entity . ToModel ( ) ) ;
await masterOfCeremonies . GlobalPlayerManager . Add ( entity . ToModel ( ) ) ;
Debug . WriteLine ( $"{entity.ID} -- {entity.Name}" ) ;
}
}
catch ( Exception ex ) { Debug . WriteLine ( $"{ex.Message}\n... Never mind" ) ; }
catch ( Exception ex ) { Debug . WriteLine ( $"{ex.Message}\n... Never mind" ) ; }
}
}
}
}
}
}
catch ( Exception ex ) { Debug . WriteLine ( $"{ex.Message}\n... Couldn't use the database" ) ; }
catch ( Exception ex ) { Console . WriteLine ( $"{ex.Message}\n... Couldn't use the database" ) ; }
string menuChoice = "nothing" ;
string menuChoice = "nothing" ;
@ -152,7 +153,7 @@ namespace App
newGroupDice . Add ( die ) ;
newGroupDice . Add ( die ) ;
}
}
}
}
await masterOfCeremonies . DiceGroupManager . Add ( new KeyValuePair< string , IEnumerable < Die > > ( newGroupName , newGroupDice ) ) ;
await masterOfCeremonies . DiceGroupManager . Add ( new DiceGroup ( newGroupName , newGroupDice ) ) ;
break ;
break ;
case "p" :
case "p" :
@ -184,22 +185,21 @@ namespace App
// create a PlayerDbManager (and inject it with the DB)
// create a PlayerDbManager (and inject it with the DB)
PlayerDbManager playerDbManager = new ( db ) ;
PlayerDbManager playerDbManager = new ( db ) ;
Debug . WriteLine ( "Saving players" ) ;
foreach ( Player model in models )
foreach ( Player model in models )
{
{
try // to persist them
try // to persist them
{ // as entities !
{ // as entities !
PlayerEntity entity = model . ToEntity ( ) ;
PlayerEntity entity = model . ToEntity ( ) ;
await playerDbManager . Add ( entity ) ;
await playerDbManager . Add ( entity ) ;
Debug . WriteLine ( $"{entity.ID} -- {entity.Name}" ) ;
}
}
// what if there's already a player with that name? Exception (see PlayerEntity's annotations)
// 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 ( ArgumentException ex ) { Debug . WriteLine ( $"{ex.Message}\n... Never mind" ) ; }
}
}
}
}
// flushing and closing NLog before quitting completely
NLog . LogManager . Shutdown ( ) ;
}
}
catch ( Exception ex ) { Debug . WriteLine ( $"{ex.Message}\n... Couldn't use the database" ) ; }
catch ( Exception ex ) { Console . WriteLine ( $"{ex.Message}\n... Couldn't use the database" ) ; }
}
}
private static async Task Play ( MasterOfCeremonies masterOfCeremonies , string name )
private static async Task Play ( MasterOfCeremonies masterOfCeremonies , string name )
@ -208,7 +208,7 @@ namespace App
while ( menuChoicePlay ! = "q" )
while ( menuChoicePlay ! = "q" )
{
{
Game game = await masterOfCeremonies . GameManager . GetOneByName ( name ) ;
Game game = await masterOfCeremonies . GameManager . GetOneByName ( name ) ;
Console . WriteLine ( $"{ game.GetWhoPlaysNow()}'s turn\n" +
Console . WriteLine ( $"{ PlayerToString(await game.GetWhoPlaysNow() )}'s turn\n" +
"q... quit\n" +
"q... quit\n" +
"h... show history\n" +
"h... show history\n" +
"s... save\n" +
"s... save\n" +
@ -219,17 +219,14 @@ namespace App
case "q" :
case "q" :
break ;
break ;
case "h" :
case "h" :
foreach ( Turn turn in game . GetHistory ( ) )
foreach ( Turn turn in game . GetHistory ( ) ) { Console . WriteLine ( TurnToString ( turn ) ) ; }
{
Console . WriteLine ( turn ) ;
}
break ;
break ;
case "s" :
case "s" :
await masterOfCeremonies . GameManager . Add ( game ) ;
await masterOfCeremonies . GameManager . Add ( game ) ;
break ;
break ;
default :
default :
await MasterOfCeremonies . PlayGame ( game ) ;
await MasterOfCeremonies . PlayGame ( game ) ;
Console . WriteLine ( game. GetHistory ( ) . Last ( ) ) ;
Console . WriteLine ( TurnToString( game. GetHistory ( ) . Last ( ) ) ) ;
break ;
break ;
}
}
}
}
@ -241,7 +238,7 @@ namespace App
Console . WriteLine ( "which of these games?\n(choose by name)\n>" ) ;
Console . WriteLine ( "which of these games?\n(choose by name)\n>" ) ;
foreach ( Game game in await masterOfCeremonies . GameManager . GetAll ( ) )
foreach ( Game game in await masterOfCeremonies . GameManager . GetAll ( ) )
{
{
Console . WriteLine ( game) ;
Console . WriteLine ( GameToString( game) ) ;
}
}
name = Console . ReadLine ( ) ;
name = Console . ReadLine ( ) ;
return name ;
return name ;
@ -252,15 +249,15 @@ namespace App
Console . WriteLine ( "Look at all them players!" ) ;
Console . WriteLine ( "Look at all them players!" ) ;
foreach ( Player player in await masterOfCeremonies . GlobalPlayerManager . GetAll ( ) )
foreach ( Player player in await masterOfCeremonies . GlobalPlayerManager . GetAll ( ) )
{
{
Console . WriteLine ( player) ;
Console . WriteLine ( PlayerToString( player) ) ;
}
}
}
}
private static async Task ShowDice ( MasterOfCeremonies masterOfCeremonies )
private static async Task ShowDice ( MasterOfCeremonies masterOfCeremonies )
{
{
foreach ( ( string name , IEnumerable < Die > dice ) in await masterOfCeremonies . DiceGroupManager . GetAll ( ) )
foreach ( ( string name , ReadOnlyCollection < Die > dice ) in await masterOfCeremonies . DiceGroupManager . GetAll ( ) )
{
{
Console . WriteLine ( $"{name} -- {dice}" ) ;
Console . WriteLine ( $"{name} -- {dice}" ) ; // maybe code a quick and dirty DieToString()
}
}
}
}
@ -333,11 +330,13 @@ namespace App
catch ( ArgumentNullException ex )
catch ( ArgumentNullException ex )
{
{
Console . WriteLine ( ex . Message ) ;
Console . WriteLine ( ex . Message ) ;
logger . Warn ( ex ) ;
}
}
catch ( UriFormatException ex )
catch ( UriFormatException ex )
{
{
Console . WriteLine ( "that URI was not valid" ) ;
Console . WriteLine ( "that URI was not valid" ) ;
Console . WriteLine ( ex . Message ) ;
Console . WriteLine ( ex . Message ) ;
logger . Warn ( ex ) ;
}
}
}
}
}
}
@ -354,7 +353,7 @@ namespace App
if ( menuChoice . Equals ( "ok" ) & & count = = 0 )
if ( menuChoice . Equals ( "ok" ) & & count = = 0 )
{
{
Console . WriteLine ( "create at least one valid face" ) ;
Console . WriteLine ( "create at least one valid face" ) ;
menuChoice = "" ; // persist e en dehors du scope de cette fo nction
menuChoice = "" ; // persist s outside the scope of this fu nction
}
}
}
}
@ -370,7 +369,7 @@ namespace App
menuChoiceDice = Console . ReadLine ( ) ;
menuChoiceDice = Console . ReadLine ( ) ;
if ( ! menuChoiceDice . Equals ( "ok" ) )
if ( ! menuChoiceDice . Equals ( "ok" ) )
{
{
IEnumerable < Die > chosenDice = ( await masterOfCeremonies . DiceGroupManager . GetOneByName ( menuChoiceDice ) ) . Valu e;
IEnumerable < Die > chosenDice = ( await masterOfCeremonies . DiceGroupManager . GetOneByName ( menuChoiceDice ) ) . Dic e;
foreach ( Die die in chosenDice )
foreach ( Die die in chosenDice )
{
{
result . Add ( die ) ;
result . Add ( die ) ;
@ -394,7 +393,7 @@ namespace App
Player player = new ( menuChoicePlayers ) ;
Player player = new ( menuChoicePlayers ) ;
if ( ! ( await masterOfCeremonies . GlobalPlayerManager . GetAll ( ) ) . Contains ( player ) )
if ( ! ( await masterOfCeremonies . GlobalPlayerManager . GetAll ( ) ) . Contains ( player ) )
{
{
// if the player didn't exist, now it does... this is temporary
// if the player didn't exist, now it does...
await masterOfCeremonies . GlobalPlayerManager . Add ( player ) ;
await masterOfCeremonies . GlobalPlayerManager . Add ( player ) ;
}
}
// almost no checks, this is temporary
// almost no checks, this is temporary
@ -402,12 +401,57 @@ namespace App
{
{
await result . Add ( player ) ;
await result . Add ( player ) ;
}
}
catch ( ArgumentException ex ) { Debug . WriteLine ( $"{ex.Message}\n... Never mind" ) ; }
catch ( ArgumentException ex ) { Console . WriteLine ( $"{ex.Message}\n... Never mind" ) ; }
}
}
}
}
return result ;
return result ;
}
}
private static string TurnToString ( Turn turn )
{
string [ ] datetime = turn . When . ToString ( "s" , System . Globalization . CultureInfo . InvariantCulture ) . Split ( "T" ) ;
string date = datetime [ 0 ] ;
string time = datetime [ 1 ] ;
StringBuilder sb = new ( ) ;
sb . AppendFormat ( "{0} {1} -- {2} rolled:" ,
date ,
time ,
PlayerToString ( turn . Player ) ) ;
foreach ( KeyValuePair < Die , Face > kvp in turn . DiceNFaces )
{
sb . Append ( " " + kvp . Value . StringValue ) ;
}
return sb . ToString ( ) ;
}
private async static Task < string > GameToString ( Game game )
{
StringBuilder sb = new ( ) ;
sb . Append ( $"Game: {game.Name}" ) ;
sb . Append ( "\nPlayers:" ) ;
foreach ( Player player in game . PlayerManager . GetAll ( ) ? . Result )
{
sb . Append ( $" {PlayerToString(player)}" ) ;
}
sb . Append ( $"\nNext: {PlayerToString(await game.GetWhoPlaysNow())}" ) ;
sb . Append ( "\nLog:\n" ) ;
foreach ( Turn turn in game . GetHistory ( ) )
{
sb . Append ( $"\t{TurnToString(turn)}\n" ) ;
}
return sb . ToString ( ) ;
}
private static string PlayerToString ( Player player )
{
return player . Name ;
}
}
}
}
}