Skip to content

Commit 1be7057

Browse files
authored
Merge pull request #1435 from julien4215/add-broadcast-result
Add new broadcast game results
2 parents 349fc13 + 78c6f5a commit 1be7057

File tree

4 files changed

+53
-38
lines changed

4 files changed

+53
-38
lines changed

lib/src/model/broadcast/broadcast.dart

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,54 @@ part 'broadcast.freezed.dart';
77

88
typedef BroadcastList = ({IList<Broadcast> active, IList<Broadcast> past, int? nextPage});
99

10-
enum BroadcastResult { whiteWins, blackWins, draw, ongoing, noResultPgnTag }
10+
enum BroadcastResult {
11+
whiteWins,
12+
blackWins,
13+
draw,
14+
canceled,
15+
whiteHalfWins,
16+
blackHalfWins,
17+
ongoing,
18+
noResultPgnTag;
19+
20+
static BroadcastResult resultFromString(String? result) {
21+
return (result == null)
22+
? BroadcastResult.noResultPgnTag
23+
: switch (result) {
24+
'½-½' => BroadcastResult.draw,
25+
'1-0' => BroadcastResult.whiteWins,
26+
'0-1' => BroadcastResult.blackWins,
27+
'0-0' => BroadcastResult.canceled,
28+
'½-0' => BroadcastResult.whiteHalfWins,
29+
'0-½' => BroadcastResult.blackHalfWins,
30+
'*' => BroadcastResult.ongoing,
31+
_ => throw FormatException("value $result can't be interpreted as a broadcast result"),
32+
};
33+
}
34+
35+
String resultToString(Side side) {
36+
return switch (this) {
37+
whiteWins => side == Side.white ? '1' : '0',
38+
blackWins => side == Side.white ? '0' : '1',
39+
draw => '½',
40+
canceled => '0',
41+
whiteHalfWins => side == Side.white ? '½' : '0',
42+
blackHalfWins => side == Side.white ? '0' : '½',
43+
_ => throw FormatException('result $this is not a game that is over'),
44+
};
45+
}
46+
47+
bool get isOngoing => switch (this) {
48+
ongoing => true,
49+
_ => false,
50+
};
51+
52+
bool get isOver => switch (this) {
53+
ongoing => false,
54+
noResultPgnTag => false,
55+
_ => true,
56+
};
57+
}
1158

1259
@freezed
1360
class Broadcast with _$Broadcast {
@@ -100,11 +147,8 @@ class BroadcastGame with _$BroadcastGame {
100147
int? mate,
101148
}) = _BroadcastGame;
102149

103-
bool get isOngoing => status == BroadcastResult.ongoing;
104-
bool get isOver =>
105-
status == BroadcastResult.draw ||
106-
status == BroadcastResult.whiteWins ||
107-
status == BroadcastResult.blackWins;
150+
bool get isOngoing => status.isOngoing;
151+
bool get isOver => status.isOver;
108152
Side get sideToMove => Setup.parseFen(fen).turn;
109153
}
110154

lib/src/model/broadcast/broadcast_repository.dart

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,7 @@ BroadcastRoundGames _gamesFromPick(RequiredPick pick) =>
149149
MapEntry<BroadcastGameId, BroadcastGame> gameFromPick(RequiredPick pick) {
150150
final stringStatus = pick('status').asStringOrNull();
151151

152-
final status =
153-
(stringStatus == null)
154-
? BroadcastResult.noResultPgnTag
155-
: switch (stringStatus) {
156-
'½-½' => BroadcastResult.draw,
157-
'1-0' => BroadcastResult.whiteWins,
158-
'0-1' => BroadcastResult.blackWins,
159-
'*' => BroadcastResult.ongoing,
160-
_ =>
161-
throw FormatException(
162-
"value $stringStatus can't be interpreted as a broadcast result",
163-
),
164-
};
152+
final status = BroadcastResult.resultFromString(stringStatus);
165153

166154
/// The amount of time that the player whose turn it is has been thinking since his last move
167155
final thinkTime = pick('thinkTime').asDurationFromSecondsOrNull() ?? Duration.zero;

lib/src/view/broadcast/broadcast_boards_tab.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,7 @@ class _PlayerWidget extends StatelessWidget {
341341
const SizedBox(width: 5),
342342
if (game.isOver)
343343
Text(
344-
(gameStatus == BroadcastResult.draw)
345-
? '½'
346-
: (gameStatus == BroadcastResult.whiteWins)
347-
? side == Side.white
348-
? '1'
349-
: '0'
350-
: side == Side.black
351-
? '1'
352-
: '0',
344+
gameStatus.resultToString(side),
353345
style: const TextStyle().copyWith(fontWeight: FontWeight.bold),
354346
)
355347
else if (player.clock != null)

lib/src/view/broadcast/broadcast_game_screen.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
55
import 'package:flutter_riverpod/flutter_riverpod.dart';
66
import 'package:lichess_mobile/src/constants.dart';
77
import 'package:lichess_mobile/src/model/analysis/analysis_preferences.dart';
8-
import 'package:lichess_mobile/src/model/broadcast/broadcast.dart';
98
import 'package:lichess_mobile/src/model/broadcast/broadcast_analysis_controller.dart';
109
import 'package:lichess_mobile/src/model/common/chess.dart';
1110
import 'package:lichess_mobile/src/model/common/eval.dart';
@@ -437,15 +436,7 @@ class _PlayerWidget extends ConsumerWidget {
437436
children: [
438437
if (game.isOver) ...[
439438
Text(
440-
(gameStatus == BroadcastResult.draw)
441-
? '½'
442-
: (gameStatus == BroadcastResult.whiteWins)
443-
? side == Side.white
444-
? '1'
445-
: '0'
446-
: side == Side.black
447-
? '1'
448-
: '0',
439+
gameStatus.resultToString(side),
449440
style: const TextStyle().copyWith(fontWeight: FontWeight.bold),
450441
),
451442
const SizedBox(width: 16.0),

0 commit comments

Comments
 (0)