400 lines
11 KiB
C#
400 lines
11 KiB
C#
// See https://aka.ms/new-console-template for more information
|
|
using Autofac;
|
|
using Autofac.Core;
|
|
using AutofacSerilogIntegration;
|
|
using CommandLine;
|
|
using GameData;
|
|
using GameData.Dummy;
|
|
using GameData.Repository;
|
|
using GameHandler;
|
|
using GameHandler.DeathGame;
|
|
using GameHandler.Extensions;
|
|
using GameModel;
|
|
using GameModel.Contract;
|
|
using GameModel.Contracts;
|
|
using GameModel.DeathGame;
|
|
using GameModel.Settings;
|
|
using KoogleCli.Model;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Serilog;
|
|
using Spectre.Console;
|
|
using Spectre.Console.Json;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
|
|
const string RootLifetimeTag = "MyIsolatedRoot";
|
|
|
|
|
|
|
|
AnsiConsole.MarkupLine("Welcome to [green]koogle[/]");
|
|
|
|
|
|
var container = Register();
|
|
|
|
//var serviceCollection = new ServiceCollection();
|
|
//serviceCollection.AddLogging();
|
|
|
|
var loggerFactory = container.Resolve<ILoggerFactory>();
|
|
loggerFactory.AddSerilog();
|
|
|
|
|
|
var scope = container.BeginLifetimeScope(RootLifetimeTag, b =>
|
|
{
|
|
//b.Populate(serviceCollection, RootLifetimeTag);
|
|
});
|
|
|
|
|
|
Autofac.IContainer Register()
|
|
{
|
|
var builder = new ContainerBuilder();
|
|
|
|
RegisterAppsettings(builder);
|
|
|
|
RegisterLogging(builder);
|
|
|
|
builder.RegisterType<ApiClient>();
|
|
builder.RegisterType<ExpenseRepository>().As<IExpenseRepository>().InstancePerLifetimeScope();
|
|
builder.RegisterType<GameRepository>().As<IGameRepository>().InstancePerLifetimeScope();
|
|
return builder.Build();
|
|
}
|
|
|
|
|
|
ShowMainMenu();
|
|
async Task ShowMainMenu()
|
|
{
|
|
do
|
|
{
|
|
var option = AnsiConsole.Prompt(
|
|
new SelectionPrompt<Option>()
|
|
.Title("Was willst du tun?")
|
|
.PageSize(10)
|
|
.MoreChoicesText("[grey](Move up and down)[/]")
|
|
.AddChoices(new[] {
|
|
new Option("Neues Spiel",NewGameAction),
|
|
new Option("Stammdaten", MasterDataAction),
|
|
new Option("Fortsetzen", ContinueGame),
|
|
new Option("Beenden", null)
|
|
}));
|
|
option.Action?.Invoke();
|
|
|
|
if (option.Name == "Beenden")
|
|
{
|
|
break;
|
|
}
|
|
} while (true);
|
|
}
|
|
|
|
void ContinueGame()
|
|
{
|
|
StartGameAction("", new Guid("4dd49b5e-7060-467a-8f79-a2e96b1b4c32"));
|
|
}
|
|
|
|
void MasterDataAction()
|
|
{
|
|
Console.WriteLine("todo");
|
|
}
|
|
|
|
GameService _gs;
|
|
|
|
void NewGameAction()
|
|
{
|
|
var option = string.Empty;
|
|
//do
|
|
//{
|
|
var games = new GameService(null).GetGameHandler().Keys.Order().ToList();
|
|
games.Add("Abbrechen");
|
|
|
|
option = AnsiConsole.Prompt(
|
|
new SelectionPrompt<string>()
|
|
.Title("Was willst du tun?")
|
|
.PageSize(10)
|
|
.MoreChoicesText("[grey](Move up and down)[/]")
|
|
.AddChoices(games));
|
|
|
|
//if (option == "Abbrechen")
|
|
//{
|
|
// break;
|
|
//}
|
|
//} while (true);
|
|
|
|
if (option == "Abbrechen")
|
|
{
|
|
ShowMainMenu();
|
|
}
|
|
else
|
|
{
|
|
StartGameAction(option, Guid.Empty);
|
|
}
|
|
}
|
|
|
|
void StartGameAction(string gameName, Guid gameId)
|
|
{
|
|
_gs = new GameService(container);
|
|
GameState gameState = null;
|
|
|
|
if (gameId.Equals(Guid.Empty))
|
|
{
|
|
var starttask = _gs.Start(new[] { 1, 2, 3, 4 }, GetGameSettings(gameName), gameName);
|
|
gameState = starttask.Result;
|
|
} else
|
|
{
|
|
var starttask = _gs.Load(gameId);
|
|
gameState = starttask.Result;
|
|
}
|
|
|
|
if (gameState == null)
|
|
{
|
|
Console.WriteLine($"game with id {gameId} not found");
|
|
ShowMainMenu();
|
|
return;
|
|
}
|
|
|
|
Show(gameState);
|
|
|
|
do
|
|
{
|
|
var proptText = _gs.FreePlayerSelection ? $"Spieler,Wurf [yellow]{gameState.ThrowState.ThrowCount + 1}[/]:" : $"Wurf [yellow]{gameState.ThrowState.ThrowCount + 1}[/]";
|
|
|
|
var stringData = AnsiConsole.Prompt(
|
|
new TextPrompt<string>(proptText)
|
|
.PromptStyle("green")
|
|
.ValidationErrorMessage("[red]That's not a valid throw[/]")
|
|
.Validate(data =>
|
|
{
|
|
try
|
|
{
|
|
_gs.ParseThrowData(data);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}));
|
|
|
|
var throwData = _gs.ParseThrowData(stringData);
|
|
if (throwData.Abort)
|
|
{
|
|
_gs.Stop();
|
|
break;
|
|
}
|
|
|
|
if (throwData.Undo)
|
|
{
|
|
gameState = _gs.Undo();
|
|
Show(gameState);
|
|
continue;
|
|
}
|
|
|
|
if (throwData.Redo)
|
|
{
|
|
gameState = _gs.Redo();
|
|
Show(gameState);
|
|
continue;
|
|
}
|
|
|
|
var task = _gs.HandleThrow(PinThrow.Create(throwData.Pindata, throwData.Bell, throwData.Sink, throwData.PlayerId));
|
|
gameState = task.Result;
|
|
Show(gameState);
|
|
|
|
} while (true);
|
|
|
|
ShowMainMenu();
|
|
}
|
|
|
|
IGameSettings GetGameSettings(string gameName)
|
|
{
|
|
if (gameName == DeathGameHandler.GAMENAME_DEATHBOX)
|
|
{
|
|
return new DeathGameSettings(6);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void Show(GameState gameState)
|
|
{
|
|
AnsiConsole.Clear();
|
|
|
|
ShowPlayer();
|
|
|
|
ShowThrow(gameState);
|
|
ShowBoard(gameState.ThrowState.BoardState);
|
|
|
|
|
|
var json = new JsonText(_gs.GetJson());
|
|
AnsiConsole.Write(
|
|
new Panel(json)
|
|
.Header("Datamodel:")
|
|
.Collapse()
|
|
.RoundedBorder()
|
|
.BorderColor(Color.Yellow));
|
|
}
|
|
|
|
void ShowThrow(GameState gameState)
|
|
{
|
|
int throwCount = gameState.ThrowState.ThrowCount;
|
|
int throwsPerRount = gameState.ThrowState.ThrowsPerRount;
|
|
Text textThrows;
|
|
if (throwsPerRount == IGameHandler.INFINIT_THROWS)
|
|
{
|
|
textThrows = new Text($"Würfe: {throwCount}", new Style(Color.Red, Color.Black));
|
|
}
|
|
else
|
|
{
|
|
textThrows = new Text($"Würfe: {throwCount}/{throwsPerRount}", new Style(Color.Red, Color.Black));
|
|
}
|
|
var textMode = new Text($"Modus: {_gs.ThrowModeName}", new Style(Color.Red, Color.Black));
|
|
|
|
// Create three text elements
|
|
var textPlayer = new Text($"Spieler: {gameState.NextPlayerId}", new Style(Color.Green, Color.Black));
|
|
var textGametime = new Text("Spielzeit: ", new Style(Color.Blue, Color.Black));
|
|
|
|
// Insert padded elements within single-row grid
|
|
var grid = new Grid();
|
|
|
|
grid.AddColumn();
|
|
grid.AddColumn();
|
|
grid.AddColumn();
|
|
|
|
grid.AddRow(textMode);
|
|
grid.AddRow(textThrows, textPlayer, textGametime);
|
|
|
|
// Write grid and it's padded contents to the Console
|
|
AnsiConsole.Write(grid);
|
|
}
|
|
|
|
void ShowBoard(BoardState board)
|
|
{
|
|
var grid = new Grid();
|
|
|
|
grid.AddColumn();
|
|
grid.AddColumn();
|
|
grid.AddColumn();
|
|
grid.AddColumn();
|
|
grid.AddColumn();
|
|
|
|
Text text1, text2, text3, text4, text5;
|
|
|
|
GetRow(1, out text1, out text2, out text3, out text4, out text5, board);
|
|
grid.AddRow(text1, text2, text3, text4, text5);
|
|
|
|
GetRow(2, out text1, out text2, out text3, out text4, out text5, board);
|
|
grid.AddRow(text1, text2, text3, text4, text5);
|
|
|
|
GetRow(3, out text1, out text2, out text3, out text4, out text5, board);
|
|
grid.AddRow(text1, text2, text3, text4, text5);
|
|
|
|
GetRow(4, out text1, out text2, out text3, out text4, out text5, board);
|
|
grid.AddRow(text1, text2, text3, text4, text5);
|
|
|
|
GetRow(5, out text1, out text2, out text3, out text4, out text5, board);
|
|
grid.AddRow(text1, text2, text3, text4, text5);
|
|
|
|
// Write grid and it's padded contents to the Console
|
|
AnsiConsole.Write(grid);
|
|
}
|
|
|
|
void ShowPlayer()
|
|
{
|
|
|
|
};
|
|
|
|
static void GetRow(int row, out Text text1, out Text text2, out Text text3, out Text text4, out Text text5, BoardState board)
|
|
{
|
|
string txt1 = "0";
|
|
string txt2 = "0";
|
|
string txt3 = "0";
|
|
string txt4 = "0";
|
|
string txt5 = "0";
|
|
if (row == 1)
|
|
{
|
|
txt1 = " ";
|
|
txt2 = " ";
|
|
txt3 = board.PinPicture.PinState1 == PinState.Down ? "0" : "1";
|
|
txt4 = " ";
|
|
txt5 = " ";
|
|
}
|
|
if (row == 2)
|
|
{
|
|
txt1 = " ";
|
|
txt2 = board.PinPicture.PinState2 == PinState.Down ? "0" : "2";
|
|
txt3 = " ";
|
|
txt4 = board.PinPicture.PinState3 == PinState.Down ? "0" : "3";
|
|
txt5 = " ";
|
|
}
|
|
if (row == 3)
|
|
{
|
|
txt1 = board.PinPicture.PinState4 == PinState.Down ? "0" : "4";
|
|
txt2 = " ";
|
|
txt3 = board.PinPicture.PinState5 == PinState.Down ? "0" : "5";
|
|
txt4 = " ";
|
|
txt5 = board.PinPicture.PinState6 == PinState.Down ? "0" : "6";
|
|
}
|
|
if (row == 4)
|
|
{
|
|
txt1 = " ";
|
|
txt2 = board.PinPicture.PinState7 == PinState.Down ? "0" : "7";
|
|
txt3 = " ";
|
|
txt4 = board.PinPicture.PinState8 == PinState.Down ? "0" : "8";
|
|
txt5 = " ";
|
|
|
|
}
|
|
if (row == 5)
|
|
{
|
|
txt1 = " ";
|
|
txt2 = " ";
|
|
txt3 = board.PinPicture.PinState9 == PinState.Down ? "0" : "9";
|
|
txt4 = " ";
|
|
txt5 = " ";
|
|
}
|
|
// Create three text elements
|
|
text1 = new Text(txt1, new Style(txt1 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
|
|
text2 = new Text(txt2, new Style(txt2 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
|
|
text3 = new Text(txt3, new Style(txt3 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
|
|
text4 = new Text(txt4, new Style(txt4 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
|
|
text5 = new Text(txt5, new Style(txt5 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
|
|
|
|
// Apply padding to the three text elements
|
|
//padCol1 = new Padder(text1).PadRight(0).PadBottom(0).PadTop(4);
|
|
//padCol2 = new Padder(text2); //.PadBottom(0).PadTop(2);
|
|
//pacCol3 = new Padder(text3); //.PadLeft(16).PadBottom(0).PadTop(0);
|
|
//padCol4 = new Padder(text4); //.PadLeft(16).PadBottom(0).PadTop(0);
|
|
//padCol5 = new Padder(text5); //.PadLeft(16).PadBottom(0).PadTop(0);
|
|
}
|
|
|
|
static void RegisterAppsettings(ContainerBuilder builder)
|
|
{
|
|
var configurationBuilder = new ConfigurationBuilder()
|
|
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
//.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)
|
|
//.AddEnvironmentVariables()
|
|
//.AddCommandLine(args);
|
|
;
|
|
|
|
// Add your custom configuration provider here if needed
|
|
// .Add(new CustomConfigurationSource());
|
|
|
|
IConfiguration configuration = configurationBuilder.Build();
|
|
var someSettings = configuration.GetSection(typeof(AppSettings).Name).Get<AppSettings>();
|
|
builder.RegisterInstance(someSettings);
|
|
}
|
|
|
|
static void RegisterLogging(ContainerBuilder builder)
|
|
{
|
|
string basedir = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
// create root logger
|
|
Log.Logger = new LoggerConfiguration()
|
|
.WriteTo.File(basedir + "/Logs/log-.txt", rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
// https://stackoverflow.com/questions/41414796/how-to-get-microsoft-extensions-loggingt-in-console-application-using-serilog
|
|
builder.RegisterInstance(new LoggerFactory())
|
|
.As<ILoggerFactory>();
|
|
builder.RegisterGeneric(typeof(Logger<>))
|
|
.As(typeof(ILogger<>))
|
|
.SingleInstance();
|
|
} |