Merge pull request #69 from AttorneyOnline/timer_improvements

Timer improvements
This commit is contained in:
scatterflower 2021-04-09 22:53:31 -05:00 committed by GitHub
commit 47b33c26d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 31 deletions

View File

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

View File

@ -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,18 @@ 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"));
sendServerMessage(getAreaTimer(area->index, timer_id));
return;
}
}
else {
QTimer* timer = area->timers[timer_id - 1];
sendServerMessage(getAreaTimer(area->index, timer));
return;
}
}
// 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"))) {
@ -711,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);
}
}
}
@ -1397,15 +1404,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.";
}
}