-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1b474d
commit 7f85577
Showing
8 changed files
with
596 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
96
src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.