[LIB-13] Tournament and game formats for JSON parsing
[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 java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21
22 import com.google.gson.annotations.SerializedName;
23
24 import org.hedgecode.chess.scanner.entity.PGNTag;
25 import org.hedgecode.chess.scanner.format.GameData;
26 import org.hedgecode.chess.scanner.format.GameFormat;
27
28 /**
29  * JSONGameFormat
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public class JSONGameFormat extends AbstractPGNFormat implements GameFormat {
34
35     private final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
36     private final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
37
38     @SerializedName("gameData")
39     private JSONGameData gameData;
40
41     JSONGameFormat() {
42     }
43
44     @Override
45     public String id() {
46         return Integer.toString(
47                 gameData().game().id()
48         );
49     }
50
51     @Override
52     public GameData gameData() {
53         return gameData;
54     }
55
56     @Override
57     protected void assignPGN() {
58         addTag(PGNTag.EVENT, gameData.room().name());
59         addTag(PGNTag.DATE, dateFormat.format(gameData.game().startAt()));
60         addTag(PGNTag.ROUND, gameData.game().roundSlug());
61         addTag(PGNTag.WHITE, gameData.game().white().name());
62         addTag(PGNTag.BLACK, gameData.game().black().name());
63         addTag(PGNTag.WHITE_TITLE, gameData.game().white().title());
64         addTag(PGNTag.BLACK_TITLE, gameData.game().black().title());
65         addTag(PGNTag.WHITE_ELO, Integer.toString(gameData.game().white().elo()));
66         addTag(PGNTag.BLACK_ELO, Integer.toString(gameData.game().black().elo()));
67         addTag(PGNTag.TIME, timeFormat.format(gameData.game().startAt()));
68         addTag(PGNTag.RESULT, gameData.game().result());
69
70         addMoves(gameData.formatMoves());
71     }
72
73 }