Refactor and clean up codebase:
- Introduced `IGameRepository` interface and its implementation. - Transitioned namespaces to `file-scoped` for consistency. - Simplified class definitions, e.g., `Game` and `SixDigitInt`. - Restructured `GameField` logic for improved readability and functionality. - Fixed formatting issues in `API.csproj` and added a `using` directive in `Program.cs`.
This commit is contained in:
+9
-13
@@ -1,18 +1,14 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.12" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.12"/>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Repository\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -4,18 +4,18 @@ namespace API.Controllers;
|
|||||||
|
|
||||||
public class GameHubSocket : Hub
|
public class GameHubSocket : Hub
|
||||||
{
|
{
|
||||||
public async Task CreateGame(string playerName, )
|
public async Task CreateGame(string playerName)
|
||||||
{
|
{
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId, gameId);
|
// TODO: create
|
||||||
await Clients.Group(gameId).SendAsync("PlayerJoined", new
|
await Groups.AddToGroupAsync(Context.ConnectionId, "gameId");
|
||||||
|
await Clients.Group("gameId").SendAsync("PlayerJoined", new
|
||||||
{
|
{
|
||||||
ConnectionId = Context.ConnectionId,
|
Context.ConnectionId,
|
||||||
PlayerName = playerName
|
PlayerName = playerName
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task JoinGame(string gameId, string playerName)
|
public async Task JoinGame(string gameId, string playerName)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
Value = value;
|
||||||
{
|
}
|
||||||
if (value < 0 || value > 999999)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(value),
|
|
||||||
"Wert muss zwischen 0 und 999999 liegen.");
|
|
||||||
|
|
||||||
Value = value;
|
public int Value { get; }
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Value.ToString("D6");
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value.ToString("D6");
|
||||||
|
}
|
||||||
|
|
||||||
public static implicit operator int(SixDigitInt v) => v.Value;
|
public static implicit operator int(SixDigitInt v)
|
||||||
|
{
|
||||||
|
return v.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+15
-22
@@ -1,26 +1,19 @@
|
|||||||
using API.Models.DataClasses;
|
using API.Models.DataClasses;
|
||||||
|
|
||||||
namespace API.Models.Game
|
namespace API.Models.Game;
|
||||||
|
|
||||||
|
public enum GameState
|
||||||
{
|
{
|
||||||
public enum GameState
|
Lobby,
|
||||||
{
|
Running,
|
||||||
Lobby,
|
Ended
|
||||||
Running,
|
}
|
||||||
Ended
|
|
||||||
}
|
public class Game(Coordinates gFs, SixDigitInt gameCode)
|
||||||
|
{
|
||||||
public class Game
|
public string Id { get; init; } = Guid.NewGuid().ToString();
|
||||||
{
|
public SixDigitInt GameCode { get; } = gameCode;
|
||||||
public string Id { get; init; } = Guid.NewGuid().ToString();
|
public string?[] PlayerConnectionIds { get; set; } = new string?[2];
|
||||||
public SixDigitInt GameCode { get; }
|
public GameState State { get; private set; } = GameState.Lobby;
|
||||||
public string?[] PlayerConnectionIds { get; set; } = new string?[2];
|
public GameField Field { get; } = new(gFs);
|
||||||
public GameState State { get; private set; } = GameState.Lobby;
|
|
||||||
public GameField Field { get; }
|
|
||||||
|
|
||||||
public Game(Coordinates gFs, SixDigitInt gameCode)
|
|
||||||
{
|
|
||||||
Field = new GameField(gFs);
|
|
||||||
GameCode = gameCode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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;
|
if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) ||
|
||||||
public int Y;
|
coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0))
|
||||||
}
|
return PlaceResult.OutOfGameField;
|
||||||
|
|
||||||
public enum PlaceResult
|
if (player != 1 && player != 2)
|
||||||
{
|
return PlaceResult.NotAllowedPlayer;
|
||||||
OutOfGameField,
|
|
||||||
NotAllowedPlayer,
|
|
||||||
OccupiedRed,
|
|
||||||
OccupiedYellow,
|
|
||||||
InvalidFieldValue,
|
|
||||||
Placed
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum FieldState
|
var currentValue = CurrentField[coordinates.Y, coordinates.X];
|
||||||
{
|
|
||||||
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];
|
|
||||||
|
|
||||||
|
if (currentValue != 0)
|
||||||
return currentValue switch
|
return currentValue switch
|
||||||
{
|
{
|
||||||
0 => FieldState.Empty,
|
1 => PlaceResult.OccupiedRed,
|
||||||
1 => FieldState.OccupiedRed,
|
2 => PlaceResult.OccupiedYellow,
|
||||||
2 => FieldState.OccupiedYellow,
|
_ => PlaceResult.InvalidFieldValue
|
||||||
_ => FieldState.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-1
@@ -1,3 +1,4 @@
|
|||||||
|
using API.Controllers;
|
||||||
using API.Services.GameManager;
|
using API.Services.GameManager;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -28,7 +29,7 @@ app.UseStaticFiles();
|
|||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapHub<API.Controllers.GameHubSocket>("/api/gamehub");
|
app.MapHub<GameHubSocket>("/api/gamehub");
|
||||||
|
|
||||||
app.MapFallbackToFile("index.html");
|
app.MapFallbackToFile("index.html");
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace API.Repository.Game;
|
||||||
|
|
||||||
|
public class GameRepository : IGameRepository
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace API.Repository.Game;
|
||||||
|
|
||||||
|
public interface IGameRepository
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
{
|
}
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool JoinGame(string playerName, int gameCode)
|
public bool JoinGame(string playerName, int gameCode)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int GenerateNonExistingGameCode()
|
private int GenerateNonExistingGameCode()
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user