From 25dbfb37f4e35a942e14bec9d71409d8ae96baf6 Mon Sep 17 00:00:00 2001 From: Matt Lowe Date: Wed, 15 Apr 2015 14:18:16 +0200 Subject: [PATCH] Game created code improvements + No longer get "5hrs" and "5+ hrs" instead will be "4hrs" and "5+ hrs". + Refactored into an else-if with a single return. + removed comments + removed code from namespace and added to class --- cockatrice/src/gamesmodel.cpp | 81 +++++++++++------------------------ cockatrice/src/gamesmodel.h | 4 +- 2 files changed, 29 insertions(+), 56 deletions(-) diff --git a/cockatrice/src/gamesmodel.cpp b/cockatrice/src/gamesmodel.cpp index 0f3a1087..6f116714 100644 --- a/cockatrice/src/gamesmodel.cpp +++ b/cockatrice/src/gamesmodel.cpp @@ -10,62 +10,33 @@ enum GameListColumn {ROOM, CREATED, DESCRIPTION, CREATOR, GAME_TYPE, RESTRICTIONS, PLAYERS, SPECTATORS}; -namespace { - const int SECS_PER_MIN = 60; - const int SECS_PER_HOUR = 60 * 60; +const QString GamesModel::getGameCreatedString(const int secs) const { - /** - * Pretty print an integer number of seconds ago. Accurate to only one unit, - * rounded; <5 minutes and >5 hours are displayed as such. As a special case, - * time between 60 and 90 minutes will display both the hours and minutes. - * - * For example... - * 0-300 seconds will return "<5m ago" - * 5-59 minutes will return "Xm ago" - * 60-90 minutes will return "Xhr Ym ago" - * 91-300 minutes will return "Xhr ago" - * 300+ minutes will return "5+ hr ago" + QString ret; + if (secs < SECS_PER_MIN) + ret = tr("<1m ago"); + else if (secs < SECS_PER_MIN * 5) + ret = tr("<5m ago"); + else if (secs < SECS_PER_HOUR) + ret = QString::number(secs / SECS_PER_MIN).append(tr("m ago")); + else if (secs < SECS_PER_MIN * 90) { + ret = tr("1hr ").append(QString::number((secs / SECS_PER_MIN) - 60)).append(tr("m ago")); + } else if (secs < SECS_PER_HOUR * 4) { + unsigned int hours = secs / SECS_PER_HOUR; + if (secs % SECS_PER_HOUR >= SECS_PER_MIN * 30) + hours++; + ret = QString::number(hours).append(tr("hr ago")); + } else + ret = tr("5+ hrs ago"); + + return ret; + + /* + todo + . would like less if() + . would like less code repition */ - QString prettyPrintSecsAgo(int secs) { - if (secs < SECS_PER_MIN) { - return QObject::tr("<1m ago"); - } - if (secs < SECS_PER_MIN * 5) { - return QObject::tr("<5m ago"); - } - if (secs < SECS_PER_HOUR) { - unsigned int mins = secs / SECS_PER_MIN; - if (secs % SECS_PER_MIN >= 30) - mins++; - //: This will have a number prepended, like "10m ago" - return QString::number(mins).append(QObject::tr("m ago")); - } - // Here, we want to display both the hours and minutes. - // - // There are two small "corner" cases which could be rectified with - // some more knotty iffy-elsey code: - // Between 1:00:00 and 1:00:29 will display "1hr 0m ago" - // Between 1:29:30 and 1:29:59 will display "1hr 31m ago" - // - // Personally, I prefer to keep the code cleaner, and allow these. - if (secs < SECS_PER_MIN * 90) { - unsigned int mins = secs / SECS_PER_MIN - 60; - if (secs % SECS_PER_MIN >= 30) - mins++; - return QObject::tr("1hr ") - .append(QString::number(mins)) - //: This will have a number prepended, like "5m ago" - .append(QObject::tr("m ago")); - } - if (secs < SECS_PER_HOUR * 5) { - unsigned int hours = secs / SECS_PER_HOUR; - if (secs % SECS_PER_HOUR >= SECS_PER_MIN * 30) - hours++; - //: This will have a number prepended, like "2h ago" - return QString::number(hours).append(QObject::tr("hr ago")); - } - return QObject::tr("5+ hrs ago"); - } + } GamesModel::GamesModel(const QMap &_rooms, const QMap &_gameTypes, QObject *parent) @@ -94,7 +65,7 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const int secs = then.secsTo(QDateTime::currentDateTime()); switch (role) { - case Qt::DisplayRole: return prettyPrintSecsAgo(secs); + case Qt::DisplayRole: return getGameCreatedString(secs); case SORT_ROLE: return QVariant(secs); default: return QVariant(); } diff --git a/cockatrice/src/gamesmodel.h b/cockatrice/src/gamesmodel.h index dc23f921..c83c3e0a 100644 --- a/cockatrice/src/gamesmodel.h +++ b/cockatrice/src/gamesmodel.h @@ -18,6 +18,8 @@ private: QMap gameTypes; static const int NUM_COLS = 8; + static const int SECS_PER_MIN = 60; + static const int SECS_PER_HOUR = 3600; public: static const int SORT_ROLE = Qt::UserRole+1; @@ -26,7 +28,7 @@ public: int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const { return NUM_COLS; } QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; - + const QString getGameCreatedString(const int secs) const; const ServerInfo_Game &getGame(int row); /**