Add db for backup and memory happiness

This commit is contained in:
jheaps
2025-07-05 18:48:30 -06:00
parent 7a532e52e1
commit 9003d2c4dd
27 changed files with 2682 additions and 245 deletions
@@ -1,9 +1,10 @@
using JoshHeaps.Net.Models;
using JoshHeaps.Net.DAL;
using JoshHeaps.Net.Models;
using JoshHeaps.Net.Services.Interfaces;
namespace JoshHeaps.Net.Services.Implementations;
public class ChessService : IChessService
public class ChessService(ChessDbAccess dbAccess) : IChessService
{
public GameState CreateNewGame()
{
@@ -122,7 +123,7 @@ public class ChessService : IChessService
return legalMoves;
}
public MoveResultDto MakeMove(GameState gameState, MoveDto moveDto)
public async Task<MoveResultDto> MakeMove(GameState gameState, MoveDto moveDto)
{
var piece = gameState.Pieces.FirstOrDefault(p => p.Id == moveDto.PieceId);
@@ -145,6 +146,12 @@ public class ChessService : IChessService
var notation = $"{piece.Id}:{piece.Position}->{targetPos}";
gameState.MoveHistory.Add(notation);
gameState.CurrentPlayer = gameState.CurrentPlayer == PieceColor.White
? PieceColor.Black
: PieceColor.White;
await dbAccess.SaveAsync(gameState);
return new MoveResultDto
{
Success = true,
@@ -166,8 +173,8 @@ public class ChessService : IChessService
piece.Position = targetPos;
gs.Board[targetPos.Row, targetPos.Col] = piece;
if (captured != null && captured != piece)
captured.Position = new Position(-1, -1);
if (captured is not null && captured != piece)
captured.Position = new Position(-1, -1); // Remove captured piece from board
bool wasFirstMove = !piece.HasMoved;
piece.HasMoved = true;
@@ -179,10 +186,6 @@ public class ChessService : IChessService
HandlePawnPromotionIfNeeded(piece, moveDto);
UpdateCastlingRights(gs, piece, oldPos);
gs.CurrentPlayer = gs.CurrentPlayer == PieceColor.White
? PieceColor.Black
: PieceColor.White;
}
private static void HandleEnPassantIfNeeded(GameState gs, ChessPiece piece, Position targetPos, ref ChessPiece? captured)