[LIB-13] Method scanUrl implementation for several scanners
[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     @Override
54     public String pgn() {
55         PGNFormat pgnFormat = ServiceRegistry.singleProvider(
56                 PGNFormat.class
57         );
58         pgnFormat.addTag(PGNTag.EVENT, gameData.room().name());
59         pgnFormat.addTag(PGNTag.DATE, pgnFormat.formatDate(gameData.game().startAt()));
60         pgnFormat.addTag(PGNTag.ROUND, gameData.game().roundSlug());
61         pgnFormat.addTag(PGNTag.WHITE, gameData.game().white().name());
62         pgnFormat.addTag(PGNTag.BLACK, gameData.game().black().name());
63         pgnFormat.addTag(PGNTag.WHITE_TITLE, gameData.game().white().title());
64         pgnFormat.addTag(PGNTag.BLACK_TITLE, gameData.game().black().title());
65         pgnFormat.addTag(PGNTag.WHITE_ELO, Integer.toString(gameData.game().whiteElo()));
66         pgnFormat.addTag(PGNTag.BLACK_ELO, Integer.toString(gameData.game().blackElo()));
67         pgnFormat.addTag(PGNTag.EVENT_DATE, pgnFormat.formatDate(gameData.room().startAt()));
68         pgnFormat.addTag(PGNTag.TIME, pgnFormat.formatTime(gameData.game().startAt()));
69         pgnFormat.addTag(PGNTag.RESULT, gameData.game().result());
70         if (gameData.moves().length > 0) {
71             pgnFormat.addTag(PGNTag.PLY_COUNT, Integer.toString(gameData.moves().length));
72         }
73         pgnFormat.addMoves(
74                 TypeMovesFormat.WRAP.format(gameData.moves())
75         );
76         return pgnFormat.format();
77     }
78
79 }