[LIB-13] Lichess format entities for JSON parsing
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / json / lichess / JSONGameData.java
diff --git a/src/main/java/org/hedgecode/chess/scanner/json/lichess/JSONGameData.java b/src/main/java/org/hedgecode/chess/scanner/json/lichess/JSONGameData.java
new file mode 100644 (file)
index 0000000..4562b88
--- /dev/null
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2019-2020. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.scanner.json.lichess;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
+import org.hedgecode.chess.scanner.format.Move;
+import org.hedgecode.chess.scanner.format.lichess.Clock;
+import org.hedgecode.chess.scanner.format.lichess.Game;
+import org.hedgecode.chess.scanner.format.lichess.GameData;
+import org.hedgecode.chess.scanner.format.lichess.Player;
+
+/**
+ * JSONGameData
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JSONGameData implements GameData {
+
+    private static final String PLAYER_WHITE = "white";
+    private static final String PLAYER_BLACK = "black";
+
+    private static final String GAME_DRAW = "draw";
+
+    private static final Map<String, String> RESULT = new HashMap<String, String>() {
+        {
+            put( PLAYER_WHITE, "1-0" );
+            put( PLAYER_BLACK, "0-1" );
+            put( GAME_DRAW, "1/2-1/2");
+        }
+    };
+
+    private static final Map<Boolean, String> RATED_GAME = new HashMap<Boolean, String>() {
+        {
+            put( Boolean.FALSE, "Unrated" );
+            put( Boolean.TRUE,  "Rated"   );
+        }
+    };
+
+    private static final String EVENT_GAME_FORMAT = "%s %s game";
+
+    @SerializedName("game")
+    private JSONGame game;
+
+    @SerializedName("player")
+    private JSONPlayer player;
+
+    @SerializedName("opponent")
+    private JSONPlayer opponent;
+
+    @SerializedName("clock")
+    private JSONClock clock;
+
+    @SerializedName("steps")
+    private JSONMove[] steps;
+
+    @SerializedName("treeParts")
+    private JSONMove[] treeParts;
+
+    @Expose
+    private Move[] moves;
+
+    JSONGameData() {
+    }
+
+    @Override
+    public Game game() {
+        return game;
+    }
+
+    @Override
+    public Player player() {
+        return player;
+    }
+
+    @Override
+    public Player opponent() {
+        return opponent;
+    }
+
+    public String event() {
+        return String.format(
+                EVENT_GAME_FORMAT,
+                RATED_GAME.get(game.rated()),
+                game.speed()
+        );
+    }
+
+    public Player white() {
+        return player.color().equals(PLAYER_WHITE)
+                ? player
+                : opponent.color().equals(PLAYER_WHITE)
+                ? opponent
+                : null;
+    }
+
+    public Player black() {
+        return player.color().equals(PLAYER_BLACK)
+                ? player
+                : opponent.color().equals(PLAYER_BLACK)
+                ? opponent
+                : null;
+    }
+
+    public String result() {
+        String result = null;
+        if (game.winner() != null) {
+            if (PLAYER_WHITE.equals(game.winner())) {
+                result = PLAYER_WHITE;
+            } else if (PLAYER_BLACK.equals(game.winner())) {
+                result = PLAYER_BLACK;
+            }
+        } else if (GAME_DRAW.equals(game.status().name())) {
+            result = GAME_DRAW;
+        }
+        return RESULT.get(result);
+    }
+
+    @Override
+    public Clock clock() {
+        return clock;
+    }
+
+    @Override
+    public Move[] moves() {
+        if (moves == null) {
+            moves = steps != null ? steps : treeParts;
+            List<Move> movesList = new ArrayList<>(Arrays.asList(moves));
+            movesList.removeIf(move -> move.move() == null);
+            moves = movesList.toArray(
+                    new Move[movesList.size()]
+            );
+        }
+        return moves;
+    }
+
+}