Skip to content

Instantly share code, notes, and snippets.

@tors42
Created April 20, 2024 12:00
Show Gist options
  • Save tors42/d460c5c709c4c80c5eff35c9176471e3 to your computer and use it in GitHub Desktop.
Save tors42/d460c5c709c4c80c5eff35c9176471e3 to your computer and use it in GitHub Desktop.
Java (jshell) program to find "Top 10" worst accuracies (of analysed Lichess games)
String userId = "tors42"; // User to check "Top" 10 worst accuracies for
// Optional token (download a bit faster) https://lichess.org/account/oauth/token/create
String token = "";
if (! Files.exists(Path.of("chariot.jar"))) {
Files.write(Path.of("chariot.jar"), URI.create(
"https://repo1.maven.org/maven2/io/github/tors42/chariot/0.0.86/chariot-0.0.86.jar"
).toURL().openStream().readAllBytes());
}
/env --class-path chariot.jar
import chariot.*;
import chariot.model.*;
import chariot.model.Player.*;
void showWorstGames(Client client, User user) {
var dateFormat = java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
var counter = new java.util.concurrent.atomic.AtomicInteger();
Consumer<Game> peekaboo = game -> {
if(Integer.valueOf(counter.getAndIncrement()) instanceof Integer count && count % 10 == 0)
System.out.print("\rFound %d games. Currently at %s".formatted(
count, game.createdAt().format(dateFormat)));
};
Function<Game, Player> pointOfView = game -> user.name().equals(game.players().white().name()) ?
game.players().white() : game.players().black();
record GameAccuracy(Game game, int accuracy) {}
var games = client.games().byUserId(user.id(), p -> p.analysed().accuracy()).stream().
peek(peekaboo).
map(game -> new GameAccuracy(game,
switch(pointOfView.apply(game)) {
case Analysed(_, Analysis(_,_,_,_,Some(var accuracy))) -> accuracy;
default -> 0;
})).
sorted(Comparator.comparingInt(GameAccuracy::accuracy)).
toList();
System.out.println();
games.stream().limit(10).map(entry -> "https://lichess.org/%s - %d%% - %s".formatted(
entry.game().id(), entry.accuracy(), entry.game().createdAt().format(dateFormat))).
forEach(System.out::println);
}
var client = Client.basic();
if (! token.isEmpty()) client = client.withToken(token);
var result = client.users().byId(userId);
if (result instanceof Entry(var user)) {
showWorstGames(client, user);
} else {
System.out.println("Failed to find " + userId);
}
/exit
@tors42
Copy link
Author

tors42 commented Apr 20, 2024

Runs with Java 22 - https://jdk.java.net/22/

Example run,

$ jshell worst.jsh

Progress info every 10 games,

Found 10 games. Currently at 2024-01-28

And upon finish, "top" 10 list is shown,

Found 270 games. Currently at 2018-06-13
https://lichess.org/9kI6chE2 - 34% - 2020-04-25
https://lichess.org/PvtBYKg3 - 36% - 2020-04-17
https://lichess.org/nZgxW2YM - 39% - 2023-05-17
https://lichess.org/qdoqT7tO - 40% - 2020-10-29
https://lichess.org/UNeUgd9H - 42% - 2023-10-12
https://lichess.org/ZxP4isRo - 42% - 2020-04-29
https://lichess.org/WoPb2dat - 43% - 2022-11-27
https://lichess.org/qHW9gXhM - 44% - 2020-05-01
https://lichess.org/DPlUpj8t - 46% - 2024-03-28
https://lichess.org/TtWXS34W - 46% - 2019-10-01

@tors42
Copy link
Author

tors42 commented Apr 20, 2024

Example video of downloading Java and running the script on Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment