Files
novafarma/src/systems/PlaytimeTrackerSystem.js
2025-12-08 17:31:18 +01:00

142 lines
3.3 KiB
JavaScript

/**
* PLAYTIME TRACKER SYSTEM
* Tracks persistent stats: playtime, deaths, harvests, etc.
*/
class PlaytimeTrackerSystem {
constructor(scene) {
this.scene = scene;
// Initialize stats
this.stats = {
playtimeSeconds: 0,
deaths: 0,
kills: 0,
harvests: 0,
plantings: 0,
distanceTraveled: 0,
moneyEarned: 0,
itemsCrafted: 0,
blocksPlaced: 0,
sessionStart: Date.now()
};
// Load from save
this.load();
// Update timer
this.updateTimer = 0;
this.saveInterval = 10000; // Save every 10 seconds
}
update(delta) {
// Increment playtime
this.stats.playtimeSeconds += delta / 1000;
// Periodic save
this.updateTimer += delta;
if (this.updateTimer >= this.saveInterval) {
this.updateTimer = 0;
this.save();
}
}
// Stat tracking methods
recordDeath() {
this.stats.deaths++;
this.save();
}
recordKill() {
this.stats.kills++;
}
recordHarvest() {
this.stats.harvests++;
}
recordPlanting() {
this.stats.plantings++;
}
recordDistance(distance) {
this.stats.distanceTraveled += distance;
}
recordMoneyEarned(amount) {
this.stats.moneyEarned += amount;
}
recordCraft() {
this.stats.itemsCrafted++;
}
recordBlockPlaced() {
this.stats.blocksPlaced++;
}
// Formatted playtime
getPlaytimeFormatted() {
const hours = Math.floor(this.stats.playtimeSeconds / 3600);
const minutes = Math.floor((this.stats.playtimeSeconds % 3600) / 60);
const seconds = Math.floor(this.stats.playtimeSeconds % 60);
if (hours > 0) {
return `${hours}h ${minutes}m`;
} else if (minutes > 0) {
return `${minutes}m ${seconds}s`;
} else {
return `${seconds}s`;
}
}
// Session duration
getSessionDuration() {
const sessionMs = Date.now() - this.stats.sessionStart;
const sessionSec = Math.floor(sessionMs / 1000);
return sessionSec;
}
// Save/Load
save() {
localStorage.setItem('novafarma_stats', JSON.stringify(this.stats));
}
load() {
const saved = localStorage.getItem('novafarma_stats');
if (saved) {
try {
const data = JSON.parse(saved);
this.stats = { ...this.stats, ...data };
this.stats.sessionStart = Date.now(); // Reset session timer
} catch (e) {
console.error('Failed to load stats:', e);
}
}
}
reset() {
this.stats = {
playtimeSeconds: 0,
deaths: 0,
kills: 0,
harvests: 0,
plantings: 0,
distanceTraveled: 0,
moneyEarned: 0,
itemsCrafted: 0,
blocksPlaced: 0,
sessionStart: Date.now()
};
this.save();
}
// Get all stats
getStats() {
return {
...this.stats,
playtimeFormatted: this.getPlaytimeFormatted(),
sessionDuration: this.getSessionDuration()
};
}
}