Created
January 29, 2023 13:54
-
-
Save tors42/b72654906ebb32dbf7cfffe8c4bb7750 to your computer and use it in GitHub Desktop.
Shows a list of Lichess Correspondence games, sorted by "urgency" to play a move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.*; | |
import java.util.function.Function; | |
import java.util.Comparator; | |
import chariot.model.Enums.Color; | |
import chariot.model.Enums.PerfType; | |
import chariot.model.Game; | |
import chariot.Client; | |
class Games { | |
public static void main(String[] args) { | |
String userId = args.length == 0 ? System.console().readLine("Lichess user id: ") : args[0]; | |
Function<Game, Color> ourColorInGame = | |
game -> game.players().white().name().equalsIgnoreCase(userId) ? Color.white : Color.black; | |
Function<Game, Color> colorToPlayInGame = | |
game -> game.lastFen().contains(" w ") ? Color.white : Color.black; | |
Function<Game, Boolean> isItOurMove = | |
game -> ourColorInGame.apply(game) == colorToPlayInGame.apply(game); | |
Function<Game, Duration> timeLeftToMove = | |
game -> Duration.between(ZonedDateTime.now(), game.lastMoveAt().plusDays(game.daysPerTurn())); | |
Comparator<Game> gamesWithOurMoveFirst = Comparator.comparing(isItOurMove).reversed(); | |
Comparator<Game> gamesWithClosestDeadline = Comparator.comparing(timeLeftToMove); | |
var client = Client.basic(); | |
var games = client.games().byUserId(userId, params -> params | |
.perfType(PerfType.correspondence) | |
.ongoing() | |
.finished(false) | |
.lastFen() | |
.clocks() | |
).stream() | |
.sorted(gamesWithOurMoveFirst.thenComparing(gamesWithClosestDeadline)) | |
.toList(); | |
int wPadding = games.stream().mapToInt(game -> game.players().white().name().length()).max().orElse(15); | |
int bPadding = games.stream().mapToInt(game -> game.players().black().name().length()).max().orElse(15); | |
games.forEach(game -> System.out.format( | |
"%3d hours left - https://lichess.org/%s %"+wPadding+"s - %-"+bPadding+"s %2d days/move%n", | |
timeLeftToMove.apply(game).toHours(), | |
game.id(), | |
game.players().white().name() + (colorToPlayInGame.apply(game) == Color.white ? "*" : ""), | |
game.players().black().name() + (colorToPlayInGame.apply(game) == Color.black ? "*" : ""), | |
game.daysPerTurn() | |
)); | |
} | |
} |
Oh,
and I also added it to tors42/playground,
where one can launch the program with the Web Browser by using GitHub Codespaces - (one needs a GitHub account for that though)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pre-requisites:
Example of how to run in a command-line terminal:
(assuming that
chariot-0.0.60.jar
andGames.java
are in the current folder, and thatjava
command is on the path, otherwise something like this: )And the output would be: