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
This commit is contained in:
Rasmus Karlsson 2018-06-19 20:02:51 +00:00
parent 7e53b44099
commit 8f00d1aece
2 changed files with 50 additions and 10 deletions

View file

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

View file

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