Created
April 20, 2024 12:00
-
-
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)
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example video of downloading Java and running the script on Windows