diff options
| author | oldmud0 <oldmud0@users.noreply.github.com> | 2021-01-09 01:18:19 -0600 |
|---|---|---|
| committer | oldmud0 <oldmud0@users.noreply.github.com> | 2021-01-09 01:18:19 -0600 |
| commit | de3533fbf2615a40efc60c9ed2e96f1a3b5da3c1 (patch) | |
| tree | 484c45a32b32a87cfa8874d53b17f00c030bab6c /src | |
| parent | 610510eb7b13475f0625d69e9a8e2b1d14669198 (diff) | |
Rework timer and ping logic
The timer's time as received by the server is clarified to be the actual
numerical time, in milliseconds, to be shown on the clock.
Diffstat (limited to 'src')
| -rw-r--r-- | src/aoclocklabel.cpp | 8 | ||||
| -rw-r--r-- | src/courtroom.cpp | 18 | ||||
| -rw-r--r-- | src/packet_distribution.cpp | 59 |
3 files changed, 60 insertions, 25 deletions
diff --git a/src/aoclocklabel.cpp b/src/aoclocklabel.cpp index d67ec36d..4c9c4819 100644 --- a/src/aoclocklabel.cpp +++ b/src/aoclocklabel.cpp @@ -4,7 +4,7 @@ AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {} void AOClockLabel::start() { - timer.start(100, this); + timer.start(1000 / 60, this); } void AOClockLabel::start(int msecs) @@ -15,11 +15,7 @@ void AOClockLabel::start(int msecs) void AOClockLabel::set(int msecs, bool update_text) { - QTime time = QTime::currentTime(); - if (msecs > time.msec()) - { - target_time = time.addMSecs(msecs); - } + target_time = QTime::currentTime().addMSecs(msecs); if (update_text) { if (QTime::currentTime() >= target_time) diff --git a/src/courtroom.cpp b/src/courtroom.cpp index f4e31647..c2fcf9b8 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -8,7 +8,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch() / 1000)); keepalive_timer = new QTimer(this); - keepalive_timer->start(1000); + keepalive_timer->start(45000); chat_tick_timer = new QTimer(this); @@ -114,6 +114,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() ui_clock = new AOClockLabel(this); ui_clock->setAttribute(Qt::WA_TransparentForMouseEvents); + ui_clock->hide(); ui_ic_chat_name = new QLineEdit(this); ui_ic_chat_name->setFrame(false); @@ -4672,10 +4673,20 @@ void Courtroom::on_switch_area_music_clicked() void Courtroom::ping_server() { ping_timer.start(); + is_pinging = true; ao_app->send_server_packet( new AOPacket("CH#" + QString::number(m_cid) + "#%")); } +qint64 Courtroom::pong() +{ + if (!is_pinging) + return -1; + + is_pinging = false; + return ping_timer.elapsed(); +} + void Courtroom::on_casing_clicked() { if (ao_app->casing_alerts_enabled) { @@ -4740,6 +4751,11 @@ void Courtroom::stop_clock() ui_clock->stop(); } +void Courtroom::set_clock_visibility(bool visible) +{ + ui_clock->setVisible(visible); +} + Courtroom::~Courtroom() { delete music_player; diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index e0a6ccbc..17e57bf7 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -723,34 +723,57 @@ void AOApplication::server_packet_received(AOPacket *p_packet) f_contents.at(5) == "1"); } else if (header == "TI") { // Timer packet - if (courtroom_constructed && f_contents.size() > 0) { - qint64 resolution = f_contents.at(0).toInt(); - //Type 0 = start/stop timer - //Type 1 = pause timer - int type = 0; - if (f_contents.size() > 1) - type = f_contents.at(1).toInt(); - qDebug() << "timer" << resolution << last_ping << resolution - last_ping; - resolution = resolution - last_ping; - if (resolution > 0) + if (!courtroom_constructed || f_contents.size() < 2) + goto end; + + // Note: timer ID is reserved as argument 0 + + // Type 0 = start/resume/sync timer at time + // Type 1 = pause timer at time + // Type 2 = show timer + // Type 3 = hide timer + int type = f_contents.at(1).toInt(); + + if (type == 0 || type == 1) + { + if (f_contents.size() < 2) + goto end; + + // The time as displayed on the clock, in milliseconds. + // If the number received is negative, stop the timer. + qint64 timer_value = f_contents.at(2).toLongLong(); + qDebug() << "timer:" << timer_value; + if (timer_value > 0) { - if (type == 1) + if (type == 0) { - w_courtroom->pause_clock(); - w_courtroom->set_clock(resolution); + timer_value -= latency / 2; + w_courtroom->start_clock(timer_value); } else - w_courtroom->start_clock(resolution); + { + w_courtroom->pause_clock(); + w_courtroom->set_clock(timer_value); + } } else + { w_courtroom->stop_clock(); + } } + else if (type == 2) + w_courtroom->set_clock_visibility(true); + else if (type == 3) + w_courtroom->set_clock_visibility(false); } else if (header == "CHECK") { - if (courtroom_constructed) { - last_ping = w_courtroom->get_ping(); - w_courtroom->set_window_title(window_title + " [ping:" + QString::number(last_ping) + "]"); - } + if (!courtroom_constructed) + goto end; + + qint64 ping_time = w_courtroom->pong(); + qDebug() << "ping:" << ping_time; + if (ping_time != -1) + latency = ping_time; } end: |
