]> git.sesse.net Git - ultimatescore/blobdiff - client/mainwindow.cpp
Add a comment about what “Mark game” means.
[ultimatescore] / client / mainwindow.cpp
index 6b8f7136e06c41087bb1fc393145897a036fc44a..b6294cf059987dfcabb193170b3e80e5994a5a0f 100644 (file)
@@ -2,7 +2,13 @@
 #include "post_to_main_thread.h"
 #include "ui_mainwindow.h"
 
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
 
 using namespace std;
 
@@ -92,8 +98,14 @@ MainWindow::MainWindow(QWidget *parent) :
                });
        });
 
+       udp_thread = std::thread(&MainWindow::udp_thread_func, this, 6000);
+       udp_thread2 = std::thread(&MainWindow::udp_thread_func, this, 6001);
+       udp_thread3 = std::thread(&MainWindow::udp_thread_func, this, 6002);
+
        connect(ui->ws_disconnect_btn, &QPushButton::clicked, this, &MainWindow::ws_disconnect_clicked);
        connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
+       connect(ui->set_match_2_initials_btn, &QPushButton::clicked, this, &MainWindow::set_match_2_initials_clicked);
+       connect(ui->set_match_3_initials_btn, &QPushButton::clicked, this, &MainWindow::set_match_3_initials_clicked);
        connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
        connect(ui->set_score_btn, &QPushButton::clicked, this, &MainWindow::set_score_clicked);
        connect(ui->set_all_scorebug_btn, &QPushButton::clicked, this, &MainWindow::set_all_scorebug_clicked);
@@ -108,11 +120,16 @@ MainWindow::MainWindow(QWidget *parent) :
        connect(ui->stop_clock_btn, &QPushButton::clicked, this, &MainWindow::stop_clock_clicked);
        connect(ui->show_clock_btn, &QPushButton::clicked, this, &MainWindow::show_clock_clicked);
        connect(ui->hide_clock_btn, &QPushButton::clicked, this, &MainWindow::hide_clock_clicked);
+       connect(ui->show_match_2_btn, &QPushButton::clicked, this, &MainWindow::show_match_2_clicked);
+       connect(ui->hide_match_2_btn, &QPushButton::clicked, this, &MainWindow::hide_match_2_clicked);
+       connect(ui->show_match_3_btn, &QPushButton::clicked, this, &MainWindow::show_match_3_clicked);
+       connect(ui->hide_match_3_btn, &QPushButton::clicked, this, &MainWindow::hide_match_3_clicked);
 
        connect(ui->set_comment_btn, &QPushButton::clicked, this, &MainWindow::set_comment_clicked);
        connect(ui->set_and_show_comment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_comment_clicked);
        connect(ui->hide_comment_btn, &QPushButton::clicked, this, &MainWindow::hide_comment_clicked);
        connect(ui->set_and_show_autocomment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_autocomment_clicked);
+       connect(ui->autoshow_autocomment, &QCheckBox::stateChanged, this, &MainWindow::autocomment_update);
 
        connect(ui->show_lower_third_btn, &QPushButton::clicked, this, &MainWindow::show_lower_third_clicked);
        connect(ui->hide_lower_third_btn, &QPushButton::clicked, this, &MainWindow::hide_lower_third_clicked);
