KoogleV4/KoogleCli/Program.cs

268 lines
7.6 KiB
C#

// See https://aka.ms/new-console-template for more information
using CommandLine;
using GameHandler;
using GameHandler.Extensions;
using GameModel;
using GameModel.Contract;
using KoogleCli.Model;
using Spectre.Console;
using static System.Runtime.InteropServices.JavaScript.JSType;
AnsiConsole.MarkupLine("Welcome to [green]koogle[/]");
ShowMainMenu();
void 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("Beenden", null)
}));
option.Action?.Invoke();
if (option.Name == "Beenden")
{
break;
}
} while (true);
}
void MasterDataAction()
{
Console.WriteLine("todo");
}
GameService _gs;
void NewGameAction()
{
var option = string.Empty;
//do
//{
var games = new GameService().GetGameHandler().Keys.Order().ToList();
games.Add("Abbreche");
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);
}
}
void StartGameAction(string gameName)
{
_gs = new GameService();
var bs = _gs.Start(new[] { 1,2,3,4}, null, gameName );
Show(bs);
do
{
var proptText = _gs.FreePlayerSelection ? $"Spieler,Wurf [yellow]{bs.ThrowCount + 1}[/]:" : $"Wurf [yellow]{bs.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;
}
bs = _gs.HandleThrow(PinThrow.Create(throwData.Pindata, throwData.Bell, throwData.Sink, throwData.PlayerId));
Show(bs);
} while (true);
ShowMainMenu();
}
void Show(ThrowState throwState)
{
AnsiConsole.Clear();
ShowPlayer();
ShowThrow(throwState);
ShowBoard(throwState.BoardState);
}
void ShowThrow(ThrowState throwState)
{
int throwCount = throwState.ThrowCount;
int throwsPerRount = throwState.ThrowsPerRount;
Text text1;
if (throwsPerRount == IGameHandler.INFINIT_THROWS)
{
text1 = new Text($"Würfe: {throwCount}", new Style(Color.Red, Color.Black));
}
else
{
text1 = new Text($"Würfe: {throwCount}/{throwsPerRount}", new Style(Color.Red, Color.Black));
}
var text1_2 = new Text($"Modus: {_gs.ThrowModeName}", new Style(Color.Red, Color.Black));
// Create three text elements
var paddedText_II = new Text("Spieler: ", new Style(Color.Green, Color.Black));
var paddedText_III = new Text("Spielzeit: ", new Style(Color.Blue, Color.Black));
// Apply padding to the three text elements
var pad1_1 = new Padder(text1).PadRight(16).PadBottom(0).PadTop(2);
var pad1_2 = new Padder(text1_2).PadRight(0).PadBottom(0).PadTop(2);
var pad_II = new Padder(paddedText_II).PadBottom(0).PadTop(2);
var pad_III = new Padder(paddedText_III).PadLeft(16).PadBottom(0).PadTop(0);
// Insert padded elements within single-row grid
var grid = new Grid();
grid.AddColumn();
grid.AddColumn();
grid.AddColumn();
grid.AddRow(pad1_2);
grid.AddRow(pad1_1, pad_II, pad_III);
// 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();
Padder pad_1, pad_2, pad_3, pad_4, pad_5;
GetRow(1, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(2, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(3, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(4, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
GetRow(5, out pad_1, out pad_2, out pad_3, out pad_4, out pad_5, board);
grid.AddRow(pad_1, pad_2, pad_3, pad_4, pad_5);
// Write grid and it's padded contents to the Console
AnsiConsole.Write(grid);
}
void ShowPlayer()
{
};
static void GetRow(int row, out Padder pad_1, out Padder pad_2, out Padder pad_3, out Padder pad_4, out Padder pad_5, 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
var paddedText_1 = new Text(txt1, new Style(txt1 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_2 = new Text(txt2, new Style(txt2 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_3 = new Text(txt3, new Style(txt3 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_4 = new Text(txt4, new Style(txt4 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
var paddedText_5 = new Text(txt5, new Style(txt5 == "0" ? Color.DarkSlateGray3 : Color.Blue, Color.Black));
// Apply padding to the three text elements
pad_1 = new Padder(paddedText_1); //.PadRight(16).PadBottom(0).PadTop(4);
pad_2 = new Padder(paddedText_2); //.PadBottom(0).PadTop(2);
pad_3 = new Padder(paddedText_3); //.PadLeft(16).PadBottom(0).PadTop(0);
pad_4 = new Padder(paddedText_4); //.PadLeft(16).PadBottom(0).PadTop(0);
pad_5 = new Padder(paddedText_5); //.PadLeft(16).PadBottom(0).PadTop(0);
}