Skip to content

Commit

Permalink
Get rid of Jackson databind & no more compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Feb 19, 2024
1 parent 09b83cb commit 94653cb
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 358 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@
<artifactId>ajlib</artifactId>
<version>0.3.15</version>
</dependency>
<!-- TODO remove this very fat dependency -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/com/fathzer/jchess/lichess/DefaultOpenings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.zip.GZIPInputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.fathzer.jchess.Board;
import com.fathzer.jchess.Move;
import com.fathzer.jchess.fen.FENUtils;
import com.fathzer.jchess.movelibrary.LibraryMove;
import com.fathzer.jchess.movelibrary.Proposal;
import com.fathzer.jchess.uci.JChessUCIEngine;
import com.fathzer.jchess.uci.UCIMove;

public class DefaultOpenings implements Function<Board<Move>, Move> {
private static final Random RND = new Random();
private static final String KNOWN = "/lichess/masters.json.gz";
private static final ObjectMapper MAPPER = new ObjectMapper();

public static final DefaultOpenings INSTANCE;

Expand All @@ -35,16 +32,19 @@ public class DefaultOpenings implements Function<Board<Move>, Move> {
}
}

private final Map<String, Proposal> db;
private final JSONObject db;

private DefaultOpenings() throws IOException {
this(() -> DefaultOpenings.class.getResourceAsStream(KNOWN), true);
}

public DefaultOpenings(Supplier<InputStream> stream, boolean zipped) throws IOException {
db = readJSON(stream, zipped);
}

private JSONObject readJSON(Supplier<InputStream> stream, boolean zipped) throws IOException {
try (InputStream in = zipped ? new GZIPInputStream(stream.get()) : stream.get()) {
MapType mapType = MAPPER.getTypeFactory().constructMapType(HashMap.class, String.class, Proposal.class);
db = MAPPER.readValue(in, mapType);
return new JSONObject(new JSONTokener(in));
}
}

