This commit is contained in:
Christian Kauer 2023-12-29 22:04:27 +01:00
parent 2b51d3319f
commit 2756c589ae
9 changed files with 129 additions and 13 deletions

View File

@ -282,6 +282,56 @@ namespace GameData
return res.Data;
}
public async Task<T> GetSingle<T>(string serviceUrl, bool suppressExceptions = false)
{
WrapperSingle<T> res = default;
try
{
_logger.LogDebug($"calling service url {serviceUrl}");
var response = await client.SendAsync(GetHttpRequestMessage(HttpMethod.Get, serviceUrl));
if (response.IsSuccessStatusCode)
{
using (var responseStream = response.Content.ReadAsStream())
{
using (var streamReader = new StreamReader(responseStream))
{
string content;
if (typeof(T).Equals(typeof(string)))
{
content = streamReader.ReadToEnd();
res = (WrapperSingle<T>)Convert.ChangeType(content, typeof(T));
}
else
{
using (var jsonTextReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
res = serializer.Deserialize<WrapperSingle<T>>(jsonTextReader);
}
}
}
}
}
else
{
var errorResult = await response.Content.ReadAsStringAsync();
_logger.LogError("error getting from service '{service}' with code '{statuscode}' - Result: '{result}'", serviceUrl, response.StatusCode, errorResult);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "error at Get");
if (!suppressExceptions)
{
throw ex;
}
}
return res.Data;
}
internal async Task Put<T>(object payload, string serviceUrl, bool suppressExceptions = false)
{
T res = default;

View File

@ -32,5 +32,10 @@ namespace GameData.Dummy
{
throw new NotImplementedException();
}
public Task<Game> LoadGame(Guid gameId)
{
throw new NotImplementedException();
}
}
}

View File

@ -58,5 +58,10 @@ namespace GameData.Repository
{
return await _client.Post<Game>(game, UrlGame);
}
public async Task<Game> LoadGame(Guid gameId)
{
return await _client.GetSingle<Game>(UrlGame + "/" + gameId);
}
}
}

View File

@ -153,6 +153,21 @@ namespace GameHandler
return ThrowCommandParser.Parse(stringData, _gh.FreePlayerSelection(), 0);
}
public async Task<GameState> Load(Guid gameId)
{
Game game = null;
using (var scope = _rootContainer?.BeginLifetimeScope())
{
var gs = new GameStateHandler(_scope.Resolve<IGameRepository>(), _scope.Resolve<IExpenseRepository>());
game = await gs.LoadGame(gameId);
}
_game = game;
InitGameServiceBehaviour(game.GameName);
var res = await _gameStateHandler.LoadState(game.Id);
return res;
}
public bool FreePlayerSelection => _gh.FreePlayerSelection();
}
}

View File

@ -118,5 +118,15 @@ namespace GameHandler
{
return await _gameRepository.Create(null);
}
internal Task<GameState> LoadState(Guid gameId)
{
throw new NotImplementedException();
}
internal async Task<Game?> LoadGame(Guid gameId)
{
return await _gameRepository.LoadGame(gameId);
}
}
}

View File

@ -10,6 +10,7 @@ namespace GameModel.Contracts
{
Task<Game> Create(Game game);
GameState Load(Guid gameId);
Task<Game> LoadGame(Guid gameId);
Task<GameState> Save(GameState gameState);
Task Update(Game game);
}

View File

@ -6,11 +6,11 @@ using System.Threading.Tasks;
namespace GameModel
{
public record Game(Guid Id)
public record Game(Guid Id, GameStatus GameStatus, string GameName)
{
public static Game Create()
public static Game Create(string gameName)
{
return new Game(Guid.NewGuid());
return new Game(Guid.NewGuid(), GameStatus.Running, gameName);
}
}
}

15
GameModel/GameStatus.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameModel
{
public enum GameStatus
{
Running,
Finnished,
Aborted
}
}

View File

@ -74,6 +74,7 @@ async Task ShowMainMenu()
.AddChoices(new[] {
new Option("Neues Spiel",NewGameAction),
new Option("Stammdaten", MasterDataAction),
new Option("Fortsetzen", ContinueGame),
new Option("Beenden", null)
}));
option.Action?.Invoke();
@ -85,6 +86,11 @@ async Task ShowMainMenu()
} while (true);
}
void ContinueGame()
{
StartGameAction("", new Guid("597956fe-6d83-424f-865f-5856b63f82c9"));
}
void MasterDataAction()
{
Console.WriteLine("todo");
@ -98,7 +104,7 @@ void NewGameAction()
//do
//{
var games = new GameService(null).GetGameHandler().Keys.Order().ToList();
games.Add("Abbreche");
games.Add("Abbrechen");
option = AnsiConsole.Prompt(
new SelectionPrompt<string>()
@ -119,21 +125,30 @@ void NewGameAction()
}
else
{
StartGameAction(option);
StartGameAction(option, Guid.Empty);
}
}
void StartGameAction(string gameName)
void StartGameAction(string gameName, Guid gameId)
{
_gs = new GameService(container);
var starttask = _gs.Start(new[] { 1, 2, 3, 4 }, GetGameSettings(gameName), gameName);
var bs = starttask.Result;
GameState gameState = null;
Show(bs);
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;
}
Show(gameState);
do
{
var proptText = _gs.FreePlayerSelection ? $"Spieler,Wurf [yellow]{bs.ThrowState.ThrowCount + 1}[/]:" : $"Wurf [yellow]{bs.ThrowState.ThrowCount + 1}[/]";
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)
@ -160,8 +175,8 @@ void StartGameAction(string gameName)
}
var task = _gs.HandleThrow(PinThrow.Create(throwData.Pindata, throwData.Bell, throwData.Sink, throwData.PlayerId));
bs = task.Result;
Show(bs);
gameState = task.Result;
Show(gameState);
} while (true);