From 8f00d1aece34540d632cb8ca1f5e392117e72c38 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Tue, 19 Jun 2018 20:02:51 +0000 Subject: [PATCH] Make timeouts show up as the biggest "even" duration type possible 61 seconds = 61 seconds 60 seconds = 1 minute 3601 seconds = 3601 seconds 3600 seconds = 1 hour 3540 seconds = 59 minutes the reason it doesn't just do 3601 seconds as 1 hour, 1 second is I wanted to keep the messages short. might change in the future eShrug --- src/channel.cpp | 2 +- src/messages/message.cpp | 58 +++++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 5c264c831..e5156a872 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -118,7 +118,7 @@ void Channel::addOrReplaceTimeout(messages::MessagePtr message) int count = s->count + 1; messages::MessagePtr replacement(Message::createSystemMessage( - message->searchText + QString("(") + QString::number(count) + " times)")); + message->searchText + QString(" (") + QString::number(count) + " times)")); replacement->timeoutUser = message->timeoutUser; replacement->count = count; diff --git a/src/messages/message.cpp b/src/messages/message.cpp index 5d0b13552..50c9de41f 100644 --- a/src/messages/message.cpp +++ b/src/messages/message.cpp @@ -7,6 +7,7 @@ using SBHighlight = chatterino::widgets::ScrollbarHighlight; namespace chatterino { namespace messages { + void Message::addElement(MessageElement *element) { this->elements.push_back(std::unique_ptr(element)); @@ -52,6 +53,47 @@ MessagePtr Message::createMessage(const QString &text) return message; } +namespace { + +QString makeDuration(int count, const QString &order) +{ + QString text; + + text.append(QString::number(count)); + text.append(" " + order); + + if (count > 1) { + text.append("s"); + } + + return text; +} + +QString makeDuration(uint32_t timeoutSeconds) +{ + int timeoutMinutes = floor(timeoutSeconds / 60); + + if (timeoutMinutes > 0 && timeoutSeconds % 60 == 0) { + int timeoutHours = floor(timeoutMinutes / 60); + + if (timeoutHours > 0 && timeoutMinutes % 60 == 0) { + int timeoutDays = floor(timeoutHours / 24); + + if (timeoutDays > 0 && timeoutHours % 24 == 0) { + return makeDuration(timeoutDays, "day"); + } + + return makeDuration(timeoutHours, "hour"); + } + + return makeDuration(timeoutMinutes, "minute"); + } + + return makeDuration(timeoutSeconds, "second"); +} + +} // namespace + MessagePtr Message::createTimeoutMessage(const QString &username, const QString &durationInSeconds, const QString &reason, bool multipleTimes) { @@ -64,12 +106,10 @@ MessagePtr Message::createTimeoutMessage(const QString &username, const QString // TODO: Implement who timed the user out text.append(" for "); - text.append(durationInSeconds); bool ok = true; - int timeoutDuration = durationInSeconds.toInt(&ok); - text.append(" second"); - if (ok && timeoutDuration > 1) { - text.append("s"); + int timeoutSeconds = durationInSeconds.toInt(&ok); + if (ok) { + text.append(makeDuration(timeoutSeconds)); } } else { text.append(" has been permanently banned"); @@ -119,15 +159,15 @@ MessagePtr Message::createTimeoutMessage(const providers::twitch::BanAction &act } } else { if (action.reason.isEmpty()) { - text = QString("%1 timed out %2 for %3 seconds.") // + text = QString("%1 timed out %2 for %3.") // .arg(action.source.name) .arg(action.target.name) - .arg(action.duration); + .arg(makeDuration(action.duration)); } else { - text = QString("%1 timed out %2 for %3 seconds: \"%4\".") // + text = QString("%1 timed out %2 for %3: \"%4\".") // .arg(action.source.name) .arg(action.target.name) - .arg(action.duration) + .arg(makeDuration(action.duration)) .arg(action.reason); }