2 * Copyright (c) 2019-2020. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.scanner.json.lichess;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
25 import com.google.gson.annotations.Expose;
26 import com.google.gson.annotations.SerializedName;
28 import org.hedgecode.chess.scanner.format.Move;
29 import org.hedgecode.chess.scanner.format.lichess.Clock;
30 import org.hedgecode.chess.scanner.format.lichess.Game;
31 import org.hedgecode.chess.scanner.format.lichess.GameData;
32 import org.hedgecode.chess.scanner.format.lichess.Player;
37 * @author Dmitry Samoshin aka gotty
39 public class JSONGameData implements GameData {
41 private static final String PLAYER_WHITE = "white";
42 private static final String PLAYER_BLACK = "black";
44 private static final String GAME_DRAW = "draw";
46 private static final Map<String, String> RESULT = new HashMap<String, String>() {
48 put( PLAYER_WHITE, "1-0" );
49 put( PLAYER_BLACK, "0-1" );
50 put( GAME_DRAW, "1/2-1/2");
54 private static final Map<Boolean, String> RATED_GAME = new HashMap<Boolean, String>() {
56 put( Boolean.FALSE, "Unrated" );
57 put( Boolean.TRUE, "Rated" );
61 private static final String EVENT_GAME_FORMAT = "%s %s game";
63 @SerializedName("game")
64 private JSONGame game;
66 @SerializedName("player")
67 private JSONPlayer player;
69 @SerializedName("opponent")
70 private JSONPlayer opponent;
72 @SerializedName("clock")
73 private JSONClock clock;
75 @SerializedName("steps")
76 private JSONMove[] steps;
78 @SerializedName("treeParts")
79 private JSONMove[] treeParts;
93 public Player player() {
98 public Player opponent() {
102 public String event() {
103 return String.format(
105 RATED_GAME.get(game.rated()),
110 public Player white() {
111 return player.color().equals(PLAYER_WHITE)
113 : opponent.color().equals(PLAYER_WHITE)
118 public Player black() {
119 return player.color().equals(PLAYER_BLACK)
121 : opponent.color().equals(PLAYER_BLACK)
126 public String result() {
127 String result = null;
128 if (game.winner() != null) {
129 if (PLAYER_WHITE.equals(game.winner())) {
130 result = PLAYER_WHITE;
131 } else if (PLAYER_BLACK.equals(game.winner())) {
132 result = PLAYER_BLACK;
134 } else if (GAME_DRAW.equals(game.status().name())) {
137 return RESULT.get(result);
141 public Clock clock() {
146 public Move[] moves() {
148 moves = steps != null ? steps : treeParts;
149 List<Move> movesList = new ArrayList<>(Arrays.asList(moves));
150 movesList.removeIf(move -> move.move() == null);
151 moves = movesList.toArray(
152 new Move[movesList.size()]