From edf3d463e990ff8573bb6655c793490e6b1ea82c Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 21 Aug 2020 17:17:49 +0300 Subject: [PATCH 01/13] add a new aoclocklabel class that is a QLabel with fancy DR-Style timing features WIP --- include/aoclocklabel.h | 29 +++++++++++++++++++++++++++++ src/aoclocklabel.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 include/aoclocklabel.h create mode 100644 src/aoclocklabel.cpp diff --git a/include/aoclocklabel.h b/include/aoclocklabel.h new file mode 100644 index 0000000..518cae7 --- /dev/null +++ b/include/aoclocklabel.h @@ -0,0 +1,29 @@ +#ifndef AOCLOCKLABEL_H +#define AOCLOCKLABEL_H + +#include +#include +#include +#include + +class AOClockLabel : public QLabel { + Q_OBJECT + +public: + AOClockLabel(QWidget *parent); + void start(); + void start(QTime p_time); + void pause(); + void resume(); + void stop(); + +protected: + void timerEvent(QTimerEvent *event) override; + +private: + QBasicTimer timer; + QTime starting_time; + QTime target_time; +}; + +#endif // AOCLOCKLABEL_H diff --git a/src/aoclocklabel.cpp b/src/aoclocklabel.cpp new file mode 100644 index 0000000..fad21f4 --- /dev/null +++ b/src/aoclocklabel.cpp @@ -0,0 +1,35 @@ +#include "aoclocklabel.h" + +AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {} + +void AOClockLabel::start() +{ + this->resume(); +} + +void AOClockLabel::start(QTime p_time) +{ + QTime time = QTime::currentTime(); + if (p_time > time) + { + target_time = p_time; + starting_time = time; + timer.start(100, this); + } +} + +void AOClockLabel::pause() {} + +void AOClockLabel::resume() {} + +void AOClockLabel::stop() {} + +void AOClockLabel::timerEvent(QTimerEvent *event) +{ + if (event->timerId() == timer.timerId()) { + QTime elapsed = QTime(0,0).addSecs(starting_time.secsTo(starting_time)); + this->setText(elapsed.toString("hh:mm:ss.zzz")); + } else { + QWidget::timerEvent(event); + } +} From f27f210efeb13a5ac29727fef271f8249e3e8c5d Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 21 Aug 2020 18:01:24 +0300 Subject: [PATCH 02/13] Proof of concept complete. The timer will now take int msecs to start, and will properly display the time remaining until target time in hh:mm:ss.zzz Clock can be defined in courtroom_config.ini and its font set in courtroom_fonts.ini Pause and resume functions will not work as expected atm. --- include/aoclocklabel.h | 4 ++-- include/courtroom.h | 3 +++ src/aoclocklabel.cpp | 33 ++++++++++++++++++++++++--------- src/courtroom.cpp | 7 +++++++ 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/include/aoclocklabel.h b/include/aoclocklabel.h index 518cae7..806ed18 100644 --- a/include/aoclocklabel.h +++ b/include/aoclocklabel.h @@ -5,6 +5,7 @@ #include #include #include +#include class AOClockLabel : public QLabel { Q_OBJECT @@ -12,7 +13,7 @@ class AOClockLabel : public QLabel { public: AOClockLabel(QWidget *parent); void start(); - void start(QTime p_time); + void start(int msecs); void pause(); void resume(); void stop(); @@ -22,7 +23,6 @@ protected: private: QBasicTimer timer; - QTime starting_time; QTime target_time; }; diff --git a/include/courtroom.h b/include/courtroom.h index 5b5ff6c..1ff74f5 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -6,6 +6,7 @@ #include "aobutton.h" #include "aocharbutton.h" #include "aocharmovie.h" +#include "aoclocklabel.h" #include "aoemotebutton.h" #include "aoevidencebutton.h" #include "aoevidencedisplay.h" @@ -517,6 +518,8 @@ private: ScrollText *ui_music_name; AOMovie *ui_music_display; + AOClockLabel *ui_clock; + AOButton *ui_pair_button; QListWidget *ui_pair_list; QSpinBox *ui_pair_offset_spinbox; diff --git a/src/aoclocklabel.cpp b/src/aoclocklabel.cpp index fad21f4..783fcc5 100644 --- a/src/aoclocklabel.cpp +++ b/src/aoclocklabel.cpp @@ -7,28 +7,43 @@ void AOClockLabel::start() this->resume(); } -void AOClockLabel::start(QTime p_time) +void AOClockLabel::start(int msecs) { QTime time = QTime::currentTime(); - if (p_time > time) + if (msecs > time.msec()) { - target_time = p_time; - starting_time = time; + target_time = time.addMSecs(msecs); timer.start(100, this); } } -void AOClockLabel::pause() {} +void AOClockLabel::pause() +{ + timer.stop(); +} -void AOClockLabel::resume() {} +void AOClockLabel::resume() +{ + timer.start(100, this); +} -void AOClockLabel::stop() {} +void AOClockLabel::stop() +{ + this->setText("00:00:00.000"); + timer.stop(); +} void AOClockLabel::timerEvent(QTimerEvent *event) { if (event->timerId() == timer.timerId()) { - QTime elapsed = QTime(0,0).addSecs(starting_time.secsTo(starting_time)); - this->setText(elapsed.toString("hh:mm:ss.zzz")); + if (QTime::currentTime() >= target_time) + { + this->stop(); + return; + } + QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time)); + QString timestring = timeleft.toString("hh:mm:ss.zzz"); + this->setText(timestring); } else { QWidget::timerEvent(event); } diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 2484bcb..e2b372c 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -112,6 +112,9 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() ui_music_name->setText(tr("None")); ui_music_name->setAttribute(Qt::WA_TransparentForMouseEvents); + ui_clock = new AOClockLabel(this); + ui_clock->setAttribute(Qt::WA_TransparentForMouseEvents); + ui_ic_chat_name = new QLineEdit(this); ui_ic_chat_name->setFrame(false); ui_ic_chat_name->setPlaceholderText(tr("Showname")); @@ -619,6 +622,9 @@ void Courtroom::set_widgets() ui_music_display->play("music_display"); ui_music_display->set_play_once(false); + set_size_and_pos(ui_clock, "clock"); + ui_clock->start(30000); + if (is_ao2_bg) { set_size_and_pos(ui_ic_chat_message, "ao2_ic_chat_message"); set_size_and_pos(ui_vp_chatbox, "ao2_chatbox"); @@ -943,6 +949,7 @@ void Courtroom::set_fonts(QString p_char) set_font(ui_music_list, "", "music_list", p_char); set_font(ui_area_list, "", "area_list", p_char); set_font(ui_music_name, "", "music_name", p_char); + set_font(ui_clock, "", "clock", p_char); set_dropdowns(); } From febfbeafc11ecad57d6e9a06575c28f1b13da8da Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:18:49 +0300 Subject: [PATCH 03/13] Actually make use of the completely useless CHECK and CH keepalive timer and use them to determine the client's ping. Display ping in the application window title. keepalive timer now fires every second instead of every minute Remove meme clock starting on set_widgets() implement get_ping() on w_courtroom --- include/aoapplication.h | 3 +++ include/courtroom.h | 10 ++++++++-- src/courtroom.cpp | 4 ++-- src/packet_distribution.cpp | 10 ++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/aoapplication.h b/include/aoapplication.h index 60d945e..3f5767c 100644 --- a/include/aoapplication.h +++ b/include/aoapplication.h @@ -61,6 +61,9 @@ public: void call_settings_menu(); void call_announce_menu(Courtroom *court); + qint64 last_ping; + QString window_title; + /////////////////server metadata////////////////// unsigned int s_decryptor = 5; diff --git a/include/courtroom.h b/include/courtroom.h index 1ff74f5..0daeb2b 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -57,7 +57,7 @@ #include #include #include -//#include +#include #include @@ -251,6 +251,8 @@ public: void check_connection_received(); + qint64 get_ping() { return ping_timer.elapsed(); } + ~Courtroom(); private: @@ -299,11 +301,15 @@ private: QVector ic_chatlog_history; - // triggers ping_server() every 60 seconds + // triggers ping_server() every 1 second QTimer *keepalive_timer; // determines how fast messages tick onto screen QTimer *chat_tick_timer; + + // count up timer to check how long it took for us to get a response from ping_server() + QElapsedTimer ping_timer; + // int chat_tick_interval = 60; // which tick position(character in chat message) we are at int tick_pos = 0; diff --git a/src/courtroom.cpp b/src/courtroom.cpp index e2b372c..ec1fc99 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -8,7 +8,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() qsrand(static_cast(QDateTime::currentMSecsSinceEpoch() / 1000)); keepalive_timer = new QTimer(this); - keepalive_timer->start(60000); + keepalive_timer->start(1000); chat_tick_timer = new QTimer(this); @@ -623,7 +623,6 @@ void Courtroom::set_widgets() ui_music_display->set_play_once(false); set_size_and_pos(ui_clock, "clock"); - ui_clock->start(30000); if (is_ao2_bg) { set_size_and_pos(ui_ic_chat_message, "ao2_ic_chat_message"); @@ -4672,6 +4671,7 @@ void Courtroom::on_switch_area_music_clicked() void Courtroom::ping_server() { + ping_timer.start(); ao_app->send_server_packet( new AOPacket("CH#" + QString::number(m_cid) + "#%")); } diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index e4e5d5c..9422cfd 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -116,7 +116,13 @@ void AOApplication::server_packet_received(AOPacket *p_packet) qDebug() << "R:" << f_packet; #endif - if (header == "decryptor") { + if (header == "CHECK") { + if (courtroom_constructed) { + last_ping = w_courtroom->get_ping(); + w_courtroom->set_window_title(window_title + " [ping:" + QString::number(last_ping) + "]"); + } + } + else if (header == "decryptor") { if (f_contents.size() == 0) goto end; @@ -250,7 +256,7 @@ void AOApplication::server_packet_received(AOPacket *p_packet) courtroom_loaded = false; - QString window_title = tr("Attorney Online 2"); + window_title = tr("Attorney Online 2"); int selected_server = w_lobby->get_selected_server(); QString server_address = "", server_name = ""; From 7e9c5726e02a65023d9563f49e833525bf8f4bae Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:21:10 +0300 Subject: [PATCH 04/13] Introduce the timer packet - "TI". This timer will start the clock accounting for latency! Cool, right? Remove useless qDebug() for music looping --- include/courtroom.h | 4 ++++ src/aomusicplayer.cpp | 1 - src/courtroom.cpp | 10 ++++++++++ src/packet_distribution.cpp | 11 +++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/courtroom.h b/include/courtroom.h index 0daeb2b..9346172 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -251,6 +251,10 @@ public: void check_connection_received(); + void start_clock(qint64 msecs); + + void stop_clock(); + qint64 get_ping() { return ping_timer.elapsed(); } ~Courtroom(); diff --git a/src/aomusicplayer.cpp b/src/aomusicplayer.cpp index 6c61b9a..0c50d50 100644 --- a/src/aomusicplayer.cpp +++ b/src/aomusicplayer.cpp @@ -145,7 +145,6 @@ void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user) void AOMusicPlayer::set_looping(bool toggle, int channel) { - qDebug() << "Setting looping for channel" << channel << "to" << toggle; m_looping = toggle; if (!m_looping) { if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP) diff --git a/src/courtroom.cpp b/src/courtroom.cpp index ec1fc99..6bd2bd2 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -4715,6 +4715,16 @@ void Courtroom::announce_case(QString title, bool def, bool pro, bool jud, } } +void Courtroom::start_clock(qint64 msecs) +{ + ui_clock->start(static_cast(msecs)); +} + +void Courtroom::stop_clock() +{ + ui_clock->stop(); +} + Courtroom::~Courtroom() { delete music_player; diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index 9422cfd..1f49719 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -728,6 +728,17 @@ void AOApplication::server_packet_received(AOPacket *p_packet) f_contents.at(4) == "1", f_contents.at(5) == "1"); } + else if (header == "TI") { // Timer packet + if (courtroom_constructed && f_contents.size() >= 1) { + qint64 resolution = f_contents.at(0).toInt(); + qDebug() << "timer" << resolution << last_ping << resolution - last_ping; + resolution = resolution - last_ping; + if (resolution > 0) + w_courtroom->start_clock(resolution); + else + w_courtroom->stop_clock(); + } + } end: From ee3bad44c72329d61a2cb0c4064c22512451372d Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:48:09 +0300 Subject: [PATCH 05/13] Implement clock pausing Implement clock setting w/o starting or stopping Both of these should make it possible for the server to start/stop/pause/resume the clock with perfect synchronization to the true time. --- include/aoclocklabel.h | 2 +- include/courtroom.h | 3 +++ src/aoclocklabel.cpp | 54 +++++++++++++++++++++---------------- src/courtroom.cpp | 15 +++++++++++ src/packet_distribution.cpp | 17 ++++++++++-- 5 files changed, 65 insertions(+), 26 deletions(-) diff --git a/include/aoclocklabel.h b/include/aoclocklabel.h index 806ed18..b948330 100644 --- a/include/aoclocklabel.h +++ b/include/aoclocklabel.h @@ -14,8 +14,8 @@ public: AOClockLabel(QWidget *parent); void start(); void start(int msecs); + void set(int msecs, bool update_text=false); void pause(); - void resume(); void stop(); protected: diff --git a/include/courtroom.h b/include/courtroom.h index 9346172..15324a0 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -251,8 +251,11 @@ public: void check_connection_received(); + void start_clock(); void start_clock(qint64 msecs); + void set_clock(qint64 msecs); + void stop_clock(); qint64 get_ping() { return ping_timer.elapsed(); } diff --git a/src/aoclocklabel.cpp b/src/aoclocklabel.cpp index 783fcc5..7bbf48c 100644 --- a/src/aoclocklabel.cpp +++ b/src/aoclocklabel.cpp @@ -4,27 +4,35 @@ AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {} void AOClockLabel::start() { - this->resume(); + timer.start(100, this); } void AOClockLabel::start(int msecs) +{ + this->set(msecs); + this->start(); +} + +void AOClockLabel::set(int msecs, bool update_text) { QTime time = QTime::currentTime(); if (msecs > time.msec()) { target_time = time.addMSecs(msecs); - timer.start(100, this); } -} - -void AOClockLabel::pause() -{ - timer.stop(); -} - -void AOClockLabel::resume() -{ - timer.start(100, this); + if (update_text) + { + if (QTime::currentTime() >= target_time) + { + this->setText("00:00:00.000"); + } + else + { + QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time)); + QString timestring = timeleft.toString("hh:mm:ss.zzz"); + this->setText(timestring); + } + } } void AOClockLabel::stop() @@ -35,16 +43,16 @@ void AOClockLabel::stop() void AOClockLabel::timerEvent(QTimerEvent *event) { - if (event->timerId() == timer.timerId()) { - if (QTime::currentTime() >= target_time) - { - this->stop(); - return; - } - QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time)); - QString timestring = timeleft.toString("hh:mm:ss.zzz"); - this->setText(timestring); - } else { - QWidget::timerEvent(event); + if (event->timerId() == timer.timerId()) { + if (QTime::currentTime() >= target_time) + { + this->stop(); + return; } + QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time)); + QString timestring = timeleft.toString("hh:mm:ss.zzz"); + this->setText(timestring); + } else { + QWidget::timerEvent(event); + } } diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 6bd2bd2..f4e3164 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -4715,11 +4715,26 @@ void Courtroom::announce_case(QString title, bool def, bool pro, bool jud, } } +void Courtroom::start_clock() +{ + ui_clock->start(); +} + void Courtroom::start_clock(qint64 msecs) { ui_clock->start(static_cast(msecs)); } +void Courtroom::set_clock(qint64 msecs) +{ + ui_clock->set(static_cast(msecs), true); +} + +void Courtroom::pause_clock() +{ + ui_clock->pause(); +} + void Courtroom::stop_clock() { ui_clock->stop(); diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index 1f49719..578f6df 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -729,12 +729,25 @@ 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() >= 1) { + 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) - w_courtroom->start_clock(resolution); + { + if (type == 1) + { + w_courtroom->pause_clock(); + w_courtroom->set_clock(resolution); + } + else + w_courtroom->start_clock(resolution); + } else w_courtroom->stop_clock(); } From 6c62980ffc9215a826ee05f9afb9874a96ce60ea Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:58:48 +0300 Subject: [PATCH 06/13] forgot to ctrl+s header definitions fuck --- include/courtroom.h | 3 +-- src/aoclocklabel.cpp | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/courtroom.h b/include/courtroom.h index 15324a0..4835b0a 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -253,9 +253,8 @@ public: void start_clock(); void start_clock(qint64 msecs); - void set_clock(qint64 msecs); - + void pause_clock(); void stop_clock(); qint64 get_ping() { return ping_timer.elapsed(); } diff --git a/src/aoclocklabel.cpp b/src/aoclocklabel.cpp index 7bbf48c..d67ec36 100644 --- a/src/aoclocklabel.cpp +++ b/src/aoclocklabel.cpp @@ -35,6 +35,11 @@ void AOClockLabel::set(int msecs, bool update_text) } } +void AOClockLabel::pause() +{ + timer.stop(); +} + void AOClockLabel::stop() { this->setText("00:00:00.000"); From 610510eb7b13475f0625d69e9a8e2b1d14669198 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 13:00:00 +0300 Subject: [PATCH 07/13] move "check" to the very end instead of very start in packet_distribution --- src/packet_distribution.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index 578f6df..e0a6ccb 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -116,13 +116,7 @@ void AOApplication::server_packet_received(AOPacket *p_packet) qDebug() << "R:" << f_packet; #endif - if (header == "CHECK") { - if (courtroom_constructed) { - last_ping = w_courtroom->get_ping(); - w_courtroom->set_window_title(window_title + " [ping:" + QString::number(last_ping) + "]"); - } - } - else if (header == "decryptor") { + if (header == "decryptor") { if (f_contents.size() == 0) goto end; @@ -752,6 +746,12 @@ void AOApplication::server_packet_received(AOPacket *p_packet) w_courtroom->stop_clock(); } } + 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) + "]"); + } + } end: From de3533fbf2615a40efc60c9ed2e96f1a3b5da3c1 Mon Sep 17 00:00:00 2001 From: oldmud0 Date: Sat, 9 Jan 2021 01:18:19 -0600 Subject: [PATCH 08/13] 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. --- include/aoapplication.h | 3 +- include/aoclocklabel.h | 2 +- include/courtroom.h | 5 ++-- src/aoclocklabel.cpp | 8 ++--- src/courtroom.cpp | 18 ++++++++++- src/packet_distribution.cpp | 59 ++++++++++++++++++++++++++----------- 6 files changed, 66 insertions(+), 29 deletions(-) diff --git a/include/aoapplication.h b/include/aoapplication.h index 3f5767c..458a884 100644 --- a/include/aoapplication.h +++ b/include/aoapplication.h @@ -26,6 +26,7 @@ #include #include #include +#include class NetworkManager; class Lobby; @@ -61,7 +62,7 @@ public: void call_settings_menu(); void call_announce_menu(Courtroom *court); - qint64 last_ping; + qint64 latency = 0; QString window_title; /////////////////server metadata////////////////// diff --git a/include/aoclocklabel.h b/include/aoclocklabel.h index b948330..b5d0794 100644 --- a/include/aoclocklabel.h +++ b/include/aoclocklabel.h @@ -14,7 +14,7 @@ public: AOClockLabel(QWidget *parent); void start(); void start(int msecs); - void set(int msecs, bool update_text=false); + void set(int msecs, bool update_text = false); void pause(); void stop(); diff --git a/include/courtroom.h b/include/courtroom.h index 4835b0a..72b4989 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -256,11 +256,11 @@ public: void set_clock(qint64 msecs); void pause_clock(); void stop_clock(); + void set_clock_visibility(bool visible); - qint64 get_ping() { return ping_timer.elapsed(); } + qint64 pong(); ~Courtroom(); - private: AOApplication *ao_app; @@ -315,6 +315,7 @@ private: // count up timer to check how long it took for us to get a response from ping_server() QElapsedTimer ping_timer; + bool is_pinging = false; // int chat_tick_interval = 60; // which tick position(character in chat message) we are at diff --git a/src/aoclocklabel.cpp b/src/aoclocklabel.cpp index d67ec36..4c9c481 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 f4e3164..c2fcf9b 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -8,7 +8,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() qsrand(static_cast(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 e0a6ccb..17e57bf 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: From 62532f0b643c5f7502f1f48781ec6ec338019275 Mon Sep 17 00:00:00 2001 From: oldmud0 Date: Sun, 10 Jan 2021 10:41:34 -0600 Subject: [PATCH 09/13] Trivial comment change --- include/courtroom.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/courtroom.h b/include/courtroom.h index e0db719..4924325 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -316,7 +316,7 @@ private: QVector ic_chatlog_history; - // triggers ping_server() every 1 second + // triggers ping_server() every 45 seconds QTimer *keepalive_timer; // determines how fast messages tick onto screen From 45c78ea5caf58b6b8494decc6021540c755fabd7 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Mon, 11 Jan 2021 17:38:08 +0300 Subject: [PATCH 10/13] Add "id" variable to the clock functions and properly parse the ID Implement scalable maximum clock count, right now it's at 5 clocks a theme can have max Theme "clock_" starts from 1 instead of 0 since users don't know when stuff starts at index 0 TODO: testing lol --- include/courtroom.h | 15 +++++----- src/courtroom.cpp | 60 ++++++++++++++++++++++++++----------- src/packet_distribution.cpp | 19 +++++++----- 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/include/courtroom.h b/include/courtroom.h index 4924325..91606c3 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -255,12 +255,12 @@ public: void check_connection_received(); - void start_clock(); - void start_clock(qint64 msecs); - void set_clock(qint64 msecs); - void pause_clock(); - void stop_clock(); - void set_clock_visibility(bool visible); + void start_clock(int id); + void start_clock(int id, qint64 msecs); + void set_clock(int id, qint64 msecs); + void pause_clock(int id); + void stop_clock(int id); + void set_clock_visibility(int id, bool visible); qint64 pong(); // Truncates text so it fits within theme-specified boundaries and sets the tooltip to the full string @@ -563,7 +563,8 @@ private: ScrollText *ui_music_name; AOMovie *ui_music_display; - AOClockLabel *ui_clock; + static const int max_clocks = 5; + AOClockLabel *ui_clock[max_clocks]; AOButton *ui_pair_button; QListWidget *ui_pair_list; diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 962c04f..d997c91 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -115,10 +115,12 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow() ui_music_name = new ScrollText(ui_music_display); ui_music_name->setText(tr("None")); ui_music_name->setAttribute(Qt::WA_TransparentForMouseEvents); - - ui_clock = new AOClockLabel(this); - ui_clock->setAttribute(Qt::WA_TransparentForMouseEvents); - ui_clock->hide(); + + for (int i = 0; i < max_clocks; i++) { + ui_clock[i] = new AOClockLabel(this); + ui_clock[i]->setAttribute(Qt::WA_TransparentForMouseEvents); + ui_clock[i]->hide(); + } ui_ic_chat_name = new QLineEdit(this); ui_ic_chat_name->setFrame(false); @@ -645,7 +647,9 @@ void Courtroom::set_widgets() ui_music_display->play("music_display"); ui_music_display->set_play_once(false); - set_size_and_pos(ui_clock, "clock"); + for (int i = 0; i < max_clocks; i++) { + set_size_and_pos(ui_clock[i], "clock_" + QString::number(i+1)); + } if (is_ao2_bg) { set_size_and_pos(ui_ic_chat_message, "ao2_ic_chat_message"); @@ -994,7 +998,9 @@ void Courtroom::set_fonts(QString p_char) set_font(ui_music_list, "", "music_list", p_char); set_font(ui_area_list, "", "area_list", p_char); set_font(ui_music_name, "", "music_name", p_char); - set_font(ui_clock, "", "clock", p_char); + + for (int i = 0; i < max_clocks; i++) + set_font(ui_clock[i], "", "clock_" + QString::number(i+1), p_char); set_dropdowns(); } @@ -5066,34 +5072,52 @@ void Courtroom::announce_case(QString title, bool def, bool pro, bool jud, } } -void Courtroom::start_clock() +void Courtroom::start_clock(int id) { - ui_clock->start(); + if (id < max_clocks && ui_clock[id] != nullptr) + { + ui_clock[id]->start(); + } } -void Courtroom::start_clock(qint64 msecs) +void Courtroom::start_clock(int id, qint64 msecs) { - ui_clock->start(static_cast(msecs)); + if (id < max_clocks && ui_clock[id] != nullptr) + { + ui_clock[id]->start(static_cast(msecs)); + } } -void Courtroom::set_clock(qint64 msecs) +void Courtroom::set_clock(int id, qint64 msecs) { - ui_clock->set(static_cast(msecs), true); + if (id < max_clocks && ui_clock[id] != nullptr) + { + ui_clock[id]->set(static_cast(msecs), true); + } } -void Courtroom::pause_clock() +void Courtroom::pause_clock(int id) { - ui_clock->pause(); + if (id < max_clocks && ui_clock[id] != nullptr) + { + ui_clock[id]->pause(); + } } -void Courtroom::stop_clock() +void Courtroom::stop_clock(int id) { - ui_clock->stop(); + if (id < max_clocks && ui_clock[id] != nullptr) + { + ui_clock[id]->stop(); + } } -void Courtroom::set_clock_visibility(bool visible) +void Courtroom::set_clock_visibility(int id, bool visible) { - ui_clock->setVisible(visible); + if (id < max_clocks && ui_clock[id] != nullptr) + { + ui_clock[id]->setVisible(visible); + } } void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier) diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index 0bc3814..2699f08 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -578,7 +578,12 @@ void AOApplication::server_packet_received(AOPacket *p_packet) if (!courtroom_constructed || f_contents.size() < 2) goto end; - // Note: timer ID is reserved as argument 0 + // Timer ID is reserved as argument 0 + int id = f_contents.at(0).toInt(); + + // ID is invalid + if (id < 0 || id >= w_courtroom->max_clocks) + goto end; // Type 0 = start/resume/sync timer at time // Type 1 = pause timer at time @@ -600,23 +605,23 @@ void AOApplication::server_packet_received(AOPacket *p_packet) if (type == 0) { timer_value -= latency / 2; - w_courtroom->start_clock(timer_value); + w_courtroom->start_clock(id, timer_value); } else { - w_courtroom->pause_clock(); - w_courtroom->set_clock(timer_value); + w_courtroom->pause_clock(id); + w_courtroom->set_clock(id, timer_value); } } else { - w_courtroom->stop_clock(); + w_courtroom->stop_clock(id); } } else if (type == 2) - w_courtroom->set_clock_visibility(true); + w_courtroom->set_clock_visibility(id, true); else if (type == 3) - w_courtroom->set_clock_visibility(false); + w_courtroom->set_clock_visibility(id, false); } else if (header == "CHECK") { if (!courtroom_constructed) From 98da2698a603c2dd131e523e305a961e805affd7 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Mon, 11 Jan 2021 17:52:23 +0300 Subject: [PATCH 11/13] Screw it, let there be clock_0 --- src/courtroom.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/courtroom.cpp b/src/courtroom.cpp index d997c91..361f08f 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -648,7 +648,7 @@ void Courtroom::set_widgets() ui_music_display->set_play_once(false); for (int i = 0; i < max_clocks; i++) { - set_size_and_pos(ui_clock[i], "clock_" + QString::number(i+1)); + set_size_and_pos(ui_clock[i], "clock_" + QString::number(i)); } if (is_ao2_bg) { @@ -1000,7 +1000,7 @@ void Courtroom::set_fonts(QString p_char) set_font(ui_music_name, "", "music_name", p_char); for (int i = 0; i < max_clocks; i++) - set_font(ui_clock[i], "", "clock_" + QString::number(i+1), p_char); + set_font(ui_clock[i], "", "clock_" + QString::number(i), p_char); set_dropdowns(); } From f96bd6cc3cb03a22d10ffdaa66d8b794faa3bf18 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 12 Jan 2021 11:59:07 +0300 Subject: [PATCH 12/13] Didn't notice I removed this from the mergening when I resolved merge conflicts, oops --- include/courtroom.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/courtroom.h b/include/courtroom.h index 4d4f173..d996715 100644 --- a/include/courtroom.h +++ b/include/courtroom.h @@ -358,6 +358,8 @@ private: QVector ic_chatlog_history; + QQueue chatmessage_queue; + // triggers ping_server() every 45 seconds QTimer *keepalive_timer; From 07993a621b046985ee39ddef1427d8b4cc6042b1 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 12 Jan 2021 12:02:07 +0300 Subject: [PATCH 13/13] Better place to check invalid ID's --- src/courtroom.cpp | 12 ++++++------ src/packet_distribution.cpp | 4 ---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 4ed4735..0c36215 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -5336,7 +5336,7 @@ void Courtroom::announce_case(QString title, bool def, bool pro, bool jud, void Courtroom::start_clock(int id) { - if (id < max_clocks && ui_clock[id] != nullptr) + if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr) { ui_clock[id]->start(); } @@ -5344,7 +5344,7 @@ void Courtroom::start_clock(int id) void Courtroom::start_clock(int id, qint64 msecs) { - if (id < max_clocks && ui_clock[id] != nullptr) + if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr) { ui_clock[id]->start(static_cast(msecs)); } @@ -5352,7 +5352,7 @@ void Courtroom::start_clock(int id, qint64 msecs) void Courtroom::set_clock(int id, qint64 msecs) { - if (id < max_clocks && ui_clock[id] != nullptr) + if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr) { ui_clock[id]->set(static_cast(msecs), true); } @@ -5360,7 +5360,7 @@ void Courtroom::set_clock(int id, qint64 msecs) void Courtroom::pause_clock(int id) { - if (id < max_clocks && ui_clock[id] != nullptr) + if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr) { ui_clock[id]->pause(); } @@ -5368,7 +5368,7 @@ void Courtroom::pause_clock(int id) void Courtroom::stop_clock(int id) { - if (id < max_clocks && ui_clock[id] != nullptr) + if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr) { ui_clock[id]->stop(); } @@ -5376,7 +5376,7 @@ void Courtroom::stop_clock(int id) void Courtroom::set_clock_visibility(int id, bool visible) { - if (id < max_clocks && ui_clock[id] != nullptr) + if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr) { ui_clock[id]->setVisible(visible); } diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index 4aae789..6865987 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -583,10 +583,6 @@ void AOApplication::server_packet_received(AOPacket *p_packet) // Timer ID is reserved as argument 0 int id = f_contents.at(0).toInt(); - // ID is invalid - if (id < 0 || id >= w_courtroom->max_clocks) - goto end; - // Type 0 = start/resume/sync timer at time // Type 1 = pause timer at time // Type 2 = show timer