Files
4Gewinnt/API/Controllers/GameHubSocket.cs
T
Jonas e8b45fa235 Add column drop, game info DTO, and disconnect
Introduce column-based drop mechanics and game info transfer; add disconnect handling and related API/interface updates. GameField API renamed Place->Drop and PlaceResult->DropResult, added ColumnFull, IsFull, and updated drop logic to insert at lowest empty row. Game model now exposes Players, adds GetPlayerByTag and makes StartGame public. New GameInformationDto conveys game state and field. Hub methods updated: GameCreated->GameJoined, RequestGameInformation, Place now forwards to Drop, and OnDisconnectedAsync notifies GameManager. GameManager implements RequestGameInformation, Drop (validates moves, broadcasts FieldUpdated and GameEnded on win/draw), improved JoinGame logic to auto-start when two players join, and handles player disconnects. Repository and interface extended with GetOneByConnectionId. Also tightened SignalR timeouts in Program.
2026-03-12 23:20:05 +01:00

64 lines
1.8 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("GameJoined", new
{
GameId = result,
GameCode = gameCode,
});
}
public async Task RequestGameInformation(string gameId)
{
await _gameManager.RequestGameInformation(gameId, Context.ConnectionId);
}
public async Task Place(string gameId, int column)
{
await _gameManager.Drop(gameId, column, Context.ConnectionId);
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
await _gameManager.DisconnectedPlayer(Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
}