Added Backend Classes and FUnctions for an working Game. But lacking Implementations
This commit is contained in:
@@ -12,9 +12,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Repository\" />
|
||||
<Folder Include="Services\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,9 +2,9 @@ using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace API.Controllers;
|
||||
|
||||
public class GameController : Hub
|
||||
public class GameHubSocket : Hub
|
||||
{
|
||||
public async Task JoinGame(string gameId, string playerName)
|
||||
public async Task CreateGame(string playerName, )
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, gameId);
|
||||
await Clients.Group(gameId).SendAsync("PlayerJoined", new
|
||||
@@ -13,4 +13,9 @@ public class GameController : Hub
|
||||
PlayerName = playerName
|
||||
});
|
||||
}
|
||||
|
||||
public async Task JoinGame(string gameId, string playerName)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace API.Models.DataClasses
|
||||
{
|
||||
public readonly record struct SixDigitInt
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using API.Models.DataClasses;
|
||||
|
||||
namespace API.Models.Game
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
namespace API.Models.Game
|
||||
{
|
||||
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)
|
||||
{
|
||||
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];
|
||||
|
||||
return currentValue switch
|
||||
{
|
||||
0 => FieldState.Empty,
|
||||
1 => FieldState.OccupiedRed,
|
||||
2 => FieldState.OccupiedYellow,
|
||||
_ => FieldState.InvalidFieldValue
|
||||
};
|
||||
}
|
||||
|
||||
private void CreateSave()
|
||||
{
|
||||
Array.Copy(CurrentField, BackupField, CurrentField.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -1,3 +1,5 @@
|
||||
using API.Services.GameManager;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
@@ -7,6 +9,8 @@ builder.Services.AddSignalR();
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSingleton<IGameManager, GameManager>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
@@ -24,9 +28,8 @@ app.UseStaticFiles();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
app.MapHub<API.Controllers.GameController>("/api/game");
|
||||
app.MapHub<API.Controllers.GameHubSocket>("/api/gamehub");
|
||||
|
||||
// SPA fallback: serve index.html for routes not matched by API or static files
|
||||
app.MapFallbackToFile("index.html");
|
||||
|
||||
app.Run();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace API.Services.GameManager
|
||||
{
|
||||
public class GameManager : IGameManager
|
||||
{
|
||||
public int CreateGame(string playerName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool JoinGame(string playerName, int gameCode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private int GenerateNonExistingGameCode()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
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