Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/demoserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DemoServer : public QObject
QTcpSocket* client_sock = nullptr;
bool client_connected = false;
bool partial_packet = false;
bool debug_mode = false;
QString temp_packet = "";
QQueue<QString> demo_data;
QString sc_packet;
Expand Down
60 changes: 48 additions & 12 deletions src/demoserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,39 @@ void DemoServer::handle_packet(AOPacket packet)
QString packet = "CT#DEMO#" + tr("min_wait is deprecated. Use the client Settings for minimum wait instead!") + "#1#%";
client_sock->write(packet.toUtf8());
}
else if (contents[1].startsWith("/debug"))
{
QStringList args = contents[1].split(" ");
if (args.size() > 1)
{
bool ok;
int toggle = args.at(1).toInt(&ok);
if (ok && (toggle == 0 || toggle == 1)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok variable is redundant as toggle would be -1 on error and that is already checked by your if statement

Copy link
Contributor Author

@Crystalwarrior Crystalwarrior Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't .at(1) cause an error if args size is less than 1, rather than returning -1?

Copy link
Member

@oldmud0 oldmud0 Mar 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args.size() > 1 holds true in this branch of code so it will not fail

also i'm clearly losing brain cells because toInt() returns 0 on failure, not -1. so actually it is not redundant

debug_mode = toggle == 1;
QString packet = "CT#DEMO#" + tr("Setting debug mode to %1").arg(static_cast<int>(debug_mode)) + "#1#%";
client_sock->write(packet.toUtf8());
// Debug mode disabled?
if (!debug_mode) {
// Reset the timer
client_sock->write("TI#4#1#0#%");
client_sock->write("TI#4#3#0#%");
}
}
else
{
QString packet = "CT#DEMO#" + tr("Valid values are 1 or 0!") + "#1#%";
client_sock->write(packet.toUtf8());
}
}
else
{
QString packet = "CT#DEMO#" + tr("Set debug mode using /debug 1 to enable, and /debug 0 to disable, which will use the fifth timer (TI#4) to show the remaining time until next demo line.") + "#1#%";
client_sock->write(packet.toUtf8());
}
}
else if (contents[1].startsWith("/help"))
{
QString packet = "CT#DEMO#" + tr("Available commands:\nload, reload, play, pause, max_wait, help") + "#1#%";
QString packet = "CT#DEMO#" + tr("Available commands:\nload, reload, play, pause, max_wait, debug, help") + "#1#%";
client_sock->write(packet.toUtf8());
}
}
Expand Down Expand Up @@ -321,8 +351,8 @@ void DemoServer::reset_state()
client_sock->write("LE##%");

// Reset timers
client_sock->write("TI#0#3#0#%");
client_sock->write("TI#0#1#0#%");
client_sock->write("TI#0#3#0#%");
client_sock->write("TI#1#1#0#%");
client_sock->write("TI#1#3#0#%");
client_sock->write("TI#2#1#0#%");
Expand Down Expand Up @@ -359,19 +389,25 @@ void DemoServer::playback()
AOPacket wait_packet = AOPacket(current_packet);

int duration = wait_packet.get_contents().at(0).toInt();
if (max_wait != -1) {
if (duration + elapsed_time > max_wait) {
duration = qMax(0, max_wait - elapsed_time);
// Skip the difference on the timers
emit skip_timers(wait_packet.get_contents().at(0).toInt() - duration);
}
else if (timer->interval() != 0 && duration + elapsed_time > timer->interval()) {
duration = qMax(0, timer->interval() - elapsed_time);
emit skip_timers(wait_packet.get_contents().at(0).toInt() - duration);
}
// Max wait reached
if (max_wait != -1 && duration + elapsed_time > max_wait) {
duration = qMax(0, max_wait - elapsed_time);
qDebug() << "Max_wait of " << max_wait << " reached. Forcing duration to " << duration << "ms";
// Skip the difference on the timers
emit skip_timers(wait_packet.get_contents().at(0).toInt() - duration);
}
// Manual user skip, such as with >
else if (timer->remainingTime() > 0) {
qDebug() << "Timer of interval " << timer->interval() << " is being skipped. Forcing to skip " << timer->remainingTime() << "ms on TI# clocks";
emit skip_timers(timer->remainingTime());
}
elapsed_time += duration;
timer->start(duration);
if (debug_mode) {
client_sock->write("TI#4#2#%");
QString debug_timer = "TI#4#0#" + QString::number(duration) + "#%";
client_sock->write(debug_timer.toUtf8());
}
}
else {
QString end_packet = "CT#DEMO#" + tr("Reached the end of the demo file. Send /play or > in OOC to restart, or /load to open a new file.") + "#1#%";
Expand Down