Files
4Gewinnt/API/Repository/GameRepo/GameRepository.cs
T
Jonas e8b45fa235 Add column drop, game info DTO, and disconnect
Introduce column-based drop mechanics and game info transfer; add disconnect handling and related API/interface updates. GameField API renamed Place->Drop and PlaceResult->DropResult, added ColumnFull, IsFull, and updated drop logic to insert at lowest empty row. Game model now exposes Players, adds GetPlayerByTag and makes StartGame public. New GameInformationDto conveys game state and field. Hub methods updated: GameCreated->GameJoined, RequestGameInformation, Place now forwards to Drop, and OnDisconnectedAsync notifies GameManager. GameManager implements RequestGameInformation, Drop (validates moves, broadcasts FieldUpdated and GameEnded on win/draw), improved JoinGame logic to auto-start when two players join, and handles player disconnects. Repository and interface extended with GetOneByConnectionId. Also tightened SignalR timeouts in Program.
2026-03-12 23:20:05 +01:00

56 lines
1.3 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? GetOneByConnectionId(string connectionId)
{
return _games.FirstOrDefault(g => g.Players.Any(p => p.ConnectionId == connectionId));
}
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);
}
}
}
}