GH-3930 Track duration of previous game sessions

This commit is contained in:
Jamie Mansfield 2021-07-13 16:59:33 +01:00
parent d63ef939be
commit 74f5255eef
No known key found for this signature in database
GPG Key ID: 36F61598F39F67B0
3 changed files with 25 additions and 3 deletions

View File

@ -37,6 +37,7 @@ BaseInstance::BaseInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr s
m_settings->registerSetting("notes", "");
m_settings->registerSetting("lastLaunchTime", 0);
m_settings->registerSetting("totalTimePlayed", 0);
m_settings->registerSetting("lastTimePlayed", 0);
// Custom Commands
auto commandSetting = m_settings->registerSetting({"OverrideCommands","OverrideLaunchCmd"}, false);
@ -146,9 +147,12 @@ void BaseInstance::setRunning(bool running)
}
else
{
qint64 current = settings()->get("totalTimePlayed").toLongLong();
QDateTime timeEnded = QDateTime::currentDateTime();
qint64 current = settings()->get("totalTimePlayed").toLongLong();
settings()->set("totalTimePlayed", current + m_timeStarted.secsTo(timeEnded));
settings()->set("lastTimePlayed", m_timeStarted.secsTo(timeEnded));
emit propertiesChanged(this);
}
@ -166,9 +170,20 @@ int64_t BaseInstance::totalTimePlayed() const
return current;
}
int64_t BaseInstance::lastTimePlayed() const
{
if(m_isRunning)
{
QDateTime timeNow = QDateTime::currentDateTime();
return m_timeStarted.secsTo(timeNow);
}
return settings()->get("lastTimePlayed").toLongLong();
}
void BaseInstance::resetTimePlayed()
{
settings()->reset("totalTimePlayed");
settings()->reset("lastTimePlayed");
}
QString BaseInstance::instanceType() const

View File

@ -87,6 +87,7 @@ public:
void setRunning(bool running);
bool isRunning() const;
int64_t totalTimePlayed() const;
int64_t lastTimePlayed() const;
void resetTimePlayed();
/// get the type of this instance

View File

@ -798,9 +798,15 @@ QString MinecraftInstance::getStatusbarDescription()
QString description;
description.append(tr("Minecraft %1 (%2)").arg(m_components->getComponentVersion("net.minecraft")).arg(typeName()));
if(m_settings->get("ShowGameTime").toBool() && totalTimePlayed() > 0)
if(m_settings->get("ShowGameTime").toBool())
{
description.append(tr(", played for %1").arg(prettifyTimeDuration(totalTimePlayed())));
if (lastTimePlayed() > 0) {
description.append(tr(", last played for %1").arg(prettifyTimeDuration(lastTimePlayed())));
}
if (totalTimePlayed() > 0) {
description.append(tr(", total played for %1").arg(prettifyTimeDuration(totalTimePlayed())));
}
}
if(hasCrashed())
{