X-Git-Url: https://git.hedgecode.org/?p=chesshog-scanner.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fjson%2Flichess%2FJSONGameData.java;fp=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fjson%2Flichess%2FJSONGameData.java;h=4562b88e6687ffb81fb2ded18801a4abe38ca108;hp=0000000000000000000000000000000000000000;hb=8b5e21d880364363f4f8d51e126c1db91f8e2c4c;hpb=d1936e2e61d422117f733e9fffec0976356121ea 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 index 0000000..4562b88 --- /dev/null +++ b/src/main/java/org/hedgecode/chess/scanner/json/lichess/JSONGameData.java @@ -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 RESULT = new HashMap() { + { + put( PLAYER_WHITE, "1-0" ); + put( PLAYER_BLACK, "0-1" ); + put( GAME_DRAW, "1/2-1/2"); + } + }; + + private static final Map RATED_GAME = new HashMap() { + { + 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 movesList = new ArrayList<>(Arrays.asList(moves)); + movesList.removeIf(move -> move.move() == null); + moves = movesList.toArray( + new Move[movesList.size()] + ); + } + return moves; + } + +}