]> git.sesse.net Git - ultimatescore/blob - client/mainwindow.cpp
Make a combo box for preselecting teams.
[ultimatescore] / client / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "post_to_main_thread.h"
3 #include "ui_mainwindow.h"
4
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12
13 using namespace std;
14
15 string escape_html(const string &str)
16 {
17         string s = "";
18         for (char ch : str) {
19                 if (ch == '<') {
20                         s += "&lt;";
21                 } else if (ch == '>') {
22                         s += "&gt;";
23                 } else if (ch == '&') {
24                         s += "&amp;";
25                 } else {
26                         s += ch;
27                 }
28         }
29         return s;
30 }
31
32 string escape_unicode(const string &str)
33 {
34         string s = "";
35         for (size_t pos = 0; pos < str.size(); ) {
36                 wchar_t wc;
37                 int len = mbtowc(&wc, str.data() + pos, str.size() - pos);
38                 if (len == -1) {
39                         wc = '?';
40                         len = 1;
41                 }
42                 pos += len;
43
44                 if (wc == '\\') {
45                         s += "\\\\";
46                 } else if (isprint(wc)) {
47                         s += wc;
48                 } else {
49                         char buf[16];
50                         snprintf(buf, sizeof(buf), "\\u%04x", wc);
51                         s += buf;
52                 }
53         }
54         return s;
55 }
56
57 string escape_quotes(const string &str)
58 {
59         string s = "";
60         for (char ch : str) {
61                 if (ch == '"') {
62                         s += '\\';
63                 }
64                 s += ch;
65         }
66         return s;
67 }
68
69 string serialize_as_json(const map<string, string> &param)
70 {
71         string s = "{";
72
73         bool first = true;
74         for (const auto &key_value : param) {
75                 if (!first) s += ", ";
76                 first = false;
77
78                 s += '"';
79                 s += escape_quotes(escape_unicode(key_value.first));
80                 s += "\": \"";
81                 s += escape_quotes(escape_unicode(key_value.second));
82                 s += '"';
83         }
84         s += "}";
85         return s;       
86 }
87
88 MainWindow::MainWindow(QWidget *parent) :
89     QMainWindow(parent),
90     ui(new Ui::MainWindow)
91 {
92         ui->setupUi(this);
93         for (QComboBox *team_combo : { ui->initials_1_edit, ui->initials_2_edit, ui->match_2_initials_1_edit, ui->match_2_initials_2_edit, ui->match_3_initials_1_edit, ui->match_3_initials_2_edit }) {
94                 team_combo->addItem("BFK");
95                 team_combo->addItem("BRI");
96                 team_combo->addItem("BSI");
97                 team_combo->addItem("ESK");
98                 team_combo->addItem("GRI");
99                 team_combo->addItem("NMBUI");
100                 team_combo->addItem("OBSI");
101                 team_combo->addItem("OSI");
102                 team_combo->addItem("PCL1");
103                 team_combo->addItem("PCL2");
104                 team_combo->addItem("TFK");
105                 team_combo->addItem("TSI");
106                 team_combo->addItem("UUJJ");
107         }
108
109         ws = new WSServer("127.0.0.1", 5250);
110         ws->set_connection_callback([this](bool connected) {
111                 string msg = connected ? "Connected" : "Not connected";
112                 post_to_main_thread([this, msg]() {
113                         ui->ws_connected_label->setText(QString::fromStdString(msg));
114                 });
115         });
116
117         udp_thread = std::thread(&MainWindow::udp_thread_func, this, 6000);
118         udp_thread2 = std::thread(&MainWindow::udp_thread_func, this, 6001);
119         udp_thread3 = std::thread(&MainWindow::udp_thread_func, this, 6002);
120
121         connect(ui->ws_disconnect_btn, &QPushButton::clicked, this, &MainWindow::ws_disconnect_clicked);
122         connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
123         connect(ui->set_match_2_initials_btn, &QPushButton::clicked, this, &MainWindow::set_match_2_initials_clicked);
124         connect(ui->set_match_3_initials_btn, &QPushButton::clicked, this, &MainWindow::set_match_3_initials_clicked);
125         connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
126         connect(ui->set_score_btn, &QPushButton::clicked, this, &MainWindow::set_score_clicked);
127         connect(ui->set_all_scorebug_btn, &QPushButton::clicked, this, &MainWindow::set_all_scorebug_clicked);
128         connect(ui->goal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, 1); });
129         connect(ui->ungoal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, -1); });
130         connect(ui->goal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, 1); });
131         connect(ui->ungoal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, -1); });
132
133         connect(ui->set_clock_btn, &QPushButton::clicked, this, &MainWindow::set_clock_clicked);
134         connect(ui->set_clock_limit_btn, &QPushButton::clicked, this, &MainWindow::set_clock_limit_clicked);
135         connect(ui->start_and_show_clock_btn, &QPushButton::clicked, this, &MainWindow::start_and_show_clock_clicked);
136         connect(ui->stop_clock_btn, &QPushButton::clicked, this, &MainWindow::stop_clock_clicked);
137         connect(ui->show_clock_btn, &QPushButton::clicked, this, &MainWindow::show_clock_clicked);
138         connect(ui->hide_clock_btn, &QPushButton::clicked, this, &MainWindow::hide_clock_clicked);
139         connect(ui->show_match_2_btn, &QPushButton::clicked, this, &MainWindow::show_match_2_clicked);
140         connect(ui->hide_match_2_btn, &QPushButton::clicked, this, &MainWindow::hide_match_2_clicked);
141         connect(ui->show_match_3_btn, &QPushButton::clicked, this, &MainWindow::show_match_3_clicked);
142         connect(ui->hide_match_3_btn, &QPushButton::clicked, this, &MainWindow::hide_match_3_clicked);
143
144         connect(ui->set_comment_btn, &QPushButton::clicked, this, &MainWindow::set_comment_clicked);
145         connect(ui->set_and_show_comment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_comment_clicked);
146         connect(ui->hide_comment_btn, &QPushButton::clicked, this, &MainWindow::hide_comment_clicked);
147         connect(ui->set_and_show_autocomment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_autocomment_clicked);
148         connect(ui->autoshow_autocomment, &QCheckBox::stateChanged, this, &MainWindow::autocomment_update);
149
150         connect(ui->show_lower_third_btn, &QPushButton::clicked, this, &MainWindow::show_lower_third_clicked);
151         connect(ui->hide_lower_third_btn, &QPushButton::clicked, this, &MainWindow::hide_lower_third_clicked);
152
153         connect(ui->quick_lower_third_edit, &QLineEdit::returnPressed, this, &MainWindow::quick_lower_third_activate);
154         connect(ui->show_quick_lower_third_btn, &QPushButton::clicked, this, &MainWindow::quick_lower_third_activate);
155
156         connect(ui->show_scorebug_btn, &QPushButton::clicked, this, &MainWindow::show_scorebug_clicked);
157         connect(ui->show_group_a_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group A"); });
158         connect(ui->show_group_b_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group B"); });
159         connect(ui->show_group_c_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group C"); });
160         connect(ui->show_schedule_btn, &QPushButton::clicked, this, &MainWindow::show_schedule_clicked);
161         connect(ui->show_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_carousel_clicked);
162         connect(ui->show_nothing_btn, &QPushButton::clicked, this, &MainWindow::show_nothing_clicked);
163         connect(ui->show_roster_1_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_1_edit->currentText().toStdString()); });
164         connect(ui->show_roster_2_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_2_edit->currentText().toStdString()); });
165         connect(ui->show_roster_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_roster_carousel_clicked);
166
167         autocomment_update();
168
169         const set<pair<unsigned, unsigned>> usb{{ 0x0e8f, 0x0041 }};
170         event_device = new EventDevice(usb, ui->quick_lower_third_edit);
171         event_device->start_thread();
172 }
173
174 MainWindow::~MainWindow()
175 {
176         delete ui;
177 }
178
179 void MainWindow::ws_disconnect_clicked()
180 {
181         ws->change_port(stoi(ui->ws_port_box->text().toStdString()));
182 }
183
184 void MainWindow::set_initials_clicked()
185 {
186         map<string, string> param;
187         param["team1"] = escape_html(ui->initials_1_edit->currentText().toStdString());
188         param["team2"] = escape_html(ui->initials_2_edit->currentText().toStdString());
189         ws->send_command("update " + serialize_as_json(param));
190         ws->send_command("eval setteams()");
191 }
192
193 void MainWindow::set_match_2_initials_clicked()
194 {
195         map<string, string> param;
196         param["team1"] = escape_html(ui->match_2_initials_1_edit->currentText().toStdString());
197         param["team2"] = escape_html(ui->match_2_initials_2_edit->currentText().toStdString());
198         ws->send_command("update " + serialize_as_json(param));
199         ws->send_command("eval setteams2()");
200 }
201
202 void MainWindow::set_match_3_initials_clicked()
203 {
204         map<string, string> param;
205         param["team1"] = escape_html(ui->match_3_initials_1_edit->currentText().toStdString());
206         param["team2"] = escape_html(ui->match_3_initials_2_edit->currentText().toStdString());
207         ws->send_command("update " + serialize_as_json(param));
208         ws->send_command("eval setteams3()");
209 }
210
211 void MainWindow::set_color_clicked()
212 {
213         map<string, string> param;
214         param["team1color"] = ui->color_1_edit->text().toStdString();  // Should maybe be escaped, but meh.
215         param["team2color"] = ui->color_2_edit->text().toStdString();
216         ws->send_command("update " + serialize_as_json(param));
217         ws->send_command("eval setcolors()");
218 }
219
220 void MainWindow::set_score_clicked()
221 {
222         map<string, string> param;
223         param["score1"] = to_string(ui->score_1_box->value());
224         param["score2"] = to_string(ui->score_2_box->value());
225         ws->send_command("update " + serialize_as_json(param));
226         ws->send_command("eval setscore()");
227         autocomment_update();
228 }
229
230 void MainWindow::set_all_scorebug_clicked()
231 {
232         set_initials_clicked();
233         set_color_clicked();
234         set_score_clicked();
235 }
236
237 void MainWindow::add_goal(QSpinBox *box, int delta)
238 {
239         box->setValue(box->value() + delta);
240         set_score_clicked();
241 }
242
243 void MainWindow::set_clock_clicked()
244 {
245         map<string, string> param;
246         param["clock_min"] = to_string(ui->clock_min_box->value());
247         param["clock_sec"] = to_string(ui->clock_sec_box->value());
248         ws->send_command("update " + serialize_as_json(param));
249         ws->send_command("eval setclockfromstate()");
250 }
251
252 void MainWindow::set_clock_limit_clicked()
253 {
254         map<string, string> param;
255         param["clock_limit_min"] = to_string(ui->clock_limit_min_box->value());
256         param["clock_limit_sec"] = to_string(ui->clock_limit_sec_box->value());
257         ws->send_command("update " + serialize_as_json(param));
258         ws->send_command("eval setclocklimitfromstate()");
259 }
260
261 void MainWindow::start_and_show_clock_clicked()
262 {
263         ws->send_command("eval startclock(0)");  // Also shows.
264 }
265
266 void MainWindow::stop_clock_clicked()
267 {
268         ws->send_command("eval stopclock(0)");
269 }
270
271 void MainWindow::show_clock_clicked()
272 {
273         ws->send_command("eval showclock()");
274 }
275
276 void MainWindow::hide_clock_clicked()
277 {
278         ws->send_command("eval hideclock()");
279 }
280
281 void MainWindow::show_match_2_clicked()
282 {
283         ws->send_command("eval showmatch2()");
284 }
285
286 void MainWindow::hide_match_2_clicked()
287 {
288         ws->send_command("eval hidematch2()");
289 }
290
291 void MainWindow::show_match_3_clicked()
292 {
293         ws->send_command("eval showmatch3()");
294 }
295
296 void MainWindow::hide_match_3_clicked()
297 {
298         ws->send_command("eval hidematch3()");
299 }
300
301 void MainWindow::set_comment_clicked()
302 {
303         map<string, string> param;
304         param["comment"] = ui->comment_edit->text().toStdString();
305         ws->send_command("update " + serialize_as_json(param));
306         ws->send_command("eval setcomment()");
307 }
308
309 void MainWindow::set_and_show_comment_clicked()
310 {
311         set_comment_clicked();
312         ws->send_command("eval showcomment()");
313 }
314
315 void MainWindow::set_and_show_autocomment_clicked()
316 {
317         ui->comment_edit->setText(ui->autocomment_edit->text());
318         set_and_show_comment_clicked();
319 }
320
321 void MainWindow::hide_comment_clicked()
322 {
323         ws->send_command("eval hidecomment()");
324 }
325
326 void MainWindow::show_lower_third_clicked()
327 {
328         map<string, string> param;
329         param["text1"] = ui->lowerthird_heading_edit->text().toStdString();
330         param["text2"] = ui->lowerthird_subheading_edit->text().toStdString();
331         ws->send_command("update " + serialize_as_json(param));
332         ws->send_command("eval setandshowlowerthird()");
333 }
334
335 void MainWindow::hide_lower_third_clicked()
336 {
337         ws->send_command("eval hidelowerthird()");
338 }
339
340 void MainWindow::quick_lower_third_activate()
341 {
342         string code = ui->quick_lower_third_edit->text().toUpper().toStdString();
343         if (code == "A") {
344                 add_goal(ui->score_1_box, 1);
345         } else if (code == "B") {
346                 add_goal(ui->score_2_box, 1);
347         } else if (code == "C") {
348                 ws->send_command("eval hidelowerthird()");
349         } else {
350                 map<string, string> param;
351                 param["code"] = code;
352                 ws->send_command("update " + serialize_as_json(param));
353                 ws->send_command("eval quicklowerthird()");
354         }
355         ui->quick_lower_third_edit->clear();
356 }
357
358 void MainWindow::autocomment_update()
359 {
360         int score1 = ui->score_1_box->value();
361         int score2 = ui->score_2_box->value();
362         string msg;
363         if (abs(score1 - score2) >= 3) {
364                 msg = "Game ends after this point";
365         } else {
366                 int cap = max(score1, score2) + 1;
367                 if (score1 == score2) ++cap;
368
369                 if (cap >= 13) {
370                         msg = "Point cap: First to 13";
371                 } else {
372                         char buf[32];
373                         snprintf(buf, sizeof(buf), "Pagacap: First to %d", cap);
374                         msg = buf;
375                 }
376         }
377         ui->autocomment_edit->setText(QString::fromStdString(msg));
378
379         map<string, string> param;
380         param["autocomment_on_clock_limit"] = ui->autoshow_autocomment->isChecked() ? "1" : "0";
381         param["autocomment"] = msg;
382         ws->send_command("update " + serialize_as_json(param));
383 }
384
385 void MainWindow::show_scorebug_clicked()
386 {
387         ws->send_command("eval stopcarousel()");
388         ws->send_command("eval hidetable()");
389         ws->send_command("eval showscorebug()");
390 }
391
392 void MainWindow::show_group_clicked(const std::string &group_name)
393 {
394         map<string, string> param;
395         param["group_name"] = group_name;
396         ws->send_command("eval stopcarousel()");
397         ws->send_command("update " + serialize_as_json(param));
398         ws->send_command("eval showgroup_from_state()");
399 }
400
401 void MainWindow::show_roster_clicked(const std::string &team_code)
402 {
403         map<string, string> param;
404         param["team_code"] = team_code;
405         ws->send_command("eval stopcarousel()");
406         ws->send_command("update " + serialize_as_json(param));
407         ws->send_command("eval showroster_from_state()");
408 }
409
410 void MainWindow::show_schedule_clicked()
411 {
412         ws->send_command("eval stopcarousel()");
413         ws->send_command("eval showschedule()");
414 }
415
416 void MainWindow::show_carousel_clicked()
417 {
418         ws->send_command("eval stopcarousel()");
419         ws->send_command("eval showcarousel()");
420 }
421
422 void MainWindow::show_roster_carousel_clicked()
423 {
424         map<string, string> param;
425         param["team1"] = escape_html(ui->initials_1_edit->currentText().toStdString());
426         param["team2"] = escape_html(ui->initials_2_edit->currentText().toStdString());
427         ws->send_command("eval stopcarousel()");
428         ws->send_command("update " + serialize_as_json(param));
429         ws->send_command("eval showrostercarousel_from_state()");
430 }
431
432 void MainWindow::show_nothing_clicked()
433 {
434         ws->send_command("eval hidescorebug()");
435         ws->send_command("eval stopcarousel()");
436         ws->send_command("eval hidetable()");
437 }
438
439 void udp_thread_nat_func(int sock, int port)
440 {
441         sockaddr_in6 saddr6;
442         memset(&saddr6, 0, sizeof(saddr6));
443         saddr6.sin6_family = AF_INET6;
444         inet_pton(AF_INET6, "::ffff:193.35.52.50", &saddr6.sin6_addr);
445         saddr6.sin6_port = htons(port);
446
447         for ( ;; ) {
448                 char buf[] = "ping";
449                 sendto(sock, buf, 4, 0, (sockaddr *)&saddr6, sizeof(saddr6));
450                 sleep(1);
451         }
452 }
453
454 void MainWindow::udp_thread_func(int port)
455 {
456         int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
457         if (sock == -1) {
458                 perror("socket");
459                 exit(1);
460         }
461
462         int one = 1;
463         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
464                 perror("setsockopt");
465                 exit(1);
466         }
467
468         sockaddr_in6 saddr6;
469         memset(&saddr6, 0, sizeof(saddr6));
470         saddr6.sin6_family = AF_INET6;
471         inet_pton(AF_INET6, "::", &saddr6.sin6_addr);
472         saddr6.sin6_port = htons(port);
473         if (bind(sock, (sockaddr *)&saddr6, sizeof(saddr6)) == -1) {
474                 perror("bind");
475                 exit(1);
476         }
477
478         std::thread(&udp_thread_nat_func, sock, port + 1000).detach();
479
480         for ( ;; ) {
481                 char buf[4096];
482                 int err = recv(sock, buf, sizeof(buf), 0);
483                 if (err == -1) {
484                         perror("recv");
485                         exit(1);
486                 }
487
488                 post_to_main_thread([buf, err, port, this] {
489                         bt6000_message_received(string(buf, err), port);
490                 });
491         }
492 }
493
494 int parse_digit(char ch)
495 {
496         if (ch >= '0' && ch <= '9') {
497                 return ch - '0';
498         }
499         return 0;
500 }
501
502 int parse_clock(char ch1, char ch2)
503 {
504         int s1 = parse_digit(ch1);
505         int s2 = parse_digit(ch2);
506         return s1 * 10 + s2;
507 }
508
509 int parse_score(char ch1, char ch2, char ch3)
510 {
511         int s1 = parse_digit(ch1);
512         int s2 = parse_digit(ch2);
513         int s3 = parse_digit(ch3);
514         return s1 * 100 + s2 * 10 + s3;
515 }
516
517 void MainWindow::bt6000_message_received(const string &msg, int port)
518 {
519         fprintf(stderr, "BT6000 message: '%s' (port %d)\n", msg.c_str(), port);
520         if (port == 6002) {
521                 if (!ui->bt6000_3_enable->isChecked()) {
522                         return;
523                 }
524         } else if (port == 6001) {
525                 if (!ui->bt6000_2_enable->isChecked()) {
526                         return;
527                 }
528         } else {
529                 if (!ui->bt6000_enable->isChecked()) {
530                         return;
531                 }
532         }
533
534         if (msg.size() >= 9 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '1') {
535                 // G01: Game clock, period number, and number of time-outs.
536                 bool clock_running = !(msg[3] & 0x02);
537 //              bool klaxon = (msg[3] & 0x04);
538                 int minutes = parse_clock(msg[5], msg[6]);
539                 int seconds = parse_clock(msg[7], msg[8]);
540
541                 map<string, string> param;
542                 param["clock_min"] = to_string(minutes);
543                 param["clock_sec"] = to_string(seconds);
544                 ws->send_command("update " + serialize_as_json(param));
545
546                 if (port == 6002) {
547                         ws->send_command("eval adjustclockfromstate(2)");
548                         if (clock_running) {
549                                 ws->send_command("eval startclock(2)");
550                         } else {
551                                 ws->send_command("eval stopclock(2)");
552                         }
553                 } else if (port == 6001) {
554                         ws->send_command("eval adjustclockfromstate(1)");
555                         if (clock_running) {
556                                 ws->send_command("eval startclock(1)");
557                         } else {
558                                 ws->send_command("eval stopclock(1)");
559                         }
560                 } else {
561                         ws->send_command("eval adjustclockfromstate(0)");
562                         if (clock_running) {
563                                 ws->send_command("eval startclock(0)");
564                         } else {
565                                 ws->send_command("eval stopclock(0)");
566                         }
567                 }
568         }
569         if (msg.size() >= 10 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '2') {
570                 int score1 = parse_score(msg[4], msg[5], msg[6]);
571                 int score2 = parse_score(msg[7], msg[8], msg[9]);
572                 if (port == 6002) {
573                         map<string, string> param;
574                         param["score1"] = to_string(score1);
575                         param["score2"] = to_string(score2);
576                         ws->send_command("update " + serialize_as_json(param));
577                         ws->send_command("eval setscore3()");
578                 } else if (port == 6001) {
579                         map<string, string> param;
580                         param["score1"] = to_string(score1);
581                         param["score2"] = to_string(score2);
582                         ws->send_command("update " + serialize_as_json(param));
583                         ws->send_command("eval setscore2()");
584                 } else {
585                         ui->score_1_box->setValue(score1);
586                         ui->score_2_box->setValue(score2);
587                         set_score_clicked();
588                 }
589         }
590
591         // Ignore type 3 (penalties) and type 4 (timeouts).
592 }