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.
This commit is contained in:
parent
610510eb7b
commit
de3533fbf2
@ -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//////////////////
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user