Add player list and wire repo/manager

Replace the nullable PlayerConnectionIds array with a private List<Player> 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.
This commit is contained in:
Jonas
2026-03-01 18:28:11 +01:00
parent 0e7bfb7241
commit ce52d1462f
3 changed files with 25 additions and 8 deletions
+15 -1
View File
@@ -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<Player> 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);
}
}
+1 -1
View File
@@ -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;
}
+9 -6
View File
@@ -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);
}
}