diff --git a/API/API.csproj b/API/API.csproj index e51252d..ac380d1 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -1,18 +1,14 @@ - - net9.0 - enable - enable - + + net9.0 + enable + enable + - - - - - - - - + + + + diff --git a/API/Controllers/GameHubSocket.cs b/API/Controllers/GameHubSocket.cs index eede8c9..e382d72 100644 --- a/API/Controllers/GameHubSocket.cs +++ b/API/Controllers/GameHubSocket.cs @@ -4,18 +4,18 @@ namespace API.Controllers; public class GameHubSocket : Hub { - public async Task CreateGame(string playerName, ) + public async Task CreateGame(string playerName) { - await Groups.AddToGroupAsync(Context.ConnectionId, gameId); - await Clients.Group(gameId).SendAsync("PlayerJoined", new + // TODO: create + await Groups.AddToGroupAsync(Context.ConnectionId, "gameId"); + await Clients.Group("gameId").SendAsync("PlayerJoined", new { - ConnectionId = Context.ConnectionId, + Context.ConnectionId, PlayerName = playerName }); } public async Task JoinGame(string gameId, string playerName) { - } } \ No newline at end of file diff --git a/API/Controllers/StatusController.cs b/API/Controllers/StatusController.cs index 1a3ccf5..00c3d41 100644 --- a/API/Controllers/StatusController.cs +++ b/API/Controllers/StatusController.cs @@ -11,4 +11,4 @@ public class StatusController : ControllerBase { return Ok("Running"); } -} +} \ No newline at end of file diff --git a/API/Models/DataClasses/SixDigitInt.cs b/API/Models/DataClasses/SixDigitInt.cs index 0ee9112..8700d0e 100644 --- a/API/Models/DataClasses/SixDigitInt.cs +++ b/API/Models/DataClasses/SixDigitInt.cs @@ -1,20 +1,25 @@ -namespace API.Models.DataClasses +namespace API.Models.DataClasses; + +public readonly record struct SixDigitInt { - public readonly record struct SixDigitInt + public SixDigitInt(int value) { - public int Value { get; } + if (value < 0 || value > 999999) + throw new ArgumentOutOfRangeException(nameof(value), + "Wert muss zwischen 0 und 999999 liegen."); - public SixDigitInt(int value) - { - if (value < 0 || value > 999999) - throw new ArgumentOutOfRangeException(nameof(value), - "Wert muss zwischen 0 und 999999 liegen."); - - Value = value; - } - - public override string ToString() => Value.ToString("D6"); - - public static implicit operator int(SixDigitInt v) => v.Value; + Value = value; } -} + + public int Value { get; } + + public override string ToString() + { + return Value.ToString("D6"); + } + + public static implicit operator int(SixDigitInt v) + { + return v.Value; + } +} \ No newline at end of file diff --git a/API/Models/Game/Game.cs b/API/Models/Game/Game.cs index 7f3359a..d0d1bb1 100644 --- a/API/Models/Game/Game.cs +++ b/API/Models/Game/Game.cs @@ -1,26 +1,19 @@ using API.Models.DataClasses; -namespace API.Models.Game +namespace API.Models.Game; + +public enum GameState { - public enum GameState - { - Lobby, - Running, - Ended - } - - public class Game - { - public string Id { get; init; } = Guid.NewGuid().ToString(); - public SixDigitInt GameCode { get; } - public string?[] PlayerConnectionIds { get; set; } = new string?[2]; - public GameState State { get; private set; } = GameState.Lobby; - public GameField Field { get; } - - public Game(Coordinates gFs, SixDigitInt gameCode) - { - Field = new GameField(gFs); - GameCode = gameCode; - } - } + Lobby, + Running, + Ended } + +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 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 98377c3..b285b15 100644 --- a/API/Models/Game/GameField.cs +++ b/API/Models/Game/GameField.cs @@ -1,86 +1,79 @@ -namespace API.Models.Game +namespace API.Models.Game; + +public class Coordinates { - public class Coordinates + public int X; + public int Y; +} + +public enum PlaceResult +{ + OutOfGameField, + NotAllowedPlayer, + OccupiedRed, + OccupiedYellow, + InvalidFieldValue, + Placed +} + +public enum FieldState +{ + OutOfGameField, + Empty, + OccupiedRed, + OccupiedYellow, + InvalidFieldValue +} + +public class GameField(Coordinates gFs) +{ + public int[,] CurrentField { get; } = new int[gFs.Y, gFs.X]; + public int[,] BackupField { get; } = new int[gFs.Y, gFs.X]; + + public PlaceResult Place(Coordinates coordinates, int player) { - public int X; - public int Y; - } + if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) || + coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0)) + return PlaceResult.OutOfGameField; - public enum PlaceResult - { - OutOfGameField, - NotAllowedPlayer, - OccupiedRed, - OccupiedYellow, - InvalidFieldValue, - Placed - } + if (player != 1 && player != 2) + return PlaceResult.NotAllowedPlayer; - public enum FieldState - { - OutOfGameField, - Empty, - OccupiedRed, - OccupiedYellow, - InvalidFieldValue - } - - public class GameField(Coordinates gFs) - { - public int[,] CurrentField { get; } = new int[gFs.Y, gFs.X]; - public int[,] BackupField { get; } = new int[gFs.Y, gFs.X]; - - public PlaceResult Place(Coordinates coordinates, int player) - { - if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) || - coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0)) - { - return PlaceResult.OutOfGameField; - } - - if (player != 1 && player != 2) - return PlaceResult.NotAllowedPlayer; - - int currentValue = CurrentField[coordinates.Y, coordinates.X]; - - if (currentValue != 0) - { - return currentValue switch - { - 1 => PlaceResult.OccupiedRed, - 2 => PlaceResult.OccupiedYellow, - _ => PlaceResult.InvalidFieldValue - }; - } - - CreateSave(); - CurrentField[coordinates.Y, coordinates.X] = player; - - return PlaceResult.Placed; - } - - public FieldState CheckField(Coordinates coordinates) - { - if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) || - coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0)) - { - return FieldState.OutOfGameField; - } - - int currentValue = CurrentField[coordinates.Y, coordinates.X]; + var currentValue = CurrentField[coordinates.Y, coordinates.X]; + if (currentValue != 0) return currentValue switch { - 0 => FieldState.Empty, - 1 => FieldState.OccupiedRed, - 2 => FieldState.OccupiedYellow, - _ => FieldState.InvalidFieldValue + 1 => PlaceResult.OccupiedRed, + 2 => PlaceResult.OccupiedYellow, + _ => PlaceResult.InvalidFieldValue }; - } - private void CreateSave() + CreateSave(); + CurrentField[coordinates.Y, coordinates.X] = player; + + return PlaceResult.Placed; + } + + public FieldState CheckField(Coordinates coordinates) + { + if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) || + coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0)) + return FieldState.OutOfGameField; + + var currentValue = CurrentField[coordinates.Y, coordinates.X]; + + return currentValue switch { - Array.Copy(CurrentField, BackupField, CurrentField.Length); - } + 0 => FieldState.Empty, + 1 => FieldState.OccupiedRed, + 2 => FieldState.OccupiedYellow, + _ => FieldState.InvalidFieldValue + }; + } + + private void CreateSave() + { + Array.Copy(CurrentField, BackupField, CurrentField.Length); } } \ No newline at end of file diff --git a/API/Program.cs b/API/Program.cs index 0e8ac50..3b4db82 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -1,3 +1,4 @@ +using API.Controllers; using API.Services.GameManager; var builder = WebApplication.CreateBuilder(args); @@ -28,8 +29,8 @@ app.UseStaticFiles(); app.UseAuthorization(); app.MapControllers(); -app.MapHub("/api/gamehub"); +app.MapHub("/api/gamehub"); app.MapFallbackToFile("index.html"); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/API/Repository/Game/GameRepository.cs b/API/Repository/Game/GameRepository.cs new file mode 100644 index 0000000..ba13f49 --- /dev/null +++ b/API/Repository/Game/GameRepository.cs @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..ef97ea1 --- /dev/null +++ b/API/Repository/Game/IGameRepository.cs @@ -0,0 +1,5 @@ +namespace API.Repository.Game; + +public interface IGameRepository +{ +} \ No newline at end of file diff --git a/API/Services/GameManager/GameManager.cs b/API/Services/GameManager/GameManager.cs index 2813ee5..c39c3ac 100644 --- a/API/Services/GameManager/GameManager.cs +++ b/API/Services/GameManager/GameManager.cs @@ -1,20 +1,19 @@ -namespace API.Services.GameManager +namespace API.Services.GameManager; + +public class GameManager : IGameManager { - public class GameManager : IGameManager + public int CreateGame(string playerName) { - public int CreateGame(string playerName) - { - throw new NotImplementedException(); - } - - public bool JoinGame(string playerName, int gameCode) - { - throw new NotImplementedException(); - } - - private int GenerateNonExistingGameCode() - { - return 0; - } + throw new NotImplementedException(); } -} + + public bool JoinGame(string 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 dbb18d7..e89a65a 100644 --- a/API/Services/GameManager/IGameManager.cs +++ b/API/Services/GameManager/IGameManager.cs @@ -1,8 +1,7 @@ -namespace API.Services.GameManager +namespace API.Services.GameManager; + +public interface IGameManager { - public interface IGameManager - { - public int CreateGame(string playerName); - public bool JoinGame(string playerName, int gameCode); - } -} + public int CreateGame(string playerName); + public bool JoinGame(string playerName, int gameCode); +} \ No newline at end of file