Skip to content

Commit e097df0

Browse files
authored
Merge pull request #384 from AttorneyOnline/feature/timerclock
Countdown timer
2 parents 0926f3c + 1b016dd commit e097df0

7 files changed

Lines changed: 239 additions & 6 deletions

File tree

include/aoapplication.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QScreen>
3030
#include <QStringList>
3131
#include <QTextStream>
32+
#include <QTime>
3233

3334
#include <QElapsedTimer>
3435

@@ -66,6 +67,9 @@ class AOApplication : public QApplication {
6667
void call_settings_menu();
6768
void call_announce_menu(Courtroom *court);
6869

70+
qint64 latency = 0;
71+
QString window_title;
72+
6973
/////////////////server metadata//////////////////
7074

7175
bool yellow_text_enabled = false;

include/aoclocklabel.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef AOCLOCKLABEL_H
2+
#define AOCLOCKLABEL_H
3+
4+
#include <QLabel>
5+
#include <QBasicTimer>
6+
#include <QTimerEvent>
7+
#include <QTime>
8+
#include <QDebug>
9+
10+
class AOClockLabel : public QLabel {
11+
Q_OBJECT
12+
13+
public:
14+
AOClockLabel(QWidget *parent);
15+
void start();
16+
void start(int msecs);
17+
void set(int msecs, bool update_text = false);
18+
void pause();
19+
void stop();
20+
21+
protected:
22+
void timerEvent(QTimerEvent *event) override;
23+
24+
private:
25+
QBasicTimer timer;
26+
QTime target_time;
27+
};
28+
29+
#endif // AOCLOCKLABEL_H

include/courtroom.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "aoblipplayer.h"
66
#include "aobutton.h"
77
#include "aocharbutton.h"
8+
#include "aoclocklabel.h"
89
#include "aoemotebutton.h"
910
#include "aoevidencebutton.h"
1011
#include "aoevidencedisplay.h"
@@ -55,7 +56,7 @@
5556
#include <QScrollBar>
5657
#include <QTextBoundaryFinder>
5758
#include <QTextCharFormat>
58-
//#include <QRandomGenerator>
59+
#include <QElapsedTimer>
5960

6061
#include <algorithm>
6162
#include <stack>
@@ -300,11 +301,18 @@ class Courtroom : public QMainWindow {
300301

301302
void check_connection_received();
302303

304+
void start_clock(int id);
305+
void start_clock(int id, qint64 msecs);
306+
void set_clock(int id, qint64 msecs);
307+
void pause_clock(int id);
308+
void stop_clock(int id);
309+
void set_clock_visibility(int id, bool visible);
310+
311+
qint64 pong();
303312
// Truncates text so it fits within theme-specified boundaries and sets the tooltip to the full string
304313
void truncate_label_text(QWidget* p_widget, QString p_identifier);
305314

306315
~Courtroom();
307-
308316
private:
309317
AOApplication *ao_app;
310318

@@ -357,11 +365,16 @@ class Courtroom : public QMainWindow {
357365

358366
QQueue<QStringList> chatmessage_queue;
359367

360-
// triggers ping_server() every 60 seconds
368+
// triggers ping_server() every 45 seconds
361369
QTimer *keepalive_timer;
362370

363371
// determines how fast messages tick onto screen
364372
QTimer *chat_tick_timer;
373+
374+
// count up timer to check how long it took for us to get a response from ping_server()
375+
QElapsedTimer ping_timer;
376+
bool is_pinging = false;
377+
365378
// int chat_tick_interval = 60;
366379
// which tick position(character in chat message) we are at
367380
int tick_pos = 0;
@@ -619,6 +632,9 @@ class Courtroom : public QMainWindow {
619632

620633
StickerLayer *ui_vp_sticker;
621634

635+
static const int max_clocks = 5;
636+
AOClockLabel *ui_clock[max_clocks];
637+
622638
AOButton *ui_pair_button;
623639
QListWidget *ui_pair_list;
624640
QSpinBox *ui_pair_offset_spinbox;

src/aoclocklabel.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "aoclocklabel.h"
2+
3+
AOClockLabel::AOClockLabel(QWidget *parent) : QLabel(parent) {}
4+
5+
void AOClockLabel::start()
6+
{
7+
timer.start(1000 / 60, this);
8+
}
9+
10+
void AOClockLabel::start(int msecs)
11+
{
12+
this->set(msecs);
13+
this->start();
14+
}
15+
16+
void AOClockLabel::set(int msecs, bool update_text)
17+
{
18+
target_time = QTime::currentTime().addMSecs(msecs);
19+
if (update_text)
20+
{
21+
if (QTime::currentTime() >= target_time)
22+
{
23+
this->setText("00:00:00.000");
24+
}
25+
else
26+
{
27+
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
28+
QString timestring = timeleft.toString("hh:mm:ss.zzz");
29+
this->setText(timestring);
30+
}
31+
}
32+
}
33+
34+
void AOClockLabel::pause()
35+
{
36+
timer.stop();
37+
}
38+
39+
void AOClockLabel::stop()
40+
{
41+
this->setText("00:00:00.000");
42+
timer.stop();
43+
}
44+
45+
void AOClockLabel::timerEvent(QTimerEvent *event)
46+
{
47+
if (event->timerId() == timer.timerId()) {
48+
if (QTime::currentTime() >= target_time)
49+
{
50+
this->stop();
51+
return;
52+
}
53+
QTime timeleft = QTime(0,0).addMSecs(QTime::currentTime().msecsTo(target_time));
54+
QString timestring = timeleft.toString("hh:mm:ss.zzz");
55+
this->setText(timestring);
56+
} else {
57+
QWidget::timerEvent(event);
58+
}
59+
}

src/aomusicplayer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ void CALLBACK loopProc(HSYNC handle, DWORD channel, DWORD data, void *user)
161161

162162
void AOMusicPlayer::set_looping(bool toggle, int channel)
163163
{
164-
qDebug() << "Setting looping for channel" << channel << "to" << toggle;
165164
m_looping = toggle;
166165
if (!m_looping) {
167166
if (BASS_ChannelFlags(m_stream_list[channel], 0, 0) & BASS_SAMPLE_LOOP)

src/courtroom.cpp

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

1414
keepalive_timer = new QTimer(this);
15-
keepalive_timer->start(60000);
15+
keepalive_timer->start(45000);
1616

1717
chat_tick_timer = new QTimer(this);
1818

@@ -120,6 +120,12 @@ Courtroom::Courtroom(AOApplication *p_ao_app) : QMainWindow()
120120
ui_music_name = new ScrollText(ui_music_display);
121121
ui_music_name->setText(tr("None"));
122122
ui_music_name->setAttribute(Qt::WA_TransparentForMouseEvents);
123+
124+
for (int i = 0; i < max_clocks; i++) {
125+
ui_clock[i] = new AOClockLabel(this);
126+
ui_clock[i]->setAttribute(Qt::WA_TransparentForMouseEvents);
127+
ui_clock[i]->hide();
128+
}
123129

124130
ui_ic_chat_name = new QLineEdit(this);
125131
ui_ic_chat_name->setFrame(false);
@@ -663,6 +669,10 @@ void Courtroom::set_widgets()
663669
ui_music_display->load_image("music_display", "");
664670

665671

672+
for (int i = 0; i < max_clocks; i++) {
673+
set_size_and_pos(ui_clock[i], "clock_" + QString::number(i));
674+
}
675+
666676
if (is_ao2_bg) {
667677
set_size_and_pos(ui_ic_chat_message, "ao2_ic_chat_message");
668678
// set_size_and_pos(ui_vp_chatbox, "ao2_chatbox");
@@ -1016,6 +1026,9 @@ void Courtroom::set_fonts(QString p_char)
10161026
set_font(ui_area_list, "", "area_list", p_char);
10171027
set_font(ui_music_name, "", "music_name", p_char);
10181028

1029+
for (int i = 0; i < max_clocks; i++)
1030+
set_font(ui_clock[i], "", "clock_" + QString::number(i), p_char);
1031+
10191032
set_dropdowns();
10201033
}
10211034

@@ -5266,10 +5279,21 @@ void Courtroom::on_switch_area_music_clicked()
52665279

52675280
void Courtroom::ping_server()
52685281
{
5282+
ping_timer.start();
5283+
is_pinging = true;
52695284
ao_app->send_server_packet(
52705285
new AOPacket("CH#" + QString::number(m_cid) + "#%"));
52715286
}
52725287

5288+
qint64 Courtroom::pong()
5289+
{
5290+
if (!is_pinging)
5291+
return -1;
5292+
5293+
is_pinging = false;
5294+
return ping_timer.elapsed();
5295+
}
5296+
52735297
void Courtroom::on_casing_clicked()
52745298
{
52755299
if (ao_app->casing_alerts_enabled) {
@@ -5309,6 +5333,54 @@ void Courtroom::announce_case(QString title, bool def, bool pro, bool jud,
53095333
}
53105334
}
53115335

5336+
void Courtroom::start_clock(int id)
5337+
{
5338+
if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr)
5339+
{
5340+
ui_clock[id]->start();
5341+
}
5342+
}
5343+
5344+
void Courtroom::start_clock(int id, qint64 msecs)
5345+
{
5346+
if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr)
5347+
{
5348+
ui_clock[id]->start(static_cast<int>(msecs));
5349+
}
5350+
}
5351+
5352+
void Courtroom::set_clock(int id, qint64 msecs)
5353+
{
5354+
if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr)
5355+
{
5356+
ui_clock[id]->set(static_cast<int>(msecs), true);
5357+
}
5358+
}
5359+
5360+
void Courtroom::pause_clock(int id)
5361+
{
5362+
if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr)
5363+
{
5364+
ui_clock[id]->pause();
5365+
}
5366+
}
5367+
5368+
void Courtroom::stop_clock(int id)
5369+
{
5370+
if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr)
5371+
{
5372+
ui_clock[id]->stop();
5373+
}
5374+
}
5375+
5376+
void Courtroom::set_clock_visibility(int id, bool visible)
5377+
{
5378+
if (id >= 0 && id < max_clocks && ui_clock[id] != nullptr)
5379+
{
5380+
ui_clock[id]->setVisible(visible);
5381+
}
5382+
}
5383+
53125384
void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier)
53135385
{
53145386
QString filename = "courtroom_design.ini";

src/packet_distribution.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
260260

261261
courtroom_loaded = false;
262262

263-
QString window_title = tr("Attorney Online 2");
263+
window_title = tr("Attorney Online 2");
264264
int selected_server = w_lobby->get_selected_server();
265265

266266
QString server_address = "", server_name = "";
@@ -604,6 +604,60 @@ void AOApplication::server_packet_received(AOPacket *p_packet)
604604
f_contents.at(4) == "1",
605605
f_contents.at(5) == "1");
606606
}
607+
else if (header == "TI") { // Timer packet
608+
if (!courtroom_constructed || f_contents.size() < 2)
609+
goto end;
610+
611+
// Timer ID is reserved as argument 0
612+
int id = f_contents.at(0).toInt();
613+
614+
// Type 0 = start/resume/sync timer at time
615+
// Type 1 = pause timer at time
616+
// Type 2 = show timer
617+
// Type 3 = hide timer
618+
int type = f_contents.at(1).toInt();
619+
620+
if (type == 0 || type == 1)
621+
{
622+
if (f_contents.size() < 2)
623+
goto end;
624+
625+
// The time as displayed on the clock, in milliseconds.
626+
// If the number received is negative, stop the timer.
627+
qint64 timer_value = f_contents.at(2).toLongLong();
628+
qDebug() << "timer:" << timer_value;
629+
if (timer_value > 0)
630+
{
631+
if (type == 0)
632+
{
633+
timer_value -= latency / 2;
634+
w_courtroom->start_clock(id, timer_value);
635+
}
636+
else
637+
{
638+
w_courtroom->pause_clock(id);
639+
w_courtroom->set_clock(id, timer_value);
640+
}
641+
}
642+
else
643+
{
644+
w_courtroom->stop_clock(id);
645+
}
646+
}
647+
else if (type == 2)
648+
w_courtroom->set_clock_visibility(id, true);
649+
else if (type == 3)
650+
w_courtroom->set_clock_visibility(id, false);
651+
}
652+
else if (header == "CHECK") {
653+
if (!courtroom_constructed)
654+
goto end;
655+
656+
qint64 ping_time = w_courtroom->pong();
657+
qDebug() << "ping:" << ping_time;
658+
if (ping_time != -1)
659+
latency = ping_time;
660+
}
607661

608662
end:
609663

0 commit comments

Comments
 (0)