@@ -123,6 +140,7 @@ MainWindow::MainWindow(QWidget *parent) :
        connect(ui->show_scorebug_btn, &QPushButton::clicked, this, &MainWindow::show_scorebug_clicked);
        connect(ui->show_group_a_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group A"); });
        connect(ui->show_group_b_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group B"); });
+       connect(ui->show_group_c_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group C"); });
        connect(ui->show_schedule_btn, &QPushButton::clicked, this, &MainWindow::show_schedule_clicked);
        connect(ui->show_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_carousel_clicked);
        connect(ui->show_nothing_btn, &QPushButton::clicked, this, &MainWindow::show_nothing_clicked);
@@ -156,6 +174,24 @@ void MainWindow::set_initials_clicked()
        ws->send_command("eval setteams()");
 }
 
+void MainWindow::set_match_2_initials_clicked()
+{
+       map<string, string> param;
+       param["team1"] = escape_html(ui->match_2_initials_1_edit->text().toStdString());
+       param["team2"] = escape_html(ui->match_2_initials_2_edit->text().toStdString());
+       ws->send_command("update " + serialize_as_json(param));
+       ws->send_command("eval setteams2()");
+}
+
+void MainWindow::set_match_3_initials_clicked()
+{
+       map<string, string> param;
+       param["team1"] = escape_html(ui->match_3_initials_1_edit->text().toStdString());
+       param["team2"] = escape_html(ui->match_3_initials_2_edit->text().toStdString());
+       ws->send_command("update " + serialize_as_json(param));
+       ws->send_command("eval setteams3()");
+}
+
 void MainWindow::set_color_clicked()
 {
        map<string, string> param;
@@ -208,12 +244,12 @@ void MainWindow::set_clock_limit_clicked()
 
 void MainWindow::start_and_show_clock_clicked()
 {
-       ws->send_command("eval startclock()");  // Also shows.
+       ws->send_command("eval startclock(0)");  // Also shows.
 }
 
 void MainWindow::stop_clock_clicked()
 {
-       ws->send_command("eval stopclock()");
+       ws->send_command("eval stopclock(0)");
 }
 
 void MainWindow::show_clock_clicked()
@@ -226,6 +262,26 @@ void MainWindow::hide_clock_clicked()
        ws->send_command("eval hideclock()");
 }
 
+void MainWindow::show_match_2_clicked()
+{
+       ws->send_command("eval showmatch2()");
+}
+
+void MainWindow::hide_match_2_clicked()
+{
+       ws->send_command("eval hidematch2()");
+}
+
+void MainWindow::show_match_3_clicked()
+{
+       ws->send_command("eval showmatch3()");
+}
+
+void MainWindow::hide_match_3_clicked()
+{
+       ws->send_command("eval hidematch3()");
+}
+
 void MainWindow::set_comment_clicked()
 {
        map<string, string> param;
@@ -303,6 +359,11 @@ void MainWindow::autocomment_update()
                }
        }
        ui->autocomment_edit->setText(QString::fromStdString(msg));
+
+       map<string, string> param;
+       param["autocomment_on_clock_limit"] = ui->autoshow_autocomment->isChecked() ? "1" : "0";
+       param["autocomment"] = msg;
+       ws->send_command("update " + serialize_as_json(param));
 }
 
 void MainWindow::show_scorebug_clicked()
@@ -358,3 +419,158 @@ void MainWindow::show_nothing_clicked()
        ws->send_command("eval stopcarousel()");
        ws->send_command("eval hidetable()");
 }
