Skip to content

Commit

Permalink
Change order of operations and reduce statefulness of Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
pdinklag committed Aug 6, 2024
1 parent 2f9cb60 commit 7c95df1
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions src/main/java/de/pdinklag/mcstats/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,9 +52,6 @@ public abstract class Updater {
// initialization
protected final Config config;

private final HashMap<String, Stat> awards = new HashMap<>();
private final HashMap<String, Event> events = new HashMap<>();

// paths
private final Path dbPath;
private final Path dbPlayersJsonPath;
Expand Down Expand Up @@ -154,7 +152,7 @@ protected PlayerFilter getInactiveFilter() {
System.currentTimeMillis() - DAYS_TO_MILLISECONDS * (long) config.getInactiveDays());
}

private HashMap<String, Player> processPlayers(Iterable<PlayerFilter> filters) {
private HashMap<String, Player> processPlayers(Iterable<PlayerFilter> filters, Map<String, Stat> awards) {
final Log log = Log.getCurrent();

HashMap<String, Player> discoveredPlayers = new HashMap<>();
Expand Down Expand Up @@ -280,7 +278,7 @@ private void writePlayerList(Collection<Player> players, String filenameFormat)
}
}

private void parseAndRegisterStat(JSONObject json) throws Exception {
private void parseAndRegisterStat(JSONObject json, Map<String, Stat> awards) throws Exception {
final Stat stat = StatParser.parse(json);
if (!awards.containsKey(stat.getId())) {
awards.put(stat.getId(), stat);
Expand All @@ -289,7 +287,7 @@ private void parseAndRegisterStat(JSONObject json) throws Exception {
}
}

private void parseAndRegisterEvent(JSONObject json) throws Exception {
private void parseAndRegisterEvent(JSONObject json, Map<String, Event> events) throws Exception {
final Event event = EventParser.parse(json);
if (!events.containsKey(event.getId())) {
events.put(event.getId(), event);
Expand All @@ -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<String, Stat> 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<String, Event> 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
Expand All @@ -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<String, Player> allPlayers = processPlayers(getHardPlayerFilters());
HashMap<String, Player> allPlayers = processPlayers(getHardPlayerFilters(), awards);

// find effective server version
final int serverDataVersion;
Expand Down

0 comments on commit 7c95df1

Please sign in to comment.