Files
4Gewinnt/API/Controllers/GameHubSocket.cs
T
Jonas 955d1e18c6 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.
2026-03-12 23:20:05 +01:00

52 lines
1.3 KiB
C#

using API.Models.DataClasses;
using API.Models.Game;
using API.Services.GameManager;
using Microsoft.AspNetCore.SignalR;
namespace API.Controllers;
public class GameHubSocket(IGameManager gameManager) : Hub
{
private readonly IGameManager _gameManager = gameManager;
public async Task CreateGame(string playerName, Coordinates gFs)
{
var player = new Player(playerName, Context.ConnectionId);
var result = _gameManager.CreateGame(gFs, player);
await Groups.AddToGroupAsync(Context.ConnectionId, result.Item1);
await Clients.Caller.SendAsync("GameCreated", new
{
GameId = result.Item1,
GameCode = result.Item2,
});
}
public async Task JoinGame(int gameCode, string playerName)
{
var player = new Player(playerName, Context.ConnectionId);
var result = await _gameManager.JoinGame(player, gameCode);
if (result == null)
{
await Clients.Caller.SendAsync("Error", "Spiel nicht gefunden oder voll.");
return;
}
await Groups.AddToGroupAsync(Context.ConnectionId, gameCode.ToString());
await Clients.Caller.SendAsync("GameCreated", new
{
GameId = result,
GameCode = gameCode,
});
}
public async Task Place(string gameId, int index)
{
}
}