Skip to content

Commit

Permalink
feat: upgrade to chessground 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-anders committed Aug 3, 2024
1 parent 0419da2 commit 5c06534
Show file tree
Hide file tree
Showing 49 changed files with 372 additions and 447 deletions.
8 changes: 8 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:chessground/chessground.dart';
import 'package:dartchess/dartchess.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -54,6 +56,12 @@ const kTabletBoardTableSidePadding = 16.0;
const kBottomBarHeight = 56.0;
const kMaterialPopupMenuMaxWidth = 500.0;

const ChessboardState kEmptyBoardState = ChessboardState(
fen: kEmptyFen,
interactableSide: InteractableSide.none,
orientation: Side.white,
);

/// The threshold to detect screens with a small remaining height left board.
const kSmallRemainingHeightLeftBoardThreshold = 160;

Expand Down
6 changes: 3 additions & 3 deletions lib/src/model/analysis/analysis_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ class AnalysisController extends _$AnalysisController {
} else {
final uci = n2child['uci'] as String;
final san = n2child['san'] as String;
final move = Move.fromUci(uci)!;
final move = Move.parse(uci)!;
n1.addChild(
Branch(
position: n1.position.playUnchecked(move),
Expand Down Expand Up @@ -656,8 +656,8 @@ class AnalysisState with _$AnalysisState {
IList<PgnComment>? pgnRootComments,
}) = _AnalysisState;

IMap<String, ISet<String>> get validMoves =>
algebraicLegalMoves(currentNode.position);
IMap<Square, ISet<Square>> get validMoves =>
makeLegalMoves(currentNode.position);

/// Whether the user can request server analysis.
///
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/common/chess.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MoveConverter implements JsonConverter<Move, String> {

// assume we are serializing only valid uci strings
@override
Move fromJson(String json) => Move.fromUci(json)!;
Move fromJson(String json) => Move.parse(json)!;

@override
String toJson(Move object) => object.uci;
Expand Down Expand Up @@ -208,7 +208,7 @@ extension ChessExtension on Pick {
return value;
}
if (value is String) {
final move = Move.fromUci(value);
final move = Move.parse(value);
if (move != null) {
return move;
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/model/common/eval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ClientEval with _$ClientEval implements Eval {
Move? get bestMove {
final uci = pvs.firstOrNull?.moves.firstOrNull;
if (uci == null) return null;
return Move.fromUci(uci);
return Move.parse(uci);
}

IList<MoveWithWinningChances> get bestMoves {
Expand Down Expand Up @@ -132,7 +132,7 @@ class PvData with _$PvData {
final List<String> res = [];
for (final uciMove in moves.sublist(0, math.min(12, moves.length))) {
// assume uciMove string is valid as it comes from stockfish
final move = Move.fromUci(uciMove)!;
final move = Move.parse(uciMove)!;
if (pos.isLegal(move)) {
final (newPos, san) = pos.makeSanUnchecked(move);
res.add(san);
Expand All @@ -145,7 +145,7 @@ class PvData with _$PvData {
}

MoveWithWinningChances? _firstMoveWithWinningChances(Side sideToMove) {
final uciMove = (moves.isNotEmpty) ? Move.fromUci(moves.first) : null;
final uciMove = (moves.isNotEmpty) ? Move.parse(moves.first) : null;
return (uciMove != null)
? (
move: uciMove,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/common/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ abstract class Node {
/// castling move and converts it to the corresponding standard castling move if so.
Move? convertAltCastlingMove(Move move) {
return altCastles.containsValue(move.uci)
? Move.fromUci(
? Move.parse(
altCastles.entries.firstWhere((e) => e.value == move.uci).key,
)
: move;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/common/uci.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class UciCharPair with _$UciCharPair {
///
/// Throws an [ArgumentError] if the move is invalid.
factory UciCharPair.fromUci(String uci) {
final move = Move.fromUci(uci);
final move = Move.parse(uci);
if (move == null) {
throw ArgumentError('Invalid uci $uci');
}
Expand All @@ -37,7 +37,7 @@ class UciCharPair with _$UciCharPair {
String.fromCharCode(35 + f),
String.fromCharCode(
p != null
? 35 + 64 + 8 * _promotionRoles.indexOf(p) + squareFile(t)
? 35 + 64 + 8 * _promotionRoles.indexOf(p) + t.file.value
: 35 + t,
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ IList<GameStep> stepsFromJson(String json) {
if (uci == null || san == null) {
break;
}
final move = Move.fromUci(uci)!;
final move = Move.parse(uci)!;
position = position.playUnchecked(move);
steps.add(
GameStep(
Expand Down
7 changes: 3 additions & 4 deletions lib/src/model/game/game_controller.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:async/async.dart';
import 'package:chessground/chessground.dart' as cg;
import 'package:collection/collection.dart';
import 'package:dartchess/dartchess.dart';
import 'package:deep_pick/deep_pick.dart';
Expand Down Expand Up @@ -196,7 +195,7 @@ class GameController extends _$GameController {
}

/// Set or unset a premove.
void setPremove(cg.Move? move) {
void setPremove(Move? move) {
final curState = state.requireValue;
state = AsyncValue.data(
curState.copyWith(
Expand Down Expand Up @@ -530,7 +529,7 @@ class GameController extends _$GameController {
// add opponent move
if (data.ply == curState.game.lastPly + 1) {
final lastPos = curState.game.lastPosition;
final move = Move.fromUci(data.uci)!;
final move = Move.parse(data.uci)!;
final sanMove = SanMove(data.san, move);
final newPos = lastPos.playUnchecked(move);
final newStep = GameStep(
Expand Down Expand Up @@ -924,7 +923,7 @@ class GameState with _$GameState {
int? lastDrawOfferAtPly,
Duration? opponentLeftCountdown,
required bool stopClockWaitingForServerAck,
cg.Move? premove,
Move? premove,

/// Game only setting to override the account preference
bool? moveConfirmSettingOverride,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/puzzle/puzzle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class LitePuzzle with _$LitePuzzle {

(Side, String, Move) get preview {
final pos1 = Chess.fromSetup(Setup.parseFen(fen));
final move = Move.fromUci(solution.first);
final move = Move.parse(solution.first);
final pos = pos1.play(move!);
return (pos.turn, pos.fen, move);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/model/puzzle/puzzle_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class PuzzleController extends _$PuzzleController {
// another puzzle move: let's continue
else if (nextUci != null) {
await Future<void>.delayed(const Duration(milliseconds: 500));
_addMove(Move.fromUci(nextUci)!);
_addMove(Move.parse(nextUci)!);
}
// no more puzzle move: it's a win
else {
Expand Down Expand Up @@ -205,7 +205,7 @@ class PuzzleController extends _$PuzzleController {
state = state.copyWith.streak!(hasSkipped: true);
final moveIndex = state.currentPath.size - state.initialPath.size;
final solution = state.puzzle.puzzle.solution[moveIndex];
onUserMove(Move.fromUci(solution)!);
onUserMove(Move.parse(solution)!);
}
}

Expand Down Expand Up @@ -475,7 +475,7 @@ class PuzzleController extends _$PuzzleController {
var currentPosition = initPosition;
final pgnMoves = state.puzzle.puzzle.solution.fold<List<String>>([],
(List<String> acc, move) {
final moveObj = Move.fromUci(move);
final moveObj = Move.parse(move);
if (moveObj != null) {
final String san;
(currentPosition, san) = currentPosition.makeSan(moveObj);
Expand Down Expand Up @@ -524,7 +524,7 @@ class PuzzleController extends _$PuzzleController {
final (_, newNodes) = state.puzzle.puzzle.solution.foldIndexed(
(initialNode.position, IList<Branch>(const [])),
(index, previous, uci) {
final move = Move.fromUci(uci);
final move = Move.parse(uci);
final (pos, nodes) = previous;
final (newPos, newSan) = pos.makeSan(move!);
return (
Expand Down Expand Up @@ -592,5 +592,5 @@ class PuzzleState with _$PuzzleState {
bool get canGoBack =>
mode == PuzzleMode.view && currentPath.size > initialPath.size;

IMap<String, ISet<String>> get validMoves => algebraicLegalMoves(position);
IMap<Square, ISet<Square>> get validMoves => makeLegalMoves(position);
}
11 changes: 5 additions & 6 deletions lib/src/model/puzzle/storm_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:core';
import 'dart:math' as math;

import 'package:async/async.dart';
import 'package:chessground/chessground.dart' as cg;
import 'package:dartchess/dartchess.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -119,7 +118,7 @@ class StormController extends _$StormController {
}
}

void setPremove(cg.Move? move) {
void setPremove(Move? move) {
state = state.copyWith(
premove: move,
);
Expand Down Expand Up @@ -335,17 +334,17 @@ class StormState with _$StormState {
required bool firstMovePlayed,

/// premove to be played
cg.Move? premove,
Move? premove,
}) = _StormState;

Move? get expectedMove => Move.fromUci(puzzle.solution[moveIndex + 1]);
Move? get expectedMove => Move.parse(puzzle.solution[moveIndex + 1]);

Move? get lastMove =>
moveIndex == -1 ? null : Move.fromUci(puzzle.solution[moveIndex]);
moveIndex == -1 ? null : Move.parse(puzzle.solution[moveIndex]);

bool get isOver => moveIndex >= puzzle.solution.length - 1;

IMap<String, ISet<String>> get validMoves => algebraicLegalMoves(position);
IMap<Square, ISet<Square>> get validMoves => makeLegalMoves(position);
}

enum StormMode { initial, running, ended }
Expand Down
50 changes: 25 additions & 25 deletions lib/src/model/settings/board_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,56 +168,56 @@ enum BoardTheme {

const BoardTheme(this.label);

BoardColorScheme get colors {
ChessboardColorScheme get colors {
switch (this) {
case BoardTheme.system:
return getBoardColorScheme() ?? BoardColorScheme.brown;
return getBoardColorScheme() ?? ChessboardColorScheme.brown;
case BoardTheme.blue:
return BoardColorScheme.blue;
return ChessboardColorScheme.blue;
case BoardTheme.blue2:
return BoardColorScheme.blue2;
return ChessboardColorScheme.blue2;
case BoardTheme.blue3:
return BoardColorScheme.blue3;
return ChessboardColorScheme.blue3;
case BoardTheme.blueMarble:
return BoardColorScheme.blueMarble;
return ChessboardColorScheme.blueMarble;
case BoardTheme.canvas:
return BoardColorScheme.canvas;
return ChessboardColorScheme.canvas;
case BoardTheme.wood:
return BoardColorScheme.wood;
return ChessboardColorScheme.wood;
case BoardTheme.wood2:
return BoardColorScheme.wood2;
return ChessboardColorScheme.wood2;
case BoardTheme.wood3:
return BoardColorScheme.wood3;
return ChessboardColorScheme.wood3;
case BoardTheme.wood4:
return BoardColorScheme.wood4;
return ChessboardColorScheme.wood4;
case BoardTheme.maple:
return BoardColorScheme.maple;
return ChessboardColorScheme.maple;
case BoardTheme.maple2:
return BoardColorScheme.maple2;
return ChessboardColorScheme.maple2;
case BoardTheme.brown:
return BoardColorScheme.brown;
return ChessboardColorScheme.brown;
case BoardTheme.leather:
return BoardColorScheme.leather;
return ChessboardColorScheme.leather;
case BoardTheme.green:
return BoardColorScheme.green;
return ChessboardColorScheme.green;
case BoardTheme.marble:
return BoardColorScheme.marble;
return ChessboardColorScheme.marble;
case BoardTheme.greenPlastic:
return BoardColorScheme.greenPlastic;
return ChessboardColorScheme.greenPlastic;
case BoardTheme.grey:
return BoardColorScheme.grey;
return ChessboardColorScheme.grey;
case BoardTheme.metal:
return BoardColorScheme.metal;
return ChessboardColorScheme.metal;
case BoardTheme.olive:
return BoardColorScheme.olive;
return ChessboardColorScheme.olive;
case BoardTheme.newspaper:
return BoardColorScheme.newspaper;
return ChessboardColorScheme.newspaper;
case BoardTheme.purpleDiag:
return BoardColorScheme.purpleDiag;
return ChessboardColorScheme.purpleDiag;
case BoardTheme.pinkPyramid:
return BoardColorScheme.pinkPyramid;
return ChessboardColorScheme.pinkPyramid;
case BoardTheme.horsey:
return BoardColorScheme.horsey;
return ChessboardColorScheme.horsey;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/tv/tv_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class TvController extends _$TvController {
final curState = state.requireValue;
final data = MoveEvent.fromJson(event.data as Map<String, dynamic>);
final lastPos = curState.game.lastPosition;
final move = Move.fromUci(data.uci)!;
final move = Move.parse(data.uci)!;
final sanMove = SanMove(data.san, move);
final newPos = lastPos.playUnchecked(move);
final newStep = GameStep(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/styles/lichess_colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LichessColors {
LichessColors._();

// material colors palette generated with:
// http://mcg.mbitson.com
// http://mmbitson.com

// primary: blue
static const MaterialColor primary =
Expand Down
41 changes: 0 additions & 41 deletions lib/src/utils/chessground_compat.dart

This file was deleted.

Loading

0 comments on commit 5c06534

Please sign in to comment.