Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursiveG committed Nov 20, 2016
1 parent a04fabc commit b290b79
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/cat/nyaa/playtimetracker/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ public DatabaseManager(File db_file, File old_db_file) {
try {
a = UUID.fromString(tmp[0]);
} catch (IllegalArgumentException ex) {
System.err.print("[PlayTimeTracker-LEGACY-DB]Illegal data line: " + line);
Main.log("Illegal data line: " + line);
continue;
}
recordMap.put(a, DatabaseRecord.deserialize_legacy(a, tmp[1]));
}
} catch (IOException ex) {
Main.log("Failed to parse legacy database");
ex.printStackTrace();
} finally {
try {
if (fr != null) fr.close();
} catch (IOException ex) {
Main.log("Failed to parse legacy database");
ex.printStackTrace();
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cat/nyaa/playtimetracker/DatabaseRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ public void reset() {
completedWeeklyMissions = new HashSet<>();
completedMonthlyMissions = new HashSet<>();
}

@Override
public String toString() {
return super.toString() + String.format(
"{\"last_seen\": \"%s\", \"daily_play_time\": %d, \"weekly_play_time\": %d, \"monthly_play_time\": %d, " +
"\"total_play_time\": %d, \"daily_completed\": \"%s\", \"weekly_completed\": \"%s\", " +
"\"monthly_completed\": \"%s\", \"lifetime_completed\": \"%s\"}",
lastSeen.toString(), dailyTime, weeklyTime, monthlyTime, totalTime,
completedDailyMissions, completedWeeklyMissions, completedMonthlyMissions, completedLifetimeMissions);
}
}
17 changes: 17 additions & 0 deletions src/main/java/cat/nyaa/playtimetracker/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;

import static java.time.temporal.ChronoUnit.DAYS;
Expand All @@ -34,6 +35,20 @@ public class Main extends JavaPlugin implements Runnable, Listener {
private Map<String, Rule> rules;
private Map<String, Reward> rewardMap;

public static void log(String msg) {
if (instance == null) {
System.out.println("[PlayTimeTracker] " + msg);
} else {
instance.getLogger().info(msg);
}
}

public static void debug(String msg) {
if (instance != null) {
instance.getLogger().log(Level.FINE, msg);
}
}

@Override
public void onDisable() {
Bukkit.getScheduler().cancelTasks(this);
Expand Down Expand Up @@ -127,6 +142,7 @@ private void applyReward(Rule rule, Player p) {
if (reward.getDescription() != null && reward.getDescription().length() > 0) {
p.sendMessage(rewardMap.get(rule.reward).getDescription());
}
log(String.format("Reward rule %s applied to player %s", rule.name, p.getName()));
}

private void notifyAcquire(Player p) {
Expand All @@ -150,6 +166,7 @@ private void notifyAcquire(Player p) {

@Override
public void run() { // Auto-save timer
log("Auto-save timer executing...");
updater.updateAllOnlinePlayers();
for (Player p : Bukkit.getOnlinePlayers()) {
notifyAcquire(p);
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/cat/nyaa/playtimetracker/RecordManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public void updateSingle(OfflinePlayer p) {
*/
private UUID updateAccumulative(UUID id) {
if (id == null) return null;
if (Main.isAFK(id)) return updateNonAccumulative(id);
if (Main.isAFK(id)) {
Main.debug("updateNonAccumulative due player AFK: " + id.toString());
return updateNonAccumulative(id);
}
Main.debug("updateAccumulative: " + id.toString());
DatabaseRecord rec = db.getRecord(id);
if (rec == null) {
db.createRecord(id, ZonedDateTime.now());
Expand All @@ -98,24 +102,28 @@ private UUID updateAccumulative(UUID id) {
rec.lastSeen = currentTime;
long duration = Duration.between(lastSeen, currentTime).toMillis();
if (duration <= 0) return null;
Main.debug(String.format("Time duration: %d (%s ~ %s)", duration, lastSeen.toString(), currentTime.toString()));

ZonedDateTime startOfToday = currentTime.truncatedTo(ChronoUnit.DAYS);
ZonedDateTime startOfWeek = currentTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).truncatedTo(ChronoUnit.DAYS);
ZonedDateTime startOfMonth = currentTime.with(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS);

if (startOfToday.isAfter(lastSeen)) {
Main.debug("Daily time reset: " + id.toString());
rec.completedDailyMissions = new HashSet<>();
rec.dailyTime = Duration.between(startOfToday, currentTime).toMillis();
} else {
rec.dailyTime += duration;
}
if (startOfWeek.isAfter(lastSeen)) {
Main.debug("Weekly time reset: " + id.toString());
rec.completedWeeklyMissions = new HashSet<>();
rec.weeklyTime = 0;
} else {
rec.weeklyTime += duration;
}
if (startOfMonth.isAfter(lastSeen)) {
Main.debug("Daily time reset: " + id.toString());
rec.completedMonthlyMissions = new HashSet<>();
rec.monthlyTime = 0;
} else {
Expand All @@ -128,6 +136,7 @@ private UUID updateAccumulative(UUID id) {

private UUID updateNonAccumulative(UUID id) {
if (id == null) return null;
Main.debug("updateNonAccumulative: " + id.toString());
DatabaseRecord rec = db.getRecord(id);
if (rec == null) {
db.createRecord(id, ZonedDateTime.now());
Expand All @@ -143,14 +152,17 @@ private UUID updateNonAccumulative(UUID id) {
ZonedDateTime startOfMonth = currentTime.with(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS);

if (startOfToday.isAfter(lastSeen)) {
Main.debug("Daily time reset: " + id.toString());
rec.completedDailyMissions = new HashSet<>();
rec.dailyTime = 0;
}
if (startOfWeek.isAfter(lastSeen)) {
Main.debug("Weekly time reset: " + id.toString());
rec.completedWeeklyMissions = new HashSet<>();
rec.weeklyTime = 0;
}
if (startOfMonth.isAfter(lastSeen)) {
Main.debug("Monthly time reset: " + id.toString());
rec.completedMonthlyMissions = new HashSet<>();
rec.monthlyTime = 0;
}
Expand All @@ -173,6 +185,8 @@ public void resetAllStatistic() {
}

public void resetSingleStatistic(UUID id) {
if (id == null) return;
Main.log(String.format("Statistic reset for %s, old record: %s", id.toString(), db.getRecord(id)));
db.createRecord(id, ZonedDateTime.now());
db.save();
sessionRewardMap.remove(id);
Expand Down

0 comments on commit b290b79

Please sign in to comment.