diff --git a/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameLogicService.cs b/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameLogicService.cs index 0cea287..7f21801 100644 --- a/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameLogicService.cs +++ b/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameLogicService.cs @@ -35,6 +35,10 @@ public class ChristmasTreeGameLogicService : IGameLogicService }; } + var playerIndexPerTeam = new Dictionary(); + for (int i = 0; i < options.Teams.Count; i++) + playerIndexPerTeam[i] = 0; + return new ChristmasTreeGameModel { Variant = options.Variant, @@ -43,6 +47,7 @@ public class ChristmasTreeGameLogicService : IGameLogicService Teams = options.Teams.ToList(), CurrentTeamIndex = 0, CurrentPlayerIndexInTeam = 0, + PlayerIndexPerTeam = playerIndexPerTeam, EnableContinueOnMiss = options.EnableContinueOnMiss, MaxContinueThrows = options.MaxContinueThrows, ContinueThrowsRemaining = 0, @@ -348,23 +353,16 @@ public class ChristmasTreeGameLogicService : IGameLogicService ChristmasTreeGameModel model, List triggers) { - // Rotate to next team and player - int nextTeamIndex = (model.CurrentTeamIndex + 1) % model.Teams.Count; - int nextPlayerIndex = 0; + // Advance current team's player index independently + var playerIndices = new Dictionary( + model.PlayerIndexPerTeam.Count > 0 ? model.PlayerIndexPerTeam + : Enumerable.Range(0, model.Teams.Count).ToDictionary(i => i, _ => 0)); - if (nextTeamIndex == 0) - { - // Wrapped around - increment player index within teams - // This assumes all teams have same number of players - // For varying team sizes, we track per-team player index - nextPlayerIndex = (model.CurrentPlayerIndexInTeam + 1) % - Math.Max(1, model.Teams[nextTeamIndex].PlayerIds.Count); - } - else - { - nextPlayerIndex = model.CurrentPlayerIndexInTeam % - Math.Max(1, model.Teams[nextTeamIndex].PlayerIds.Count); - } + int currentTeamSize = Math.Max(1, model.Teams[model.CurrentTeamIndex].PlayerIds.Count); + playerIndices[model.CurrentTeamIndex] = (playerIndices[model.CurrentTeamIndex] + 1) % currentTeamSize; + + int nextTeamIndex = (model.CurrentTeamIndex + 1) % model.Teams.Count; + int nextPlayerIndex = playerIndices[nextTeamIndex]; var nextPlayerId = model.Teams[nextTeamIndex].PlayerIds.Count > nextPlayerIndex ? model.Teams[nextTeamIndex].PlayerIds[nextPlayerIndex] @@ -373,7 +371,8 @@ public class ChristmasTreeGameLogicService : IGameLogicService model = model with { CurrentTeamIndex = nextTeamIndex, - CurrentPlayerIndexInTeam = nextPlayerIndex + CurrentPlayerIndexInTeam = nextPlayerIndex, + PlayerIndexPerTeam = playerIndices }; return (model, new ThrowResult @@ -643,16 +642,15 @@ public class ChristmasTreeGameLogicService : IGameLogicService } // Clear selection state and rotate to next player + var playerIndices = new Dictionary( + model.PlayerIndexPerTeam.Count > 0 ? model.PlayerIndexPerTeam + : Enumerable.Range(0, model.Teams.Count).ToDictionary(i => i, _ => 0)); + + int currentTeamSize = Math.Max(1, model.Teams[model.CurrentTeamIndex].PlayerIds.Count); + playerIndices[model.CurrentTeamIndex] = (playerIndices[model.CurrentTeamIndex] + 1) % currentTeamSize; + int nextTeamIndex = (model.CurrentTeamIndex + 1) % model.Teams.Count; - int nextPlayerIndex = model.CurrentPlayerIndexInTeam; - if (nextTeamIndex == 0) - { - nextPlayerIndex = (nextPlayerIndex + 1) % Math.Max(1, model.Teams[nextTeamIndex].PlayerIds.Count); - } - else - { - nextPlayerIndex = nextPlayerIndex % Math.Max(1, model.Teams[nextTeamIndex].PlayerIds.Count); - } + int nextPlayerIndex = playerIndices[nextTeamIndex]; var nextPlayerId = model.Teams[nextTeamIndex].PlayerIds.Count > nextPlayerIndex ? model.Teams[nextTeamIndex].PlayerIds[nextPlayerIndex] @@ -663,7 +661,8 @@ public class ChristmasTreeGameLogicService : IGameLogicService TeamTrees = teamTrees, OpponentSelectingTeamIndex = null, CurrentTeamIndex = nextTeamIndex, - CurrentPlayerIndexInTeam = nextPlayerIndex + CurrentPlayerIndexInTeam = nextPlayerIndex, + PlayerIndexPerTeam = playerIndices }; return GameActionResult.SuccessResult( diff --git a/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameModel.cs b/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameModel.cs index ddb5980..4219041 100644 --- a/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameModel.cs +++ b/src/GoodWood.Application/Games/ChristmasTree/ChristmasTreeGameModel.cs @@ -35,6 +35,11 @@ public record ChristmasTreeGameModel : IGameModel /// public int CurrentPlayerIndexInTeam { get; set; } + /// + /// Player index per team (key = team index). Each team cycles independently. + /// + public Dictionary PlayerIndexPerTeam { get; init; } = new(); + /// /// Whether continue-on-miss variant is enabled. ///