Return game ID+code; update hub and Player model
Change GameManager/CreateGame to return (gameId, gameCode) and Make JoinGame return the game Id (or null) instead of a bool. Update IGameManager signature accordingly. Refactor Hub methods to construct Player objects, accept Coordinates for game creation, use the manager results to add connections to groups and send GameCreated with GameId and GameCode, and handle join failures with an error message. Simplify Player class to use a primary-constructor style with property initialization; add necessary using directives.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using API.Models.DataClasses;
|
||||
using API.Models.Game;
|
||||
using API.Services.GameManager;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
@@ -7,18 +9,39 @@ public class GameHubSocket(IGameManager gameManager) : Hub
|
||||
{
|
||||
private readonly IGameManager _gameManager = gameManager;
|
||||
|
||||
public async Task CreateGame(string playerName)
|
||||
public async Task CreateGame(string playerName, Coordinates gFs)
|
||||
{
|
||||
// TODO: create
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, "gameId");
|
||||
await Clients.Group("gameId").SendAsync("PlayerJoined", new
|
||||
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
|
||||
{
|
||||
Context.ConnectionId,
|
||||
PlayerName = playerName
|
||||
GameId = result.Item1,
|
||||
GameCode = result.Item2,
|
||||
});
|
||||
}
|
||||
|
||||
public async Task JoinGame(string gameId, string playerName)
|
||||
public async Task JoinGame(int gameCode, string playerName)
|
||||
{
|
||||
var player = new Player(playerName, Context.ConnectionId);
|
||||
|
||||
var result = _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,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user