Skip to content

Commit 2244380

Browse files
Merge pull request #174 from CodebreakerApp/57-viewmodels
ViewModel refactoring and simplification
2 parents aca25d9 + 5391cbd commit 2244380

11 files changed

Lines changed: 129 additions & 297 deletions

File tree

src/Codebreaker.ViewModels/Codebreaker.ViewModels.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<ItemGroup>
2828
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
29-
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
29+
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
3030
<PackageReference Include="CNinnovation.Codebreaker.GamesClient" Version="3.6.0-beta.21" />
3131
</ItemGroup>
3232

src/Codebreaker.ViewModels/Components/GameViewModel.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Codebreaker.ViewModels/Components/MoveViewModel.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Codebreaker.ViewModels.Messages;
2+
3+
public record class GameEndedMessage(Game Game)
4+
{
5+
public bool IsVictory => Game.IsVictory;
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace Codebreaker.ViewModels.Messages;
2+
3+
public record class GameStartedMessage(Game Game);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Codebreaker.ViewModels.Messages;
2+
3+
public record class MakeMoveMessage(Move Move)
4+
{
5+
/// <summary>
6+
/// Specifies if the move was already made.
7+
/// </summary>
8+
public bool IsSet => Move.KeyPegs is not null;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Codebreaker.ViewModels.Models;
2+
3+
public partial class Field(string[] possibleColors) : ObservableObject
4+
{
5+
[ObservableProperty]
6+
private string? _color;
7+
8+
public string[] PossibleColors { get; init; } = possibleColors;
9+
}

src/Codebreaker.ViewModels/Models/Game.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
namespace Codebreaker.ViewModels.Models;
22

33
public partial class Game(
4-
Guid gameId,
4+
Guid id,
55
GameType gameType,
66
string playerName,
77
DateTime startTime,
88
int numberCodes,
9-
int maxMoves) : ObservableObject
9+
int maxMoves,
10+
IDictionary<string, string[]> fieldValues) : ObservableObject
1011
{
1112
/// <summary>
1213
/// Gets the unique identifier of the game.
1314
/// </summary>
14-
public Guid GameId { get; private set; } = gameId;
15+
public Guid Id { get; private set; } = id;
1516

1617
/// <summary>
1718
/// Gets the type of the game. <see cref="GameType"/>
@@ -37,20 +38,15 @@ public partial class Game(
3738
/// Gets the end time of the game or null if it did not end yet. This value is set from a game guess anylzer after the game was ended.
3839
/// </summary>
3940
[ObservableProperty]
40-
private string? _endTime;
41+
[NotifyPropertyChangedFor(nameof(IsFinished))]
42+
private DateTime? _endTime;
4143

4244
/// <summary>
4345
/// Gets the duration of the game or null if it did not end yet
4446
/// </summary>
4547
[ObservableProperty]
4648
private TimeSpan? _duration;
4749

48-
/// <summary>
49-
/// Gets the last move number. This number is set from an game move analyer after the move was set.
50-
/// </summary>
51-
[ObservableProperty]
52-
private int _lastMoveNumber;
53-
5450
/// <summary>
5551
/// Gets the number of codes the player needs to fill.
5652
/// </summary>
@@ -61,6 +57,11 @@ public partial class Game(
6157
/// </summary>
6258
public int MaxMoves { get; private set; } = maxMoves;
6359

60+
/// <summary>
61+
/// Gets a boolean value indicating if the game is finished.
62+
/// </summary>
63+
public bool IsFinished => EndTime is not null;
64+
6465
/// <summary>
6566
/// Did the player win the game?
6667
/// </summary>
@@ -70,12 +71,10 @@ public partial class Game(
7071
/// <summary>
7172
/// A list of possible field values the user has to chose from
7273
/// </summary>
73-
public required IDictionary<string, string[]> FieldValues { get; init; }
74+
public IDictionary<string, string[]> FieldValues { get; init; } = fieldValues;
7475

7576
/// <summary>
7677
/// A list of moves the player made
7778
/// </summary>
78-
public ICollection<Move> Moves { get; } = new ObservableCollection<Move>();
79-
80-
public override string ToString() => $"{GameId}:{GameType} - {StartTime}";
79+
public ObservableCollection<Move> Moves { get; } = [];
8180
}
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
namespace Codebreaker.ViewModels.Models;
2-
public partial class Move(Guid moveId, int moveNumber) : ObservableObject
3-
{
4-
public Guid MoveId { get; private set; } = moveId;
5-
6-
/// <summary>
7-
/// The move number for this move within the associated game.
8-
/// </summary>
9-
[ObservableProperty]
10-
private int _moveNumber = moveNumber;
112

3+
public class Move(ICollection<string> guessPegs, ICollection<string>? keyPegs = null)
4+
{
125
/// <summary>
136
/// The guess pegs from the user for this move.
147
/// </summary>
15-
public ObservableCollection<string> GuessPegs { get; } = [];
8+
public ICollection<string> GuessPegs { get; } = guessPegs;
169

1710
/// <summary>
1811
/// The result from the analyer for this move based on the associated game that contains the move.
1912
/// </summary>
20-
public ObservableCollection<string> KeyPegs { get; } = [];
21-
22-
public override string ToString() => $"{MoveNumber}. " +
23-
$"{string.Join('#', GuessPegs)} : " +
24-
$"{string.Join('#', KeyPegs)}";
13+
/// <remarks>
14+
/// Null if the move was not analyzed yet.
15+
/// </remarks>
16+
public ICollection<string>? KeyPegs { get; } = keyPegs;
2517
}

0 commit comments

Comments
 (0)