Created
December 20, 2023 20:10
-
-
Save tors42/251da2cea832d0416cf4e1ed6097e04e to your computer and use it in GitHub Desktop.
Example chariot jshell script to trigger "opponent gone", runnable with lichess-org/lila-docker
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
if (Path.of("chariot-0.0.77.jar") instanceof Path chariot | |
&& ! Files.exists(chariot)) { | |
Files.write(chariot, URI.create( | |
"https://repo1.maven.org/maven2/io/github/tors42/chariot/0.0.77/chariot-0.0.77.jar" | |
).toURL().openStream().readAllBytes()); | |
} | |
/env --module-path chariot-0.0.77.jar --add-module chariot | |
import chariot.*; | |
import chariot.model.*; | |
import chariot.model.Event.GameStartEvent; | |
import chariot.model.GameStateEvent.*; | |
String lichessAPI = "http://localhost:8080"; | |
var diego = Client.auth(conf -> conf.api(lichessAPI), "lip_diego"); | |
var bobby = Client.auth(conf -> conf.api(lichessAPI), "lip_bobby"); | |
//diego.logging(l -> l.request().all().response().all()); | |
//bobby.logging(l -> l.request().all().response().all()); | |
var diegoConnect = diego.board().connect(); | |
var bobbyConnect = bobby.board().connect(); | |
if (diegoConnect instanceof Fail || bobbyConnect instanceof Fail) { | |
System.out.println("Failed to connect"); | |
System.out.println("diego: " + diegoConnect); | |
System.out.println("bob: " + bobbyConnect); | |
System.exit(1); | |
} | |
var diegoEvents = diegoConnect.stream(); | |
var bobbyEvents = bobbyConnect.stream(); | |
var bobbyReady = new java.util.concurrent.Semaphore(0); | |
Thread.ofVirtual().name("diego").start(() -> { | |
diegoEvents.forEach(event -> { | |
switch(event) { | |
case GameStartEvent game -> { | |
var gameConnect = diego.board().connectToGame(game.id()); | |
if (! (gameConnect instanceof Entries(var gameEvents))) { | |
throw new RuntimeException("" + gameConnect); | |
} | |
var playing = new java.util.concurrent.atomic.AtomicBoolean(true); | |
gameEvents | |
.takeWhile(__ -> playing.get()) | |
.forEach(gameEvent -> { | |
switch (gameEvent) { | |
case Full f -> { | |
bobbyReady.acquireUninterruptibly(); | |
System.out.println("diego playing e4"); | |
diego.board().move(game.id(), "e2e4"); | |
} | |
case State s when s.moveList().getLast().equals("e7e6") -> { | |
diego.board().chat(game.id(), "French? Really?"); | |
playing.set(false); | |
} | |
default -> {} | |
} | |
}); | |
gameEvents.close(); | |
System.out.println("diego has left the building..."); | |
} | |
default -> {} | |
}; | |
}); | |
}); | |
Thread.ofVirtual().name("bobby").start(() -> { | |
bobbyEvents.forEach(event -> { | |
if (event instanceof GameStartEvent game) { | |
var gameConnect = bobby.board().connectToGame(game.id()); | |
if (! (gameConnect instanceof Entries(var gameEvents))) { | |
throw new RuntimeException("" + gameConnect); | |
} | |
gameEvents.forEach(gameEvent -> { | |
switch(gameEvent) { | |
case Full f -> bobbyReady.release(); | |
case State s -> { | |
if (s.moveList().size() == 1) { | |
System.out.println("bobby playing e6"); | |
bobby.board().move(game.id(), "e7e6"); | |
} | |
} | |
case Chat(var from, var text, var room) -> System.out.println("Message from " + from + ": " + text); | |
case OpponentGone(boolean gone, Claim claim) -> System.out.println("bobby " + switch(claim) { | |
case Yes() -> { bobby.board().claimVictory(game.id()); yield "claimed!"; } | |
case No() -> "can't claim!"; | |
case Soon(var duration) -> "can claim in " + duration.toSeconds() + " seconds!"; | |
}); | |
}; | |
}); | |
} | |
}); | |
}); | |
Thread.ofVirtual().name("diego-seek").start(() -> { | |
var seek = diego.board().seekRealTime(param -> param.clockRapid10m0s().color(c -> c.white())); | |
if (seek instanceof Entries(var stream)) { | |
System.out.println("diego seek"); | |
stream.forEach(s -> System.out.println("diego seeking" + s)); | |
System.out.println("diego seek done"); | |
} else { | |
System.out.println("no diego seek: " + seek); | |
} | |
}); | |
Thread.ofVirtual().name("bobby-seek").start(() -> { | |
var seek = bobby.board().seekRealTime(param -> param.clockRapid10m0s().color(c -> c.black())); | |
if (seek instanceof Entries(var stream)) { | |
System.out.println("bobby seek"); | |
stream.forEach(s -> System.out.println("bob seeking " + s)); | |
System.out.println("bobby seek done"); | |
} else { | |
System.out.println("no bobby seek: " + seek); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example run,
$ jshell -s opponent-gone.jsh
/exit