diff --git a/API/Controllers/GameHubSocket.cs b/API/Controllers/GameHubSocket.cs index e382d72..af8e0da 100644 --- a/API/Controllers/GameHubSocket.cs +++ b/API/Controllers/GameHubSocket.cs @@ -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 diff --git a/API/Models/Game/Game.cs b/API/Models/Game/Game.cs index d0d1bb1..9c911f2 100644 --- a/API/Models/Game/Game.cs +++ b/API/Models/Game/Game.cs @@ -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); } \ No newline at end of file diff --git a/API/Models/Game/GameField.cs b/API/Models/Game/GameField.cs index b285b15..2b61d08 100644 --- a/API/Models/Game/GameField.cs +++ b/API/Models/Game/GameField.cs @@ -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 diff --git a/API/Models/Game/Player.cs b/API/Models/Game/Player.cs new file mode 100644 index 0000000..b55c6e3 --- /dev/null +++ b/API/Models/Game/Player.cs @@ -0,0 +1,7 @@ +namespace API.Models.Game; + +public class Player +{ + public string Name { get; set; } + public string ConnectionId { get; set; } +} \ No newline at end of file diff --git a/API/Program.cs b/API/Program.cs index 3b4db82..fe14ad8 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -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(); +builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/API/Repository/Game/GameRepository.cs b/API/Repository/Game/GameRepository.cs deleted file mode 100644 index ba13f49..0000000 --- a/API/Repository/Game/GameRepository.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace API.Repository.Game; - -public class GameRepository : IGameRepository -{ -} \ No newline at end of file diff --git a/API/Repository/Game/IGameRepository.cs b/API/Repository/Game/IGameRepository.cs deleted file mode 100644 index ef97ea1..0000000 --- a/API/Repository/Game/IGameRepository.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace API.Repository.Game; - -public interface IGameRepository -{ -} \ No newline at end of file diff --git a/API/Repository/GameRepo/GameRepository.cs b/API/Repository/GameRepo/GameRepository.cs new file mode 100644 index 0000000..88f20fa --- /dev/null +++ b/API/Repository/GameRepo/GameRepository.cs @@ -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 _games = []; + public List 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); + } + } + } +} \ No newline at end of file diff --git a/API/Repository/GameRepo/IGameRepository.cs b/API/Repository/GameRepo/IGameRepository.cs new file mode 100644 index 0000000..25843f0 --- /dev/null +++ b/API/Repository/GameRepo/IGameRepository.cs @@ -0,0 +1,13 @@ +using API.Models.DataClasses; +using API.Models.Game; + +namespace API.Repository.GameRepo; + +public interface IGameRepository +{ + public List GetAll(); + public Game? GetOne(string id); + public Game? GetOne(SixDigitInt gameCode); + public Game Create(Coordinates gameFieldSize, Player player); + public void Destroy(string id); +} \ No newline at end of file diff --git a/API/Services/GameManager/GameManager.cs b/API/Services/GameManager/GameManager.cs index c39c3ac..dbd74dc 100644 --- a/API/Services/GameManager/GameManager.cs +++ b/API/Services/GameManager/GameManager.cs @@ -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; - } } \ No newline at end of file diff --git a/API/Services/GameManager/IGameManager.cs b/API/Services/GameManager/IGameManager.cs index e89a65a..d17c3ef 100644 --- a/API/Services/GameManager/IGameManager.cs +++ b/API/Services/GameManager/IGameManager.cs @@ -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); } \ No newline at end of file