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