diff --git a/src/main/java/de/pdinklag/mcstats/Updater.java b/src/main/java/de/pdinklag/mcstats/Updater.java index 05b5edf1..a6e7ab9e 100644 --- a/src/main/java/de/pdinklag/mcstats/Updater.java +++ b/src/main/java/de/pdinklag/mcstats/Updater.java @@ -12,6 +12,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; @@ -51,9 +52,6 @@ public abstract class Updater { // initialization protected final Config config; - private final HashMap awards = new HashMap<>(); - private final HashMap events = new HashMap<>(); - // paths private final Path dbPath; private final Path dbPlayersJsonPath; @@ -154,7 +152,7 @@ protected PlayerFilter getInactiveFilter() { System.currentTimeMillis() - DAYS_TO_MILLISECONDS * (long) config.getInactiveDays()); } - private HashMap processPlayers(Iterable filters) { + private HashMap processPlayers(Iterable filters, Map awards) { final Log log = Log.getCurrent(); HashMap discoveredPlayers = new HashMap<>(); @@ -280,7 +278,7 @@ private void writePlayerList(Collection players, String filenameFormat) } } - private void parseAndRegisterStat(JSONObject json) throws Exception { + private void parseAndRegisterStat(JSONObject json, Map awards) throws Exception { final Stat stat = StatParser.parse(json); if (!awards.containsKey(stat.getId())) { awards.put(stat.getId(), stat); @@ -289,7 +287,7 @@ private void parseAndRegisterStat(JSONObject json) throws Exception { } } - private void parseAndRegisterEvent(JSONObject json) throws Exception { + private void parseAndRegisterEvent(JSONObject json, Map events) throws Exception { final Event event = EventParser.parse(json); if (!events.containsKey(event.getId())) { events.put(event.getId(), event); @@ -309,82 +307,81 @@ public Updater(Config config) { this.dbPlayercachePath = dbPath.resolve(DATABASE_PLAYERCACHE); this.dbPlayerdataPath = dbPath.resolve(DATABASE_PLAYERDATA); this.dbPlayerlistPath = dbPath.resolve(DATABASE_PLAYERLIST); + } - // make sure any legacy summary file is deleted - { - final Path legacySummaryPath = dbPath.resolve(DATABASE_LEGACY_SUMMARY); - if (Files.isRegularFile(legacySummaryPath)) { - try { - Files.delete(legacySummaryPath); - } catch (IOException ex) { - Log.getCurrent().writeError("failed to delete legacy summary file", ex); - } - } + /** + * Gets the server's message of the day. + * + * @return the server's message of the day + */ + protected abstract String getServerMotd(); + + /** + * Gets the updater version as a string. + * + * @return the updater version + */ + protected abstract String getVersion(); + + public void run(ConsoleWriter consoleWriter) { + // initialize log + final Log log; + try { + log = new Log(config.getLogfilePath(), consoleWriter); + Log.setCurrent(log); + } catch (IOException ex) { + consoleWriter.writeError("failed to initialize logging", ex); + return; } // discover and instantiate stats + HashMap awards = new HashMap<>(); try { if (Files.isDirectory(config.getStatsPath())) { Files.list(config.getStatsPath()).forEach(path -> { if (path.getFileName().toString().endsWith(JSON_FILE_EXT)) { try { final JSONObject obj = new JSONObject(Files.readString(path)); - parseAndRegisterStat(obj); + parseAndRegisterStat(obj, awards); } catch (Exception e2) { - Log.getCurrent().writeError("failed to load stat from file: " + path.toString(), e2); + log.writeError("failed to load stat from file: " + path.toString(), e2); } } }); } } catch (Exception e) { - Log.getCurrent().writeError("failed to discover stats", e); + log.writeError("failed to discover stats", e); } // discover events + HashMap events = new HashMap<>(); try { if (Files.isDirectory(config.getEventsPath())) { Files.list(config.getEventsPath()).forEach(path -> { if (path.getFileName().toString().endsWith(JSON_FILE_EXT)) { try { final JSONObject obj = new JSONObject(Files.readString(path)); - parseAndRegisterEvent(obj); + parseAndRegisterEvent(obj, events); } catch (Exception e2) { - Log.getCurrent().writeError("failed to load stat from file: " + path.toString(), e2); + log.writeError("failed to load stat from file: " + path.toString(), e2); } } }); } } catch (Exception e) { - Log.getCurrent().writeError("failed to discover events", e); + log.writeError("failed to discover events", e); } - } - /** - * Gets the server's message of the day. - * - * @return the server's message of the day - */ - protected abstract String getServerMotd(); - - /** - * Gets the updater version as a string. - * - * @return the updater version - */ - protected abstract String getVersion(); - - public void run(ConsoleWriter consoleWriter) { - // get current timestamp - final long now = System.currentTimeMillis(); - - // initialize log - final Log log; - try { - log = new Log(config.getLogfilePath(), consoleWriter); - Log.setCurrent(log); - } catch (IOException ex) { - consoleWriter.writeError("failed to initialize logging", ex); - return; + // make sure the legacy summary file is deleted + { + final Path legacySummaryPath = dbPath.resolve(DATABASE_LEGACY_SUMMARY); + if (Files.isRegularFile(legacySummaryPath)) { + try { + Files.delete(legacySummaryPath); + } catch (IOException ex) { + consoleWriter.writeError("failed to delete legacy summary file", ex); + } + } } // create database directories @@ -394,8 +391,11 @@ public void run(ConsoleWriter consoleWriter) { log.writeError("failed to create database directories: " + dbPath.toString(), e); } + // get current timestamp + final long now = System.currentTimeMillis(); + // discover and process players - HashMap allPlayers = processPlayers(getHardPlayerFilters()); + HashMap allPlayers = processPlayers(getHardPlayerFilters(), awards); // find effective server version final int serverDataVersion;