Skip to content

Commit 498860e

Browse files
committed
Created GameCancelledMessage record and implemented the CancelGameCommand in the GamePageViewModel.
1 parent ebeff98 commit 498860e

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

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 GameCancelledMessage();

src/Codebreaker.ViewModels/Pages/GamePageViewModel.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,31 @@ private async Task MakeMoveAsync(CancellationToken cancellationToken)
119119
IsLoading = false;
120120
}
121121
}
122+
123+
private bool CanCancelGame() => Game is not null && !Game.IsFinished;
124+
125+
[RelayCommand(CanExecute = nameof(CanCancelGame))]
126+
private async Task CancelGameAsync(CancellationToken cancellationToken)
127+
{
128+
if (Game is null)
129+
throw new InvalidOperationException("Cannot cancel a not-started game.");
130+
131+
try
132+
{
133+
await gamesClient.CancelGameAsync(Game.Id, Game.PlayerName, Game.GameType, cancellationToken);
134+
}
135+
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.BadRequest)
136+
{
137+
infoBarService.New.WithMessage("Could not cancel the game").Show();
138+
return;
139+
}
140+
catch (HttpRequestException)
141+
{
142+
infoBarService.New.WithMessage("Networking issues").Show();
143+
return;
144+
}
145+
146+
Game = null;
147+
WeakReferenceMessenger.Default.Send(new GameCancelledMessage());
148+
}
122149
}

0 commit comments

Comments
 (0)