diff --git a/GameHandler/GameHandler/FreeGameHandler.cs b/GameHandler/GameHandler/FreeGameHandler.cs index 54a9837..4661a57 100644 --- a/GameHandler/GameHandler/FreeGameHandler.cs +++ b/GameHandler/GameHandler/FreeGameHandler.cs @@ -20,18 +20,33 @@ namespace GameHandler.GameHandler public int GetCurrentPlayerId(IGameModel gameModel) { - var gm = gameModel as FreeGameModel; - return gm.PlayerIds.First(); + var gm = gameModel as FreeGameModel; + return gm.LastPlayerId; } public IGameModel InitGameModel(int[] playerIds, IGameSettings gameSettings) { - return new FreeGameModel(playerIds); + var throws = new List(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() }; } } } diff --git a/GameModel/FreeGame/FreeGameModel.cs b/GameModel/FreeGame/FreeGameModel.cs index 637ef60..d2e23f2 100644 --- a/GameModel/FreeGame/FreeGameModel.cs +++ b/GameModel/FreeGame/FreeGameModel.cs @@ -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 { } } diff --git a/GameModel/FreeGame/FreeGameThrow.cs b/GameModel/FreeGame/FreeGameThrow.cs new file mode 100644 index 0000000..8cb3f9c --- /dev/null +++ b/GameModel/FreeGame/FreeGameThrow.cs @@ -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), + }; + } + } +} diff --git a/GameModel/FreeGame/FreeGameThrowExcepetion.cs b/GameModel/FreeGame/FreeGameThrowExcepetion.cs new file mode 100644 index 0000000..fca06c5 --- /dev/null +++ b/GameModel/FreeGame/FreeGameThrowExcepetion.cs @@ -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) + { + } + } +} diff --git a/KoogleV4.sln b/KoogleV4.sln index 7192522..970ed25 100644 --- a/KoogleV4.sln +++ b/KoogleV4.sln @@ -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