@@ -20,18 +20,27 @@ public partial class GamePageViewModel(IGamesClient gamesClient, IInfoBarService
2020 [ NotifyCanExecuteChangedFor ( nameof ( StartGameCommand ) ) ]
2121 private string _username = string . Empty ;
2222
23+ [ ObservableProperty ]
24+ private GameType _selectedGameType = GameType . Game6x4 ;
25+
26+ public IEnumerable < GameType > GameTypes { get ; } = [
27+ GameType . Game6x4 ,
28+ GameType . Game6x4Mini ,
29+ GameType . Game8x5 ,
30+ GameType . Game5x5x4
31+ ] ;
32+
2333 private bool CanStartGame ( ) => Username is not null && Username . Length > 2 ;
2434
2535 [ RelayCommand ( CanExecute = nameof ( CanStartGame ) ) ]
2636 private async Task StartGameAsync ( CancellationToken cancellationToken )
2737 {
2838 IsLoading = true ;
29- var usedGameMode = GameType . Game6x4 ;
3039
3140 try
3241 {
33- var response = await gamesClient . StartGameAsync ( usedGameMode , Username ) ;
34- Game = new Game ( response . Id , usedGameMode , Username , DateTime . Now , response . NumberCodes , response . MaxMoves , response . FieldValues ) ;
42+ var response = await gamesClient . StartGameAsync ( SelectedGameType , Username ) ;
43+ Game = new Game ( response . Id , SelectedGameType , Username , DateTime . Now , response . NumberCodes , response . MaxMoves , response . FieldValues ) ;
3544 }
3645 catch ( InvalidOperationException )
3746 {
@@ -59,7 +68,7 @@ private async Task StartGameAsync(CancellationToken cancellationToken)
5968 SelectedFields = Enumerable . Range ( 0 , Game . NumberCodes )
6069 . Select ( i =>
6170 {
62- var field = new Field ( Game . FieldValues [ "colors" ] ) ; // TODO: Hardcoding "colors" is not suitable for all game types
71+ var field = new Field ( ) ;
6372 field . PropertyChanged += ( object ? sender , PropertyChangedEventArgs args ) => MakeMoveCommand . NotifyCanExecuteChanged ( ) ;
6473 return field ;
6574 } )
@@ -74,21 +83,20 @@ private async Task MakeMoveAsync(CancellationToken cancellationToken)
7483 if ( Game is null )
7584 throw new InvalidOperationException ( "A game needs to be started before making a move" ) ;
7685
77- var selectedColors = SelectedFields
78- . Select ( x => x . Color )
79- . ToArray ( ) ;
80-
81- if ( selectedColors . Any ( color => color is null ) )
86+ if ( SelectedFields . Any ( field => field . Color is null ) )
8287 throw new InvalidOperationException ( "All colors need to be selected before making a move" ) ;
8388
84- WeakReferenceMessenger . Default . Send ( new MakeMoveMessage ( new ( selectedColors ! ) ) ) ;
89+ WeakReferenceMessenger . Default . Send ( new MakeMoveMessage ( new ( SelectedFields ) ) ) ;
8590
91+ var serializedFields = SelectedFields . Select ( field => field . Serialize ( ) ) . ToArray ( ) ;
8692 IsLoading = true ;
8793 try
8894 {
89- var response = await gamesClient . SetMoveAsync ( Game . Id , Game . PlayerName , GameType . Game6x4 , Game . Moves . Count + 1 , selectedColors ! ) ;
95+ var response = await gamesClient . SetMoveAsync ( Game . Id , Game . PlayerName , Game . GameType , Game . Moves . Count + 1 , serializedFields ) ;
9096
91- var newMove = new Move ( selectedColors ! , response . Results ) ;
97+ // It is necessary to copy the fields to avoid every move having the same reference to the same fields
98+ var copiedFields = SelectedFields . Select ( f => new Field ( f . Color , f . Shape ) ) . ToArray ( ) ;
99+ var newMove = new Move ( copiedFields , response . Results ) ;
92100 Game . Moves . Add ( newMove ) ;
93101 WeakReferenceMessenger . Default . Send ( new MakeMoveMessage ( newMove ) ) ;
94102
0 commit comments