Skip to content

Commit

Permalink
fixing showing points option when logging cooperative games (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkieres authored Apr 28, 2024
1 parent 81cd9b5 commit 9a0e1e4
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class PlaythroughDetails with _$PlaythroughDetails {
List<PlayerScore> get tiedPlayerScores =>
playerScores.where((playerScore) => playerScore.id != null && playerScore.isTied).toList();

GameClassification get playerScoreBasedGameClassification {
GameClassification? get playerScoreBasedGameClassification {
if (!playerScores.any((playerScore) => playerScore.score.hasScore)) {
return null;
}

if (playerScores.any((playerScore) => playerScore.score.noScoreGameResult != null)) {
return GameClassification.NoScore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ abstract class _EditPlaythoughViewModel with Store {
}

@computed
CooperativeGameResult? get cooperativeGameResult =>
playerScores.first.score.noScoreGameResult?.cooperativeGameResult;
CooperativeGameResult? get cooperativeGameResult => _playthroughDetailsWorkingCopy
?.playerScores.first.score.noScoreGameResult?.cooperativeGameResult;

bool get isDirty => _playthroughDetailsWorkingCopy != playthroughDetails;

Expand Down Expand Up @@ -363,7 +363,11 @@ abstract class _EditPlaythoughViewModel with Store {
}

void _updateEditPlaythroughPageVisualState() {
switch (_playthroughDetailsWorkingCopy!.playerScoreBasedGameClassification) {
final gameClassificaition =
_playthroughDetailsWorkingCopy!.playerScoreBasedGameClassification ??
_gamePlaythroughsDetailsStore.gameClassification;

switch (gameClassificaition) {
case GameClassification.Score:
editPlaythroughPageVisualState = EditPlaythroughPageVisualStates.editScoreGame(
gameFamily: _gamePlaythroughsDetailsStore.gameGameFamily,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:board_games_companion/common/enums/game_classification.dart';
import 'package:board_games_companion/common/enums/game_family.dart';
import 'package:board_games_companion/models/hive/no_score_game_result.dart';
import 'package:board_games_companion/models/hive/player.dart';
import 'package:board_games_companion/models/hive/playthrough.dart';
import 'package:board_games_companion/models/hive/score.dart';
import 'package:board_games_companion/models/hive/score_game_results.dart';
import 'package:board_games_companion/models/player_score.dart';
import 'package:board_games_companion/models/playthroughs/playthrough_details.dart';
import 'package:board_games_companion/pages/edit_playthrough/edit_playthrough_page_visual_states.dart';
import 'package:board_games_companion/pages/edit_playthrough/edit_playthrough_view_model.dart';
import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -53,7 +55,6 @@ void main() {
editPlaythrouhgViewModel = EditPlaythoughViewModel(
mockGamePlaythroughsDetailsStore,
);
editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);
});

tearDown(() {
Expand All @@ -64,6 +65,7 @@ void main() {
'GIVEN edit playthrough view model '
'WHEN setting a playthrough with an id '
'THEN then the playthrough details should reflect that ', () {
editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);
expect(editPlaythrouhgViewModel.playthroughDetails, mockPlaythroughDetails);
});

Expand All @@ -75,6 +77,7 @@ void main() {
.firstWhereOrNull((element) => element.id == mockEmptyPlayerScoreId);
const newScore = 10.0;

editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);
editPlaythrouhgViewModel.updatePlayerScore(playerScoreToUpdate!.id!, newScore);

final updatedPlayerScore = editPlaythrouhgViewModel.playerScores
Expand Down Expand Up @@ -122,11 +125,137 @@ void main() {
.firstWhereOrNull((playerScore) => playerScore.player!.id == firstPlayerId);
const newScore = 20.0;

editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);
editPlaythrouhgViewModel.updatePlayerScore(playerScoreToUpdate!.id!, newScore);

for (final playerScore in editPlaythrouhgViewModel.playerScores) {
expect(playerScore.score.isTied, isFalse);
expect(playerScore.score.scoreGameResult!.tiebreakerType, isNull);
}
});

test(
'GIVEN a non score game '
'WHEN a player scored a cooperative game '
'THEN cooperative game result should reflect that score ', () {
const firstPlayerId = '1';
const cooperativeResult = CooperativeGameResult.win;
when(() => mockGamePlaythroughsDetailsStore.playthroughsDetails).thenReturn(
ObservableList.of(
[
mockPlaythroughDetails.copyWith(
playerScores: [
emptyPlayerScore.copyWith(
player: const Player(id: firstPlayerId),
score: emptyScore.copyWith(
noScoreGameResult: const NoScoreGameResult(
cooperativeGameResult: cooperativeResult,
),
),
),
],
)
],
),
);

editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);

expect(editPlaythrouhgViewModel.cooperativeGameResult, cooperativeResult);
});

test(
'GIVEN a non score game '
'WHEN a non of the players scored '
'THEN cooperative game result should be null ', () {
const firstPlayerId = '1';
when(() => mockGamePlaythroughsDetailsStore.playthroughsDetails).thenReturn(
ObservableList.of(
[
mockPlaythroughDetails.copyWith(
playerScores: [
emptyPlayerScore.copyWith(
player: const Player(id: firstPlayerId),
score: emptyScore,
),
],
)
],
),
);

editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);

expect(editPlaythrouhgViewModel.cooperativeGameResult, null);
});

group('GIVEN edit playthrough page visual state', () {
test(
'WHEN players have no scores yet '
'THEN visual state should be based on the game setting classification ', () {
const gameFamily = GameFamily.Cooperative;
when(() => mockGamePlaythroughsDetailsStore.gameClassification)
.thenReturn(GameClassification.NoScore);
when(() => mockGamePlaythroughsDetailsStore.gameGameFamily).thenReturn(gameFamily);
when(() => mockGamePlaythroughsDetailsStore.playthroughsDetails).thenReturn(
ObservableList.of(
[
mockPlaythroughDetails.copyWith(
playerScores: [
emptyPlayerScore.copyWith(
player: const Player(id: '1'),
score: emptyScore,
),
emptyPlayerScore.copyWith(
player: const Player(id: '2'),
score: emptyScore,
),
],
)
],
),
);

editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);

expect(editPlaythrouhgViewModel.editPlaythroughPageVisualState,
const EditPlaythroughPageVisualStates.editNoScoreGame(gameFamily: gameFamily));
});

// MK We need this behaviour in order to support / migrate results that were logged for a game
// which classification was changed after logging games
test(
'WHEN players have a score '
'THEN visual state should be based on their score classificiation ', () {
const gameFamily = GameFamily.Cooperative;
when(() => mockGamePlaythroughsDetailsStore.gameClassification)
.thenReturn(GameClassification.NoScore);
when(() => mockGamePlaythroughsDetailsStore.gameGameFamily).thenReturn(gameFamily);
when(() => mockGamePlaythroughsDetailsStore.playthroughsDetails).thenReturn(
ObservableList.of(
[
mockPlaythroughDetails.copyWith(
playerScores: [
emptyPlayerScore.copyWith(
player: const Player(id: '1'),
score: emptyScore.copyWith(
scoreGameResult: const ScoreGameResult(points: 10),
),
),
emptyPlayerScore.copyWith(
player: const Player(id: '2'),
score: emptyScore,
),
],
)
],
),
);

editPlaythrouhgViewModel.setPlaythroughId(mockPlaythroughId);

expect(editPlaythrouhgViewModel.editPlaythroughPageVisualState,
const EditPlaythroughPageVisualStates.editScoreGame(gameFamily: gameFamily));
});
});
}

0 comments on commit 9a0e1e4

Please sign in to comment.