@@ -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
756779end:
0 commit comments