Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,6 @@ public async Task<Result<ProfileDto>> Handle(UpdateProfileAliasCommand request,
profile.UpdateAlias(newAlias);
await profileRepository.SaveAsync(profile);

var games = await gameRepository.GetByPlayerAsync(oldAlias);
foreach (var game in games)
{
try
{
// Update playerAlias via reconstitution is not straightforward;
// games store alias as a string field - update via SaveAsync after mutation
// The domain game does not expose alias mutation — we need to handle this at document level
// For now, we log a note; a full implementation would update the document directly
logger.LogDebug("Game {GameId} associated with old alias {OldAlias} — batch update pending", game.Id, oldAlias.Value);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to update game {GameId} for alias change", game.Id);
}
}

logger.LogInformation("Updated alias from {OldAlias} to {NewAlias} for profile {ProfileId}",
oldAlias.Value, newAlias.Value, profile.Id);
return Result<ProfileDto>.Success(ProfileDto.FromProfile(profile));
Expand Down
2 changes: 1 addition & 1 deletion src/backend/Sudoku.Domain/Entities/SudokuGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void AddPossibleValue(int row, int column, int value)

if (cell.HasValue)
{
throw new InvalidOperationException($"Cannot add possible values to cell with a definite value at position ({row}, {column})");
throw new CellAlreadyHasValueException($"Cannot add possible values to cell with a definite value at position ({row}, {column})");
}

cell.AddPossibleValue(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Sudoku.Domain.Exceptions;

public class CellAlreadyHasValueException(string message) : DomainException(message);
11 changes: 2 additions & 9 deletions src/backend/Sudoku.Domain/ValueObjects/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void SetValue(int value)
}

Value = value;
// Clear possible values when a definite value is set
PossibleValues.Clear();
}

Expand All @@ -83,7 +82,6 @@ public void SetValue(int? value)

Value = value;

// Clear possible values when a definite value is set
if (value.HasValue)
{
PossibleValues.Clear();
Expand All @@ -109,7 +107,7 @@ public void AddPossibleValue(int value)

if (Value.HasValue)
{
throw new InvalidOperationException($"Cannot add possible values to cell with a definite value at position ({Row}, {Column})");
throw new CellAlreadyHasValueException($"Cannot add possible values to cell with a definite value at position ({Row}, {Column})");
}

if (value < 1 || value > 9)
Expand Down Expand Up @@ -153,15 +151,10 @@ public override string ToString()
return Value?.ToString() ?? ".";
}

/// <summary>
/// Creates a deep copy of the current cell.
/// </summary>
/// <returns>A deep copy of the current cell with all possible values copied.</returns>
public Cell DeepCopy()
{
var clonedCell = new Cell(Row, Column, Value, IsFixed);

// Deep copy the possible values

foreach (var possibleValue in PossibleValues)
{
clonedCell.PossibleValues.Add(possibleValue);
Expand Down
17 changes: 13 additions & 4 deletions src/backend/Sudoku.Infrastructure/Services/PuzzleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ namespace Sudoku.Infrastructure.Services;

public class PuzzleGenerator(IPuzzleSolver puzzleSolver) : IPuzzleGenerator
{
private const int EASY_EMPTY_MIN = 40;
private const int EASY_EMPTY_MAX = 45;
private const int MEDIUM_EMPTY_MIN = 46;
private const int MEDIUM_EMPTY_MAX = 49;
private const int HARD_EMPTY_MIN = 50;
private const int HARD_EMPTY_MAX = 53;
private const int EXPERT_EMPTY_MIN = 54;
private const int EXPERT_EMPTY_MAX = 58;

public async Task<SudokuPuzzle> GeneratePuzzleAsync(GameDifficulty difficulty)
{
var puzzle = await GenerateEmptyPuzzleAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -41,10 +50,10 @@ private SudokuPuzzle CreateEmptyCells(SudokuPuzzle puzzle, GameDifficulty diffic
{
var numberOfEmptyCells = difficulty.Name switch
{
"Easy" => RandomGenerator.RandomNumber(40, 45),
"Medium" => RandomGenerator.RandomNumber(46, 49),
"Hard" => RandomGenerator.RandomNumber(50, 53),
"Expert" => RandomGenerator.RandomNumber(54, 58),
"Easy" => RandomGenerator.RandomNumber(EASY_EMPTY_MIN, EASY_EMPTY_MAX),
"Medium" => RandomGenerator.RandomNumber(MEDIUM_EMPTY_MIN, MEDIUM_EMPTY_MAX),
"Hard" => RandomGenerator.RandomNumber(HARD_EMPTY_MIN, HARD_EMPTY_MAX),
"Expert" => RandomGenerator.RandomNumber(EXPERT_EMPTY_MIN, EXPERT_EMPTY_MAX),
_ => 0
};
var emptyCellCoords = new List<(int Row, int Col)>();
Expand Down
4 changes: 2 additions & 2 deletions src/backend/Tests/Domain/CellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public void AddPossibleValue_OnFixedCell_ThrowsCellIsFixedException()
}

[Fact]
public void AddPossibleValue_OnCellWithValue_ThrowsInvalidOperationException()
public void AddPossibleValue_OnCellWithValue_ThrowsCellAlreadyHasValueException()
{
// Arrange
var cell = Cell.Create(5, 5, 7);
Expand All @@ -362,7 +362,7 @@ public void AddPossibleValue_OnCellWithValue_ThrowsInvalidOperationException()
Action act = () => cell.AddPossibleValue(3);

// Assert
act.Should().Throw<InvalidOperationException>()
act.Should().Throw<CellAlreadyHasValueException>()
.WithMessage($"*Cannot add possible values to cell with a definite value*");
}

Expand Down
4 changes: 2 additions & 2 deletions src/backend/Tests/Domain/SudokuGamePossibleValuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void AddPossibleValue_ToFixedCell_ThrowsCellIsFixedException()
}

[Fact]
public void AddPossibleValue_ToCellWithValue_ThrowsInvalidOperationException()
public void AddPossibleValue_ToCellWithValue_ThrowsCellAlreadyHasValueException()
{
// Arrange
var game = SudokuGame.Create(_playerAlias, _difficulty, _initialCells);
Expand All @@ -69,7 +69,7 @@ public void AddPossibleValue_ToCellWithValue_ThrowsInvalidOperationException()
Action act = () => game.AddPossibleValue(row, col, value);

// Assert
act.Should().Throw<InvalidOperationException>()
act.Should().Throw<CellAlreadyHasValueException>()
.WithMessage($"*Cannot add possible values to cell with a definite value*");
}

Expand Down
Loading