GH-1217 add simple instance play time tracking

Not reliable in the face of crashes... but what is?
This commit is contained in:
Petr Mrázek 2015-09-22 01:06:45 +02:00
parent b107617112
commit 9ba1cd15e7
13 changed files with 103 additions and 51 deletions

View File

@ -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();

View File

@ -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<QString, QString> 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<BaseInstance>)

View File

@ -78,4 +78,8 @@ public:
{
return instanceRoot();
}
virtual QString typeName() const
{
return "Null";
}
};

View File

@ -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");
}

View File

@ -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;
};

View File

@ -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<Task> OneSixFTBInstance::createUpdateTask()
{
return OneSixInstance::createUpdateTask();
}
QString OneSixFTBInstance::typeName() const
{
return tr("OneSix FTB");
}
#include "OneSixFTBInstance.moc"

View File

@ -15,8 +15,6 @@ public:
virtual void createProfile();
virtual QString getStatusbarDescription();
virtual std::shared_ptr<Task> 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;
};

View File

@ -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");
}

View File

@ -120,7 +120,7 @@ public:
virtual void cleanupAfterRun() override;
virtual QString getStatusbarDescription() override;
virtual QString typeName() const;
protected:
mutable std::shared_ptr<ModList> jar_mod_list;

View File

@ -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"

View File

@ -55,8 +55,12 @@ public:
virtual QString getLogFileRoot() override;
virtual QString getStatusbarDescription() override;
protected:
QMap<QString, QString> createCensorFilterFromSession(AuthSessionPtr session);
private:
QString prettifyTimeDuration(int64_t duration);
};
typedef std::shared_ptr<MinecraftInstance> MinecraftInstancePtr;

View File

@ -476,24 +476,6 @@ std::shared_ptr<MinecraftProfile> 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> OneSixInstance::getSharedPtr()
{
return std::dynamic_pointer_cast<OneSixInstance>(BaseInstance::getSharedPtr());
}
QString OneSixInstance::typeName() const
{
return tr("OneSix");
}

View File

@ -79,8 +79,6 @@ public:
/// get the current full version info
std::shared_ptr<MinecraftProfile> 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<OneSixInstance> getSharedPtr();
virtual QString typeName() const;
signals:
void versionReloaded();