2 * Copyright (c) 2017-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker;
19 import org.hedgecode.snooker.api.APIException;
20 import org.hedgecode.snooker.api.Event;
21 import org.hedgecode.snooker.api.Events;
22 import org.hedgecode.snooker.api.Match;
23 import org.hedgecode.snooker.api.Player;
24 import org.hedgecode.snooker.api.Round;
25 import org.hedgecode.snooker.api.Rounds;
26 import org.hedgecode.snooker.api.Season;
27 import org.hedgecode.snooker.api.Seeding;
28 import org.hedgecode.snooker.api.Seedings;
29 import org.hedgecode.snooker.api.SnookerScoreAPI;
32 * Snooker Score Main Application.
34 * @author Dmitry Samoshin aka gotty
36 public final class SnookerScoreApp {
38 private static final String ARG_PREFIX = "^[-/]";
39 private static final String ARG_POSTFIX = "$";
40 private static final String ARG_QUIET_REGEX = ARG_PREFIX + SnookerScoreConsole.ARG_QUIET + ARG_POSTFIX;
41 private static final String ARG_SERVER_REGEX = ARG_PREFIX + SnookerScoreConsole.ARG_SERVER + ARG_POSTFIX;
42 private static final String ARG_PORT_REGEX = ARG_PREFIX + SnookerScoreConsole.ARG_PORT + ARG_POSTFIX;
43 private static final String ARG_HELP_REGEX = ARG_PREFIX + SnookerScoreConsole.ARG_HELP + ARG_POSTFIX;
45 private static final int INVALID_PORT = -1;
46 private static final int PORT_RANGE_MIN = 0;
47 private static final int PORT_RANGE_MAX = 65535;
48 private static final int PORT_RANGE_SYSTEM = 1024;
49 private static final String PORT_NUMBER_REGEX = "[0-9]+";
51 private static final int UPCOMING_COUNT = 5;
52 private static final int TOP_SEEDING_COUNT = 10;
54 private static final String INDENT = " ";
55 private static final String CURRENT_EVENTS = "Current Snooker Events:";
56 private static final String UPCOMING_EVENTS = "Upcoming Snooker Events:";
57 private static final String EVENT_ROUNDS = "Rounds of";
58 private static final String EVENT_SEEDING = "Seeding of";
60 public static void main(String[] args) throws APIException
62 if (isHelpMode(args)) {
64 if (!isQuietMode(args)) {
69 } else if (!isQuietMode(args)) {
73 Events events = Snooker.uncachedAPI().getSeasonEvents(
78 printCurrentEvents(events);
79 printUpcomingEvents(events);
83 int serverPort = serverMode(args);
84 if (isValidPort(serverPort)) {
89 private static boolean isQuietMode(String[] args) {
90 for (String arg : args) {
91 if (arg.matches(ARG_QUIET_REGEX)) {
98 private static boolean isHelpMode(String[] args) {
99 for (String arg : args) {
100 if (arg.matches(ARG_HELP_REGEX)) {
108 private static int serverMode(String[] args) {
109 boolean isServerMode = false;
110 int serverPort = Integer.parseInt(
111 SnookerScoreProperties.get("snooker.port.number")
113 boolean isPortArg = false;
114 for (String arg : args) {
116 if (arg.matches(PORT_NUMBER_REGEX)) {
117 serverPort = Integer.parseInt(arg);
120 } else if (arg.matches(ARG_PORT_REGEX)) {
122 } else if (arg.matches(ARG_SERVER_REGEX)) {
126 return isServerMode ? serverPort : INVALID_PORT;
129 private static boolean isValidPort(int portNumber) {
130 if (portNumber >= PORT_RANGE_MIN && portNumber <= PORT_RANGE_MAX) {
131 if (portNumber < PORT_RANGE_SYSTEM) {
132 SnookerScoreConsole.console(
136 "Don't recommend using port numbers less than",
146 private static void printWelcome() {
147 SnookerScoreConsole.init();
148 SnookerScoreConsole.welcome();
149 SnookerScoreConsole.empty();
150 SnookerScoreConsole.delimiter();
153 private static void printHelp() {
154 SnookerScoreConsole.empty();
155 SnookerScoreConsole.help();
156 SnookerScoreConsole.empty();
159 private static void printCurrentEvents(Events events) {
160 Events currentEvents = events.current();
161 if (!currentEvents.isEmpty()) {
162 SnookerScoreConsole.console(
163 String.format("%s%s", INDENT, CURRENT_EVENTS)
165 for (Event currentEvent : currentEvents) {
166 printEvent(currentEvent, INDENT.concat(INDENT));
168 SnookerScoreConsole.delimiter();
172 private static void printUpcomingEvents(Events events) {
173 Events upcomingEvents = events.upcoming();
174 if (!upcomingEvents.isEmpty()) {
175 SnookerScoreConsole.console(
176 String.format("%s%s", INDENT, UPCOMING_EVENTS)
179 for (Event upcomingEvent : upcomingEvents) {
180 printEvent(upcomingEvent, INDENT.concat(INDENT));
182 if (count >= UPCOMING_COUNT) break;
184 SnookerScoreConsole.delimiter();
188 private static void printEvent(Event event, String indent) {
189 SnookerScoreConsole.console(
191 "%s%s [%s - %s] (%s, %s)",
194 SnookerDateUtils.formatDate(event.startDate()),
195 SnookerDateUtils.formatDate(event.endDate()),
202 private static void printPlayer(Player player) throws APIException {
203 SnookerScoreConsole.console(
208 SnookerDateUtils.formatDate(player.born()),
214 private static void printMatch(Match match) throws APIException {
215 SnookerScoreAPI api = Snooker.uncachedAPI();
216 Player player1 = api.getPlayer(match.player1Id());
217 Player player2 = api.getPlayer(match.player2Id());
218 SnookerScoreConsole.console(
220 "%s[%s %s - %s] %s %s-%s %s",
222 SnookerDateUtils.formatDate(match.startDate()),
223 SnookerDateUtils.formatTime(match.startDate()),
224 SnookerDateUtils.formatTime(match.endDate()),
233 private static void printRounds(Rounds rounds, Event event) throws APIException {
234 if (!rounds.isEmpty()) {
235 rounds.sortByRound();
237 SnookerScoreConsole.console(
238 String.format("%s%s %s:", INDENT, EVENT_ROUNDS, event.name())
241 for (Round round : rounds) {
242 printRound(round, event, INDENT.concat(INDENT));
244 SnookerScoreConsole.delimiter();
248 private static void printRound(Round round, Event event, String indent) throws APIException {
249 if (event == null || event.getId() != round.eventId()) {
250 SnookerScoreAPI api = Snooker.uncachedAPI();
251 Event roundEvent = api.getEvent(round.eventId());
252 SnookerScoreConsole.console(
262 SnookerScoreConsole.console(
273 private static void printSeedings(Seedings seedings, Event event) throws APIException {
274 if (!seedings.isEmpty()) {
275 seedings.sortBySeeding();
277 SnookerScoreConsole.console(
288 for (Seeding seeding : seedings) {
289 printSeeding(seeding, INDENT.concat(INDENT));
291 if (count >= TOP_SEEDING_COUNT) break;
293 SnookerScoreConsole.delimiter();
297 private static void printSeeding(Seeding seeding, String indent) throws APIException {
298 SnookerScoreAPI api = Snooker.uncachedAPI();
299 Player player = api.getPlayer(seeding.playerId());
300 SnookerScoreConsole.console(