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.Date;
21 import com.google.gson.annotations.SerializedName;
23 import org.hedgecode.chess.scanner.format.PGNFormat;
24 import org.hedgecode.chess.scanner.format.PGNTag;
25 import org.hedgecode.chess.scanner.format.TypeMovesFormat;
26 import org.hedgecode.chess.scanner.format.lichess.GameData;
27 import org.hedgecode.chess.scanner.format.lichess.GameFormat;
28 import org.hedgecode.chess.scanner.spi.ServiceRegistry;
33 * @author Dmitry Samoshin aka gotty
35 public class JSONGameFormat implements GameFormat {
37 private static final String TIME_CONTROL_FORMAT = "%d+%d";
39 @SerializedName("data")
40 private JSONGameData gameData;
47 return gameData.game().id();
51 public GameData gameData() {
57 PGNFormat pgnFormat = ServiceRegistry.singleProvider(
60 Date startDate = new Date(gameData.game().createdAt());
61 pgnFormat.addTag(PGNTag.EVENT, gameData.event());
62 pgnFormat.addTag(PGNTag.DATE, pgnFormat.formatDate(startDate));
63 pgnFormat.addTag(PGNTag.WHITE, gameData.white().user().username());
64 pgnFormat.addTag(PGNTag.BLACK, gameData.black().user().username());
65 pgnFormat.addTag(PGNTag.WHITE_ELO, Integer.toString(gameData.white().rating()));
66 pgnFormat.addTag(PGNTag.BLACK_ELO, Integer.toString(gameData.black().rating()));
67 pgnFormat.addTag(PGNTag.TIME, pgnFormat.formatTime(startDate));
68 pgnFormat.addTag(PGNTag.TIME_CONTROL,
70 TIME_CONTROL_FORMAT, gameData.clock().initial(), gameData.clock().increment()
73 pgnFormat.addTag(PGNTag.RESULT, gameData.result());
74 if (gameData.game().turns() > 0) {
75 pgnFormat.addTag(PGNTag.PLY_COUNT, Integer.toString(gameData.game().turns()));
77 if (gameData.game().startedAtTurn() > 0) {
78 pgnFormat.addTag(PGNTag.FEN, gameData.game().fen());
81 TypeMovesFormat.WRAP.format(gameData.moves())
83 return pgnFormat.format();