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:
@@ -11,8 +11,4 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Repository\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
namespace API.Models.DataClasses
|
||||
{
|
||||
namespace API.Models.DataClasses;
|
||||
|
||||
public readonly record struct SixDigitInt
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
public SixDigitInt(int value)
|
||||
{
|
||||
if (value < 0 || value > 999999)
|
||||
@@ -13,8 +11,15 @@
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override string ToString() => Value.ToString("D6");
|
||||
public int Value { get; }
|
||||
|
||||
public static implicit operator int(SixDigitInt v) => v.Value;
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString("D6");
|
||||
}
|
||||
|
||||
public static implicit operator int(SixDigitInt v)
|
||||
{
|
||||
return v.Value;
|
||||
}
|
||||
}
|
||||
+5
-12
@@ -1,7 +1,7 @@
|
||||
using API.Models.DataClasses;
|
||||
|
||||
namespace API.Models.Game
|
||||
{
|
||||
namespace API.Models.Game;
|
||||
|
||||
public enum GameState
|
||||
{
|
||||
Lobby,
|
||||
@@ -9,18 +9,11 @@ namespace API.Models.Game
|
||||
Ended
|
||||
}
|
||||
|
||||
public class Game
|
||||
public class Game(Coordinates gFs, SixDigitInt gameCode)
|
||||
{
|
||||
public string Id { get; init; } = Guid.NewGuid().ToString();
|
||||
public SixDigitInt GameCode { get; }
|
||||
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; }
|
||||
|
||||
public Game(Coordinates gFs, SixDigitInt gameCode)
|
||||
{
|
||||
Field = new GameField(gFs);
|
||||
GameCode = gameCode;
|
||||
}
|
||||
}
|
||||
public GameField Field { get; } = new(gFs);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace API.Models.Game
|
||||
{
|
||||
namespace API.Models.Game;
|
||||
|
||||
public class Coordinates
|
||||
{
|
||||
public int X;
|
||||
@@ -34,24 +34,20 @@
|
||||
{
|
||||
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];
|
||||
var 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;
|
||||
@@ -63,11 +59,9 @@
|
||||
{
|
||||
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];
|
||||
|
||||
return currentValue switch
|
||||
{
|
||||
@@ -83,4 +77,3 @@
|
||||
Array.Copy(CurrentField, BackupField, CurrentField.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
using API.Controllers;
|
||||
using API.Services.GameManager;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -28,7 +29,7 @@ app.UseStaticFiles();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
app.MapHub<API.Controllers.GameHubSocket>("/api/gamehub");
|
||||
app.MapHub<GameHubSocket>("/api/gamehub");
|
||||
|
||||
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,5 +1,5 @@
|
||||
namespace API.Services.GameManager
|
||||
{
|
||||
namespace API.Services.GameManager;
|
||||
|
||||
public class GameManager : IGameManager
|
||||
{
|
||||
public int CreateGame(string playerName)
|
||||
@@ -17,4 +17,3 @@
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
namespace API.Services.GameManager
|
||||
{
|
||||
namespace API.Services.GameManager;
|
||||
|
||||
public interface IGameManager
|
||||
{
|
||||
public int CreateGame(string playerName);
|
||||
public bool JoinGame(string playerName, int gameCode);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user