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.
51 lines
1.1 KiB
C#
51 lines
1.1 KiB
C#
using System.Security.Cryptography;
|
|
using API.Models.DataClasses;
|
|
using API.Models.Game;
|
|
|
|
namespace API.Repository.GameRepo;
|
|
|
|
public class GameRepository : IGameRepository
|
|
{
|
|
private List<Game> _games = [];
|
|
public List<Game> GetAll()
|
|
{
|
|
return _games;
|
|
}
|
|
|
|
public Game? GetOne(string id)
|
|
{
|
|
return _games.FirstOrDefault(g => g.Id == id);
|
|
}
|
|
|
|
public Game? GetOne(SixDigitInt gameCode)
|
|
{
|
|
return _games.FirstOrDefault(g => g.GameCode == gameCode);
|
|
}
|
|
|
|
public Game Create(Coordinates gameFieldSize)
|
|
{
|
|
Game newGame = new(gameFieldSize, GenerateGameCode());
|
|
_games.Add(newGame);
|
|
|
|
return newGame;
|
|
}
|
|
|
|
public void Destroy(string id)
|
|
{
|
|
_games.RemoveAll(g => g.Id == id);
|
|
}
|
|
|
|
private SixDigitInt GenerateGameCode()
|
|
{
|
|
while (true)
|
|
{
|
|
var value = RandomNumberGenerator.GetInt32(100000, 1000000);
|
|
|
|
var exists = _games.Any(g => g.GameCode.Value == value);
|
|
if (!exists)
|
|
{
|
|
return new SixDigitInt(value);
|
|
}
|
|
}
|
|
}
|
|
} |