-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
3982 lines (3151 loc) · 81.1 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Race for the Galaxy AI
*
* Copyright (C) 2009-2011 Keldon Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "rftg.h"
#include "comm.h"
#include <mysql/mysql.h>
#include <pthread.h>
/*
* Session status types.
*/
#define SS_EMPTY 0
#define SS_WAITING 1
#define SS_STARTED 2
#define SS_DONE 3
#define SS_ABANDONED 4
/*
* Number of random bytes to store per session (2 needed per number generated).
*/
#define MAX_RAND 1024
/*
* A connection from a client.
*/
typedef struct conn
{
/* File descriptor of socket */
int fd;
/* Connection is to a local AI client */
int ai;
/* Data buffer for incoming bytes */
char buf[2048];
/* Amount of data currently in buffer */
int buf_full;
/* Data buffer for unsent messages */
char *out_buf;
/* Amount of data needing to be sent */
int out_len;
/* Current size of outgoing buffer */
int out_size;
/* Connection state */
int state;
/* Username of connected player */
char user[80];
/* User ID */
int uid;
/* IP address of remote client */
char addr[80];
/* Session this player has joined (if any) */
int sid;
/* Connection has been sent keepalive ping */
int ping_sent;
/* Time of last activity */
time_t last_active;
/* Time of last communication */
time_t last_seen;
/* Mutex to protect outgoing buffer */
pthread_mutex_t conn_mutex;
} conn;
/*
* An outstanding choice to be made.
*
* We save these in case a client disconnects and later reconnects (and needs
* to be re-asked), or for when a player is kicked and an AI replacement is
* added.
*/
typedef struct choice
{
/* Type */
int type;
/* List of choices */
int list[MAX_DECK];
int num;
/* Special options */
int special[MAX_DECK];
int num_special;
/* Arguments */
int arg1;
int arg2;
int arg3;
} choice;
/*
* A game to be started, or in progress.
*/
typedef struct session
{
/* Session description */
char desc[1024];
/* Password needed to join this game */
char pass[21];
/* Game number */
int gid;
/* Game information */
game g;
/* Game information remembered by each client */
game old[MAX_PLAYER];
/* Outstanding choice for each player */
choice out[MAX_PLAYER];
/* Pool of random bytes */
unsigned char random_pool[MAX_RAND];
/* Current position in random pool */
int random_pos;
/* User ID who created session */
int created;
/* List of users who have joined this session */
int uids[MAX_PLAYER];
/* Connection IDs of users (if online) */
int cids[MAX_PLAYER];
/* Player has been taken over by an AI */
int ai_control[MAX_PLAYER];
/* Number of users attached to this session */
int num_users;
/* Session state */
int state;
/* Desired min/max number of players */
int min_player;
int max_player;
/* Desired expansion level */
int expanded;
/* Other options */
int advanced;
int disable_goal;
int disable_takeover;
/* Game speed */
int speed;
/* Player(s) being waited upon */
int waiting[MAX_PLAYER];
/* Ticks (roughly 10 seconds) we've been waiting on each player */
int wait_ticks[MAX_PLAYER];
/* Mutex for access to session variables */
pthread_mutex_t session_mutex;
/* Condition variable to signal game thread when reply is ready */
pthread_cond_t wait_cond;
/* Time since last player joined */
time_t last_join;
} session;
/*
* List of all active connections.
*/
static conn c_list[1024];
static int num_conn;
/*
* List of active game sessions.
*/
static session s_list[1024];
static int num_session;
/*
* Connection to the database server.
*/
MYSQL *mysql;
/*
* Check for a user in the database with the given password.
*
* If the user does not exist, create an entry for them.
*
* Return -1 if the password given does not match an existing entry.
*/
static int db_user(char *user, char *pass)
{
MYSQL_RES *res;
MYSQL_ROW row;
char query[1024];
char euser[1024], epass[1024];
int uid;
/* Escape user and password */
mysql_real_escape_string(mysql, euser, user, strlen(user));
mysql_real_escape_string(mysql, epass, pass, strlen(pass));
/* Create lookup query */
sprintf(query, "SELECT pass, uid FROM users WHERE user='%s'", euser);
/* Run query */
mysql_query(mysql, query);
/* Fetch results */
res = mysql_store_result(mysql);
/* Check for no rows returned */
if (!(row = mysql_fetch_row(res)))
{
/* Free old results */
mysql_free_result(res);
/* Create insertion query */
sprintf(query, "INSERT INTO users (user, pass) VALUES \
('%s', '%s')", euser, epass);
/* Send query */
mysql_query(mysql, query);
/* Get ID of user inserted */
strcpy(query, "SELECT LAST_INSERT_ID()");
/* Run query */
mysql_query(mysql, query);
/* Fetch results */
res = mysql_store_result(mysql);
/* Get row */
row = mysql_fetch_row(res);
/* Get user ID */
uid = strtol(row[0], NULL, 0);
/* Free result */
mysql_free_result(res);
/* Return ID */
return uid;
}
/* Check for matching password */
if (!strcmp(pass, row[0]))
{
/* Get ID */
uid = strtol(row[1], NULL, 0);
/* Free result */
mysql_free_result(res);
/* Return ID */
return uid;
}
/* Free result */
mysql_free_result(res);
/* Bad password */
return -1;
}
/*
* Return the user name for a given user ID.
*/
static void db_user_name(int uid, char *name)
{
MYSQL_RES *res;
MYSQL_ROW row;
char query[1024];
/* Create query */
sprintf(query, "SELECT user FROM users WHERE uid=%d", uid);
/* Run query */
mysql_query(mysql, query);
/* Fetch results */
res = mysql_store_result(mysql);
/* Get row */
row = mysql_fetch_row(res);
/* Copy user name */
strcpy(name, row[0]);
/* Free result */
mysql_free_result(res);
}
/*
* Create an entry for a game in the database.
*
* Return the game ID.
*/
static int db_new_game(int sid)
{
MYSQL_RES *res;
MYSQL_ROW row;
session *s_ptr = &s_list[sid];
char query[1024];
char edesc[1024], epass[1024];
int gid;
/* Escape game description and password */
mysql_real_escape_string(mysql, edesc, s_ptr->desc,strlen(s_ptr->desc));
mysql_real_escape_string(mysql, epass, s_ptr->pass,strlen(s_ptr->pass));
/* Create insertion query */
sprintf(query, "INSERT INTO games (description, pass, created, state, \
minp, maxp, \
exp, adv, dis_goal, dis_takeover, \
speed, version) \
VALUES ('%s', '%s', %d, 'WAITING', %d, %d, %d, %d, \
%d, %d, %d, '%s')",
edesc, epass, s_ptr->created,
s_ptr->min_player, s_ptr->max_player,
s_ptr->expanded, s_ptr->advanced, s_ptr->disable_goal,
s_ptr->disable_takeover, s_ptr->speed, VERSION);
/* Send query */
mysql_query(mysql, query);
/* Check for error */
if (*mysql_error(mysql))
{
/* Print error */
printf("%s\n", mysql_error(mysql));
exit(1);
}
/* Get ID of game inserted */
strcpy(query, "SELECT LAST_INSERT_ID()");
/* Run query */
mysql_query(mysql, query);
/* Fetch results */
res = mysql_store_result(mysql);
/* Get row */
row = mysql_fetch_row(res);
/* Get ID */
gid = strtol(row[0], NULL, 0);
/* Free result */
mysql_free_result(res);
/* Return ID */
return gid;
}
/*
* Read waiting/running games from database.
*/
static void db_load_sessions(void)
{
MYSQL_RES *res;
MYSQL_ROW row;
session *s_ptr;
int sid = 0;
/* Run query */
mysql_query(mysql, "SELECT gid, description, pass, created, state, \
minp, maxp, exp, adv, dis_goal, \
dis_takeover, speed \
FROM games \
WHERE state='WAITING' OR state='STARTED'");
/* Fetch results */
res = mysql_store_result(mysql);
/* Loop over rows returned */
while ((row = mysql_fetch_row(res)))
{
/* Get pointer to session */
s_ptr = &s_list[sid];
/* Read fields */
s_ptr->gid = strtol(row[0], NULL, 0);
strcpy(s_ptr->desc, row[1]);
strcpy(s_ptr->pass, row[2]);
s_ptr->created = strtol(row[3], NULL, 0);
/* Check state */
if (!strcmp(row[4], "WAITING"))
{
/* Set state */
s_ptr->state = SS_WAITING;
}
else
{
/* Game is started */
s_ptr->state = SS_STARTED;
}
/* Read fields */
s_ptr->min_player = strtol(row[5], NULL, 0);
s_ptr->max_player = strtol(row[6], NULL, 0);
s_ptr->expanded = strtol(row[7], NULL, 0);
s_ptr->advanced = strtol(row[8], NULL, 0);
s_ptr->disable_goal = strtol(row[9], NULL, 0);
s_ptr->disable_takeover = strtol(row[10], NULL, 0);
s_ptr->speed = strtol(row[11], NULL, 0);
/* Set last join time */
s_ptr->last_join = time(NULL);
/* Increase number of sessions */
num_session++;
/* Go to next session ID */
sid++;
}
/* Free results */
mysql_free_result(res);
}
/*
* Read list of players in waiting/running games from database.
*/
static void db_load_attendance(void)
{
MYSQL_RES *res;
MYSQL_ROW row;
session *s_ptr;
int uid, gid, ai;
int i;
/* Run query */
mysql_query(mysql, "SELECT uid, gid, ai \
FROM attendance \
JOIN games USING (gid) \
WHERE state='WAITING' OR state='STARTED' \
ORDER BY seat");
/* Fetch results */
res = mysql_store_result(mysql);
/* Loop over rows returned */
while ((row = mysql_fetch_row(res)))
{
/* Get user ID */
uid = strtol(row[0], NULL, 0);
/* Get game ID */
gid = strtol(row[1], NULL, 0);
/* Get AI control */
ai = strtol(row[2], NULL, 0);
/* Loop over sessions */
for (i = 0; i < num_session; i++)
{
/* Stop at correct game ID */
if (s_list[i].gid == gid) break;
}
/* Check for error */
if (i == num_session)
{
/* Error */
printf("Bad attendance: no gid %d\n", gid);
continue;
}
/* Get pointer to session */
s_ptr = &s_list[i];
/* Add user to session */
s_ptr->uids[s_ptr->num_users] = uid;
/* No connection for user yet */
s_ptr->cids[s_ptr->num_users] = -1;
/* Set AI control */
s_ptr->ai_control[s_ptr->num_users] = ai;
/* Count users */
s_ptr->num_users++;
}
/* Free results */
mysql_free_result(res);
}
/*
* Add a player to a game in the database.
*/
static void db_join_game(int uid, int gid)
{
char query[1024];
/* Create query */
sprintf(query, "INSERT INTO attendance (uid, gid) VALUES (%d, %d)",
uid, gid);
/* Run query */
mysql_query(mysql, query);
}
/*
* Remove a player from a game in the database.
*/
static void db_leave_game(int uid, int gid)
{
char query[1024];
/* Create query */
sprintf(query, "DELETE FROM attendance WHERE uid=%d AND gid=%d",
uid, gid);
/* Run query */
mysql_query(mysql, query);
}
/*
* Load a game's saved state from the database.
*
* Return 0 if no state is available.
*/
static int db_load_game_state(int sid)
{
MYSQL_RES *res;
MYSQL_ROW row;
session *s_ptr = &s_list[sid];
unsigned long *field_len;
char query[1024];
int i;
/* Create query */
sprintf(query, "SELECT pool FROM seed WHERE gid=%d", s_ptr->gid);
/* Run query */
mysql_query(mysql, query);
/* Fetch results */
res = mysql_store_result(mysql);
/* Check for no rows returned */
if (!(row = mysql_fetch_row(res)))
{
/* Free result */
mysql_free_result(res);
/* No pool to load */
return 0;
}
/* Copy returned data to random byte pool */
memcpy(s_ptr->random_pool, row[0], MAX_RAND);
/* Start at beginning of byte pool */
s_ptr->random_pos = 0;
/* Free result */
mysql_free_result(res);
/* Loop over players in session */
for (i = 0; i < s_ptr->num_users; i++)
{
/* Create query to load choice log */
sprintf(query,"SELECT log FROM choices WHERE gid=%d AND uid=%d",
s_ptr->gid, s_ptr->uids[i]);
/* Run query */
mysql_query(mysql, query);
/* Fetch results */
res = mysql_store_result(mysql);
/* Check for no rows returned */
if (!(row = mysql_fetch_row(res)))
{
/* Free result */
mysql_free_result(res);
/* Go to next player */
continue;
}
/* Get length of log in bytes */
field_len = mysql_fetch_lengths(res);
/* Copy log */
memcpy(s_ptr->g.p[i].choice_log, row[0], field_len[0]);
/* Remember length */
s_ptr->g.p[i].choice_size = field_len[0] / sizeof(int);
/* Free result */
mysql_free_result(res);
}
/* Success */
return 1;
}
/*
* Save the basic state about a game, including random seeds and which
* players begin in each seat.
*/
static void db_save_game_state(int sid)
{
session *s_ptr = &s_list[sid];
char query[4096], pool[4096], *status = "";
/* Determine session status */
switch (s_ptr->state)
{
case SS_WAITING: status = "WAITING"; break;
case SS_STARTED: status = "STARTED"; break;
case SS_DONE: status = "DONE"; break;
case SS_ABANDONED: status = "ABANDONED"; break;
}
/* Create query for session status */
sprintf(query, "UPDATE games SET state='%s' WHERE gid=%d", status,
s_ptr->gid);
/* Run query */
mysql_query(mysql, query);
/* No need to save further data if game has not started */
if (s_ptr->state == SS_WAITING || s_ptr->state == SS_ABANDONED) return;
/* Escape random byte pool */
mysql_real_escape_string(mysql, pool, (char *)s_ptr->random_pool,
MAX_RAND);
/* Create query to save random byte pool */
sprintf(query, "INSERT IGNORE INTO seed VALUES (%d, '%s')",
s_ptr->gid, pool);
/* Run query */
mysql_query(mysql, query);
}
/*
* Save the initial seating position of players just before a game starts.
*/
static void db_save_seats(int sid)
{
session *s_ptr = &s_list[sid];
char query[1024];
int i;
/* Loop over players in game */
for (i = 0; i < s_ptr->num_users; i++)
{
/* Update seat number */
sprintf(query, "UPDATE attendance SET seat=%d \
WHERE gid=%d AND uid=%d",
i, s_ptr->gid, s_ptr->uids[i]);
/* Run query */
mysql_query(mysql, query);
}
}
/*
* Save AI control flags.
*/
static void db_save_ai_control(int sid)
{
session *s_ptr = &s_list[sid];
char query[1024];
int i;
/* Loop over players in game */
for (i = 0; i < s_ptr->num_users; i++)
{
/* Update seat number */
sprintf(query, "UPDATE attendance SET ai=%d \
WHERE gid=%d AND uid=%d",
s_ptr->ai_control[i], s_ptr->gid,
s_ptr->uids[i]);
/* Run query */
mysql_query(mysql, query);
}
}
/*
* Save a player's choice log to the database.
*/
static void db_save_choices(int sid, int who)
{
session *s_ptr = &s_list[sid];
player *p_ptr;
char query[20000], log[20000];
/* Get player pointer */
p_ptr = &s_ptr->g.p[who];
/* Escape choice log string */
mysql_real_escape_string(mysql, log, (char *)p_ptr->choice_log,
sizeof(int) * p_ptr->choice_size);
/* Create query */
sprintf(query, "REPLACE INTO choices VALUES (%d, %d, '%s')", s_ptr->gid,
s_ptr->uids[who], log);
/* Run query */
mysql_query(mysql, query);
}
/*
* Save the results from a finished game.
*/
static void db_save_results(int sid)
{
session *s_ptr = &s_list[sid];
player *p_ptr;
int i, tie;
char query[1024];
/* Save finished choice logs */
for (i = 0; i < s_ptr->num_users; i++)
{
/* Save choice log for this player */
db_save_choices(sid, i);
}
/* Loop over players */
for (i = 0; i < s_ptr->num_users; i++)
{
/* Get player pointer */
p_ptr = &s_ptr->g.p[i];
/* Get tiebreaker value for player */
tie = count_player_area(&s_ptr->g, i, WHERE_HAND) +
count_player_area(&s_ptr->g, i, WHERE_GOOD);
/* Create query */
sprintf(query, "INSERT INTO results VALUES (%d, %d, %d, %d,%d)",
s_ptr->gid, s_ptr->uids[i], p_ptr->end_vp, tie,
p_ptr->winner);
/* Run query */
mysql_query(mysql, query);
}
}
/*
* Send a message to a client.
*/
void send_msg(int cid, char *msg)
{
conn *c;
int size, x;
char *ptr;
/* Ensure valid connection */
if (cid < 0) return;
/* Get connection pointer */
c = &c_list[cid];
/* Check for kicked player */
if (c->fd < 0) return;
/* Go to size area of message */
ptr = msg + 4;
/* Read size */
size = get_integer(&ptr);
/* Grab mutex for connection */
pthread_mutex_lock(&c->conn_mutex);
/* Check for insufficient buffer size to hold message */
if (c->out_size < c->out_len + size)
{
/* Reallocate buffer */
c->out_buf = (char *)realloc(c->out_buf, c->out_len + size);
}
/* Copy current message to end of buffer */
memcpy(c->out_buf + c->out_len, msg, size);
/* Add to current buffer length */
c->out_len += size;
/* Attempt to send full amount of buffer */
x = send(c->fd, c->out_buf, c->out_len, 0);
/* Check for errors */
if (x < 0)
{
/* Check for try again error */
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
/* Release mutex */
pthread_mutex_unlock(&c->conn_mutex);
return;
}
/* Print error */
perror("send");
/* Release mutex */
pthread_mutex_unlock(&c->conn_mutex);
return;
}
/* Reduce buffer length by amount sent */
c->out_len -= x;
/* Shift buffer */
memmove(c->out_buf, c->out_buf + x, c->out_len);
/* Release connection mutex */
pthread_mutex_unlock(&c->conn_mutex);
}
/*
* Create a new AI client connection.
*/
static int new_ai_client(int sid)
{
int fds[2];
int i;
/* Loop through current list looking for an empty spot */
for (i = 0; i < num_conn; i++)
{
/* Stop at empty connection */
if (c_list[i].state == CS_EMPTY ||
c_list[i].state == CS_DISCONN) break;
}
/* Check for end of list reached */
if (i == num_conn)
{
/* Increase count of active connections */
num_conn++;
}
/* Set connection state */
c_list[i].state = CS_PLAYING;
/* Create a socket pair to communicate with AI client */
socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
/* Fork a child process */
switch (fork())
{
/* Error */
case -1:
/* Print error */
perror("fork");
exit(1);
/* Child */
case 0:
/* Close our copy of one end of socket */
close(fds[0]);
/* Close standard input */
close(0);
/* Move socket to FD zero */
dup2(fds[1], 0);
/* Execute AI client program */
execl("./ai_client", "ai_client", NULL);
/* XXX */
perror("execlp");
exit(1);
/* Server */
default:
/* Close our copy of one end of socket */
close(fds[1]);
/* Remember socket */
c_list[i].fd = fds[0];
break;
}
/* Mark connection as AI */
c_list[i].ai = 1;
/* Mark session ID of connection */
c_list[i].sid = sid;
/* Reset timeout information */
c_list[i].last_active = c_list[i].last_seen = time(NULL);
/* Clear buffer length */
c_list[i].buf_full = 0;
/* Clear outgoing buffer length */
c_list[i].out_len = 0;
/* Clear username */
strcpy(c_list[i].user, "AI client");
/* Return connection index */
return i;
}
/*
* Send information about a player to all clients.
*/
static void send_player_one(int dest, int who)
{
/* Check for disconnected player */
if (c_list[who].state == CS_DISCONN)
{
/* Send "player left" message */
send_msgf(dest, MSG_PLAYER_LEFT, "s", c_list[who].user);
}
else
{
/* Send "new player" message */
send_msgf(dest, MSG_PLAYER_NEW, "sd", c_list[who].user,
c_list[who].state == CS_PLAYING);
}
}
/*
* Send information about a player to all connected clients.
*/
static void send_player(int who)
{
int i;
/* Loop over connections */
for (i = 0; i < num_conn; i++)
{
/* Skip non-active connections */
if (c_list[i].state != CS_LOBBY &&
c_list[i].state != CS_PLAYING) continue;
/* Skip AI connections */
if (c_list[i].ai) continue;
/* Send information */
send_player_one(i, who);
}
}
/*
* Send information about all connected players to a new client.
*/
static void send_all_players(int dest)
{
int i;
/* Loop over connections */