[LIB-13] Locale resource settings
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / json / lichess / JSONGameData.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.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import com.google.gson.annotations.Expose;
26 import com.google.gson.annotations.SerializedName;
27
28 import org.hedgecode.chess.scanner.format.Move;
29 import org.hedgecode.chess.scanner.format.lichess.Clock;
30 import org.hedgecode.chess.scanner.format.lichess.Game;
31 import org.hedgecode.chess.scanner.format.lichess.GameData;
32 import org.hedgecode.chess.scanner.format.lichess.Player;
33
34 /**
35  * JSONGameData
36  *
37  * @author Dmitry Samoshin aka gotty
38  */
39 public class JSONGameData implements GameData {
40
41     private static final String PLAYER_WHITE = "white";
42     private static final String PLAYER_BLACK = "black";
43
44     private static final String GAME_DRAW = "draw";
45
46     private static final Map<String, String> RESULT = new HashMap<String, String>() {
47         {
48             put( PLAYER_WHITE, "1-0" );
49             put( PLAYER_BLACK, "0-1" );
50             put( GAME_DRAW, "1/2-1/2");
51         }
52     };
53
54     private static final Map<Boolean, String> RATED_GAME = new HashMap<Boolean, String>() {
55         {
56             put( Boolean.FALSE, "Unrated" );
57             put( Boolean.TRUE,  "Rated"   );
58         }
59     };
60
61     private static final String EVENT_GAME_FORMAT = "%s %s game";
62
63     @SerializedName("game")
64     private JSONGame game;
65
66     @SerializedName("player")
67     private JSONPlayer player;
68
69     @SerializedName("opponent")
70     private JSONPlayer opponent;
71
72     @SerializedName("clock")
73     private JSONClock clock;
74
75     @SerializedName("steps")
76     private JSONMove[] steps;
77
78     @SerializedName("treeParts")
79     private JSONMove[] treeParts;
80
81     @Expose
82     private Move[] moves;
83
84     JSONGameData() {
85     }
86
87     @Override
88     public Game game() {
89         return game;
90     }
91
92     @Override
93     public Player player() {
94         return player;
95     }
96
97     @Override
98     public Player opponent() {
99         return opponent;
100     }
101
102     public String event() {
103         return String.format(
104                 EVENT_GAME_FORMAT,
105                 RATED_GAME.get(game.rated()),
106                 game.speed()
107         );
108     }
109
110     public Player white() {
111         return player.color().equals(PLAYER_WHITE)
112                 ? player
113                 : opponent.color().equals(PLAYER_WHITE)
114                 ? opponent
115                 : null;
116     }
117
118     public Player black() {
119         return player.color().equals(PLAYER_BLACK)
120                 ? player
121                 : opponent.color().equals(PLAYER_BLACK)
122                 ? opponent
123                 : null;
124     }
125
126     public String result() {
127         String result = null;
128         if (game.winner() != null) {
129             if (PLAYER_WHITE.equals(game.winner())) {
130                 result = PLAYER_WHITE;
131             } else if (PLAYER_BLACK.equals(game.winner())) {
132                 result = PLAYER_BLACK;
133             }
134         } else if (GAME_DRAW.equals(game.status().name())) {
135             result = GAME_DRAW;
136         }
137         return RESULT.get(result);
138     }
139
140     @Override
141     public Clock clock() {
142         return clock;
143     }
144
145     @Override
146     public Move[] moves() {
147         if (moves == null) {
148             moves = steps != null ? steps : treeParts;
149             List<Move> movesList = new ArrayList<>(Arrays.asList(moves));
150             movesList.removeIf(move -> move.move() == null);
151             moves = movesList.toArray(
152                     new Move[movesList.size()]
153             );
154         }
155         return moves;
156     }
157
158 }