From 2a2171dc42863a24c3e518bdb6625b1dfc795019 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Fri, 9 Apr 2021 15:32:17 -0500 Subject: [PATCH 1/2] clean up timer command, fix crash --- include/aoclient.h | 4 ++-- src/commands.cpp | 49 ++++++++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index e99d417..f0a580d 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -1414,12 +1414,12 @@ class AOClient : public QObject { * @brief Returns a textual representation of the time left in an area's Timer. * * @param area_idx The ID of the area whose timer to grab. - * @param timer The pointer to the area's timer. + * @param timer_idx The ID of the timer to grab * * @return A textual representation of the time left over on the Timer, * or `"Timer is inactive"` if the timer wasn't started. */ - QString getAreaTimer(int area_idx, QTimer* timer); + QString getAreaTimer(int area_idx, int timer_idx); /** * @brief Generates a tsuserver3-style area list to be displayed to the user in the out-of-character chat. diff --git a/src/commands.cpp b/src/commands.cpp index 0947746..35a75b2 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -664,20 +664,19 @@ void AOClient::cmdTimer(int argc, QStringList argv) { AreaData* area = server->areas[current_area]; + // Called without arguments + // Shows a brief of all timers if (argc == 0) { QStringList timers; timers.append("Currently active timers:"); - QTimer* global_timer = server->timer; - if (global_timer->isActive()) { - QTime current_time = QTime(0,0).addMSecs(global_timer->remainingTime()); - timers.append("Global timer is at " + current_time.toString("hh:mm:ss.zzz")); - } - for (QTimer* timer : area->timers) { - timers.append(getAreaTimer(area->index, timer)); + for (int i = 0; i <= 4; i++) { + timers.append(getAreaTimer(area->index, i)); } sendServerMessage(timers.join("\n")); return; } + + // Called with more than one argument bool ok; int timer_id = argv[0].toInt(&ok); if (!ok || timer_id < 0 || timer_id > 4) { @@ -685,22 +684,15 @@ void AOClient::cmdTimer(int argc, QStringList argv) return; } + // Called with one argument + // Shows the status of one timer if (argc == 1) { - if (timer_id == 0) { - QTimer* global_timer = server->timer; - if (global_timer->isActive()) { - QTime current_time = QTime(0, 0, 0, global_timer->remainingTime()); - sendServerMessage("Global timer is at " + current_time.toString("hh:mm:ss.zzz")); - return; - } - } - else { - QTimer* timer = area->timers[timer_id - 1]; - sendServerMessage(getAreaTimer(area->index, timer)); - return; - } + sendServerMessage(getAreaTimer(area->index, timer_id)); + return; } + // Called with more than one argument + // Updates the state of a timer QTimer* requested_timer; if (timer_id == 0) { if (!checkAuth(ACLFlags.value("GLOBAL_TIMER"))) { @@ -1397,15 +1389,26 @@ void AOClient::diceThrower(int argc, QStringList argv, RollType type) } } -QString AOClient::getAreaTimer(int area_idx, QTimer* timer) +QString AOClient::getAreaTimer(int area_idx, int timer_idx) { AreaData* area = server->areas[area_idx]; + QTimer* timer; + QString timer_name = (timer_idx == 0) ? "Global timer" : "Timer " + QString::number(timer_idx); + + if (timer_idx == 0) + timer = server->timer; + else if (timer_idx > 0 && timer_idx <= 4) + timer = area->timers[timer_idx - 1]; + else + return "Invalid timer ID."; + if (timer->isActive()) { QTime current_time = QTime(0,0).addMSecs(timer->remainingTime()); - return "Timer " + QString::number(area->timers.indexOf(timer) + 1) + " is at " + current_time.toString("hh:mm:ss.zzz"); + + return timer_name + " is at " + current_time.toString("hh:mm:ss.zzz"); } else { - return "Timer " + QString::number(area->timers.indexOf(timer) + 1) + " is inactive."; + return timer_name + " is inactive."; } } From 8a5be8bc2c67d41fba3d74263aa1a69b03080fd2 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Fri, 9 Apr 2021 22:51:00 -0500 Subject: [PATCH 2/2] fix timer --- src/commands.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index 35a75b2..9aaff9f 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -693,6 +693,9 @@ void AOClient::cmdTimer(int argc, QStringList argv) // Called with more than one argument // Updates the state of a timer + + // Select the proper timer + // Check against permissions if global timer is selected QTimer* requested_timer; if (timer_id == 0) { if (!checkAuth(ACLFlags.value("GLOBAL_TIMER"))) { @@ -703,33 +706,45 @@ void AOClient::cmdTimer(int argc, QStringList argv) } else requested_timer = area->timers[timer_id - 1]; + + AOPacket show_timer("TI", {QString::number(timer_id), "2"}); + AOPacket hide_timer("TI", {QString::number(timer_id), "3"}); + bool is_global = timer_id == 0; + + // Set the timer's time remaining if the second + // argument is a valid time QTime requested_time = QTime::fromString(argv[1], "hh:mm:ss"); if (requested_time.isValid()) { requested_timer->setInterval(QTime(0,0).msecsTo(requested_time)); requested_timer->start(); sendServerMessage("Set timer " + QString::number(timer_id) + " to " + argv[1] + "."); - sendPacket("TI", {QString::number(timer_id), "2"}); // Show the timer - sendPacket("TI", {QString::number(timer_id), "0", QString::number(QTime(0,0).msecsTo(requested_time))}); + AOPacket update_timer("TI", {QString::number(timer_id), "0", QString::number(QTime(0,0).msecsTo(requested_time))}); + is_global ? server->broadcast(show_timer) : server->broadcast(show_timer, current_area); // Show the timer + is_global ? server->broadcast(update_timer) : server->broadcast(update_timer, current_area); return; } + // Otherwise, update the state of the timer else { if (argv[1] == "start") { requested_timer->start(); sendServerMessage("Started timer " + QString::number(timer_id) + "."); - sendPacket("TI", {QString::number(timer_id), "2"}); // Show the timer - sendPacket("TI", {QString::number(timer_id), "0", QString::number(QTime(0,0).msecsTo(QTime(0,0).addMSecs(requested_timer->remainingTime())))}); + AOPacket update_timer("TI", {QString::number(timer_id), "0", QString::number(QTime(0,0).msecsTo(QTime(0,0).addMSecs(requested_timer->remainingTime())))}); + is_global ? server->broadcast(show_timer) : server->broadcast(show_timer, current_area); + is_global ? server->broadcast(update_timer) : server->broadcast(update_timer, current_area); } else if (argv[1] == "pause" || argv[1] == "stop") { requested_timer->setInterval(requested_timer->remainingTime()); requested_timer->stop(); sendServerMessage("Stopped timer " + QString::number(timer_id) + "."); - sendPacket("TI", {QString::number(timer_id), "1", QString::number(QTime(0,0).msecsTo(QTime(0,0).addMSecs(requested_timer->interval())))}); + AOPacket update_timer("TI", {QString::number(timer_id), "1", QString::number(QTime(0,0).msecsTo(QTime(0,0).addMSecs(requested_timer->interval())))}); + is_global ? server->broadcast(update_timer) : server->broadcast(update_timer, current_area); } else if (argv[1] == "hide" || argv[1] == "unset") { requested_timer->setInterval(0); requested_timer->stop(); sendServerMessage("Hid timer " + QString::number(timer_id) + "."); - sendPacket("TI", {QString::number(timer_id), "3"}); // Hide the timer + // Hide the timer + is_global ? server->broadcast(hide_timer) : server->broadcast(hide_timer, current_area); } } }