Skip to content

Commit 6cb849f

Browse files
committed
refactor: move youAre, me and opponent fields to BaseGame
1 parent 7e0603b commit 6cb849f

10 files changed

+37
-55
lines changed

lib/src/model/correspondence/correspondence_game_storage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class CorrespondenceGameStorage {
115115
Future<void> save(OfflineCorrespondenceGame game) async {
116116
try {
117117
await _db.insert(kCorrespondenceStorageTable, {
118-
'userId': game.me.user?.id.toString() ?? kCorrespondenceStorageAnonId,
118+
'userId': game.me?.user?.id.toString() ?? kCorrespondenceStorageAnonId,
119119
'gameId': game.id.toString(),
120120
'lastModified': DateTime.now().toIso8601String(),
121121
'data': jsonEncode(game.toJson()),

lib/src/model/correspondence/correspondence_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class CorrespondenceService {
218218
perf: game.meta.perf,
219219
white: game.white,
220220
black: game.black,
221-
youAre: game.youAre!,
221+
youAre: game.youAre,
222222
daysPerTurn: game.meta.daysPerTurn,
223223
clock: game.correspondenceClock,
224224
winner: game.winner,

lib/src/model/correspondence/offline_correspondence_game.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ part 'offline_correspondence_game.freezed.dart';
1414
part 'offline_correspondence_game.g.dart';
1515

1616
/// An offline correspondence game.
17+
///
18+
/// This is always a game of the current user, so [youAre], [me] and [opponent]
19+
/// are always guaranteed to be non-null.
1720
@Freezed(fromJson: true, toJson: true)
1821
class OfflineCorrespondenceGame
1922
with _$OfflineCorrespondenceGame, BaseGame, IndexableSteps
@@ -35,7 +38,7 @@ class OfflineCorrespondenceGame
3538
required Perf perf,
3639
required Player white,
3740
required Player black,
38-
required Side youAre,
41+
required Side? youAre,
3942
int? daysPerTurn,
4043
Side? winner,
4144
bool? isThreefoldRepetition,
@@ -45,22 +48,19 @@ class OfflineCorrespondenceGame
4548
factory OfflineCorrespondenceGame.fromJson(Map<String, dynamic> json) =>
4649
_$OfflineCorrespondenceGameFromJson(json);
4750

48-
Side get orientation => youAre;
51+
Side get orientation => youAre!;
4952

5053
@override
5154
IList<Duration>? get clocks => null;
5255

5356
@override
5457
IList<ExternalEval>? get evals => null;
5558

56-
Player get me => youAre == Side.white ? white : black;
57-
Player get opponent => youAre == Side.white ? black : white;
58-
5959
Side get sideToMove => lastPosition.turn;
6060

6161
bool get isMyTurn => sideToMove == youAre;
6262

63-
Duration? myTimeLeft(DateTime lastModifiedTime) => estimatedTimeLeft(youAre, lastModifiedTime);
63+
Duration? myTimeLeft(DateTime lastModifiedTime) => estimatedTimeLeft(youAre!, lastModifiedTime);
6464

6565
Duration? estimatedTimeLeft(Side side, DateTime lastModifiedTime) {
6666
final timeLeft = side == Side.white ? clock?.white : clock?.black;

lib/src/model/game/archived_game.dart

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,6 @@ class ArchivedGame with _$ArchivedGame, BaseGame, IndexableSteps implements Base
6464

6565
/// Create an archived game from a local storage JSON.
6666
factory ArchivedGame.fromJson(Map<String, dynamic> json) => _$ArchivedGameFromJson(json);
67-
68-
/// Player point of view. Null if spectating.
69-
Player? get me =>
70-
youAre == null
71-
? null
72-
: youAre == Side.white
73-
? white
74-
: black;
75-
76-
/// Opponent point of view. Null if spectating.
77-
Player? get opponent =>
78-
youAre == null
79-
? null
80-
: youAre == Side.white
81-
? black
82-
: white;
8367
}
8468

8569
/// A [LightArchivedGame] associated with a point of view of a player.

lib/src/model/game/game.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,33 @@ abstract mixin class BaseGame {
3939

4040
GameStatus get status;
4141

42+
/// This field is null if the game is being watched as a spectator, and
43+
/// represents the side that the current player is playing as otherwise.
44+
Side? get youAre;
45+
4246
Side? get winner;
4347

4448
bool? get isThreefoldRepetition;
4549

4650
Player get white;
4751
Player get black;
4852

53+
/// Player of the playing point of view. Null if spectating.
54+
Player? get me =>
55+
youAre == null
56+
? null
57+
: youAre == Side.white
58+
? white
59+
: black;
60+
61+
/// Opponent from the playing point of view. Null if spectating.
62+
Player? get opponent =>
63+
youAre == null
64+
? null
65+
: youAre == Side.white
66+
? black
67+
: white;
68+
4969
Position get lastPosition;
5070

5171
Side? playerSideOf(UserId id) {

lib/src/model/game/playable_game.dart

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,6 @@ class PlayableGame with _$PlayableGame, BaseGame, IndexableSteps implements Base
7373
return _playableGameFromPick(pick(json).required());
7474
}
7575

76-
/// Player of the playing point of view. Null if spectating.
77-
Player? get me =>
78-
youAre == null
79-
? null
80-
: youAre == Side.white
81-
? white
82-
: black;
83-
84-
/// Opponent from the playing point of view. Null if spectating.
85-
Player? get opponent =>
86-
youAre == null
87-
? null
88-
: youAre == Side.white
89-
? black
90-
: white;
91-
9276
Side get sideToMove => lastPosition.turn;
9377

9478
bool get hasAI => white.isAI || black.isAI;

lib/src/view/correspondence/offline_correspondence_game_screen.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class _BodyState extends ConsumerState<_Body> {
181181
child: SafeArea(
182182
bottom: false,
183183
child: BoardTable(
184-
orientation: isBoardTurned ? youAre.opposite : youAre,
184+
orientation: isBoardTurned ? youAre!.opposite : youAre!,
185185
fen: position.fen,
186186
lastMove: game.moveAt(stepCursor) as NormalMove?,
187187
gameData: GameData(
@@ -229,7 +229,7 @@ class _BodyState extends ConsumerState<_Body> {
229229
builder:
230230
(_) => AnalysisScreen(
231231
options: AnalysisOptions(
232-
orientation: game.youAre,
232+
orientation: game.youAre!,
233233
standalone: (
234234
pgn: game.makePgn(),
235235
isComputerAnalysisAllowed: false,

lib/src/view/game/game_player.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import 'package:flutter/cupertino.dart';
77
import 'package:flutter/material.dart';
88
import 'package:flutter_riverpod/flutter_riverpod.dart';
99
import 'package:lichess_mobile/src/constants.dart';
10-
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
1110
import 'package:lichess_mobile/src/model/common/service/sound_service.dart';
1211
import 'package:lichess_mobile/src/model/game/game.dart';
13-
import 'package:lichess_mobile/src/model/game/game_status.dart';
1412
import 'package:lichess_mobile/src/model/game/material_diff.dart';
1513
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
1614
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
@@ -26,7 +24,7 @@ import 'package:lichess_mobile/src/view/user/user_screen.dart';
2624
import 'package:lichess_mobile/src/widgets/buttons.dart';
2725

2826
/// A widget to display player information above/below the chess board.
29-
class GamePlayer extends ConsumerWidget {
27+
class GamePlayer extends StatelessWidget {
3028
const GamePlayer({
3129
required this.game,
3230
required this.side,
@@ -61,14 +59,12 @@ class GamePlayer extends ConsumerWidget {
6159
final Duration? timeToMove;
6260

6361
@override
64-
Widget build(BuildContext context, WidgetRef ref) {
62+
Widget build(BuildContext context) {
6563
final remaingHeight = estimateRemainingHeightLeftBoard(context);
6664
final playerFontSize = remaingHeight <= kSmallRemainingHeightLeftBoardThreshold ? 14.0 : 16.0;
6765

6866
final player = side == Side.white ? game.white : game.black;
6967

70-
final session = ref.watch(authSessionProvider);
71-
7268
final playerWidget = Column(
7369
mainAxisAlignment: MainAxisAlignment.center,
7470
crossAxisAlignment: CrossAxisAlignment.start,
@@ -129,9 +125,7 @@ class GamePlayer extends ConsumerWidget {
129125
if (player.rating != null)
130126
RatingPrefAware(
131127
isActiveGameOfCurrentUser:
132-
game.status.value < GameStatus.aborted.value &&
133-
session != null &&
134-
game.playerSideOf(session.user.id) != null,
128+
game.status.value < GameStatus.aborted.value && game.me != null,
135129
child: Text.rich(
136130
TextSpan(
137131
text: ' ${player.rating}${player.provisional == true ? '?' : ''}',

lib/src/view/game/offline_correspondence_games_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class OfflineCorrespondenceGamePreview extends ConsumerWidget {
6767
crossAxisAlignment: CrossAxisAlignment.start,
6868
mainAxisAlignment: MainAxisAlignment.spaceAround,
6969
children: [
70-
UserFullNameWidget(user: game.opponent.user, style: Styles.boardPreviewTitle),
70+
UserFullNameWidget(user: game.opponent!.user, style: Styles.boardPreviewTitle),
7171
if (game.myTimeLeft(lastModified) != null)
7272
Text(relativeDate(context.l10n, DateTime.now().add(game.myTimeLeft(lastModified)!))),
7373
Icon(game.perf.icon, size: 40, color: DefaultTextStyle.of(context).style.color),

lib/src/view/home/home_tab_screen.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,10 @@ class _OfflineCorrespondenceCarousel extends ConsumerWidget {
537537
perf: el.$2.perf,
538538
speed: el.$2.speed,
539539
variant: el.$2.variant,
540-
opponent: el.$2.opponent.user,
540+
opponent: el.$2.opponent!.user,
541541
isMyTurn: el.$2.isMyTurn,
542-
opponentRating: el.$2.opponent.rating,
543-
opponentAiLevel: el.$2.opponent.aiLevel,
542+
opponentRating: el.$2.opponent!.rating,
543+
opponentAiLevel: el.$2.opponent!.aiLevel,
544544
lastMove: el.$2.lastMove,
545545
secondsLeft: el.$2.myTimeLeft(el.$1)?.inSeconds,
546546
),

0 commit comments

Comments
 (0)