Skip to content

Commit

Permalink
refactor GamePlayer to infer info from game data + side
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-anders committed Dec 25, 2024
1 parent 2d84b79 commit 7e0603b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 51 deletions.
13 changes: 11 additions & 2 deletions lib/src/model/game/over_the_board_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/model/common/eval.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/user/user.dart';
import 'package:lichess_mobile/src/utils/string.dart';

import 'game.dart';
import 'game_status.dart';
Expand All @@ -19,9 +21,16 @@ abstract class OverTheBoardGame with _$OverTheBoardGame, BaseGame, IndexableStep
const OverTheBoardGame._();

@override
Player get white => const Player();
Player get white => Player(
onGame: true,
user: LightUser(id: UserId(Side.white.name), name: Side.white.name.capitalize()),
);

@override
Player get black => const Player();
Player get black => Player(
onGame: true,
user: LightUser(id: UserId(Side.black.name), name: Side.black.name.capitalize()),
);

@override
IList<ExternalEval>? get evals => null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class _BodyState extends ConsumerState<_Body> {
final youAre = game.youAre;

final black = GamePlayer(
player: game.black,
isActiveGameOfCurrentUser: !game.finished,
game: game,
side: Side.black,
materialDiff: materialDifference.visible ? game.materialDiffAt(stepCursor, Side.black) : null,
materialDifferenceFormat: materialDifference,
shouldLinkToUserProfile: false,
Expand All @@ -153,8 +153,8 @@ class _BodyState extends ConsumerState<_Body> {
: null,
);
final white = GamePlayer(
player: game.white,
isActiveGameOfCurrentUser: !game.finished,
game: game,
side: Side.white,
materialDiff: materialDifference.visible ? game.materialDiffAt(stepCursor, Side.white) : null,
materialDifferenceFormat: materialDifference,
shouldLinkToUserProfile: false,
Expand Down
22 changes: 4 additions & 18 deletions lib/src/view/game/archived_game_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,9 @@ class _BoardBody extends ConsumerWidget {

final isBoardTurned = ref.watch(isBoardTurnedProvider);
final gameCursor = ref.watch(gameCursorProvider(gameData.id));
final black = GamePlayer(
key: const ValueKey('black-player'),
player: gameData.black,
isActiveGameOfCurrentUser: false,
);
final white = GamePlayer(
key: const ValueKey('white-player'),
player: gameData.white,
isActiveGameOfCurrentUser: false,
);
final topPlayer = orientation == Side.white ? black : white;
final bottomPlayer = orientation == Side.white ? white : black;
final loadingBoard = BoardTable(
orientation: (isBoardTurned ? orientation.opposite : orientation),
fen: initialCursor == null ? gameData.lastFen ?? kEmptyBoardFEN : kEmptyBoardFEN,
topTable: topPlayer,
bottomTable: bottomPlayer,
showMoveListPlaceholder: true,
);

Expand All @@ -212,15 +198,15 @@ class _BoardBody extends ConsumerWidget {
final blackClock = game.archivedBlackClockAt(cursor);
final black = GamePlayer(
key: const ValueKey('black-player'),
player: gameData.black,
isActiveGameOfCurrentUser: false,
game: game,
side: Side.black,
clock: blackClock != null ? Clock(timeLeft: blackClock) : null,
materialDiff: game.materialDiffAt(cursor, Side.black),
);
final white = GamePlayer(
key: const ValueKey('white-player'),
player: gameData.white,
isActiveGameOfCurrentUser: false,
game: game,
side: Side.white,
clock: whiteClock != null ? Clock(timeLeft: whiteClock) : null,
materialDiff: game.materialDiffAt(cursor, Side.white),
);
Expand Down
8 changes: 4 additions & 4 deletions lib/src/view/game/game_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class GameBody extends ConsumerWidget {
final archivedWhiteClock = gameState.game.archivedWhiteClockAt(gameState.stepCursor);

final black = GamePlayer(
player: gameState.game.black,
isActiveGameOfCurrentUser: !gameState.game.finished,
game: gameState.game,
side: Side.black,
materialDiff:
boardPreferences.materialDifferenceFormat.visible
? gameState.game.materialDiffAt(gameState.stepCursor, Side.black)
Expand Down Expand Up @@ -159,8 +159,8 @@ class GameBody extends ConsumerWidget {
: null,
);
final white = GamePlayer(
player: gameState.game.white,
isActiveGameOfCurrentUser: !gameState.game.finished,
game: gameState.game,
side: Side.white,
materialDiff:
boardPreferences.materialDifferenceFormat.visible
? gameState.game.materialDiffAt(gameState.stepCursor, Side.white)
Expand Down
27 changes: 17 additions & 10 deletions lib/src/view/game/game_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/constants.dart';
import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/service/sound_service.dart';
import 'package:lichess_mobile/src/model/game/game.dart';
import 'package:lichess_mobile/src/model/game/game_status.dart';
import 'package:lichess_mobile/src/model/game/material_diff.dart';
import 'package:lichess_mobile/src/model/game/player.dart';
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
Expand All @@ -24,10 +26,10 @@ import 'package:lichess_mobile/src/view/user/user_screen.dart';
import 'package:lichess_mobile/src/widgets/buttons.dart';

/// A widget to display player information above/below the chess board.
class GamePlayer extends StatelessWidget {
class GamePlayer extends ConsumerWidget {
const GamePlayer({
required this.player,
required this.isActiveGameOfCurrentUser,
required this.game,
required this.side,
this.clock,
this.materialDiff,
this.materialDifferenceFormat,
Expand All @@ -40,11 +42,9 @@ class GamePlayer extends StatelessWidget {
super.key,
});

final Player player;
final BaseGame game;
final Side side;

/// Whether we're displaying this player as part of an active game of the current user.
/// This might influence whether their rating is shown or not, based on the [ShowRatings] setting.
final bool isActiveGameOfCurrentUser;
final Widget? clock;
final MaterialDiffSide? materialDiff;
final MaterialDifferenceFormat? materialDifferenceFormat;
Expand All @@ -61,10 +61,14 @@ class GamePlayer extends StatelessWidget {
final Duration? timeToMove;

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final remaingHeight = estimateRemainingHeightLeftBoard(context);
final playerFontSize = remaingHeight <= kSmallRemainingHeightLeftBoardThreshold ? 14.0 : 16.0;

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

final session = ref.watch(authSessionProvider);

final playerWidget = Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
Expand Down Expand Up @@ -124,7 +128,10 @@ class GamePlayer extends StatelessWidget {
],
if (player.rating != null)
RatingPrefAware(
isActiveGameOfCurrentUser: isActiveGameOfCurrentUser,
isActiveGameOfCurrentUser:
game.status.value < GameStatus.aborted.value &&
session != null &&
game.playerSideOf(session.user.id) != null,
child: Text.rich(
TextSpan(
text: ' ${player.rating}${player.provisional == true ? '?' : ''}',
Expand Down
11 changes: 2 additions & 9 deletions lib/src/view/over_the_board/over_the_board_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/game/player.dart';
import 'package:lichess_mobile/src/model/over_the_board/over_the_board_clock.dart';
import 'package:lichess_mobile/src/model/over_the_board/over_the_board_game_controller.dart';
import 'package:lichess_mobile/src/model/settings/board_preferences.dart';
import 'package:lichess_mobile/src/model/settings/brightness.dart';
import 'package:lichess_mobile/src/model/settings/over_the_board_preferences.dart';
import 'package:lichess_mobile/src/model/user/user.dart';
import 'package:lichess_mobile/src/utils/immersive_mode.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/string.dart';
import 'package:lichess_mobile/src/view/game/game_player.dart';
import 'package:lichess_mobile/src/view/game/game_result_dialog.dart';
import 'package:lichess_mobile/src/view/over_the_board/configure_over_the_board_game.dart';
Expand Down Expand Up @@ -276,11 +272,8 @@ class _Player extends ConsumerWidget {
return RotatedBox(
quarterTurns: upsideDown ? 2 : 0,
child: GamePlayer(
isActiveGameOfCurrentUser: true,
player: Player(
onGame: true,
user: LightUser(id: UserId(side.name), name: side.name.capitalize()),
),
game: gameState.game,
side: side,
materialDiff:
boardPreferences.materialDifferenceFormat.visible
? gameState.currentMaterialDiff(side)
Expand Down
8 changes: 4 additions & 4 deletions lib/src/view/watch/tv_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class _Body extends ConsumerWidget {
final position = gameState.game.positionAt(gameState.stepCursor);

final blackPlayerWidget = GamePlayer(
player: game.black.setOnGame(true),
isActiveGameOfCurrentUser: false,
game: game.copyWith(black: game.black.setOnGame(true)),
side: Side.black,
clock:
gameState.game.clock != null
? CountdownClockBuilder(
Expand All @@ -107,8 +107,8 @@ class _Body extends ConsumerWidget {
materialDiff: game.lastMaterialDiffAt(Side.black),
);
final whitePlayerWidget = GamePlayer(
player: game.white.setOnGame(true),
isActiveGameOfCurrentUser: false,
game: game.copyWith(white: game.white.setOnGame(true)),
side: Side.white,
clock:
gameState.game.clock != null
? CountdownClockBuilder(
Expand Down

0 comments on commit 7e0603b

Please sign in to comment.