Add player tags, game lifecycle and win checks

Additions and refactors across controllers, models, repo and services to support game lifecycle and win detection. Key changes:

- Models: Player now has PlayerTag; Game assigns tags, starts when 2 players join, exposes GetPlayerByConnectionId, StartGame and EndGame helpers.
- GameField: Implemented win-detection (CountInDirection, CheckLine, CheckForWin) and minor backing-field init.
- Repository/API: GameRepository.Create no longer accepts a Player (repo only creates games); minor variable cleanups; IGameRepository signature updated accordingly.
- Services: GameManager now injects IHubContext<GameHubSocket>, moves player-adding into manager (CreateGame), makes JoinGame async and notifies the SignalR group when a game starts, and adds a placeholder async Place method. Controller (GameHubSocket) also exposes a Place method stub to match the service API.

Motivation: separate responsibilities so repository only creates games, manager handles player joins and notifications, and add core game logic (player tagging and win checks) needed for gameplay. Place methods are added as placeholders for future move handling.
This commit is contained in:
Jonas
2026-03-03 19:12:54 +01:00
parent 85521f3b23
commit 955d1e18c6
8 changed files with 104 additions and 13 deletions
+22
View File
@@ -22,7 +22,14 @@ public class Game(Coordinates gFs, SixDigitInt gameCode)
if(Players.Count >= 2)
return false;
player.PlayerTag = Players.Count + 1;
Players.Add(player);
if (Players.Count == 2)
{
StartGame();
}
return true;
}
@@ -30,4 +37,19 @@ public class Game(Coordinates gFs, SixDigitInt gameCode)
{
Players.RemoveAll(x => x.ConnectionId == playerConnectionId);
}
public Player? GetPlayerByConnectionId(string playerConnectionId)
{
return Players.FirstOrDefault(x => x.ConnectionId == playerConnectionId);
}
private void StartGame()
{
State = GameState.Running;
}
public void EndGame()
{
State = GameState.Ended;
}
}
+53
View File
@@ -27,6 +27,7 @@ public enum FieldState
public class GameField(Coordinates gFs)
{
private Coordinates _gFs = gFs;
public int[,] CurrentField { get; } = new int[gFs.Y, gFs.X];
public int[,] BackupField { get; } = new int[gFs.Y, gFs.X];
@@ -72,6 +73,58 @@ public class GameField(Coordinates gFs)
};
}
private int CountInDirection(int startX, int startY, int dx, int dy, int player)
{
var count = 0;
var x = startX;
var y = startY;
while (
x >= 0 && x < CurrentField.GetLength(1) &&
y >= 0 && y < CurrentField.GetLength(0) &&
CurrentField[y, x] == player)
{
count++;
x += dx;
y += dy;
}
return count;
}
private bool CheckLine(int startX, int startY, int dx, int dy, int player)
{
int count = CountInDirection(startX + dx, startY + dy, dx, dy, player);
count += CountInDirection(startX - dx, startY - dy, -dx, -dy, player);
return count + 1 >= 4;
}
public int CheckForWin()
{
int rows = CurrentField.GetLength(0);
int cols = CurrentField.GetLength(1);
(int dx, int dy)[] directions = [(1, 0), (0, 1), (1, 1), (-1, 1)];
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
int player = CurrentField[y, x];
if (player != 1 && player != 2) continue;
foreach (var (dx, dy) in directions)
{
if (CheckLine(x, y, dx, dy, player))
return player;
}
}
}
return 0;
}
private void CreateSave()
{
Array.Copy(CurrentField, BackupField, CurrentField.Length);
+1
View File
@@ -4,4 +4,5 @@ public class Player(string name, string connectionId)
{
public string Name { get; set; } = name;
public string ConnectionId { get; set; } = connectionId;
public int PlayerTag { get; set; }
}