Refactor GameRepository and GameManager architecture, introduce Player model, and update dependency registration in Program.cs.
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
using API.Services.GameManager;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
public class GameHubSocket : Hub
|
||||
public class GameHubSocket(IGameManager gameManager) : Hub
|
||||
{
|
||||
private readonly IGameManager _gameManager = gameManager;
|
||||
|
||||
public async Task CreateGame(string playerName)
|
||||
{
|
||||
// TODO: create
|
||||
|
||||
@@ -13,7 +13,7 @@ public class Game(Coordinates gFs, SixDigitInt gameCode)
|
||||
{
|
||||
public string Id { get; init; } = Guid.NewGuid().ToString();
|
||||
public SixDigitInt GameCode { get; } = gameCode;
|
||||
public string?[] PlayerConnectionIds { get; set; } = new string?[2];
|
||||
public Player?[] PlayerConnectionIds { get; set; } = new Player?[2];
|
||||
public GameState State { get; private set; } = GameState.Lobby;
|
||||
public GameField Field { get; } = new(gFs);
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
namespace API.Models.Game;
|
||||
|
||||
public class Coordinates
|
||||
public readonly struct Coordinates(int x, int y)
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
public readonly int X = x;
|
||||
public readonly int Y = y;
|
||||
}
|
||||
|
||||
public enum PlaceResult
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace API.Models.Game;
|
||||
|
||||
public class Player
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string ConnectionId { get; set; }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using API.Controllers;
|
||||
using API.Repository.GameRepo;
|
||||
using API.Services.GameManager;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -11,6 +12,7 @@ builder.Services.AddOpenApi();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSingleton<IGameManager, GameManager>();
|
||||
builder.Services.AddSingleton<IGameRepository, GameRepository>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
namespace API.Repository.Game;
|
||||
|
||||
public class GameRepository : IGameRepository
|
||||
{
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
namespace API.Repository.Game;
|
||||
|
||||
public interface IGameRepository
|
||||
{
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
namespace API.Services.GameManager;
|
||||
using API.Models.Game;
|
||||
using API.Repository.GameRepo;
|
||||
|
||||
public class GameManager : IGameManager
|
||||
namespace API.Services.GameManager;
|
||||
|
||||
public class GameManager(IGameRepository gameRepository) : IGameManager
|
||||
{
|
||||
public int CreateGame(string playerName)
|
||||
private readonly IGameRepository _gameRepo = gameRepository;
|
||||
|
||||
public int CreateGame(Coordinates gFs, Player player)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool JoinGame(string playerName, int gameCode)
|
||||
public bool JoinGame(Player playerName, int gameCode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private int GenerateNonExistingGameCode()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
namespace API.Services.GameManager;
|
||||
using API.Models.Game;
|
||||
|
||||
namespace API.Services.GameManager;
|
||||
|
||||
public interface IGameManager
|
||||
{
|
||||
public int CreateGame(string playerName);
|
||||
public bool JoinGame(string playerName, int gameCode);
|
||||
public int CreateGame(Coordinates gFs, Player player);
|
||||
public bool JoinGame(Player playerName, int gameCode);
|
||||
}
|
||||
Reference in New Issue
Block a user