955d1e18c6
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.
34 lines
999 B
C#
34 lines
999 B
C#
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, IHubContext<GameHubSocket> hubContext) : IGameManager
|
|
{
|
|
public (string, int) CreateGame(Coordinates gFs, Player player)
|
|
{
|
|
var game = gameRepository.Create(gFs);
|
|
game.AddPlayer(player);
|
|
return (game.Id, game.GameCode);
|
|
}
|
|
|
|
public async Task<string?> JoinGame(Player player, int gameCode)
|
|
{
|
|
var game = gameRepository.GetOne(new SixDigitInt(gameCode));
|
|
|
|
var success = game != null && game.AddPlayer(player);
|
|
|
|
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;
|
|
}
|
|
} |