+
+void udp_thread_nat_func(int sock, int port)
+{
+       sockaddr_in6 saddr6;
+       memset(&saddr6, 0, sizeof(saddr6));
+       saddr6.sin6_family = AF_INET6;
+       inet_pton(AF_INET6, "::ffff:193.35.52.50", &saddr6.sin6_addr);
+       saddr6.sin6_port = htons(port);
+
+       for ( ;; ) {
+               char buf[] = "ping";
+               sendto(sock, buf, 4, 0, (sockaddr *)&saddr6, sizeof(saddr6));
+               sleep(1);
+       }
+}
+
+void MainWindow::udp_thread_func(int port)
+{
+       int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
+       if (sock == -1) {
+               perror("socket");
+               exit(1);
+       }
+
+       int one = 1;
+       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
+               perror("setsockopt");
+               exit(1);
+       }
+
+       sockaddr_in6 saddr6;
+       memset(&saddr6, 0, sizeof(saddr6));
+       saddr6.sin6_family = AF_INET6;
+       inet_pton(AF_INET6, "::", &saddr6.sin6_addr);
+       saddr6.sin6_port = htons(port);
+       if (bind(sock, (sockaddr *)&saddr6, sizeof(saddr6)) == -1) {
+               perror("bind");
+               exit(1);
+       }
+
+       std::thread(&udp_thread_nat_func, sock, port + 1000).detach();
+
+       for ( ;; ) {
+               char buf[4096];
+               int err = recv(sock, buf, sizeof(buf), 0);
+               if (err == -1) {
+                       perror("recv");
+                       exit(1);
+               }
+
+               post_to_main_thread([buf, err, port, this] {
+                       bt6000_message_received(string(buf, err), port);
+               });
+       }
+}
+
+int parse_digit(char ch)
+{
+       if (ch >= '0' && ch <= '9') {
+               return ch - '0';
+       }
+       return 0;
+}
+
+int parse_clock(char ch1, char ch2)
+{
+       int s1 = parse_digit(ch1);
+       int s2 = parse_digit(ch2);
+       return s1 * 10 + s2;
+}
+
+int parse_score(char ch1, char ch2, char ch3)
+{
+       int s1 = parse_digit(ch1);
+       int s2 = parse_digit(ch2);
+       int s3 = parse_digit(ch3);
+       return s1 * 100 + s2 * 10 + s3;
+}
+
+void MainWindow::bt6000_message_received(const string &msg, int port)
+{
+       fprintf(stderr, "BT6000 message: '%s' (port %d)\n", msg.c_str(), port);
+       if (port == 6002) {
+               if (!ui->bt6000_3_enable->isChecked()) {
+                       return;
+               }
+       } else if (port == 6001) {
+               if (!ui->bt6000_2_enable->isChecked()) {
+                       return;
+               }
+       } else {
+               if (!ui->bt6000_enable->isChecked()) {
+                       return;
+               }
+       }
+
+       if (msg.size() >= 9 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '1') {
+               // G01: Game clock, period number, and number of time-outs.
+               bool clock_running = !(msg[3] & 0x02);
+//             bool klaxon = (msg[3] & 0x04);
+               int minutes = parse_clock(msg[5], msg[6]);
+               int seconds = parse_clock(msg[7], msg[8]);
+
+               map<string, string> param;
+               param["clock_min"] = to_string(minutes);
+               param["clock_sec"] = to_string(seconds);
+               ws->send_command("update " + serialize_as_json(param));
+
+               if (port == 6002) {
+                       ws->send_command("eval adjustclockfromstate(2)");
+                       if (clock_running) {
+                               ws->send_command("eval startclock(2)");
+                       } else {
+                               ws->send_command("eval stopclock(2)");
+                       }
+               } else if (port == 6001) {
+                       ws->send_command("eval adjustclockfromstate(1)");
+                       if (clock_running) {
+                               ws->send_command("eval startclock(1)");
+                       } else {
+                               ws->send_command("eval stopclock(1)");
+                       }
+               } else {
+                       ws->send_command("eval adjustclockfromstate(0)");
+                       if (clock_running) {
+                               ws->send_command("eval startclock(0)");
+                       } else {
+                               ws->send_command("eval stopclock(0)");
+                       }
+               }
+       }
+       if (msg.size() >= 10 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '2') {
+               int score1 = parse_score(msg[4], msg[5], msg[6]);
+               int score2 = parse_score(msg[7], msg[8], msg[9]);
+               if (port == 6002) {
+                       map<string, string> param;
+                       param["score1"] = to_string(score1);
+                       param["score2"] = to_string(score2);
+                       ws->send_command("update " + serialize_as_json(param));
+                       ws->send_command("eval setscore3()");
+               } else if (port == 6001) {
+                       map<string, string> param;
+                       param["score1"] = to_string(score1);
+                       param["score2"] = to_string(score2);
+                       ws->send_command("update " + serialize_as_json(param));
+                       ws->send_command("eval setscore2()");
+               } else {
+                       ui->score_1_box->setValue(score1);
+                       ui->score_2_box->setValue(score2);
+                       set_score_clicked();
+               }
+       }
+
+       // Ignore type 3 (penalties) and type 4 (timeouts).
+}