This commit is contained in:
Christian Kauer 2023-12-24 13:28:30 +01:00
parent 670dfcc438
commit a6508da7a5
5 changed files with 81 additions and 5 deletions

View File

@ -21,17 +21,32 @@ namespace GameHandler.GameHandler
public int GetCurrentPlayerId(IGameModel gameModel)
{
var gm = gameModel as FreeGameModel;
return gm.PlayerIds.First();
return gm.LastPlayerId;
}
public IGameModel InitGameModel(int[] playerIds, IGameSettings gameSettings)
{
return new FreeGameModel(playerIds);
var throws = new List<FreeGameThrow>(playerIds.Select(_ => FreeGameThrow.Create(_))).ToArray();
return new FreeGameModel(playerIds, playerIds.First(), throws);
}
public IGameModel Update(PinThrow pinThrow, IGameModel gameModel, BoardState boardStateBeforeUpdate)
{
return gameModel; // TODO update model
var gm = gameModel as FreeGameModel;
var throws = gm.Throws.ToList();
var model = throws.FirstOrDefault(_ => _.PlayerId == pinThrow.PlayerId);
if (model == null)
{
model = FreeGameThrow.Create(pinThrow.PlayerId);
throws.Add(model);
}
var model2 = model + pinThrow;
var idx = throws.IndexOf(model);
throws.Remove(model);
throws.Insert(idx, model2);
return gm with { LastPlayerId = pinThrow.PlayerId, Throws = throws.ToArray() };
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace GameModel.FreeGame
{
public record FreeGameModel(int[] PlayerIds) : IGameModel
public record FreeGameModel(int[] PlayerIds, int LastPlayerId, FreeGameThrow[] Throws) : IGameModel
{
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameModel.FreeGame
{
public record FreeGameThrow(int PlayerId, int PinCount, int CircleCount, int SinkCount, int StrikeCount, int ClearedCount, int ThrowCount, int BellCount)
{
public static FreeGameThrow Create(int playerId)
{
return new FreeGameThrow(playerId, 0, 0, 0, 0, 0, 0, 0);
}
public static FreeGameThrow operator +(FreeGameThrow model, PinThrow pinThrow)
{
if (model.PlayerId != pinThrow.PlayerId)
{
throw new FreeGameThrowExcepetion("invalid Player id");
}
return model with
{
PinCount = model.PinCount + pinThrow.PinCount,
BellCount = model.BellCount + (pinThrow.IsBell ? 1 : 0),
CircleCount = model.CircleCount + (pinThrow.IsCircle ? 1 : 0),
SinkCount = model.SinkCount + (pinThrow.IsSink ? 1 : 0),
StrikeCount = model.StrikeCount + (pinThrow.IsSink ? 1 : 0),
ThrowCount = model.ThrowCount + 1,
ClearedCount = model.ClearedCount + ( pinThrow.IsCleared ? 1 : 0),
};
}
}
}

View File

@ -0,0 +1,20 @@
using GameModel.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameModel.FreeGame
{
public class FreeGameThrowExcepetion : KoogleException
{
public FreeGameThrowExcepetion(string message) : base(message)
{
}
public FreeGameThrowExcepetion(string message, Exception innerException) : base(message, innerException)
{
}
}
}

View File

@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KoogleCli", "KoogleCli\Koog
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameData", "GameData\GameData.csproj", "{D026F84B-06F5-4BA4-8AB7-D1D385F0611C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{5DC1FCEB-EF9B-4BFD-9AEA-56B6B81E204B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -49,6 +51,10 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E2F3CE36-0051-4C9A-B3FF-0BB44292B756} = {5DC1FCEB-EF9B-4BFD-9AEA-56B6B81E204B}
{C752388E-815A-4911-AC75-B6C27337D81A} = {5DC1FCEB-EF9B-4BFD-9AEA-56B6B81E204B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {00DAFA57-4F14-4807-886E-392D4E67BA46}
EndGlobalSection