From 9ba1cd15e7f902cae705ff22451e4f76ad0d71ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Tue, 22 Sep 2015 01:06:45 +0200 Subject: [PATCH] GH-1217 add simple instance play time tracking Not reliable in the face of crashes... but what is? --- logic/BaseInstance.cpp | 23 +++++++++++++++ logic/BaseInstance.h | 4 +++ logic/NullInstance.h | 4 +++ logic/ftb/LegacyFTBInstance.cpp | 14 ++++----- logic/ftb/LegacyFTBInstance.h | 2 +- logic/ftb/OneSixFTBInstance.cpp | 14 ++++----- logic/ftb/OneSixFTBInstance.h | 4 +-- logic/minecraft/LegacyInstance.cpp | 14 ++++----- logic/minecraft/LegacyInstance.h | 2 +- logic/minecraft/MinecraftInstance.cpp | 42 +++++++++++++++++++++++++++ logic/minecraft/MinecraftInstance.h | 4 +++ logic/minecraft/OneSixInstance.cpp | 23 ++++----------- logic/minecraft/OneSixInstance.h | 4 +-- 13 files changed, 103 insertions(+), 51 deletions(-) diff --git a/logic/BaseInstance.cpp b/logic/BaseInstance.cpp index 3e02235e..b0235b47 100644 --- a/logic/BaseInstance.cpp +++ b/logic/BaseInstance.cpp @@ -38,6 +38,7 @@ BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr s connect(ENV.icons().get(), SIGNAL(iconUpdated(QString)), SLOT(iconUpdated(QString))); m_settings->registerSetting("notes", ""); m_settings->registerSetting("lastLaunchTime", 0); + m_settings->registerSetting("totalTimePlayed", 0); // Custom Commands auto commandSetting = m_settings->registerSetting({"OverrideCommands","OverrideLaunchCmd"}, false); @@ -93,9 +94,31 @@ bool BaseInstance::isRunning() const void BaseInstance::setRunning(bool running) { + if(running && !m_isRunning) + { + m_timeStarted = QDateTime::currentDateTime(); + } + else if(!running && m_isRunning) + { + qint64 current = settings()->get("totalTimePlayed").toLongLong(); + QDateTime timeEnded = QDateTime::currentDateTime(); + settings()->set("totalTimePlayed", current + m_timeStarted.secsTo(timeEnded)); + emit propertiesChanged(this); + } m_isRunning = running; } +int64_t BaseInstance::totalTimePlayed() +{ + qint64 current = settings()->get("totalTimePlayed").toLongLong(); + if(m_isRunning) + { + QDateTime timeNow = QDateTime::currentDateTime(); + return current + m_timeStarted.secsTo(timeNow); + } + return current; +} + QString BaseInstance::instanceType() const { return m_settings->get("InstanceType").toString(); diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h index 7fbd5de5..32a4d1cd 100644 --- a/logic/BaseInstance.h +++ b/logic/BaseInstance.h @@ -71,6 +71,7 @@ public: void setRunning(bool running); bool isRunning() const; + int64_t totalTimePlayed(); /// get the type of this instance QString instanceType() const; @@ -190,6 +191,8 @@ public: /// get variables this instance exports virtual QMap getVariables() const = 0; + virtual QString typeName() const = 0; + enum InstanceFlag { VersionBrokenFlag = 0x01, @@ -230,6 +233,7 @@ protected: SettingsObjectPtr m_settings; InstanceFlags m_flags; bool m_isRunning = false; + QDateTime m_timeStarted; }; Q_DECLARE_METATYPE(std::shared_ptr) diff --git a/logic/NullInstance.h b/logic/NullInstance.h index ebfe4f56..cf304f9f 100644 --- a/logic/NullInstance.h +++ b/logic/NullInstance.h @@ -78,4 +78,8 @@ public: { return instanceRoot(); } + virtual QString typeName() const + { + return "Null"; + } }; diff --git a/logic/ftb/LegacyFTBInstance.cpp b/logic/ftb/LegacyFTBInstance.cpp index e7490679..a7091f1d 100644 --- a/logic/ftb/LegacyFTBInstance.cpp +++ b/logic/ftb/LegacyFTBInstance.cpp @@ -7,15 +7,6 @@ LegacyFTBInstance::LegacyFTBInstance(SettingsObjectPtr globalSettings, SettingsO { } -QString LegacyFTBInstance::getStatusbarDescription() -{ - if (flags() & VersionBrokenFlag) - { - return "Legacy FTB: " + intendedVersionId() + " (broken)"; - } - return "Legacy FTB: " + intendedVersionId(); -} - QString LegacyFTBInstance::id() const { return "FTB/" + BaseInstance::id(); @@ -29,3 +20,8 @@ void LegacyFTBInstance::copy(const QDir &newDir) QString inst_type = settings_obj.get("InstanceType").toString(); settings_obj.set("InstanceType", "Legacy"); } + +QString LegacyFTBInstance::typeName() const +{ + return tr("Legacy FTB"); +} diff --git a/logic/ftb/LegacyFTBInstance.h b/logic/ftb/LegacyFTBInstance.h index 3bacf653..c878dcc6 100644 --- a/logic/ftb/LegacyFTBInstance.h +++ b/logic/ftb/LegacyFTBInstance.h @@ -7,7 +7,7 @@ class LegacyFTBInstance : public LegacyInstance Q_OBJECT public: explicit LegacyFTBInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir); - virtual QString getStatusbarDescription(); virtual QString id() const; virtual void copy(const QDir &newDir); + virtual QString typeName() const; }; diff --git a/logic/ftb/OneSixFTBInstance.cpp b/logic/ftb/OneSixFTBInstance.cpp index 7cce1f48..1fc5b49b 100644 --- a/logic/ftb/OneSixFTBInstance.cpp +++ b/logic/ftb/OneSixFTBInstance.cpp @@ -125,18 +125,14 @@ void OneSixFTBInstance::createProfile() m_version.reset(new MinecraftProfile(new FTBProfileStrategy(this))); } -QString OneSixFTBInstance::getStatusbarDescription() -{ - if (flags() & VersionBrokenFlag) - { - return "OneSix FTB: " + intendedVersionId() + " (broken)"; - } - return "OneSix FTB: " + intendedVersionId(); -} - std::shared_ptr OneSixFTBInstance::createUpdateTask() { return OneSixInstance::createUpdateTask(); } +QString OneSixFTBInstance::typeName() const +{ + return tr("OneSix FTB"); +} + #include "OneSixFTBInstance.moc" diff --git a/logic/ftb/OneSixFTBInstance.h b/logic/ftb/OneSixFTBInstance.h index 564f275d..c53a2ed8 100644 --- a/logic/ftb/OneSixFTBInstance.h +++ b/logic/ftb/OneSixFTBInstance.h @@ -15,8 +15,6 @@ public: virtual void createProfile(); - virtual QString getStatusbarDescription(); - virtual std::shared_ptr createUpdateTask() override; virtual QString id() const; @@ -24,6 +22,8 @@ public: QDir librariesPath() const override; QDir versionsPath() const override; bool providesVersionFile() const override; + virtual QString typeName() const; + private: SettingsObjectPtr m_globalSettings; }; diff --git a/logic/minecraft/LegacyInstance.cpp b/logic/minecraft/LegacyInstance.cpp index b9082bc6..f39f3c8b 100644 --- a/logic/minecraft/LegacyInstance.cpp +++ b/logic/minecraft/LegacyInstance.cpp @@ -445,16 +445,12 @@ QString LegacyInstance::defaultCustomBaseJar() const return PathCombine(binDir(), "mcbackup.jar"); } -QString LegacyInstance::getStatusbarDescription() -{ - if (flags() & VersionBrokenFlag) - { - return tr("Legacy : %1 (broken)").arg(intendedVersionId()); - } - return tr("Legacy : %1").arg(intendedVersionId()); -} - QString LegacyInstance::lwjglFolder() const { return m_lwjglFolderSetting->get().toString(); } + +QString LegacyInstance::typeName() const +{ + return tr("Legacy"); +} diff --git a/logic/minecraft/LegacyInstance.h b/logic/minecraft/LegacyInstance.h index 724fcd33..14f13b20 100644 --- a/logic/minecraft/LegacyInstance.h +++ b/logic/minecraft/LegacyInstance.h @@ -120,7 +120,7 @@ public: virtual void cleanupAfterRun() override; - virtual QString getStatusbarDescription() override; + virtual QString typeName() const; protected: mutable std::shared_ptr jar_mod_list; diff --git a/logic/minecraft/MinecraftInstance.cpp b/logic/minecraft/MinecraftInstance.cpp index e694045b..8fd65d50 100644 --- a/logic/minecraft/MinecraftInstance.cpp +++ b/logic/minecraft/MinecraftInstance.cpp @@ -305,4 +305,46 @@ QString MinecraftInstance::getLogFileRoot() return minecraftRoot(); } +QString MinecraftInstance::prettifyTimeDuration(int64_t duration) +{ + int seconds = (int) (duration % 60); + duration /= 60; + int minutes = (int) (duration % 60); + duration /= 60; + int hours = (int) (duration % 24); + int days = (int) (duration / 24); + if((hours == 0)&&(days == 0)) + { + return tr("%1m %2s").arg(minutes).arg(seconds); + } + if (days == 0) + { + return tr("%1h %2m").arg(hours).arg(minutes); + } + return tr("%1d %2h %3m").arg(days).arg(hours).arg(minutes); +} + +QString MinecraftInstance::getStatusbarDescription() +{ + QStringList traits; + if (flags() & VersionBrokenFlag) + { + traits.append(tr("broken")); + } + + QString description; + description.append(tr("Minecraft %1 (%2)").arg(intendedVersionId()).arg(typeName())); + if(totalTimePlayed() > 0) + { + description.append(tr(", played for %1").arg(prettifyTimeDuration(totalTimePlayed()))); + } + /* + if(traits.size()) + { + description.append(QString(" (%1)").arg(traits.join(", "))); + } + */ + return description; +} + #include "MinecraftInstance.moc" diff --git a/logic/minecraft/MinecraftInstance.h b/logic/minecraft/MinecraftInstance.h index 8c6838e7..b5483d1b 100644 --- a/logic/minecraft/MinecraftInstance.h +++ b/logic/minecraft/MinecraftInstance.h @@ -55,8 +55,12 @@ public: virtual QString getLogFileRoot() override; + virtual QString getStatusbarDescription() override; + protected: QMap createCensorFilterFromSession(AuthSessionPtr session); +private: + QString prettifyTimeDuration(int64_t duration); }; typedef std::shared_ptr MinecraftInstancePtr; diff --git a/logic/minecraft/OneSixInstance.cpp b/logic/minecraft/OneSixInstance.cpp index 87994cef..a5be8354 100644 --- a/logic/minecraft/OneSixInstance.cpp +++ b/logic/minecraft/OneSixInstance.cpp @@ -476,24 +476,6 @@ std::shared_ptr OneSixInstance::getMinecraftProfile() const return m_version; } -QString OneSixInstance::getStatusbarDescription() -{ - QStringList traits; - if (flags() & VersionBrokenFlag) - { - traits.append(tr("broken")); - } - - if (traits.size()) - { - return tr("Minecraft %1 (%2)").arg(intendedVersionId()).arg(traits.join(", ")); - } - else - { - return tr("Minecraft %1").arg(intendedVersionId()); - } -} - QDir OneSixInstance::librariesPath() const { return QDir::current().absoluteFilePath("libraries"); @@ -590,3 +572,8 @@ std::shared_ptr OneSixInstance::getSharedPtr() { return std::dynamic_pointer_cast(BaseInstance::getSharedPtr()); } + +QString OneSixInstance::typeName() const +{ + return tr("OneSix"); +} diff --git a/logic/minecraft/OneSixInstance.h b/logic/minecraft/OneSixInstance.h index 68be1d4d..6d09b81a 100644 --- a/logic/minecraft/OneSixInstance.h +++ b/logic/minecraft/OneSixInstance.h @@ -79,8 +79,6 @@ public: /// get the current full version info std::shared_ptr getMinecraftProfile() const; - virtual QString getStatusbarDescription() override; - virtual QDir jarmodsPath() const; virtual QDir librariesPath() const; virtual QDir versionsPath() const; @@ -92,6 +90,8 @@ public: std::shared_ptr getSharedPtr(); + virtual QString typeName() const; + signals: void versionReloaded();