Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 63 additions & 22 deletions src/clock/mumeclock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

#include "mumeclock.h"

#include "../configuration/configuration.h"
#include "../global/Array.h"
#include "../global/JsonObj.h"
#include "../proxy/GmcpMessage.h"
#include "../proxy/MudTelnet.h" // FIXME: move MsspTime somewhere more appropriate, or just use MumeMoment.
#include "mumemoment.h"
#if 1
// FIXME: move MsspTime somewhere more appropriate, or just use MumeMoment.
#include "../proxy/MudTelnet.h"
#endif

#include <cassert>

Expand Down Expand Up @@ -111,12 +109,14 @@ using westronWeekDayNames = mmqt::QME<MumeClock::WestronWeekDayNamesEnum>;

MumeClock::MumeClock(int64_t mumeEpoch, GameObserver &observer, QObject *const parent)
: QObject(parent)
, m_observer{observer}
, m_mumeStartEpoch(mumeEpoch)
, m_precision(MumeClockPrecisionEnum::UNSET)
, m_observer{observer}
{
m_observer.sig2_sentToUserGmcp.connect(m_lifetime,
[this](const GmcpMessage &gmcp) { onUserGmcp(gmcp); });
connect(&m_timer, &QTimer::timeout, this, &MumeClock::slot_tick);
m_timer.start(1000);
}

MumeClock::MumeClock(GameObserver &observer)
Expand All @@ -125,13 +125,12 @@ MumeClock::MumeClock(GameObserver &observer)

MumeMoment MumeClock::getMumeMoment() const
{
const int64_t t = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
const int64_t t = QDateTime::currentSecsSinceEpoch();
return MumeMoment::sinceMumeEpoch(t - m_mumeStartEpoch);
}

MumeMoment MumeClock::getMumeMoment(const int64_t secsSinceUnixEpoch) const
{
/* This will break on 2038-01-19 if you use 32-bit. */
if (secsSinceUnixEpoch < 0) {
assert(secsSinceUnixEpoch == -1);
return getMumeMoment();
Expand All @@ -141,7 +140,7 @@ MumeMoment MumeClock::getMumeMoment(const int64_t secsSinceUnixEpoch) const

void MumeClock::parseMumeTime(const QString &mumeTime)
{
const int64_t secsSinceEpoch = QDateTime::QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
const int64_t secsSinceEpoch = QDateTime::currentSecsSinceEpoch();
parseMumeTime(mumeTime, secsSinceEpoch);
}

Expand Down Expand Up @@ -221,6 +220,7 @@ void MumeClock::parseMumeTime(const QString &mumeTime, const int64_t secsSinceEp
qWarning() << "Calculated week day does not match MUME";
}
m_mumeStartEpoch = newStartEpoch;
setConfig().mumeClock.startEpoch = newStartEpoch;
}

void MumeClock::onUserGmcp(const GmcpMessage &msg)
Expand Down Expand Up @@ -262,12 +262,15 @@ void MumeClock::onUserGmcp(const GmcpMessage &msg)
break;
}
}
const int64_t secsSinceEpoch = QDateTime::QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
const int64_t secsSinceEpoch = QDateTime::currentSecsSinceEpoch();
parseWeather(time, secsSinceEpoch);
}

void MumeClock::parseWeather(const MumeTimeEnum time, int64_t secsSinceEpoch)
{
// Restart the timer to sync with the game's tick
m_timer.start(1000);

// Update last sync timestamp
setLastSyncEpoch(secsSinceEpoch);

Expand Down Expand Up @@ -312,18 +315,19 @@ void MumeClock::parseWeather(const MumeTimeEnum time, int64_t secsSinceEpoch)
log(QString("Unsychronized tick detected using %1 (off by %2 seconds)")
.arg(reason)
.arg(moment.minute));
return;
} else {
log(QString("Synchronized tick using %1").arg(reason));
if (time != MumeTimeEnum::UNKNOWN || m_precision >= MumeClockPrecisionEnum::HOUR) {
m_precision = MumeClockPrecisionEnum::MINUTE;
}
}

log(QString("Synchronized tick using %1").arg(reason));
if (time != MumeTimeEnum::UNKNOWN || m_precision >= MumeClockPrecisionEnum::HOUR) {
m_precision = MumeClockPrecisionEnum::MINUTE;
}
updateObserver(moment);
}

void MumeClock::parseClockTime(const QString &clockTime)
{
const int64_t secsSinceEpoch = QDateTime::QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
const int64_t secsSinceEpoch = QDateTime::currentSecsSinceEpoch();
parseClockTime(clockTime, secsSinceEpoch);
}

Expand Down Expand Up @@ -366,7 +370,7 @@ void MumeClock::parseMSSP(const MsspTime &msspTime)
return;
}

const int64_t secsSinceEpoch = QDateTime::QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
const int64_t secsSinceEpoch = QDateTime::currentSecsSinceEpoch();

auto moment = getMumeMoment();
moment.year = msspTime.year;
Expand All @@ -390,9 +394,14 @@ void MumeClock::setPrecision(const MumeClockPrecisionEnum precision)
m_precision = precision;
}

void MumeClock::setLastSyncEpoch(int64_t epoch)
{
m_lastSyncEpoch = epoch;
}

