Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed Nov 20, 2016
1 parent c1b474d commit 7f85577
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 575 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
apply plugin: 'java'
apply plugin: 'idea'

repositories {
jcenter()
mavenCentral()
maven {
name 'Sonatype'
url 'https://oss.sonatype.org/content/groups/public'
Expand All @@ -12,6 +13,13 @@ repositories {
}
}

idea {
module {
downloadJavadoc = true
downloadSources = true
}
}

dependencies {
compile 'org.spigotmc:spigot-api:1.10.2-R0.1-SNAPSHOT'
compile files('libs/Essentials-2.x-SNAPSHOT.jar')
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cat.nyaa.playtimetracker;

import com.google.common.io.LineReader;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DatabaseManager {
private final File dbFile;
private Map<UUID, DatabaseRecord> recordMap;

public DatabaseManager(File db_file) {
dbFile = db_file;
recordMap = new HashMap<>();
if (dbFile.isFile()) { // Read in
ConfigurationSection cfg = YamlConfiguration.loadConfiguration(dbFile);
for (String uuid_str : cfg.getKeys(false)) {
ConfigurationSection sec = cfg.getConfigurationSection(uuid_str);
UUID id = UUID.fromString(uuid_str);
recordMap.put(id, DatabaseRecord.deserialize(id, sec));
}
}
}

public DatabaseManager(File db_file, File old_db_file) {
dbFile = db_file;
recordMap = new HashMap<>();
if (old_db_file.isFile()) {
FileReader fr = null;
LineReader lr;
try {
fr = new FileReader(old_db_file);
lr = new LineReader(fr);
String line;
while ((line = lr.readLine()) != null) {
String[] tmp = line.split(" ", 2);
if (tmp.length != 2) continue;
UUID a = null;
try {
a = UUID.fromString(tmp[0]);
} catch (IllegalArgumentException ex) {
System.err.print("[PlayTimeTracker-LEGACY-DB]Illegal data line: " + line);
continue;
}
recordMap.put(a, DatabaseRecord.deserialize_legacy(a, tmp[1]));
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (fr != null) fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

public void save() {
YamlConfiguration cfg = new YamlConfiguration();
for (UUID id : recordMap.keySet()) {
recordMap.get(id).serialize(cfg.createSection(id.toString()));
}
try {
cfg.save(dbFile);
} catch (IOException ex) {
System.out.print(">>>>>> PlayTimeTracker Database Emergency Dump <<<<<<\n" +
cfg.saveToString() +
"\n>>>>>> Emergency dump ends <<<<<<"
);
ex.printStackTrace();
}
}

public DatabaseRecord getRecord(UUID id) {
return recordMap.get(id);
}

public void createRecord(UUID id, ZonedDateTime time) {
recordMap.put(id, new DatabaseRecord(id, time));
}

public Map<UUID, DatabaseRecord> getAllRecords() {
return recordMap;
}
}
96 changes: 96 additions & 0 deletions src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cat.nyaa.playtimetracker;

import org.bukkit.configuration.ConfigurationSection;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class DatabaseRecord {
public UUID uuid;
public ZonedDateTime lastSeen; // timestamp millisecond
public long dailyTime; // millisecond
public long weeklyTime; // millisecond
public long monthlyTime; // millisecond
public long totalTime; // millisecond
public Set<String> completedDailyMissions;
public Set<String> completedWeeklyMissions;
public Set<String> completedMonthlyMissions;
public Set<String> completedLifetimeMissions;

public DatabaseRecord() {
reset();
}

public DatabaseRecord(UUID id, ZonedDateTime time) {
reset();
uuid = id;
lastSeen = time;
}

public static DatabaseRecord deserialize(UUID id, ConfigurationSection sec) {
DatabaseRecord rec = new DatabaseRecord();
rec.uuid = id;
rec.lastSeen = ZonedDateTime.parse(sec.getString("last_seen"));
rec.dailyTime = sec.getLong("daily_play_time");
rec.weeklyTime = sec.getLong("weekly_play_time");
rec.monthlyTime = sec.getLong("monthly_play_time");
rec.totalTime = sec.getLong("total_play_time");
rec.completedDailyMissions = new HashSet<>(sec.getStringList("completed_daily_mission"));
rec.completedWeeklyMissions = new HashSet<>(sec.getStringList("completed_weekly_mission"));
rec.completedMonthlyMissions = new HashSet<>(sec.getStringList("completed_monthly_mission"));
rec.completedLifetimeMissions = new HashSet<>(sec.getStringList("completed_lifetime_mission"));
return rec;
}

private static HashSet<String> _legacy_deserializeSet(String str) {
if ("{}".equals(str)) return new HashSet<>();
HashSet<String> r = new HashSet<>();
r.addAll(Arrays.asList(str.substring(1, str.length() - 1).split(",")));
return r;
}

public static DatabaseRecord deserialize_legacy(UUID id, String old_format_string) {
String[] tmp = old_format_string.split(" ");
DatabaseRecord rec = new DatabaseRecord();
Instant instantTime = Instant.ofEpochMilli(Long.parseLong(tmp[0]));
rec.lastSeen = ZonedDateTime.ofInstant(instantTime, ZoneId.systemDefault());
rec.dailyTime = Long.parseLong(tmp[4]);
rec.weeklyTime = Long.parseLong(tmp[5]);
rec.monthlyTime = Long.parseLong(tmp[6]);
rec.totalTime = Long.parseLong(tmp[7]);
rec.completedDailyMissions = _legacy_deserializeSet(tmp[8]);
rec.completedWeeklyMissions = _legacy_deserializeSet(tmp[9]);
rec.completedMonthlyMissions = _legacy_deserializeSet(tmp[10]);
rec.completedLifetimeMissions = _legacy_deserializeSet(tmp[11]);
return rec;
}

public void serialize(ConfigurationSection sec) {
sec.set("last_seen", lastSeen);
sec.set("daily_play_time", dailyTime);
sec.set("weekly_play_time", weeklyTime);
sec.set("monthly_play_time", monthlyTime);
sec.set("total_play_time", totalTime);
sec.set("completed_daily_mission", completedDailyMissions.toArray(new String[0]));
sec.set("completed_weekly_mission", completedWeeklyMissions.toArray(new String[0]));
sec.set("completed_monthly_mission", completedMonthlyMissions.toArray(new String[0]));
sec.set("completed_lifetime_mission", completedLifetimeMissions.toArray(new String[0]));
}

public void reset() {
lastSeen = ZonedDateTime.now();
dailyTime = 0;
weeklyTime = 0;
monthlyTime = 0;
totalTime = 0;
completedLifetimeMissions = new HashSet<>();
completedDailyMissions = new HashSet<>();
completedWeeklyMissions = new HashSet<>();
completedMonthlyMissions = new HashSet<>();
}
}
12 changes: 6 additions & 6 deletions src/main/java/cat/nyaa/playtimetracker/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ public static void init(ConfigurationSection langSection) {
prefix = getConfigStringWithColor(lang, "prefix");
}

public static String get(String name, String... args) {
public static String get(String name, Object... args) {
return prefix + String.format(getConfigStringWithColor(lang, name), args);
}

public static String formatTime(long ms) {
ConfigurationSection s = lang.getConfigurationSection("statistic-time-format");
String str = "";
if (ms == 0)
return getConfigStringWithColor(s,"zero");
return getConfigStringWithColor(s, "zero");

if (ms > 0) {
str = String.format(getConfigStringWithColor(s,"ms"), ms % 1000) + str;
str = String.format(getConfigStringWithColor(s, "ms"), ms % 1000) + str;
ms = Math.floorDiv(ms, 1000);
}
if (ms > 0) {
str = String.format(getConfigStringWithColor(s,"s"), ms % 60) + str;
str = String.format(getConfigStringWithColor(s, "s"), ms % 60) + str;
ms = Math.floorDiv(ms, 60);
}
if (ms > 0) {
str = String.format(getConfigStringWithColor(s,"m"), ms % 60) + str;
str = String.format(getConfigStringWithColor(s, "m"), ms % 60) + str;
ms = Math.floorDiv(ms, 60);
}
if (ms > 0) {
str = String.format(getConfigStringWithColor(s,"h"), ms) + str;
str = String.format(getConfigStringWithColor(s, "h"), ms) + str;
}

return str;
Expand Down
Loading

0 comments on commit 7f85577

Please sign in to comment.