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.
55 lines
1.2 KiB
C#
55 lines
1.2 KiB
C#
using API.Models.DataClasses;
|
|
|
|
namespace API.Models.Game;
|
|
|
|
public enum GameState
|
|
{
|
|
Lobby,
|
|
Running,
|
|
Ended
|
|
}
|
|
|
|
public class Game(Coordinates gFs, SixDigitInt gameCode)
|
|
{
|
|
public string Id { get; init; } = Guid.NewGuid().ToString();
|
|
public SixDigitInt GameCode { get; } = gameCode;
|
|
private List<Player> Players { get; set; } = new();
|
|
public GameState State { get; private set; } = GameState.Lobby;
|
|
public GameField Field { get; } = new(gFs);
|
|
|
|
public bool AddPlayer(Player player)
|
|
{
|
|
if(Players.Count >= 2)
|
|
return false;
|
|
|
|
player.PlayerTag = Players.Count + 1;
|
|
Players.Add(player);
|
|
|
|
if (Players.Count == 2)
|
|
{
|
|
StartGame();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void RemovePlayer(string playerConnectionId)
|
|
{
|
|
Players.RemoveAll(x => x.ConnectionId == playerConnectionId);
|
|
}
|
|
|
|
public Player? GetPlayerByConnectionId(string playerConnectionId)
|
|
{
|
|
return Players.FirstOrDefault(x => x.ConnectionId == playerConnectionId);
|
|
}
|
|
|
|
private void StartGame()
|
|
{
|
|
State = GameState.Running;
|
|
}
|
|
|
|
public void EndGame()
|
|
{
|
|
State = GameState.Ended;
|
|
}
|
|
} |