From ce52d1462ff15d63b3e86ccf78e7f7f3c7221c0a Mon Sep 17 00:00:00 2001 From: Jonas <77726472+kobolol@users.noreply.github.com> Date: Sun, 1 Mar 2026 18:28:11 +0100 Subject: [PATCH] Add player list and wire repo/manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the nullable PlayerConnectionIds array with a private List inside Game and add AddPlayer/RemovePlayer methods to manage membership. Have GameRepository.Create attach the initial player to the newly created game. Implement GameManager.CreateGame and JoinGame to call the repository (using the injected gameRepository) — CreateGame returns the created game's code and JoinGame looks up the game and tries to add the player. Also update imports accordingly. These changes centralize player handling in the Game model and connect repository/manager flows to use it. --- API/Models/Game/Game.cs | 16 +++++++++++++++- API/Repository/GameRepo/GameRepository.cs | 2 +- API/Services/GameManager/GameManager.cs | 15 +++++++++------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/API/Models/Game/Game.cs b/API/Models/Game/Game.cs index 9c911f2..efcc1b7 100644 --- a/API/Models/Game/Game.cs +++ b/API/Models/Game/Game.cs @@ -13,7 +13,21 @@ public class Game(Coordinates gFs, SixDigitInt gameCode) { public string Id { get; init; } = Guid.NewGuid().ToString(); public SixDigitInt GameCode { get; } = gameCode; - public Player?[] PlayerConnectionIds { get; set; } = new Player?[2]; + private List Players { get; set; } = new(); public GameState State { get; private set; } = GameState.Lobby; public GameField Field { get; } = new(gFs); + + public bool AddPlayer(Player player) + { + if(Players.Count >= 2) + return false; + + Players.Add(player); + return true; + } + + public void RemovePlayer(string playerConnectionId) + { + Players.RemoveAll(x => x.ConnectionId == playerConnectionId); + } } \ No newline at end of file diff --git a/API/Repository/GameRepo/GameRepository.cs b/API/Repository/GameRepo/GameRepository.cs index 88f20fa..395a7a4 100644 --- a/API/Repository/GameRepo/GameRepository.cs +++ b/API/Repository/GameRepo/GameRepository.cs @@ -25,8 +25,8 @@ public class GameRepository : IGameRepository public Game Create(Coordinates gameFieldSize, Player player) { Game newGame = new(gameFieldSize, GenerateGameCode()); - _games.Add(newGame); + newGame.AddPlayer(player); return newGame; } diff --git a/API/Services/GameManager/GameManager.cs b/API/Services/GameManager/GameManager.cs index dbd74dc..c3561a1 100644 --- a/API/Services/GameManager/GameManager.cs +++ b/API/Services/GameManager/GameManager.cs @@ -1,19 +1,22 @@ -using API.Models.Game; +using API.Models.DataClasses; +using API.Models.Game; using API.Repository.GameRepo; namespace API.Services.GameManager; public class GameManager(IGameRepository gameRepository) : IGameManager { - private readonly IGameRepository _gameRepo = gameRepository; - public int CreateGame(Coordinates gFs, Player player) { - throw new NotImplementedException(); + var game = gameRepository.Create(gFs, player); + + return game.GameCode; } - public bool JoinGame(Player playerName, int gameCode) + public bool JoinGame(Player player, int gameCode) { - throw new NotImplementedException(); + var game = gameRepository.GetOne(new SixDigitInt(gameCode)); + + return game != null && game.AddPlayer(player); } } \ No newline at end of file