Expand All @@ -56,11 +56,12 @@ private String toFen(Board<Move> board) {

@Override
public Move apply(Board<Move> board) {
final Proposal opening = db.get(toFen(board));
if (opening==null || opening.getMoves().isEmpty()) {
final JSONObject opening = db.getJSONObject(toFen(board));
if (opening==null) {
return null;
}
final LibraryMove move = opening.getMoves().get(RND.nextInt(opening.getMoves().size()));
return JChessUCIEngine.toMove(board, UCIMove.from(move.getCoord()));
JSONArray moves = opening.getJSONArray("moves");
final JSONObject move = moves.getJSONObject(RND.nextInt(moves.length()));
return JChessUCIEngine.toMove(board, UCIMove.from(move.getString("coord")));
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/fathzer/jchess/settings/BasicClockSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fathzer.jchess.settings;

import com.fathzer.games.clock.ClockSettings;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/** A very simplified version of games-core clock settings.
*/
@Getter
@Setter
@NoArgsConstructor
public class BasicClockSettings {
/** Given number of seconds at the beginning of game. */
private int initialTime;
/** Time increment in seconds. */
private int increment;
/** Number of moves between increments. */
private int movesNumberBeforeIncrement;

public ClockSettings toClockSettings() {
final ClockSettings result = new ClockSettings(initialTime);
if (increment>0) {
result.withIncrement(increment, movesNumberBeforeIncrement<=0 ? 1 : movesNumberBeforeIncrement, true);
}
return result;
}
}

This file was deleted.

29 changes: 13 additions & 16 deletions src/main/java/com/fathzer/jchess/settings/GameSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.Random;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.json.JSONObject;

import com.fathzer.games.Color;
import com.fathzer.games.GameBuilder;
import com.fathzer.games.clock.ClockSettings;
import com.fathzer.jchess.Board;
import com.fathzer.jchess.Move;
import com.fathzer.util.TinyJackson;
import com.fathzer.jchess.GameBuilders;

import lombok.AllArgsConstructor;
Expand All @@ -20,21 +20,14 @@
@Getter
@Setter
public class GameSettings {
public static final ObjectMapper MAPPER = new ObjectMapper();

static {
SimpleModule module = new SimpleModule();
module.addDeserializer(ClockSettings.class, new ClockSettingsDeserializer());
MAPPER.registerModule(module);
}

private static final Random RANDOM_GENERATOR = new Random();

private Variant variant = Variant.STANDARD;
private boolean tabletMode = true;
private boolean showPossibleMoves = true;
private boolean touchMove = false;
private boolean startClockAfterFirstMove = false;
private ClockSettings clock = null;
private BasicClockSettings clock = null;
private PlayerSettings player1 = new PlayerSettings();
private ColorSetting player1Color = ColorSetting.RANDOM;
private PlayerSettings player2 = new PlayerSettings();
Expand Down Expand Up @@ -67,15 +60,19 @@ public Color getColor() {
public static class PlayerSettings {
private String name = null;
private EngineSettings engine = null;
private ClockSettings extraClock = null;
}

@NoArgsConstructor
@Getter
@Setter
public static class EngineSettings {
private String name = "jchess";
private int level = 6;
private String evaluator;
private String name;
}

public static void main(String[] args) {
String json = "{\"variant\":\"STANDARD\",\"tabletMode\":true,\"showPossibleMoves\":true,\"touchMove\":false,\"startClockAfterFirstMove\":false,\"clock\":{\"initialTime\":180,\"increment\":2,\"movesNumberBeforeIncrement\":1,\"canAccumulate\":true,\"movesNumberBeforeNext\":2147483647,\"maxRemainingKept\":0,\"next\":null},\"player1\":{\"name\":null,\"engine\":null,\"extraClock\":null},\"player1Color\":\"RANDOM\",\"player2\":{\"name\":null,\"engine\":{\"name\":\"jchess\",\"level\":16,\"evaluator\":\"simple\"},\"extraClock\":null}}";
JSONObject jsonO = new JSONObject(json);
GameSettings settings = TinyJackson.toObject(jsonO, GameSettings.class);
System.out.println(settings);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/fathzer/jchess/swing/GameRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static String who(GameSettings settings, Color player1Color, Color color
if (player.getName()!=null) {
return player.getName();
} else if (player.getEngine()!=null) {
return player.getEngine().getName()+"-"+player.getEngine().getLevel();
return player.getEngine().getName();
} else {
return "?";
}
Expand Down
39 changes: 11 additions & 28 deletions src/main/java/com/fathzer/jchess/swing/GameSession.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fathzer.jchess.swing;

import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

import javax.swing.JOptionPane;
Expand All @@ -17,14 +17,14 @@
import com.fathzer.jchess.ai.evaluator.NaiveEvaluator;
import com.fathzer.jchess.ai.evaluator.SimplifiedEvaluator;
import com.fathzer.jchess.bot.Engine;
import com.fathzer.jchess.bot.uci.EngineLoader;
import com.fathzer.jchess.bot.uci.EngineLoader.EngineData;
import com.fathzer.games.clock.Clock;
import com.fathzer.games.clock.ClockSettings;
import com.fathzer.games.util.PhysicalCores;
import com.fathzer.jchess.lichess.DefaultOpenings;
import com.fathzer.jchess.settings.GameSettings;
import com.fathzer.jchess.settings.GameSettings.ColorSetting;
import com.fathzer.jchess.settings.GameSettings.EngineSettings;
import com.fathzer.jchess.settings.GameSettings.Variant;
import com.fathzer.util.Observable;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -114,17 +114,8 @@ private void initGame() {

private Clock buildClock() {
if (settings.getClock()!=null) {
final ClockSettings extraWhite;
final ClockSettings extraBlack;
if (Color.WHITE.equals(player1Color)) {
extraWhite = settings.getPlayer1().getExtraClock();
extraBlack = settings.getPlayer2().getExtraClock();
} else {
extraBlack = settings.getPlayer1().getExtraClock();
extraWhite = settings.getPlayer2().getExtraClock();
}
final ClockSettings common = settings.getClock();
final Clock clock = new Clock(extraWhite==null?common:extraWhite, extraBlack==null?common:extraBlack);
final ClockSettings common = settings.getClock().toClockSettings();
final Clock clock = new Clock(common);
clock.addStatusListener(this::timeUp);
clock.addClockListener(e -> log.debug("Clock {} state changes from {} to {}",e.getClock(), e.getPreviousState(), e.getNewState()));
if (settings.isStartClockAfterFirstMove()) {
Expand All @@ -136,20 +127,12 @@ private Clock buildClock() {
}
}

public Engine getEngine(Variant variant, EngineSettings settings) {
final JChessEngine engine;
public Engine getEngine(EngineSettings settings) {
if (settings==null) {
engine = null;
} else if (Variant.STANDARD.equals(variant)) {
engine = getEngine(settings.getLevel(), settings.getEvaluator());
engine.setOpenings(DefaultOpenings.INSTANCE);
} else if (Variant.CHESS960.equals(variant)) {
engine = getEngine(settings.getLevel(), settings.getEvaluator());
engine.setOpenings(null);
} else {
throw new IllegalArgumentException("The "+this+" variant does not support engine");
return null;
}
return engine;
final Optional<EngineData> found = EngineLoader.getEngines().stream().filter(e -> e.getEngine()!=null && e.getName().equals(settings.getName())).findAny();
return found.orElseThrow().getEngine();
}

private JChessEngine getEngine(int level, String evaluatorName) {
Expand Down Expand Up @@ -207,8 +190,8 @@ public void start() {
this.gameCount = 0;
initGame();
}
setEngine(player1Color, getEngine(settings.getVariant(), settings.getPlayer1().getEngine()));
setEngine(player1Color.opposite(), getEngine(settings.getVariant(), settings.getPlayer2().getEngine()));
setEngine(player1Color, getEngine(settings.getPlayer1().getEngine()));
setEngine(player1Color.opposite(), getEngine(settings.getPlayer2().getEngine()));
setState(State.RUNNING);
}

Expand Down
Loading

0 comments on commit 94653cb

Please sign in to comment.