Refactor GameRepository and GameManager architecture, introduce Player model, and update dependency registration in Program.cs.

This commit is contained in:
2026-02-28 23:06:54 +01:00
committed by Jonas
parent b967bd70eb
commit 0e7bfb7241
11 changed files with 96 additions and 27 deletions
+52
View File
@@ -0,0 +1,52 @@
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, Player player)
{
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)
{
int value = RandomNumberGenerator.GetInt32(100000, 1000000);
bool exists = _games.Any(g => g.GameCode.Value == value);
if (!exists)
{
return new SixDigitInt(value);
}
}
}
}
@@ -0,0 +1,13 @@
using API.Models.DataClasses;
using API.Models.Game;
namespace API.Repository.GameRepo;
public interface IGameRepository
{
public List<Game> GetAll();
public Game? GetOne(string id);
public Game? GetOne(SixDigitInt gameCode);
public Game Create(Coordinates gameFieldSize, Player player);
public void Destroy(string id);
}