Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de3533f

Browse files
committed
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.
1 parent 610510e commit de3533f

6 files changed

Lines changed: 66 additions & 29 deletions

File tree

include/aoapplication.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <QScreen>
2727
#include <QStringList>
2828
#include <QTextStream>
29+
#include <QTime>
2930

3031
class NetworkManager;
3132
class Lobby;
@@ -61,7 +62,7 @@ class AOApplication : public QApplication {
6162
void call_settings_menu();
6263
void call_announce_menu(Courtroom *court);
6364

64-
qint64 last_ping;
65+
qint64 latency = 0;
6566
QString window_title;
6667

6768
/////////////////server metadata//////////////////

include/aoclocklabel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AOClockLabel : public QLabel {
1414
AOClockLabel(QWidget *parent);
1515
void start();
1616
void start(int msecs);
17-
void set(int msecs, bool update_text=false);
17+
void set(int msecs, bool update_text = false);
1818
void pause();
1919
void stop();
2020

include/courtroom.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ class Courtroom : public QMainWindow {
256256
void set_clock(qint64 msecs);
257257
void pause_clock();
258258
void stop_clock();
259+
void set_clock_visibility(bool visible);
259260

260-
qint64 get_ping() { return ping_timer.elapsed(); }
261+
qint64 pong();
261262

262263
~Courtroom();
263-
264264
private:
265265
AOApplication *ao_app;
266266

@@ -315,6 +315,7 @@ class Courtroom : public QMainWindow {
315315

316316
// count up timer to check how long it took for us to get a response from ping_server()
317317
QElapsedTimer ping_timer;
318+
bool is_pinging = false;
318319

319320
// int chat_tick_interval = 60;
320321
// which tick position(character in chat message) we are at

src/aoclocklabel.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {}
44

55
void AOClockLabel::start()
66
{
7-
timer.start(100, this);
7+
timer.start(1000 / 60, this);
88
}
99

1010
void AOClockLabel::start(int msecs)
@@ -15,11 +15,7 @@ void AOClockLabel::start(int msecs)
1515

1616
void AOClockLabel::set(int msecs, bool update_text)
1717
{
18-
QTime time = QTime::currentTime();
19-
if (msecs > time.msec())
20-
{
21-
target_time = time.addMSecs(msecs);
22-
}
18+
target_time = QTime::currentTime().addMSecs(msecs);
2319
if (update_text)
2420
{
2521
if (QTime::currentTime() >= target_time)

src/courtroom.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
88
qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch() / 1000));
99

1010
keepalive_timer = new QTimer(this);
11-
keepalive_timer->start(1000);
11+
keepalive_timer->start(45000);
1212

1313
chat_tick_timer = new QTimer(this);
1414

@@ -114,6 +114,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
114114

115115
ui_clock = new AOClockLabel(this);
116116
ui_clock->setAttribute(Qt::WA_TransparentForMouseEvents);
117+
ui_clock->hide();
117118

118119
ui_ic_chat_name = new QLineEdit(this);
119120
ui_ic_chat_name->setFrame(false);
@@ -4672,10 +4673,20 @@ void Courtroom::on_switch_area_music_clicked()
46724673
void Courtroom::ping_server()
46734674
{
46744675
ping_timer.start();
4676+
is_pinging = true;
46754677
ao_app->send_server_packet(
46764678
new AOPacket("CH#" + QString::number(m_cid) + "#%"));
46774679
}
46784680

4681+
qint64 Courtroom::pong()
4682+
{
4683+
if (!is_pinging)
4684+
return -1;
4685+
4686+
is_pinging = false;
4687+
return ping_timer.elapsed();
4688+
}
4689+
46794690
void Courtroom::on_casing_clicked()
46804691
{
46814692
if (ao_app->casing_alerts_enabled) {
@@ -4740,6 +4751,11 @@ void Courtroom::stop_clock()
47404751
ui_clock->stop();
47414752
}
47424753

4754+
void Courtroom::set_clock_visibility(bool visible)
4755+
{
4756+
ui_clock->setVisible(visible);
4757+
}
4758+
47434759
Courtroom::~Courtroom()
47444760
{
47454761
delete music_player;

src/packet_distribution.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -723,34 +723,57 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
723723
f_contents.at(5) == "1");
724724
}
725725
else if (header == "TI") { // Timer packet
726-
if (courtroom_constructed && f_contents.size() > 0) {
727-
qint64 resolution = f_contents.at(0).toInt();
728-
//Type 0 = start/stop timer
729-
//Type 1 = pause timer
730-
int type = 0;
731-
if (f_contents.size() > 1)
732-
type = f_contents.at(1).toInt();
733-
qDebug() << "timer" << resolution << last_ping << resolution - last_ping;
734-
resolution = resolution - last_ping;
735-
if (resolution > 0)
726+
if (!courtroom_constructed || f_contents.size() < 2)
727+
goto end;
728+
729+
// Note: timer ID is reserved as argument 0
730+
731+
// Type 0 = start/resume/sync timer at time
732+
// Type 1 = pause timer at time
733+
// Type 2 = show timer
734+
// Type 3 = hide timer
735+
int type = f_contents.at(1).toInt();
736+
737+
if (type == 0 || type == 1)
738+
{
739+
if (f_contents.size() < 2)
740+
goto end;
741+
742+
// The time as displayed on the clock, in milliseconds.
743+
// If the number received is negative, stop the timer.
744+
qint64 timer_value = f_contents.at(2).toLongLong();
745+
qDebug() << "timer:" << timer_value;
746+
if (timer_value > 0)
736747
{
737-
if (type == 1)
748+
if (type == 0)
738749
{
739-
w_courtroom->pause_clock();
740-
w_courtroom->set_clock(resolution);
750+
timer_value -= latency / 2;
751+
w_courtroom->start_clock(timer_value);
741752
}
742753
else
743-
w_courtroom->start_clock(resolution);
754+
{
755+
w_courtroom->pause_clock();
756+
w_courtroom->set_clock(timer_value);
757+
}
744758
}
745759
else
760+
{
746761
w_courtroom->stop_clock();
762+
}
747763
}
764+
else if (type == 2)
765+
w_courtroom->set_clock_visibility(true);
766+
else if (type == 3)
767+
w_courtroom->set_clock_visibility(false);
748768
}
749769
else if (header == "CHECK") {
750-
if (courtroom_constructed) {
751-
last_ping = w_courtroom->get_ping();
752-
w_courtroom->set_window_title(window_title + " [ping:" + QString::number(last_ping) + "]");
753-
}
770+
if (!courtroom_constructed)
771+
goto end;
772+
773+
qint64 ping_time = w_courtroom->pong();
774+
qDebug() << "ping:" << ping_time;
775+
if (ping_time != -1)
776+
latency = ping_time;
754777
}
755778

756779
end:

0 commit comments

Comments
 (0)