Skip to content
This repository has been archived by the owner on Nov 27, 2021. It is now read-only.

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
timmyRS committed Aug 13, 2018
1 parent 988fe65 commit e5c850f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
6 changes: 1 addition & 5 deletions src/sh/hell/compactchess/lichess/ExampleLichessBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void main(String[] args) throws ChessException, IOException
LichessEngineSelector engineSelector = new LichessEngineSelector()
{
@Override
public LichessEngineSelectorResult select(LichessBot lc, Variant variant, TimeControl timeControl, long msecs, long increment, boolean rated, String opponentName, boolean botOpponent) throws IOException, ChessException
public LichessEngineSelectorResult select(LichessBot lc, Variant variant, TimeControl timeControl, long msecs, long increment, boolean rated, String opponentName, boolean botOpponent) throws IOException
{
LichessEngineSelectorResult res = new LichessEngineSelectorResult();
res.engineName = "Leela Chess Zero v0.7 at Network ID 190";
Expand All @@ -25,10 +25,6 @@ public LichessEngineSelectorResult select(LichessBot lc, Variant variant, TimeCo
{
res.abortReason = "Sorry, time controls above Classic are disabled for now.";
}
else if(variant == null)
{
res.abortReason = "Sorry, for now I only accept Standard, King of the Hill, Three-check, Antichess, Horde, Racing Kings and From Position.";
}
else if(variant != Variant.STANDARD)
{
if(rated)
Expand Down
3 changes: 1 addition & 2 deletions src/sh/hell/compactchess/lichess/LichessAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public String importGame(final Game game) throws IOException, ChessException
final JsonObject jsonObject = new JsonObject();
jsonObject.add("pgn", game.toPGN());
final String data = jsonObject.toString();
System.out.println("< " + data);
HttpsURLConnection con = (HttpsURLConnection) new URL(baseUrl + "/import").openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "*/*");
Expand Down Expand Up @@ -183,7 +182,7 @@ public JsonObject getProfile() throws IOException
{
if(this.profile == null)
{
InputStream is = sendRequest("GET", "/account/me");
InputStream is = sendRequest("GET", "/api/account");
Scanner scanner = new Scanner(is, "UTF-8");
scanner.useDelimiter("\n");
String res = scanner.next();
Expand Down
5 changes: 2 additions & 3 deletions src/sh/hell/compactchess/lichess/LichessBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class LichessBot extends Thread
final LichessEngineSelector engineSelector;
final ArrayList<LichessBotGame> activeGames = new ArrayList<>();
final LichessAPI lichessAPI;
private Thread thread;

LichessBot(LichessAPI lichessAPI, LichessEngineSelector engineSelector)
{
Expand Down Expand Up @@ -72,7 +71,7 @@ public void run()
InputStream is = lichessAPI.sendRequest("GET", "/api/stream/event");
BufferedInputStream bis = new BufferedInputStream(is);
Scanner sc = new Scanner(bis).useDelimiter("\\n");
while(!this.thread.isInterrupted())
while(!this.isInterrupted())
{
try
{
Expand All @@ -84,7 +83,7 @@ public void run()
{
case "challenge":
JsonObject challenge = event.get("challenge").asObject();
lichessAPI.sendRequest("POST", "/challenge/" + challenge.get("id").asString() + "/accept");
lichessAPI.sendRequest("POST", "/api/challenge/" + challenge.get("id").asString() + "/accept");
break;
case "gameStart":
synchronized(activeGames)
Expand Down
47 changes: 32 additions & 15 deletions src/sh/hell/compactchess/lichess/LichessBotGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void run()
{
try
{
InputStream is = lichessBot.lichessAPI.sendRequest("GET", "/bot/game/stream/" + id);
InputStream is = lichessBot.lichessAPI.sendRequest("GET", "/api/bot/game/stream/" + id);
BufferedInputStream bis = new BufferedInputStream(is);
Scanner sc = new Scanner(bis).useDelimiter("\\n");
while(!this.isInterrupted())
Expand All @@ -63,8 +63,9 @@ public void run()
if(obj.get("type").asString().equals("gameFull"))
{
botColor = (obj.get("white").asObject().get("id").asString().equals(lichessBot.lichessAPI.getProfile().get("id").asString()) ? Color.WHITE : Color.BLACK);
String opponentName = obj.get(botColor == Color.WHITE ? "black" : "white").asObject().get("name").asString();
String opponentTitle = obj.get(botColor == Color.WHITE ? "black" : "white").asObject().getString("title", "");
JsonObject opponentObject = obj.get(botColor == Color.WHITE ? "black" : "white").asObject();
String opponentName = opponentObject.get("name").asString();
String opponentTitle = opponentObject.get("title").isString() ? opponentObject.get("title").asString() : "";
if(opponentTitle.equals(""))
{
opponent = opponentName;
Expand All @@ -86,13 +87,27 @@ public void run()
}
}
game.variant = Variant.fromKey(obj.get("variant").asObject().get("key").asString());
LichessEngineSelectorResult selectorResult = lichessBot.engineSelector.select(lichessBot, game.variant, game.timeControl, game.whitemsecs, game.increment, obj.get("rated").asBoolean(), opponentName, opponentTitle.equals("BOT"));
if(selectorResult.abortReason != null)
LichessEngineSelectorResult selectorResult = null;
String abortReason;
if(game.variant == null)
{
lichessBot.lichessAPI.sendRequest("POST", "/bot/game/" + id + "/abort");
endReason = "Aborted: " + selectorResult.abortReason;
sendMessage("player", selectorResult.abortReason);
sendMessage("spectator", selectorResult.abortReason);
abortReason = "Sorry, for now I only accept Standard, King of the Hill, Three-check, Antichess, Horde, Racing Kings and From Position.";
}
else
{
selectorResult = lichessBot.engineSelector.select(lichessBot, game.variant, game.timeControl, game.whitemsecs, game.increment, obj.get("rated").asBoolean(), opponentName, opponentTitle.equals("BOT"));
abortReason = selectorResult.abortReason;
}
if(selectorResult == null)
{
abortReason = "selectorResult is null";
}
if(abortReason != null)
{
lichessBot.lichessAPI.sendRequest("POST", "/api/bot/game/" + id + "/abort");
endReason = "Aborted: " + abortReason;
sendMessage("player", abortReason);
sendMessage("spectator", abortReason);
break;
}
engineName = selectorResult.engineName;
Expand Down Expand Up @@ -123,7 +138,7 @@ public void run()
try
{
Thread.sleep(20000);
lichessBot.lichessAPI.sendRequest("POST", "/bot/game/" + id + "/abort");
lichessBot.lichessAPI.sendRequest("POST", "/api/bot/game/" + id + "/abort");
ligame.sendMessage("player", "Please actually play if you challenge me.");
ligame.endReason = "Opponent played no move after 20 seconds.";
ligame.interrupt();
Expand Down Expand Up @@ -289,7 +304,7 @@ else if(game.whitemsecs <= 120000)
{
try
{
lichessBot.lichessAPI.sendRequest("POST", "/bot/game/" + id + "/move/" + bestMove);
lichessBot.lichessAPI.sendRequest("POST", "/api/bot/game/" + id + "/move/" + bestMove);
if(game.plyCount <= 2 && botColor == Color.BLACK)
{
final LichessBotGame ligame = this;
Expand All @@ -298,7 +313,7 @@ else if(game.whitemsecs <= 120000)
try
{
Thread.sleep(20000);
lichessBot.lichessAPI.sendRequest("POST", "/bot/game/" + id + "/abort");
lichessBot.lichessAPI.sendRequest("POST", "/api/bot/game/" + id + "/abort");
ligame.sendMessage("player", "Please actually play if you challenge me.");
ligame.endReason = "Opponent played no move after 20 seconds.";
ligame.interrupt();
Expand Down Expand Up @@ -328,7 +343,10 @@ else if(game.whitemsecs <= 120000)
}
else
{
endReason = "No move was found.";
lichessBot.lichessAPI.sendRequest("POST", "/api/bot/game/" + id + "/resign");
sendMessage("player", "The engine found no move.");
sendMessage("spectator", "The engine found no move.");
endReason = "The engine found no move.";
break;
}
}
Expand Down Expand Up @@ -379,7 +397,6 @@ else if(!e.getMessage().contains("Server returned HTTP response code: 400"))
if(game.plyCount > 2)
{
sendMessage("player", "Good game. Well played.");
//sendMessage("spectator", "Goodbye!");
}
if(lichessBot.countGames() > 1)
{
Expand Down Expand Up @@ -407,7 +424,7 @@ private void sendMessage(String room, String text)
{
try
{
lichessBot.lichessAPI.sendPOSTRequest("/bot/game/" + id + "/chat", "room=" + room + "&text=" + URLEncoder.encode(text, "UTF-8"));
lichessBot.lichessAPI.sendPOSTRequest("/api/bot/game/" + id + "/chat", "room=" + room + "&text=" + URLEncoder.encode(text, "UTF-8"));
}
catch(Exception e)
{
Expand Down
18 changes: 14 additions & 4 deletions src/sh/hell/compactchess/lichess/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@

public class Tests
{
private static void visualize(Game game) throws ChessException
private static void visualize(Game game)
{
System.out.println(game.toString(true));
}

@Test(timeout = 3000L)
public void lichessExport() throws IOException, ChessException
@Test(timeout = 10000L)
public void importGame() throws ChessException, IOException
{
System.out.println("Lichess Export\n");
System.out.println("Import Game\n");
final LichessAPI lichessAPI = new LichessAPI();
final Game game = Game.fromPGN("[Event \"Grandmasters-Young Masters\"]\n" + "[Site \"Sochi URS\"]\n" + "[Date \"1970.10.??\"]\n" + "[EventDate \"?\"]\n" + "[Round \"9\"]\n" + "[Result \"1-0\"]\n" + "[White \"Viktor Kupreichik\"]\n" + "[Black \"Mikhail Tal\"]\n" + "[ECO \"B57\"]\n" + "[WhiteElo \"?\"]\n" + "[BlackElo \"?\"]\n" + "[PlyCount \"61\"]\n" + "\n" + "1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nc6 5.Nc3 Nf6 6.Bc4 Qb6\n" + "7.Nb3 e6 8.Be3 Qc7 9.f4 a6 10.Bd3 b5 11.a3 Be7 12.Qf3 Bb7\n" + "13.O-O Rc8 14.Rae1 O-O 15.Qh3 b4 16.Nd5 exd5 17.exd5 Nb8\n" + "18.Bd4 g6 19.Rf3 Bxd5 20.Rfe3 Bd8 21.Qh4 Nbd7 22.Qh6 Qb7\n" + "23.Rg3 Nc5 24.Nxc5 dxc5 25.f5 cxd4 26.fxg6 fxg6 27.Bxg6 Kh8\n" + "28.Qxf8+ Ng8 29.Bf5 Rb8 30.Re8 Qf7 31.Rh3 1-0").get(0);
String url = lichessAPI.importGame(game);
assertEquals("https://lichess.org/3jwajJiC", url);
}

@Test(timeout = 10000L)
public void exportGame() throws IOException, ChessException
{
System.out.println("Export Game\n");
final LichessAPI lichessAPI = new LichessAPI();
final Game game = lichessAPI.exportGames(new String[]{"XdQ8329y"}).get(0);
visualize(game);
Expand Down

0 comments on commit e5c850f

Please sign in to comment.