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 <QScreen>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
#include <QTime>
|
||||||
|
|
||||||
class NetworkManager;
|
class NetworkManager;
|
||||||
class Lobby;
|
class Lobby;
|
||||||
@ -61,7 +62,7 @@ public:
|
|||||||
void call_settings_menu();
|
void call_settings_menu();
|
||||||
void call_announce_menu(Courtroom *court);
|
void call_announce_menu(Courtroom *court);
|
||||||
|
|
||||||
qint64 last_ping;
|
qint64 latency = 0;
|
||||||
QString window_title;
|
QString window_title;
|
||||||
|
|
||||||
/////////////////server metadata//////////////////
|
/////////////////server metadata//////////////////
|
||||||
|
@ -256,11 +256,11 @@ public:
|
|||||||
void set_clock(qint64 msecs);
|
void set_clock(qint64 msecs);
|
||||||
void pause_clock();
|
void pause_clock();
|
||||||
void stop_clock();
|
void stop_clock();
|
||||||
|
void set_clock_visibility(bool visible);
|
||||||
|
|
||||||
qint64 get_ping() { return ping_timer.elapsed(); }
|
qint64 pong();
|
||||||
|
|
||||||
~Courtroom();
|
~Courtroom();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
AOApplication *ao_app;
|
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()
|
// count up timer to check how long it took for us to get a response from ping_server()
|
||||||
QElapsedTimer ping_timer;
|
QElapsedTimer ping_timer;
|
||||||
|
bool is_pinging = false;
|
||||||
|
|
||||||
// int chat_tick_interval = 60;
|
// int chat_tick_interval = 60;
|
||||||
// which tick position(character in chat message) we are at
|
// which tick position(character in chat message) we are at
|
||||||
|
@ -4,7 +4,7 @@ AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {}
|
|||||||
|
|
||||||
void AOClockLabel::start()
|
void AOClockLabel::start()
|
||||||
{
|
{
|
||||||
timer.start(100, this);
|
timer.start(1000 / 60, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AOClockLabel::start(int msecs)
|
void AOClockLabel::start(int msecs)
|
||||||
@ -15,11 +15,7 @@ void AOClockLabel::start(int msecs)
|
|||||||
|
|
||||||
void AOClockLabel::set(int msecs, bool update_text)
|
void AOClockLabel::set(int msecs, bool update_text)
|
||||||
{
|
{
|
||||||
QTime time = QTime::currentTime();
|
target_time = QTime::currentTime().addMSecs(msecs);
|
||||||
if (msecs > time.msec())
|
|
||||||
{
|
|
||||||
target_time = time.addMSecs(msecs);
|
|
||||||
}
|
|
||||||
if (update_text)
|
if (update_text)
|
||||||
{
|
{
|
||||||
if (QTime::currentTime() >= target_time)
|
if (QTime::currentTime() >= target_time)
|
||||||
|
@ -8,7 +8,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
|
|||||||
qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch() / 1000));
|
qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch() / 1000));
|
||||||
|
|
||||||
keepalive_timer = new QTimer(this);
|
keepalive_timer = new QTimer(this);
|
||||||
keepalive_timer->start(1000);
|
keepalive_timer->start(45000);
|
||||||
|
|
||||||
chat_tick_timer = new QTimer(this);
|
chat_tick_timer = new QTimer(this);
|
||||||
|
|
||||||
@ -114,6 +114,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
|
|||||||
|
|
||||||
ui_clock = new AOClockLabel(this);
|
ui_clock = new AOClockLabel(this);
|
||||||
ui_clock->setAttribute(Qt::WA_TransparentForMouseEvents);
|
ui_clock->setAttribute(Qt::WA_TransparentForMouseEvents);
|
||||||
|
ui_clock->hide();
|
||||||
|
|
||||||
ui_ic_chat_name = new QLineEdit(this);
|
ui_ic_chat_name = new QLineEdit(this);
|
||||||
ui_ic_chat_name->setFrame(false);
|
ui_ic_chat_name->setFrame(false);
|
||||||
@ -4672,10 +4673,20 @@ void Courtroom::on_switch_area_music_clicked()
|
|||||||
void Courtroom::ping_server()
|
void Courtroom::ping_server()
|
||||||
{
|
{
|
||||||
ping_timer.start();
|
ping_timer.start();
|
||||||
|
is_pinging = true;
|
||||||
ao_app->send_server_packet(
|
ao_app->send_server_packet(
|
||||||
new AOPacket("CH#" + QString::number(m_cid) + "#%"));
|
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()
|
void Courtroom::on_casing_clicked()
|
||||||
{
|
{
|
||||||
if (ao_app->casing_alerts_enabled) {
|
if (ao_app->casing_alerts_enabled) {
|
||||||
@ -4740,6 +4751,11 @@ void Courtroom::stop_clock()
|
|||||||
ui_clock->stop();
|
ui_clock->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Courtroom::set_clock_visibility(bool visible)
|
||||||
|
{
|
||||||
|
ui_clock->setVisible(visible);
|
||||||
|
}
|
||||||
|
|
||||||
Courtroom::~Courtroom()
|
Courtroom::~Courtroom()
|
||||||
{
|
{
|
||||||
delete music_player;
|
delete music_player;
|
||||||
|
@ -723,34 +723,57 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
|
|||||||
f_contents.at(5) == "1");
|
f_contents.at(5) == "1");
|
||||||
}
|
}
|
||||||
else if (header == "TI") { // Timer packet
|
else if (header == "TI") { // Timer packet
|
||||||
if (courtroom_constructed && f_contents.size() > 0) {
|
if (!courtroom_constructed || f_contents.size() < 2)
|
||||||
qint64 resolution = f_contents.at(0).toInt();
|
goto end;
|
||||||
//Type 0 = start/stop timer
|
|
||||||
//Type 1 = pause timer
|
// Note: timer ID is reserved as argument 0
|
||||||
int type = 0;
|
|
||||||
if (f_contents.size() > 1)
|
// Type 0 = start/resume/sync timer at time
|
||||||
type = f_contents.at(1).toInt();
|
// Type 1 = pause timer at time
|
||||||
qDebug() << "timer" << resolution << last_ping << resolution - last_ping;
|
// Type 2 = show timer
|
||||||
resolution = resolution - last_ping;
|
// Type 3 = hide timer
|
||||||
if (resolution > 0)
|
int type = f_contents.at(1).toInt();
|
||||||
|
|
||||||
|
if (type == 0 || type == 1)
|
||||||
{
|
{
|
||||||
if (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 == 0)
|
||||||
|
{
|
||||||
|
timer_value -= latency / 2;
|
||||||
|
w_courtroom->start_clock(timer_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
w_courtroom->pause_clock();
|
w_courtroom->pause_clock();
|
||||||
w_courtroom->set_clock(resolution);
|
w_courtroom->set_clock(timer_value);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
w_courtroom->start_clock(resolution);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
w_courtroom->stop_clock();
|
w_courtroom->stop_clock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (header == "CHECK") {
|
else if (type == 2)
|
||||||
if (courtroom_constructed) {
|
w_courtroom->set_clock_visibility(true);
|
||||||
last_ping = w_courtroom->get_ping();
|
else if (type == 3)
|
||||||
w_courtroom->set_window_title(window_title + " [ping:" + QString::number(last_ping) + "]");
|
w_courtroom->set_clock_visibility(false);
|
||||||
}
|
}
|
||||||
|
else if (header == "CHECK") {
|
||||||
|
if (!courtroom_constructed)
|
||||||
|
goto end;
|
||||||
|
|
||||||
|
qint64 ping_time = w_courtroom->pong();
|
||||||
|
qDebug() << "ping:" << ping_time;
|
||||||
|
if (ping_time != -1)
|
||||||
|
latency = ping_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
Loading…
Reference in New Issue
Block a user