0db82339881cc26d81d646554edce5f7c46e1075
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / json / format / 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.format;
18
19 import com.google.gson.annotations.SerializedName;
20
21 import org.hedgecode.chess.scanner.format.PGNFormat;
22 import org.hedgecode.chess.scanner.format.PGNTag;
23 import org.hedgecode.chess.scanner.format.GameData;
24 import org.hedgecode.chess.scanner.format.GameFormat;
25 import org.hedgecode.chess.scanner.format.TypeMovesFormat;
26 import org.hedgecode.chess.scanner.spi.ServiceRegistry;
27
28 /**
29  * JSONGameFormat
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public class JSONGameFormat extends AbstractBaseFormat implements GameFormat {
34
35     @SerializedName("gameData")
36     private JSONGameData gameData;
37
38     JSONGameFormat() {
39     }
40
41     @Override
42     public String id() {
43         return Integer.toString(
44                 gameData().game().id()
45         );
46     }
47
48     @Override
49     public GameData gameData() {
50         return gameData;
51     }
52
53
54     @Override
55     public String pgn() {
56         PGNFormat pgnFormat = ServiceRegistry.singleProvider(
57                 PGNFormat.class
58         );
59         pgnFormat.addTag(PGNTag.EVENT, gameData.room().name());
60         pgnFormat.addTag(PGNTag.DATE, pgnFormat.formatDate(gameData.game().startAt()));
61         pgnFormat.addTag(PGNTag.ROUND, gameData.game().roundSlug());
62         pgnFormat.addTag(PGNTag.WHITE, gameData.game().white().name());
63         pgnFormat.addTag(PGNTag.BLACK, gameData.game().black().name());
64         pgnFormat.addTag(PGNTag.WHITE_TITLE, gameData.game().white().title());
65         pgnFormat.addTag(PGNTag.BLACK_TITLE, gameData.game().black().title());
66         pgnFormat.addTag(PGNTag.WHITE_ELO, Integer.toString(gameData.game().whiteElo()));
67         pgnFormat.addTag(PGNTag.BLACK_ELO, Integer.toString(gameData.game().blackElo()));
68         pgnFormat.addTag(PGNTag.EVENT_DATE, pgnFormat.formatDate(gameData.room().startAt()));
69         pgnFormat.addTag(PGNTag.TIME, pgnFormat.formatTime(gameData.game().startAt()));
70         pgnFormat.addTag(PGNTag.RESULT, gameData.game().result());
71         if (gameData.moves().length > 0) {
72             pgnFormat.addTag(PGNTag.PLY_COUNT, Integer.toString(gameData.moves().length));
73         }
74         pgnFormat.addMoves(
75                 TypeMovesFormat.WRAP.format(gameData.moves())
76         );
77         return pgnFormat.format();
78     }
79
80 }