mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-11-30 17:53:42 +01:00
Add the basics of a played-time tracker.
This commit is contained in:
1 parent
aa4ddbe9ae
commit
31c85ae0a5
12 files changed
+186
-11
No files matched your search
@@ -1157,6 +1157,10 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
|
||||
}
|
||||
}
|
||||
|
||||
// Time tracking
|
||||
Section *playTime = iniFile.GetOrCreateSection("PlayTime");
|
||||
playTimeTracker_.Load(playTime);
|
||||
|
||||
auto pinnedPaths = iniFile.GetOrCreateSection("PinnedPaths")->ToMap();
|
||||
vPinnedPaths.clear();
|
||||
for (auto it = pinnedPaths.begin(), end = pinnedPaths.end(); it != end; ++it) {
|
||||
@@ -1317,6 +1321,10 @@ bool Config::Save(const char *saveReason) {
|
||||
if (LogManager::GetInstance())
|
||||
LogManager::GetInstance()->SaveConfig(log);
|
||||
|
||||
// Time tracking
|
||||
Section *playTime = iniFile.GetOrCreateSection("PlayTime");
|
||||
playTimeTracker_.Save(playTime);
|
||||
|
||||
if (!iniFile.Save(iniFilename_)) {
|
||||
ERROR_LOG(LOADER, "Error saving config (%s)- can't write ini '%s'", saveReason, iniFilename_.c_str());
|
||||
return false;
|
||||
@@ -1827,3 +1835,85 @@ int Config::GetPSPLanguage() {
|
||||
return g_Config.iLanguage;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayTimeTracker::Start(std::string gameId) {
|
||||
if (gameId.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto iter = tracker_.find(std::string(gameId));
|
||||
if (iter != tracker_.end()) {
|
||||
iter->second.lastTimePlayed = time_now_unix_utc();
|
||||
iter->second.startTime = time_now_d();
|
||||
return;
|
||||
}
|
||||
|
||||
PlayTime playTime;
|
||||
playTime.lastTimePlayed = time_now_unix_utc();
|
||||
playTime.totalTimePlayed = 0.0;
|
||||
playTime.startTime = time_now_d();
|
||||
tracker_[gameId] = playTime;
|
||||
}
|
||||
|
||||
void PlayTimeTracker::Stop(std::string gameId) {
|
||||
_dbg_assert_(!gameId.empty());
|
||||
|
||||
auto iter = tracker_.find(std::string(gameId));
|
||||
if (iter != tracker_.end()) {
|
||||
if (iter->second.startTime != 0.0) {
|
||||
iter->second.totalTimePlayed += time_now_d() - iter->second.startTime;
|
||||
}
|
||||
iter->second.lastTimePlayed = time_now_unix_utc();
|
||||
iter->second.startTime = 0.0;
|
||||
return;
|
||||
}
|
||||
|
||||
// Shouldn't happen, ignore this case.
|
||||
WARN_LOG(SYSTEM, "GameTimeTracker::Stop called without corresponding GameTimeTracker::Start");
|
||||
}
|
||||
|
||||
void PlayTimeTracker::Load(const Section *section) {
|
||||
tracker_.clear();
|
||||
|
||||
std::vector<std::string> keys;
|
||||
section->GetKeys(keys);
|
||||
|
||||
for (auto key : keys) {
|
||||
std::string value;
|
||||
if (!section->Get(key.c_str(), &value, nullptr)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse the string.
|
||||
PlayTime gameTime{};
|
||||
if (2 == sscanf(value.c_str(), "%d,%llu", &gameTime.totalTimePlayed, &gameTime.lastTimePlayed)) {
|
||||
tracker_[key] = gameTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayTimeTracker::Save(Section *section) {
|
||||
for (auto iter : tracker_) {
|
||||
std::string formatted = StringFromFormat("%d,%llu", iter.second.totalTimePlayed, iter.second.lastTimePlayed);
|
||||
section->Set(iter.first.c_str(), formatted);
|
||||
}
|
||||
}
|
||||
|
||||
bool PlayTimeTracker::GetPlayedTimeString(const std::string &gameId, std::string *str) const {
|
||||
auto ga = GetI18NCategory(I18NCat::GAME);
|
||||
|
||||
auto iter = tracker_.find(gameId);
|
||||
if (iter == tracker_.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int totalSeconds = iter->second.totalTimePlayed;
|
||||
int seconds = totalSeconds % 60;
|
||||
totalSeconds /= 60;
|
||||
int minutes = totalSeconds % 60;
|
||||
totalSeconds /= 60;
|
||||
int hours = totalSeconds;
|
||||
|
||||
*str = StringFromFormat(ga->T("Play time: %02d:%02d:%02d"), hours, minutes, seconds);
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user