Add player tags, game lifecycle and win checks

Additions and refactors across controllers, models, repo and services to support game lifecycle and win detection. Key changes:

- Models: Player now has PlayerTag; Game assigns tags, starts when 2 players join, exposes GetPlayerByConnectionId, StartGame and EndGame helpers.
- GameField: Implemented win-detection (CountInDirection, CheckLine, CheckForWin) and minor backing-field init.
- Repository/API: GameRepository.Create no longer accepts a Player (repo only creates games); minor variable cleanups; IGameRepository signature updated accordingly.
- Services: GameManager now injects IHubContext<GameHubSocket>, moves player-adding into manager (CreateGame), makes JoinGame async and notifies the SignalR group when a game starts, and adds a placeholder async Place method. Controller (GameHubSocket) also exposes a Place method stub to match the service API.

Motivation: separate responsibilities so repository only creates games, manager handles player joins and notifications, and add core game logic (player tagging and win checks) needed for gameplay. Place methods are added as placeholders for future move handling.
This commit is contained in:
Jonas
2026-03-03 19:12:54 +01:00
parent 85521f3b23
commit 955d1e18c6
8 changed files with 104 additions and 13 deletions
+16 -6
View File
@@ -1,24 +1,34 @@
using API.Models.DataClasses;
using API.Controllers;
using API.Models.DataClasses;
using API.Models.Game;
using API.Repository.GameRepo;
using Microsoft.AspNetCore.SignalR;
namespace API.Services.GameManager;
public class GameManager(IGameRepository gameRepository) : IGameManager
public class GameManager(IGameRepository gameRepository, IHubContext<GameHubSocket> hubContext) : IGameManager
{
public (string, int) CreateGame(Coordinates gFs, Player player)
{
var game = gameRepository.Create(gFs, player);
var game = gameRepository.Create(gFs);
game.AddPlayer(player);
return (game.Id, game.GameCode);
}
public string? JoinGame(Player player, int gameCode)
public async Task<string?> JoinGame(Player player, int gameCode)
{
var game = gameRepository.GetOne(new SixDigitInt(gameCode));
var success = game != null && game.AddPlayer(player);
return success ? game?.Id : null;
if (game!.State == GameState.Running)
await hubContext.Clients.Group(game.Id).SendAsync("GameStarted");
return game.Id;
}
public async Task<bool> Place(string gameCode, int coordinates, string playerConnectionId)
{
return true;
}
}
+2 -1
View File
@@ -5,5 +5,6 @@ namespace API.Services.GameManager;
public interface IGameManager
{
public (string, int) CreateGame(Coordinates gFs, Player player);
public string? JoinGame(Player playerName, int gameCode);
public Task<string?> JoinGame(Player player, int gameCode)
public Task<bool> Place(string gameCode, int coordinates, string playerConnectionId)
}