]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/stand_server.cpp
use app name instead of title for prefs path
[taylor/freespace2.git] / src / network / stand_server.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9
10
11 #include "pstypes.h"
12 #include "osregistry.h"
13 #include "multi_options.h"
14 #include "gamesequence.h"
15 #include "timer.h"
16 #include "version.h"
17 #include "multi.h"
18 #include "stand_server.h"
19 #include "multi_pmsg.h"
20 #include "multi_endgame.h"
21 #include "multimsgs.h"
22 #include "multiutil.h"
23 #include "freespace.h"
24 #include "missiongoals.h"
25 #include "cmdline.h"
26 #include "multi_kick.h"
27 #include "multi_fstracker.h"
28 #include "osregistry.h"
29 #include "standalone_html.h"
30
31 #include <libwebsockets.h>
32 #include <string>
33 #include <vector>
34 #include <list>
35
36
37 struct std_state {
38         std::string title;
39         std::string debug_txt;
40         std::string popup_title;
41         std::string popup_field1;
42         std::string popup_field2;
43
44         std::string active_player;
45 };
46
47 static std_state Standalone_state;
48
49 static std::list<std::string> Standalone_send_buf;
50
51
52 #define STANDALONE_MAX_BAN              50
53 static std::vector<std::string> Standalone_ban_list;
54
55 #define STD_STATS_UPDATE_TIME           500             // ms between updating player stats
56 #define STD_NG_UPDATE_TIME                      1500    // ms between updating netgame information
57 #define STD_PING_UPDATE_TIME            1000    // ms between updating pings
58 #define STD_FPS_UPDATE_TIME                     250             // ms between fps updates
59
60 static int Standalone_stats_stamp = -1;
61 static int Standalone_ng_stamp = -1;
62 static int Standalone_ping_stamp = -1;
63 static int Standalone_fps_stamp = -1;
64
65 static lws_context *stand_context = NULL;
66
67 static int startup_reset_stamp;
68 static struct lws *active_wsi = NULL;
69
70
71 static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
72 {
73         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + standalone_html_len + LWS_SEND_BUFFER_POST_PADDING];
74         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
75         unsigned char *start = p;
76         unsigned char *end = p + standalone_html_len;
77         bool try_reuse = false;
78         int rval;
79         int size;
80         static unsigned int sent = 0;
81
82         switch (reason) {
83                 case LWS_CALLBACK_HTTP: {
84                         if (len < 1) {
85                                 lws_return_http_status(wsi, HTTP_STATUS_BAD_REQUEST, NULL);
86                                 try_reuse = true;
87
88                                 break;
89                         }
90
91 #ifndef NDEBUG
92                         FILE *html = fopen("./standalone.html", "rb");
93
94                         if (html) {
95                                 fclose(html);
96
97                                 rval = lws_serve_http_file(wsi, "./standalone.html", "text/html", NULL, 0);
98
99                                 if ( (rval < 0) || ((rval > 0) && lws_http_transaction_completed(wsi)) ) {
100                                         // error or can't reuse connection, close the socket
101                                         return -1;
102                                 }
103                         } else
104 #endif
105                         {
106                                 if ( lws_add_http_header_status(wsi, 200, &p, end) ) {
107                                         return 1;
108                                 }
109
110                                 if ( lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER, (unsigned char *)"libwebsockets", 13, &p, end) ) {
111                                         return 1;
112                                 }
113
114                                 if ( lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE, (unsigned char *)"text/html", 9, &p, end) ) {
115                                         return 1;
116                                 }
117
118                                 if ( lws_add_http_header_content_length(wsi, standalone_html_len, &p, end) ) {
119                                         return 1;
120                                 }
121
122                                 if ( lws_finalize_http_header(wsi, &p, end) ) {
123                                         return 1;
124                                 }
125
126                                 rval = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS);
127
128                                 if (rval != (p - start)) {
129                                         return -1;
130                                 }
131
132                                 sent = 0;
133
134                                 lws_callback_on_writable(wsi);
135                         }
136
137                         break;
138                 }
139
140                 case LWS_CALLBACK_HTTP_BODY_COMPLETION: {
141                         lws_return_http_status(wsi, HTTP_STATUS_OK, NULL);
142                         try_reuse = true;
143
144                         break;
145                 }
146
147                 case LWS_CALLBACK_HTTP_FILE_COMPLETION: {
148                         try_reuse = true;
149
150                         break;
151                 }
152
153                 case LWS_CALLBACK_HTTP_WRITEABLE: {
154                         while ( !lws_send_pipe_choked(wsi) && (sent < standalone_html_len) ) {
155                                 size = standalone_html_len - sent;
156
157                                 int pwa = lws_get_peer_write_allowance(wsi);
158
159                                 if (pwa == 0) {
160                                         lws_callback_on_writable(wsi);
161
162                                         break;
163                                 }
164
165                                 if ( (pwa != -1) && (pwa < size) ) {
166                                         size = pwa;
167                                 }
168
169                                 memcpy(p, standalone_html + sent, size);
170
171                                 rval = lws_write(wsi, p, size, LWS_WRITE_HTTP);
172
173                                 if (rval < 0) {
174                                         return -1;
175                                 }
176
177                                 if (rval) {
178                                         // while still active, extent timeout
179                                         lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, 5);
180                                 }
181
182                                 sent += rval;
183                         }
184
185                         try_reuse = true;
186
187                         break;
188                 }
189
190                 default:
191                         break;
192         }
193
194         if (try_reuse) {
195                 if (lws_http_transaction_completed(wsi)) {
196                         return -1;
197                 }
198         }
199
200         return 0;
201 }
202
203 static int callback_standalone(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
204 {
205         #define MAX_BUF_SIZE    1024
206         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BUF_SIZE + LWS_SEND_BUFFER_POST_PADDING];
207         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
208         int rval;
209         int size;
210
211         switch (reason) {
212                 case LWS_CALLBACK_ESTABLISHED: {
213                         active_wsi = wsi;
214
215                         if ( timestamp_elapsed(startup_reset_stamp) ) {
216                                 std_reset_standalone_gui();
217                         }
218
219                         break;
220                 }
221
222                 case LWS_CALLBACK_CLOSED: {
223                         active_wsi = NULL;
224
225                         break;
226                 }
227
228                 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: {
229                         if (active_wsi) {
230                                 return -1;
231                         }
232
233                         break;
234                 }
235
236                 case LWS_CALLBACK_SERVER_WRITEABLE: {
237                         while ( !Standalone_send_buf.empty() ) {
238                                 size = SDL_strlcpy((char *)p, Standalone_send_buf.front().c_str(), MAX_BUF_SIZE);
239
240                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
241
242                                 if (rval < size) {
243                                         lwsl_err("ERROR sending buffer!\n");
244                                         lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, (unsigned char *)"write error", 11);
245
246                                         return -1;
247                                 }
248
249                                 Standalone_send_buf.pop_front();
250
251                                 if ( lws_send_pipe_choked(wsi) ) {
252                                         lws_callback_on_writable(wsi);
253
254                                         break;
255                                 }
256                         }
257
258                         break;
259                 }
260
261                 case LWS_CALLBACK_RECEIVE: {
262                         if (in != NULL && len > 0) {
263                                 const char *msg = (const char *)in;
264                                 char mtype = msg[0];
265
266                                 if ( !SDL_strcmp(msg, "shutdown") ) {
267                                         gameseq_post_event(GS_EVENT_QUIT_GAME);
268                                         lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY, (unsigned char *)"shutdown", 8);
269
270                                         return -1;
271                                 }
272
273                                 if ( !SDL_strcmp(msg, "reset") ) {
274                                         multi_quit_game(PROMPT_NONE);
275                                         std_reset_standalone_gui();
276
277                                         break;
278                                 }
279
280                                 // server tab
281                                 if (mtype == 'S') {
282                                         if (len >= 7) {
283                                                 if ( !SDL_strncmp(msg+2, "name ", 5) ) {
284                                                         SDL_strlcpy(Netgame.name, msg+7, SDL_arraysize(Netgame.name));
285                                                         SDL_strlcpy(Multi_options_g.std_pname, Netgame.name, SDL_arraysize(Multi_options_g.std_pname));
286                                                 } else if ( !SDL_strncmp(msg+2, "pass ", 5) ) {
287                                                         SDL_strlcpy(Multi_options_g.std_passwd, msg+7, SDL_arraysize(Multi_options_g.std_passwd));
288                                                 } else if ( !SDL_strncmp(msg+2, "kick ", 5) ) {
289                                                         char ip_string[60];
290
291                                                         for (int i = 0; i < MAX_PLAYERS; i++) {
292                                                                 if ( MULTI_CONNECTED(Net_players[i]) ) {
293                                                                         psnet_addr_to_string(ip_string, SDL_arraysize(ip_string), &Net_players[i].p_info.addr);
294
295                                                                         if ( !SDL_strcmp(msg+7, ip_string) ) {
296                                                                                 multi_kick_player(i, 0);
297
298                                                                                 break;
299                                                                         }
300                                                                 }
301                                                         }
302                                                 }
303                                         }
304                                 }
305                                 // multi-player tab
306                                 else if (mtype == 'M') {
307                                         if (len >= 6) {
308                                                 if ( !SDL_strncmp(msg+2, "fps ", 4) ) {
309                                                         int fps = SDL_atoi(msg+6);
310
311                                                         Multi_options_g.std_framecap = fps;
312                                                 }
313                                         }
314                                 }
315                                 // player info tab
316                                 else if (mtype == 'P') {
317                                         if (len >= 7) {
318                                                 if ( !SDL_strncmp(msg+2, "info ", 5) ) {
319                                                         int i;
320
321                                                         for (i = 0; i < MAX_PLAYERS; i++) {
322                                                                 net_player *np = &Net_players[i];
323
324                                                                 if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
325                                                                         if ( !SDL_strcmp(msg+7, np->player->callsign) ) {
326                                                                                 Standalone_state.active_player = msg+7;
327                                                                                 std_pinfo_display_player_info(np);
328
329                                                                                 break;
330                                                                         }
331                                                                 }
332                                                         }
333
334                                                         if (i == MAX_PLAYERS) {
335                                                                 Standalone_state.active_player.clear();
336                                                         }
337                                                 }
338                                         }
339                                 }
340                                 // god stuff tab
341                                 else if (mtype == 'G') {
342                                         if (len >= 7) {
343                                                 if ( !SDL_strncmp(msg+2, "smsg ", 5) ) {
344                                                         char txt[256];
345
346                                                         SDL_strlcpy(txt, msg+7, SDL_arraysize(txt));
347
348                                                         if (SDL_strlen(txt) > 0) {
349                                                                 send_game_chat_packet(Net_player, txt, MULTI_MSG_ALL, NULL);
350
351                                                                 std_add_chat_text(txt, MY_NET_PLAYER_NUM, 1);
352                                                         }
353                                                 } else if ( !SDL_strcmp(msg+2, "mrefresh") ) {
354                                                         if (MULTI_IS_TRACKER_GAME) {
355                                                                 cf_delete(MULTI_VALID_MISSION_FILE, CF_TYPE_DATA);
356
357                                                                 multi_update_valid_missions();
358                                                         }
359                                                 }
360                                         }
361                                 }
362                         }
363
364                         break;
365                 }
366
367                 default:
368                         break;
369         }
370
371         return 0;
372 }
373
374 static struct lws_protocols stand_protocols[] = {
375         {
376                 "http-only",
377                 callback_http,
378                 0,
379                 0
380         },
381         {
382                 "standalone",
383                 callback_standalone,
384                 0,
385                 0
386         },
387         // terminator
388         {
389                 NULL,
390                 NULL,
391                 0,
392                 0
393         }
394 };
395
396 static void std_lws_logger(int level, const char *line)
397 {
398         if (level & (LLL_WARN|LLL_ERR)) {
399                 mprintf(("STD: %s", line));
400         } else if (level & LLL_NOTICE) {
401                 nprintf(("lws", "STD: %s", line));
402         }
403 }
404
405
406
407 static void std_add_ws_message(const char *id, const char *val)
408 {
409         std::string msg;
410
411         // if no client, and startup stamp elapsed, then don't add more messages
412         if ( (active_wsi == NULL) && timestamp_elapsed(startup_reset_stamp) ) {
413                 return;
414         }
415
416         msg.assign(id);
417
418         if (val) {
419                 msg.append(val);
420         }
421
422         Standalone_send_buf.push_back(msg);
423 }
424
425 void std_deinit_standalone()
426 {
427         if (stand_context) {
428                 lws_cancel_service(stand_context);
429                 lws_context_destroy(stand_context);
430                 stand_context = NULL;
431         }
432 }
433
434 void std_init_standalone()
435 {
436         struct lws_context_creation_info info;
437
438         if (stand_context) {
439                 return;
440         }
441
442         SDL_zero(info);
443
444         // basic security measure for admin interface
445         //   "1" bind to loopback iface only *default*
446         //   "0" bind to any/all
447         //   any other value should be ip or iface name to bind
448         //   (invalid values will trigger error)
449
450         const char *stand_iface = os_config_read_string("Network", "RestrictStandAdmin", "1");
451
452         if ( !SDL_strcmp(stand_iface, "1") ) {
453                 info.iface = "127.0.0.1";
454         } else if ( !SDL_strcmp(stand_iface, "0") ) {
455                 info.iface = NULL;
456         } else {
457                 info.iface = stand_iface;
458         }
459
460         info.port = Multi_options_g.port;
461
462         info.protocols = stand_protocols;
463
464         info.gid = -1;
465         info.uid = -1;
466
467         lws_set_log_level(LLL_ERR|LLL_WARN|LLL_NOTICE, std_lws_logger);
468
469         stand_context = lws_create_context(&info);
470
471         if (stand_context == NULL) {
472                 Error(LOCATION, "Unable to initialize standalone server!");
473         }
474
475         atexit(std_deinit_standalone);
476
477         // turn off all sound and music
478         Cmdline_freespace_no_sound = 1;
479         Cmdline_freespace_no_music = 1;
480
481         char title[64];
482         SDL_snprintf(title, SDL_arraysize(title), "%s %d.%02d.%02d", XSTR("FreeSpace Standalone", 935), FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD);
483         Standalone_state.title = title;
484
485         // connections > 5 sec after startup should get gui reset
486         startup_reset_stamp = timestamp(5000);
487
488         std_reset_standalone_gui();
489
490         std_multi_update_netgame_info_controls();
491 }
492
493 static void std_update_ping_all()
494 {
495         std::string ping_upd;
496         char ping_str[10];
497
498         for (int i = 0, idx = 0; i < MAX_PLAYERS; i++) {
499                 net_player *np = &Net_players[i];
500
501                 if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
502                         if (np->s_info.ping.ping_avg > -1) {
503                                 if (np->s_info.ping.ping_avg >= 1000) {
504                                         SDL_snprintf(ping_str, SDL_arraysize(ping_str), "%s", XSTR("> 1 sec", 914));
505                                 } else {
506                                         SDL_snprintf(ping_str, SDL_arraysize(ping_str), "%d%s", np->s_info.ping.ping_avg, XSTR(" ms", 915));
507                                 }
508                         } else {
509                                 SDL_zero(ping_str);
510                         }
511
512                         // append separator if not first
513                         if (idx++) {
514                                 ping_upd.append(",");
515                         }
516
517                         ping_upd.append(ping_str);
518                 }
519         }
520
521         if ( !ping_upd.empty() ) {
522                 std_add_ws_message("S:ping ", ping_upd.c_str());
523         }
524 }
525
526 static void std_update_connections()
527 {
528         std::string conn_str;
529         char ip_address[60];
530
531         conn_str.reserve(1024);
532
533         for (int i = 0, idx = 0; i < MAX_PLAYERS; i++) {
534                 net_player *np = &Net_players[i];
535
536                 if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
537                         // append seperator if not first
538                         if (idx++) {
539                                 conn_str.append(";");
540                         }
541
542                         conn_str.append(np->player->callsign);
543                         conn_str.append(",");
544
545                         psnet_addr_to_string(ip_address, SDL_arraysize(ip_address), &np->p_info.addr);
546                         conn_str.append(ip_address);
547                 }
548         }
549
550         SDL_assert(conn_str.length() < 1024);
551
552         if ( !conn_str.empty() ) {
553                 std_add_ws_message("S:conn ", conn_str.c_str());
554         }
555 }
556
557 void std_do_gui_frame()
558 {
559         // maybe update selected player stats
560         if ( ((Standalone_stats_stamp == -1) || timestamp_elapsed(Standalone_stats_stamp)) && !Standalone_state.active_player.empty() ) {
561                 Standalone_stats_stamp = timestamp(STD_STATS_UPDATE_TIME);
562
563                 for (int i = 0; i < MAX_PLAYERS; i++) {
564                         net_player *np = &Net_players[i];
565
566                         if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
567                                 if ( !SDL_strcmp(Standalone_state.active_player.c_str(), np->player->callsign) ) {
568                                         std_pinfo_display_player_info(np);
569
570                                         break;
571                                 }
572                         }
573                 }
574         }
575
576         // maybe update netgame info
577         if ( (Standalone_ng_stamp == -1) || timestamp_elapsed(Standalone_ng_stamp) ) {
578                 Standalone_ng_stamp = timestamp(STD_NG_UPDATE_TIME);
579
580                 std_multi_update_netgame_info_controls();
581         }
582
583         // update connection ping times
584         if ( ((Standalone_ping_stamp == -1) || timestamp_elapsed(Standalone_ping_stamp)) ) {
585                 Standalone_ping_stamp = timestamp(STD_PING_UPDATE_TIME);
586
587                 std_update_ping_all();
588         }
589
590         if ( !Standalone_send_buf.empty() ) {
591                 lws_callback_on_writable_all_protocol(stand_context, &stand_protocols[1]);
592         }
593
594         lws_service(stand_context, 0);
595 }
596
597 void std_debug_set_standalone_state_string(const char *str)
598 {
599         Standalone_state.debug_txt = str;
600
601         std_add_ws_message("D:", str);
602 }
603
604 void std_connect_set_gamename(const char *name)
605 {
606         if (name == NULL) {
607                 // if a permanent name exists, use that instead of the default
608                 if ( SDL_strlen(Multi_options_g.std_pname) ) {
609                         SDL_strlcpy(Netgame.name, Multi_options_g.std_pname, SDL_arraysize(Netgame.name));
610                 } else {
611                         SDL_strlcpy(Netgame.name, XSTR("Standalone Server", 916), SDL_arraysize(Netgame.name));
612                 }
613         } else {
614                 SDL_strlcpy(Netgame.name, name, SDL_arraysize(Netgame.name));
615         }
616
617         std_add_ws_message("S:name ", Netgame.name);
618 }
619
620 int std_connect_set_connect_count()
621 {
622         int count = 0;
623
624         for (int i = 0; i < MAX_PLAYERS; i++) {
625                 if (MULTI_CONNECTED(Net_players[i]) && (Net_player != &Net_players[i]) ) {
626                         count++;
627                 }
628         }
629
630         return count;
631 }
632
633 void std_add_player(net_player *p)
634 {
635         std_update_connections();
636
637         // clear active player, client should reset if needed
638         Standalone_state.active_player.clear();
639
640         // check to see if this guy is the host
641         std_connect_set_host_connect_status();
642 }
643
644 int std_remove_player(net_player *p)
645 {
646         int count;
647
648         std_update_connections();
649
650         // clear active player, client should reset if needed
651         Standalone_state.active_player.clear();
652
653         // update the host connect count
654         std_connect_set_host_connect_status();
655
656         // update the currently connected players
657         count = std_connect_set_connect_count();
658
659         if (count == 0) {
660                 multi_quit_game(PROMPT_NONE);
661                 return 1;
662         }
663
664         return 0;
665 }
666
667 void std_update_player_ping(net_player *p)
668 {
669 }
670
671 void std_pinfo_display_player_info(net_player *p)
672 {
673         char sml_ping[30];
674         std::string pinfo;
675
676         pinfo.reserve(256);
677
678         // ship type
679         pinfo.append(Ship_info[p->p_info.ship_class].name);
680         pinfo.append(";");
681
682         // avg ping time
683         if (p->s_info.ping.ping_avg > 1000) {
684                 SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%s", XSTR("> 1 sec", 914));
685         } else {
686                 SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d%s", p->s_info.ping.ping_avg, XSTR(" ms", 915));
687         }
688
689         pinfo.append(sml_ping);
690         pinfo.append(";");
691
692         scoring_struct *ptr = &p->player->stats;
693
694         // all-time stats
695         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired);
696         pinfo.append(sml_ping);
697         pinfo.append(",");
698         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_hit);
699         pinfo.append(sml_ping);
700         pinfo.append(",");
701         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_bonehead_hits);
702         pinfo.append(sml_ping);
703         pinfo.append(",");
704         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired ? (int)(100.0f * ((float)ptr->p_shots_hit / (float)ptr->p_shots_fired)) : 0);
705         pinfo.append(sml_ping);
706         pinfo.append(",");
707         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired ? (int)(100.0f * ((float)ptr->p_bonehead_hits / (float)ptr->p_shots_fired)) : 0);
708         pinfo.append(sml_ping);
709         pinfo.append(",");
710         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired);
711         pinfo.append(sml_ping);
712         pinfo.append(",");
713         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_hit);
714         pinfo.append(sml_ping);
715         pinfo.append(",");
716         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_bonehead_hits);
717         pinfo.append(sml_ping);
718         pinfo.append(",");
719         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired ? (int)(100.0f * ((float)ptr->s_shots_hit / (float)ptr->s_shots_fired)) : 0);
720         pinfo.append(sml_ping);
721         pinfo.append(",");
722         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired ? (int)(100.0f * ((float)ptr->s_bonehead_hits / (float)ptr->s_shots_fired)) : 0);
723         pinfo.append(sml_ping);
724         pinfo.append(",");
725         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->assists);
726         pinfo.append(sml_ping);
727         pinfo.append(";");      // <- end of block
728
729         // mission stats
730         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired);
731         pinfo.append(sml_ping);
732         pinfo.append(",");
733         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_hit);
734         pinfo.append(sml_ping);
735         pinfo.append(",");
736         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_bonehead_hits);
737         pinfo.append(sml_ping);
738         pinfo.append(",");
739         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired ? (int)(100.0f * ((float)ptr->mp_shots_hit / (float)ptr->mp_shots_fired)) : 0);
740         pinfo.append(sml_ping);
741         pinfo.append(",");
742         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired ? (int)(100.0f * ((float)ptr->mp_bonehead_hits / (float)ptr->mp_shots_fired)) : 0);
743         pinfo.append(sml_ping);
744         pinfo.append(",");
745         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired);
746         pinfo.append(sml_ping);
747         pinfo.append(",");
748         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_hit);
749         pinfo.append(sml_ping);
750         pinfo.append(",");
751         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_bonehead_hits);
752         pinfo.append(sml_ping);
753         pinfo.append(",");
754         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired ? (int)(100.0f * ((float)ptr->ms_shots_hit / (float)ptr->ms_shots_fired)) : 0);
755         pinfo.append(sml_ping);
756         pinfo.append(",");
757         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired ? (int)(100.0f * ((float)ptr->ms_bonehead_hits / (float)ptr->ms_shots_fired)) : 0);
758         pinfo.append(sml_ping);
759         pinfo.append(",");
760         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->m_assists);
761         pinfo.append(sml_ping);
762
763         std_add_ws_message("P:info ", pinfo.c_str());
764 }
765
766 void std_add_chat_text(const char *text, int player_index, int add_id)
767 {
768         char id[32];
769         std::string msg;
770
771         if ( (player_index < 0) || (player_index >= MAX_PLAYERS) ) {
772                 return;
773         }
774
775         // format the chat text nicely
776         if (add_id) {
777                 if ( MULTI_STANDALONE(Net_players[player_index]) ) {
778                         SDL_snprintf(id, SDL_arraysize(id), XSTR("<SERVER> %s", 924), "");
779                 } else {
780                         SDL_snprintf(id, SDL_arraysize(id), "%s: ", Net_players[player_index].player->callsign);
781                 }
782
783                 msg.append(id);
784         }
785
786         msg.append(text);
787         msg.append("\n");
788
789         std_add_ws_message("G:mesg ", msg.c_str());
790 }
791
792 void std_reset_timestamps()
793 {
794         // reset the stats update stamp
795         Standalone_stats_stamp = timestamp(STD_STATS_UPDATE_TIME);
796
797         // reset the netgame controls update timestamp
798         Standalone_ng_stamp = timestamp(STD_NG_UPDATE_TIME);
799
800         // reset the ping update stamp
801         Standalone_ping_stamp = timestamp(STD_PING_UPDATE_TIME);
802
803         // reset fps update stamp
804         Standalone_fps_stamp = timestamp(STD_FPS_UPDATE_TIME);
805 }
806
807 void std_add_ban(const char *name)
808 {
809         if ( (name == NULL) || !SDL_strlen(name) ) {
810                 return;
811         }
812
813         if (Standalone_ban_list.size() >= STANDALONE_MAX_BAN) {
814                 return;
815         }
816
817         Standalone_ban_list.push_back(name);
818 }
819
820 int std_player_is_banned(const char *name)
821 {
822         if ( Standalone_ban_list.empty() ) {
823                 return 0;
824         }
825
826         for (size_t i = 0; i < Standalone_ban_list.size(); i++) {
827                 if ( !SDL_strcasecmp(name, Standalone_ban_list[i].c_str()) ) {
828                         return 1;
829                 }
830         }
831
832         return 0;
833 }
834
835 int std_is_host_passwd()
836 {
837         return (SDL_strlen(Multi_options_g.std_passwd) > 0) ? 1 : 0;
838 }
839
840 void std_multi_set_standalone_mission_name(const char *mission_name)
841 {
842         std_add_ws_message("M:name ", mission_name);
843 }
844
845 void std_multi_set_standalone_missiontime(float mission_time)
846 {
847         char txt[80];
848         char timestr[50];
849         fix m_time = fl2f(mission_time);
850
851         // format the time string and set the text
852         game_format_time(m_time, timestr, SDL_arraysize(timestr));
853         SDL_snprintf(txt, SDL_arraysize(txt), "%s  :  %.1f", timestr, mission_time);
854
855         std_add_ws_message("M:time ", txt);
856 }
857
858 void std_multi_update_netgame_info_controls()
859 {
860         char nginfo[50];
861
862         SDL_snprintf(nginfo, SDL_arraysize(nginfo), "%d,%d,%d,%d", Netgame.max_players, Netgame.options.max_observers, Netgame.security, Netgame.respawn);
863
864         std_add_ws_message("M:info ", nginfo);
865 }
866
867 void std_set_standalone_fps(float fps)
868 {
869         if ( (Standalone_fps_stamp == -1) || timestamp_elapsed(Standalone_fps_stamp) ) {
870                 Standalone_fps_stamp = timestamp(STD_FPS_UPDATE_TIME);
871
872                 char rfps[10];
873
874                 SDL_snprintf(rfps, SDL_arraysize(rfps), "%.1f", fps);
875
876                 std_add_ws_message("M:rfps ", rfps);
877         }
878 }
879
880 void std_multi_setup_goal_tree()
881 {
882         std::string mission_goals;
883         std::string primary;
884         std::string secondary;
885         std::string bonus;
886         std::string status;
887
888         for (int i = 0; i < Num_goals; i++) {
889                 switch (Mission_goals[i].satisfied) {
890                         case GOAL_FAILED: {
891                                 status = "f ";
892                                 break;
893                         }
894
895                         case GOAL_COMPLETE: {
896                                 status = "c ";
897                                 break;
898                         }
899
900                         case GOAL_INCOMPLETE:
901                         default: {
902                                 status = "i ";
903                                 break;
904                         }
905                 }
906
907                 switch (Mission_goals[i].type & GOAL_TYPE_MASK) {
908                         case PRIMARY_GOAL: {
909                                 primary.append(status);
910                                 primary.append(Mission_goals[i].name);
911                                 primary.append(",");
912
913                                 break;
914                         }
915
916                         case SECONDARY_GOAL: {
917                                 secondary.append(status);
918                                 secondary.append(Mission_goals[i].name);
919                                 secondary.append(",");
920
921                                 break;
922                         }
923
924                         case BONUS_GOAL: {
925                                 bonus.append(status);
926                                 bonus.append(Mission_goals[i].name);
927                                 bonus.append(",");
928
929                                 break;
930                         }
931
932                         default:
933                                 break;
934                 }
935         }
936
937         if ( primary.empty() ) {
938                 mission_goals.append("i none");
939         } else {
940                 mission_goals.append(primary.substr(0, primary.size()-1));
941         }
942
943         mission_goals.append(";");
944
945         if ( secondary.empty() ) {
946                 mission_goals.append("i none");
947         } else {
948                 mission_goals.append(secondary.substr(0, secondary.size()-1));
949         }
950
951         mission_goals.append(";");
952
953         if ( bonus.empty() ) {
954                 mission_goals.append("i none");
955         } else {
956                 mission_goals.append(bonus.substr(0, bonus.size()-1));
957         }
958
959         std_add_ws_message("M:goal ", mission_goals.c_str());
960 }
961
962 void std_multi_add_goals()
963 {
964         std_multi_setup_goal_tree();
965 }
966
967 void std_multi_update_goals()
968 {
969         std_multi_setup_goal_tree();
970 }
971
972 void std_reset_standalone_gui()
973 {
974         Standalone_send_buf.clear();
975
976         std_add_ws_message("reset", NULL);
977
978         std_add_ws_message("T:", Standalone_state.title.c_str());
979         std_add_ws_message("D:", Standalone_state.debug_txt.c_str());
980
981         std_add_ws_message("S:name ", Netgame.name);
982         std_add_ws_message("S:pass ", Multi_options_g.std_passwd);
983
984         std_update_connections();
985         std_set_standalone_fps(0.0f);
986         std_multi_set_standalone_missiontime(0.0f);
987         std_multi_update_netgame_info_controls();
988
989         Standalone_fps_stamp = -1;
990         Standalone_ng_stamp = -1;
991         Standalone_ping_stamp = -1;
992         Standalone_stats_stamp = -1;
993
994         Standalone_state.active_player.clear();
995 }
996
997
998 void std_create_gen_dialog(const char *title)
999 {
1000         Standalone_state.popup_title = title;
1001
1002         Standalone_state.popup_field1 = "";
1003         Standalone_state.popup_field2 = "";
1004 }
1005
1006 void std_destroy_gen_dialog()
1007 {
1008         std_add_ws_message("popup ", NULL);
1009 }
1010
1011 void std_gen_set_text(const char *str, int field_num)
1012 {
1013         std::string popup_str;
1014
1015         switch (field_num) {
1016                 case 0:
1017                         Standalone_state.popup_title = str;
1018                         break;
1019
1020                 case 1:
1021                         Standalone_state.popup_field1 = str;
1022                         break;
1023
1024                 case 2:
1025                         Standalone_state.popup_field2 = str;
1026                         break;
1027
1028                 default:
1029                         break;
1030         }
1031
1032         popup_str.append(Standalone_state.popup_title);
1033         popup_str.append(";");
1034         popup_str.append(Standalone_state.popup_field1);
1035         popup_str.append(";");
1036         popup_str.append(Standalone_state.popup_field2);
1037
1038         std_add_ws_message("popup ", popup_str.c_str());
1039
1040         // force ws write since do_frame() may not happen until popup is done
1041         lws_callback_on_writable_all_protocol(stand_context, &stand_protocols[1]);
1042         lws_service(stand_context, 0);
1043 }
1044
1045 void std_tracker_notify_login_fail()
1046 {
1047
1048 }
1049
1050 void std_tracker_login()
1051 {
1052         if ( !Multi_options_g.pxo ) {
1053                 return;
1054         }
1055
1056         multi_fs_tracker_init();
1057
1058         if ( !multi_fs_tracker_inited() ) {
1059                 std_tracker_notify_login_fail();
1060                 return;
1061         }
1062
1063         multi_fs_tracker_login_freespace();
1064 }
1065
1066 void std_connect_set_host_connect_status()
1067 {
1068 }