4826760d73
Move SignalR group join into GameManager and add support for player names and improved online join flow. - Moved Groups.AddToGroupAsync call out of GameHubSocket into GameManager (hubContext.Groups.AddToGroupAsync) so group membership is handled when adding players to a game. - GUI: added player name input to GameJoinMenu and updated JoinGameObject to include playerName and use string gameCode. - OnlineMode & LocalMode: constructors updated to instantiate games without passing settings; removed WaitingForOpponent state and cleaned up event bindings. - OnlineGame: rewrote to handle onGameCreated/onGameJoined/onGameStarted/onFieldUpdated/onGameEnded/onError, added joinGame validation (expects 6-digit code), join flow (connect + join), state updates, drop forwarding to player.drop(gameId, index) and place description updates. - LocalGame: constructor signature simplified to no-arg. These changes centralize group management, add player identification, validate join codes, and improve game state/event handling for online play.
69 lines
1.8 KiB
C#
69 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)
|
|
{
|
|
if (gFs.X <= 0 || gFs.Y <= 0)
|
|
{
|
|
await Clients.Caller.SendAsync("Error", "Ungültige Spielfeldgröße.");
|
|
return;
|
|
}
|
|
|
|
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(string playerName, int gameCode)
|
|
{
|
|
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 Clients.Caller.SendAsync("GameJoined", new
|
|
{
|
|
GameId = result,
|
|
GameCode = gameCode,
|
|
});
|
|
}
|
|
|
|
public async Task RequestGameInformation(string gameId)
|
|
{
|
|
await _gameManager.RequestGameInformation(gameId, Context.ConnectionId);
|
|
}
|
|
|
|
public async Task Drop(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);
|
|
}
|
|
}
|