MumeClockPrecisionEnum MumeClock::getPrecision()
{
const int64_t secsSinceEpoch = QDateTime::QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
const int64_t secsSinceEpoch = QDateTime::QDateTime::currentSecsSinceEpoch();
Comment on lines 402 to +404
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The QDateTime qualifier in getPrecision is incorrect and will not compile.

const int64_t secsSinceEpoch = QDateTime::QDateTime::currentSecsSinceEpoch(); uses an invalid QDateTime::QDateTime:: qualifier and will not compile. This should be QDateTime::currentSecsSinceEpoch();, consistent with the other call sites.

if (m_precision >= MumeClockPrecisionEnum::HOUR
&& secsSinceEpoch - m_lastSyncEpoch > ONE_RL_DAY_IN_SECONDS) {
m_precision = MumeClockPrecisionEnum::DAY;
Expand Down Expand Up @@ -421,14 +430,13 @@ QString MumeClock::toMumeTime(const MumeMoment &moment) const
QString time;
switch (m_precision) {
case MumeClockPrecisionEnum::HOUR:
time = QString("%1%2 on %3").arg(hour).arg(period).arg(weekDay);
time = QString("%1%2 on %3").arg(hour).arg(period, weekDay);
break;
case MumeClockPrecisionEnum::MINUTE:
time = QString("%1:%2%3 on %4")
.arg(hour)
.arg(moment.minute, 2, 10, QChar('0'))
.arg(period)
.arg(weekDay);
.arg(period, weekDay);
break;
case MumeClockPrecisionEnum::UNSET:
case MumeClockPrecisionEnum::DAY:
Expand All @@ -443,8 +451,7 @@ QString MumeClock::toMumeTime(const MumeMoment &moment) const
return QString("%1, the %2%3 of %4, year %5 of the Third Age.")
.arg(time)
.arg(day)
.arg(QString{getOrdinalSuffix(day)})
.arg(monthName)
.arg(QString{getOrdinalSuffix(day)}, monthName)
.arg(moment.year);
}

Expand Down Expand Up @@ -491,3 +498,37 @@ int MumeClock::getMumeWeekday(const QString &weekdayName)
{
return mmqt::parseTwoEnums<WestronWeekDayNamesEnum, SindarinWeekDayNamesEnum>(weekdayName);
}

void MumeClock::slot_tick()
{
const MumeMoment moment = getMumeMoment();
m_observer.observeTick(moment);
updateObserver(moment);
}

void MumeClock::updateObserver(const MumeMoment &moment)
{
const auto timeOfDay = moment.toTimeOfDay();
if (timeOfDay != m_timeOfDay) {
m_timeOfDay = timeOfDay;
m_observer.observeTimeOfDay(m_timeOfDay);
}

const auto moonPhase = moment.moonPhase();
if (moonPhase != m_moonPhase) {
m_moonPhase = moonPhase;
m_observer.observeMoonPhase(m_moonPhase);
}

const auto moonVisibility = moment.moonVisibility();
if (moonVisibility != m_moonVisibility) {
m_moonVisibility = moonVisibility;
m_observer.observeMoonVisibility(m_moonVisibility);
}

const auto season = moment.toSeason();
if (season != m_season) {
m_season = season;
m_observer.observeSeason(m_season);
}
}
13 changes: 10 additions & 3 deletions src/clock/mumeclock.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ class NODISCARD_QOBJECT MumeClock final : public QObject
friend class TestClock;

private:
Signal2Lifetime m_lifetime;
QTimer m_timer;
GameObserver &m_observer;
int64_t m_lastSyncEpoch = 0;
int64_t m_mumeStartEpoch = 0;
MumeClockPrecisionEnum m_precision = MumeClockPrecisionEnum::UNSET;
GameObserver &m_observer;
Signal2Lifetime m_lifetime;
MumeTimeEnum m_timeOfDay = MumeTimeEnum::UNKNOWN;
MumeMoonPhaseEnum m_moonPhase = MumeMoonPhaseEnum::UNKNOWN;
MumeMoonVisibilityEnum m_moonVisibility = MumeMoonVisibilityEnum::UNKNOWN;
MumeSeasonEnum m_season = MumeSeasonEnum::UNKNOWN;

public:
static inline constexpr const int NUM_MONTHS = 12;
Expand Down Expand Up @@ -140,7 +145,7 @@ class NODISCARD_QOBJECT MumeClock final : public QObject

public:
void setPrecision(MumeClockPrecisionEnum state);
void setLastSyncEpoch(int64_t epoch) { m_lastSyncEpoch = epoch; }
void setLastSyncEpoch(int64_t epoch);

NODISCARD static int getMumeMonth(const QString &monthName);
NODISCARD static int getMumeWeekday(const QString &weekdayName);
Expand All @@ -153,11 +158,13 @@ class NODISCARD_QOBJECT MumeClock final : public QObject

private:
void onUserGmcp(const GmcpMessage &msg);
void updateObserver(const MumeMoment &moment);

signals:
void sig_log(const QString &, const QString &);

public slots:
void parseMumeTime(const QString &mumeTime);
void parseClockTime(const QString &clockTime);
void slot_tick();
};
Loading