[LIB-13] Lichess format entities for JSON parsing
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / json / lichess / JSONGameFormat.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.scanner.json.lichess;
18
19 import java.util.Date;
20
21 import com.google.gson.annotations.SerializedName;
22
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;
29
30 /**
31  * JSONGameFormat
32  *
33  * @author Dmitry Samoshin aka gotty
34  */
35 public class JSONGameFormat implements GameFormat {
36
37     private static final String TIME_CONTROL_FORMAT = "%d+%d";
38
39     @SerializedName("data")
40     private JSONGameData gameData;
41
42     JSONGameFormat() {
43     }
44
45     @Override
46     public String id() {
47         return gameData.game().id();
48     }
49
50     @Override
51     public GameData gameData() {
52         return gameData;
53     }
54
55     @Override
56     public String pgn() {
57         PGNFormat pgnFormat = ServiceRegistry.singleProvider(
58                 PGNFormat.class
59         );
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,
69                 String.format(
70                         TIME_CONTROL_FORMAT, gameData.clock().initial(), gameData.clock().increment()
71                 )
72         );
73         pgnFormat.addTag(PGNTag.RESULT, gameData.result());
74         if (gameData.game().turns() > 0) {
75             pgnFormat.addTag(PGNTag.PLY_COUNT, Integer.toString(gameData.game().turns()));
76         }
77         if (gameData.game().startedAtTurn() > 0) {
78             pgnFormat.addTag(PGNTag.FEN, gameData.game().fen());
79         }
80         pgnFormat.addMoves(
81                 TypeMovesFormat.WRAP.format(gameData.moves())
82         );
83         return pgnFormat.format();
84     }
85
86 }