From edf3d463e990ff8573bb6655c793490e6b1ea82c Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 21 Aug 2020 17:17:49 +0300 Subject: add a new aoclocklabel class that is a QLabel with fancy DR-Style timing features WIP --- src/aoclocklabel.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/aoclocklabel.cpp (limited to 'src') 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); + } +} -- cgit From f27f210efeb13a5ac29727fef271f8249e3e8c5d Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Fri, 21 Aug 2020 18:01:24 +0300 Subject: 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. --- src/aoclocklabel.cpp | 33 ++++++++++++++++++++++++--------- src/courtroom.cpp | 7 +++++++ 2 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src') 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(); } -- cgit From febfbeafc11ecad57d6e9a06575c28f1b13da8da Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:18:49 +0300 Subject: 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 --- src/courtroom.cpp | 4 ++-- src/packet_distribution.cpp | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'src') 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 = ""; -- cgit From 7e9c5726e02a65023d9563f49e833525bf8f4bae Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:21:10 +0300 Subject: Introduce the timer packet - "TI". This timer will start the clock accounting for latency! Cool, right? Remove useless qDebug() for music looping --- src/aomusicplayer.cpp | 1 - src/courtroom.cpp | 10 ++++++++++ src/packet_distribution.cpp | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) (limited to 'src') 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: -- cgit From ee3bad44c72329d61a2cb0c4064c22512451372d Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:48:09 +0300 Subject: 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. --- src/aoclocklabel.cpp | 54 ++++++++++++++++++++++++++------------------- src/courtroom.cpp | 15 +++++++++++++ src/packet_distribution.cpp | 17 ++++++++++++-- 3 files changed, 61 insertions(+), 25 deletions(-) (limited to 'src') 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(); } -- cgit From 6c62980ffc9215a826ee05f9afb9874a96ce60ea Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 12:58:48 +0300 Subject: forgot to ctrl+s header definitions fuck --- src/aoclocklabel.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') 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"); -- cgit From 610510eb7b13475f0625d69e9a8e2b1d14669198 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 25 Aug 2020 13:00:00 +0300 Subject: 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(-) (limited to 'src') 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: -- cgit From de3533fbf2615a40efc60c9ed2e96f1a3b5da3c1 Mon Sep 17 00:00:00 2001 From: oldmud0 Date: Sat, 9 Jan 2021 01:18:19 -0600 Subject: 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. --- src/aoclocklabel.cpp | 8 ++---- src/courtroom.cpp | 18 +++++++++++++- src/packet_distribution.cpp | 59 +++++++++++++++++++++++++++++++-------------- 3 files changed, 60 insertions(+), 25 deletions(-) (limited to 'src') 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: -- cgit From 45c78ea5caf58b6b8494decc6021540c755fabd7 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Mon, 11 Jan 2021 17:38:08 +0300 Subject: 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 --- src/courtroom.cpp | 60 +++++++++++++++++++++++++++++++-------------- src/packet_distribution.cpp | 19 ++++++++------ 2 files changed, 54 insertions(+), 25 deletions(-) (limited to 'src') 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) -- cgit From 98da2698a603c2dd131e523e305a961e805affd7 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Mon, 11 Jan 2021 17:52:23 +0300 Subject: Screw it, let there be clock_0 --- src/courtroom.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') 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(); } -- cgit From 07993a621b046985ee39ddef1427d8b4cc6042b1 Mon Sep 17 00:00:00 2001 From: Crystalwarrior Date: Tue, 12 Jan 2021 12:02:07 +0300 Subject: Better place to check invalid ID's --- src/courtroom.cpp | 12 ++++++------ src/packet_distribution.cpp | 4 ---- 2 files changed, 6 insertions(+), 10 deletions(-) (limited to 'src') 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 -- cgit