aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoroldmud0 <oldmud0@users.noreply.github.com>2021-01-09 01:18:19 -0600
committeroldmud0 <oldmud0@users.noreply.github.com>2021-01-09 01:18:19 -0600
commitde3533fbf2615a40efc60c9ed2e96f1a3b5da3c1 (patch)
tree484c45a32b32a87cfa8874d53b17f00c030bab6c
parent610510eb7b13475f0625d69e9a8e2b1d14669198 (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.
-rw-r--r--include/aoapplication.h3
-rw-r--r--include/aoclocklabel.h2
-rw-r--r--include/courtroom.h5
-rw-r--r--src/aoclocklabel.cpp8
-rw-r--r--src/courtroom.cpp18
-rw-r--r--src/packet_distribution.cpp59
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 <QScreen>
#include <QStringList>
#include <QTextStream>
+#include <QTime>
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<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 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: