File: play.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (1496 lines) | stat: -rw-r--r-- 35,674 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <riq@corest.com>
 *
 * 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; only version 2 of the License
 *
 * 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.
 */
/*
 * Controls all the messages that receives the server
 */

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "../common/net.h"

#include "server.h"
#include "tegdebug.h"
#include "xmlscores.h"
#include "fow.h"
#include "fcintl.h"
#include "parser.h"
#include "missions.h"

#undef DEBUG_PLAY
#define PLAY_DEBUG PDEBUG

#ifdef DEBUG_PLAY
# define STATIC
#else
# define STATIC static
#endif


namespace teg::server
{

STATIC TEG_STATUS token_status(int, char *);
STATIC TEG_STATUS token_test(int, char *);
STATIC TEG_STATUS token_sversion(int, char *);
STATIC TEG_STATUS token_cversion(int, char *);
STATIC TEG_STATUS token_pversion(int, char *);
STATIC TEG_STATUS token_playerid(int, char *);
STATIC TEG_STATUS token_help(int, char *);
STATIC TEG_STATUS token_countries(int, char *);
STATIC TEG_STATUS token_fichas(int, char*);
STATIC TEG_STATUS token_fichas2(int, char *);
STATIC TEG_STATUS token_fichasc(int, char *);
STATIC TEG_STATUS token_attack(int, char *);
STATIC TEG_STATUS token_qumm(int fd, char *);
STATIC TEG_STATUS token_rem(int fd, char *);
STATIC TEG_STATUS token_message(int fd, char *);
STATIC TEG_STATUS token_tropas(int fd, char *str);
STATIC TEG_STATUS token_turn(int fd, char *str);
STATIC TEG_STATUS token_card(int fd, char *str);
STATIC TEG_STATUS token_regroup(int, char *);
STATIC TEG_STATUS token_canje(int, char*);
STATIC TEG_STATUS token_ejer2(int, char *);
STATIC TEG_STATUS token_mission(int, char *);
STATIC TEG_STATUS token_color(int, char*);
STATIC TEG_STATUS token_loque(int, char*);
STATIC TEG_STATUS token_surrender(int, char*);
STATIC TEG_STATUS token_set(int, char*);
STATIC TEG_STATUS token_scores(int, char*);
STATIC TEG_STATUS token_enum_cards(int, char*);
STATIC TEG_STATUS token_robot(int, char*);
STATIC TEG_STATUS token_new_round(int fd, char *str);
STATIC TEG_STATUS token_typeofgame(int fd, char *str);

struct {
	char const *label;
	TEG_STATUS(*func)(int, char*);
	char const *help;
} tokens[] = {
	{ TOKEN_START,		token_start,	N_("to start the game") },
	{ TOKEN_STATUS,		token_status,	N_("shows the status of the players") },
	{ TOKEN_MESSAGE,	token_message,	N_("to send a message") },
	{ TOKEN_EXIT,		token_exit,	N_("to exit the game") },
	{ TOKEN_TEST,		token_test,	N_("internal use. Dont use it.") },
	{ TOKEN_CVERSION,	token_cversion,	N_("client version") },
	{ TOKEN_SVERSION,	token_sversion,	N_("server version") },
	{ TOKEN_PVERSION,	token_pversion,	N_("protocol version") },
	{ TOKEN_PLAYERID,	token_playerid,	N_("to register as a player") },
	{ TOKEN_HELP,		token_help,	N_("to ask for help") },
	{ TOKEN_REM,		token_rem,	N_("to comment a command") },
	{ TOKEN_QUMM,		token_qumm,	N_("It makes you more happy") },
	{ TOKEN_COUNTRIES,		token_countries, N_("It shows info about the countries") },
	{ TOKEN_FICHAS,		token_fichas,	N_("to place the initials 5 armies") },
	{ TOKEN_FICHAS2,	token_fichas2,	N_("to place the initials 3 armies") },
	{ TOKEN_FICHASC,	token_fichasc,	N_("to place the armies after a turn have finished") },
	{ TOKEN_ATAQUE,		token_attack,	N_("to attack a country") },
	{ TOKEN_TROPAS,		token_tropas,	N_("to send armies to a conquered country") },
	{ TOKEN_TARJETA,	token_card,	N_("to pick up a country-card") },
	{ TOKEN_REAGRUPE,	token_regroup,	N_("to reorder your armies") },
	{ TOKEN_TURNO,		token_turn,	N_("to finish your turn") },
	{ TOKEN_CANJE,		token_canje,	N_("to exchange your cards for armies") },
	{ TOKEN_EJER2,		token_ejer2,	N_("to place 2 armies in a country. You must have that card") },
	{ TOKEN_MISSION,	token_mission,	N_("request a mission") },
	{ TOKEN_COLOR,		token_color,	N_("to select a color") },
	{ TOKEN_LOQUE,		token_loque,	N_("to remind me what to do") },
	{ TOKEN_SURRENDER,	token_surrender, N_("to surrender") },
	{ TOKEN_SET,		token_set,	N_("to set options") },
	{ TOKEN_SCORES,		token_scores,	N_("to show the highscores") },
	{ TOKEN_ENUM_CARDS,	token_enum_cards, N_("to show the cards a player has") },
	{ TOKEN_ROBOT,		token_robot,	N_("to play with a robot") },
	{ TOKEN_NEW_ROUND,	token_new_round, N_("to know who started the round, and the round number") },
	{ TOKEN_MODALIDAD,	token_typeofgame, N_("to know the type of game it is being played") },
};
#define	NTOKENS  (sizeof(tokens)/sizeof(tokens[0]))


/* Sets a server option */
STATIC TEG_STATUS token_set(int fd, char *str)
{
	if(strlen(str)==0) {
		goto error;
	}

	if(option_parse(fd, str) == TEG_STATUS_SUCCESS) {
		return TEG_STATUS_SUCCESS;
	}

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_SET "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Sends the player the actions he must do. Usefull as callback */
STATIC TEG_STATUS token_loque(int fd, char *unused)
{
	PSPLAYER pJ;
	PLAY_DEBUG("token_loque()");
	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	switch(pJ->estado) {
	case PLAYER_STATUS_FICHAS:
		net_printf(fd, "%s=%d,%d\n", TOKEN_FICHAS, g_game.turno->numjug, g_game.fichas);
		break;
	case PLAYER_STATUS_FICHAS2:
		net_printf(fd, "%s=%d,%d\n", TOKEN_FICHAS2, g_game.turno->numjug, g_game.fichas);
		break;
	case PLAYER_STATUS_FICHASC:
	case PLAYER_STATUS_CANJE:
		aux_token_fichasc(g_game.turno);
		break;
	case PLAYER_STATUS_ATAQUE:
	case PLAYER_STATUS_TROPAS:
		net_printf(fd, "%s=%d\n", TOKEN_TURNO, g_game.turno->numjug);
		break;
	default:
		break;
	}

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_LOQUE "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Assigns the player a color */
STATIC TEG_STATUS token_color(int fd, char *str)
{
	int color;
	int a;
	PSPLAYER pJ;
	PLAY_DEBUG("token_color()");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(strlen(str)==0) {
		goto error;
	}

	if(pJ->estado != PLAYER_STATUS_CONNECTED) {
		goto error;
	}

	if(pJ->is_player == false) {
		goto error;
	}

	a = atoi(str);
	if(a < 0 ||  a >= maximum_player_count) {
		goto error;
	}

	color = a;
	if(color_libre(&color)  == false) {
		goto error;
	}

	pJ->estado = PLAYER_STATUS_HABILITADO;
	pJ->color = color;
	con_text_out(M_INF, _("Player %s(%d) has color %s\n"), pJ->name, pJ->numjug, _(g_colores[color]));

	netall_printf("%s=%s,%d,%d\n", TOKEN_NEWPLAYER, pJ->name, pJ->numjug, pJ->color);
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_COLOR "\n");
	return TEG_STATUS_PARSEERROR;
}


/* The player is asking a mission */
STATIC TEG_STATUS token_mission(int fd, char *unused)
{
	PSPLAYER pJ;
	PLAY_DEBUG("token_mission()");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(pJ->estado < PLAYER_STATUS_START) {
		goto error;
	}

	if(mission_asignar(pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	net_printf(fd, "%s=%d\n", TOKEN_MISSION, pJ->mission);
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_MISSION "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Called when a player finish his turn */
STATIC TEG_STATUS token_turn(int fd, char *unused)
{
	PSPLAYER pJ;
	PLAY_DEBUG("token_turn()");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(pJ->estado < PLAYER_STATUS_TURNOSTART || pJ->estado > PLAYER_STATUS_TURNOEND) {
		goto error;
	}

	if(pJ != g_game.turno) {
		con_text_out(M_ERR, _("BUG: The server believes that player `%s' does not have the turn"), pJ->name);
		goto error;
	}

	pJ->estado = PLAYER_STATUS_IDLE;

	if(turno_end(pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	/* give turn to the next player */
	if(turno_next() != TEG_STATUS_SUCCESS) {
		goto error;
	}

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_TURNO "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Sends all the player a message. An observer may use this function */
STATIC TEG_STATUS token_message(int fd, char *msg)
{
	PSPLAYER j;
	PLAY_DEBUG("token_message()\n");

	if(player_whoisfd(fd, &j)!=TEG_STATUS_SUCCESS || strlen(msg)==0) {
		goto error;
	}

	strip_invalid_msg(msg);

	if(j->is_player) {
		netall_printf("%s=%s,%d,\"%s\"\n", TOKEN_MESSAGE, j->name, j->numjug, msg);
	} else {
		netall_printf("%s=observer-%s,%d,\"%s\"\n", TOKEN_MESSAGE, j->name, j->numjug, msg);
	}
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_MESSAGE "\n");
	return TEG_STATUS_PARSEERROR;
}

/* mostly-Useless function */
STATIC TEG_STATUS token_rem(int fd, char *unused)
{
	PLAY_DEBUG("token_rem()\n");

	if(!SPLAYER_HABILITADO(fd)) {
		goto error;
	}

	net_printf(fd, "%s=%s\n", TOKEN_REM, _("Para que me envias un rem?"));
	return TEG_STATUS_SUCCESS;
error:
	return TEG_STATUS_PARSEERROR;
}

/* I want a better world "Quiero Un Mundo Mejor" */
STATIC TEG_STATUS token_qumm(int fd, char *str)
{
	PLAY_DEBUG("token_qumm()\n");

	if(!SPLAYER_HABILITADO(fd)) {
		goto error;
	}

	net_printf(fd, "%s=%s\n", TOKEN_REM, _("Yo tambien quiero un mundo mejor!"));
	return TEG_STATUS_SUCCESS;
error:
	return TEG_STATUS_PARSEERROR;
}

/* Creates a Player */
STATIC TEG_STATUS token_playerid(int fd, char *str)
{
	PARSER p{str};
	SPLAYER j, *pJ;
	char c[maximum_player_count];
	char colores[100];
	int i;
	int reconnect = false;

	PLAY_DEBUG("token_playerid( fd=%d)\n", fd);

	/* si existe entonces da error, porque no tiene que existir */
	if(player_whoisfd(fd, &pJ) == TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(strlen(str)==0) {
		goto error;
	}

	memset(&j, 0, sizeof(SPLAYER));

	/* averigua el name */
	if(p.parse_fragment()) {
		player_fillname(&j, p.token);
	} else {
		goto error;
	}

	if(p.parse_fragment()) {
		j.is_player = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_everything()) {
		j.human = atoi(p.token);
	} else {
		goto error;
	}


	if(j.is_player) {
		if(JUEGO_EMPEZADO) {
			if(!(reconnect = player_is_disconnected(&j))) {
				net_print(fd, TOKEN_GAMEINPROGRESS"\n");
				fd_remove(fd);
				return TEG_STATUS_CONNCLOSED;
			}
		}
		if(reconnect) {
			pJ = player_return_disconnected(&j);
		} else {
			pJ = player_ins_player(&j);
		}
	} else {
		pJ = player_ins_ro(&j);
	}


	if(pJ == NULL) {
		net_print(fd, TOKEN_SERVERFULL "\n");
		fd_remove(fd);
		return TEG_STATUS_CONNCLOSED;
	}

	pJ->fd = fd;

	aux_find_inaddr(pJ);

	if(reconnect) {

		pJ->estado = pJ->status_before_discon;

		net_printf(fd, "%s=%s,%d,%d\n", TOKEN_RECONNECT, pJ->name, pJ->numjug, pJ->color);
		con_text_out(M_INF, _("Player %s(%d) is re-connected from %s\n"), pJ->name, pJ->numjug, pJ->addr);

	} else {
		colores_libres(c);
		memset(colores, 0, sizeof(colores));

		for(i=0; i<maximum_player_count; i++) {
			char buf[100];
			sprintf(buf, ",%d", c[i]);
			strncat(colores, buf, sizeof(colores)-1);
		}

		net_printf(fd, "%s=%s,%d%s\n", TOKEN_PLAYERID, pJ->name, pJ->numjug, colores);

		con_text_out(M_INF, _("Player %s(%d) is connected from %s\n"), pJ->name, pJ->numjug, pJ->addr);
	}
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_PLAYERID "\n");
	return TEG_STATUS_PARSEERROR;
}

STATIC TEG_STATUS token_cversion(int fd, char *str)
{
	con_text_out(M_INF, _("Using client version: %s\n"), str);
	return TEG_STATUS_SUCCESS;
}

/* Parses the armies the player must place for the 1st time */
STATIC TEG_STATUS token_fichas(int fd, char *str)
{
	PLAY_DEBUG("token_fichas()\n");

	if(!SPLAYER_FICHAS(fd)) {
		goto error;
	}

	if(aux_token_fichas(fd, str, g_game.fichas, 0) == TEG_STATUS_SUCCESS) {

		g_game.turno->estado = PLAYER_STATUS_POSTFICHAS;

		fichas_next();
		return TEG_STATUS_SUCCESS;
	}

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_FICHAS "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Parses the armies the player must place for the 2nd time */
STATIC TEG_STATUS token_fichas2(int fd, char *str)
{
	PLAY_DEBUG("token_fichas2()\n");

	if(!SPLAYER_FICHAS2(fd)) {
		goto error;
	}

	if(aux_token_fichas(fd, str, g_game.fichas2, 0) == TEG_STATUS_SUCCESS) {

		g_game.turno->estado = PLAYER_STATUS_POSTFICHAS2;

		fichas2_next();
		return TEG_STATUS_SUCCESS;
	}
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_FICHAS2 "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Parses the armies the player must place the rest of the times */
STATIC TEG_STATUS token_fichasc(int fd, char *str)
{
	PSPLAYER pJ;
	int total_armies;
	PLAY_DEBUG("token_fichasc()\n");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(pJ->estado != PLAYER_STATUS_FICHASC && pJ->estado != PLAYER_STATUS_CANJE) {
		goto error;
	}

	total_armies = pJ->fichasc_armies + cont_tot(pJ->fichasc_conts);

	if(pJ->hizo_canje) {
		total_armies += cards_for_this_exchange(pJ->tot_exchanges);
	}

	if(aux_token_fichas(fd, str, total_armies, pJ->fichasc_conts) == TEG_STATUS_SUCCESS) {

		pJ->estado = PLAYER_STATUS_IDLE;

		fichasc_next();

		pJ->hizo_canje = false;
		pJ->fichasc_armies = 0;
		pJ->fichasc_conts = 0;

		return TEG_STATUS_SUCCESS;
	}

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_FICHASC "\n");
	return TEG_STATUS_PARSEERROR;
}

/* a player request to play with a robot. the robot will be launched in the server-side */
STATIC TEG_STATUS token_robot(int fd, char *str)
{
	PSPLAYER pJ;
	int newfd;

	PLAY_DEBUG("token_robot()\n");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(! pJ->is_player) {
		goto error;
	}

	if(JUEGO_EMPEZADO) {
		goto error;
	}

	if(launch_robot(&newfd, "--connected") != TEG_STATUS_SUCCESS) {
		goto error;
	}

	fd_add(newfd);

	return TEG_STATUS_SUCCESS;

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_ROBOT "\n");
	return TEG_STATUS_PARSEERROR;
}


/* A player is attacking from src to dst */
STATIC TEG_STATUS token_attack(int fd, char *str)
{
	PARSER p{str};
	int src, dst, src_lost, dst_lost;
	char d_src[3], d_dst[3];
	PSPLAYER pJ_src, pJ_dst;
	int conq = 0;
	int tropas = 0;
	char buffer[4096];

	PLAY_DEBUG("token_attack()\n");

	if(strlen(str)==0) {
		goto error;
	}

	if(!SPLAYER_ATAQUE_P(fd, &pJ_src)) {
		if(SPLAYER_TROPAS_P(fd, &pJ_src)) {
			pJ_src->estado=PLAYER_STATUS_ATAQUE;
			pJ_src->country_src = pJ_src->country_dst = -1;
		} else {
			goto error;
		}
	}

	if(p.parse_fragment()) {
		src = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_everything()) {
		dst = atoi(p.token);
	} else {
		goto error;
	}

	if(src >= COUNTRIES_CANT || src < 0 || dst >= COUNTRIES_CANT || dst < 0) {
		goto error;
	}

	if(pJ_src->numjug != g_countries[src].numjug || pJ_src->numjug == g_countries[dst].numjug)  {
		goto error;
	}

	if(g_countries[src].ejercitos < 2 || !countries_eslimitrofe(src, dst)) {
		goto error;
	}

	if(player_whois(g_countries[dst].numjug, &pJ_dst) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	/* aviso a todos que hay un attack */
	if(! g_game.fog_of_war) {
		netall_printf("%s=%d,%d\n", TOKEN_ATAQUE, src, dst);
	} else {
		fow_2_netall_printf(src, dst, "%s=%s,%s\n", TOKEN_ATAQUE, "%d", "%d");
	}

	/* so far, attack... */
	aux_token_attack(g_countries[src].ejercitos, g_countries[dst].ejercitos, &src_lost, &dst_lost, d_src, d_dst);

	g_countries[src].ejercitos -= src_lost;
	g_countries[dst].ejercitos -= dst_lost;
	pJ_src->tot_armies -= src_lost;
	pJ_dst->tot_armies -= dst_lost;

	/* updated statistics */
	pJ_src->player_stats.armies_killed += dst_lost;
	pJ_dst->player_stats.armies_killed += src_lost;

	/* conquisto el country */
	if(g_countries[dst].ejercitos == 0) {
		conq = 1;

		pJ_src->turno_conq++;
		pJ_src->tot_countries++;


		g_countries[dst].numjug = pJ_src->numjug;

		g_countries[dst].ejercitos++;		/* se pasa automaticamente */
		g_countries[src].ejercitos--;		/* un ejercito */

		tropas = g_countries[src].ejercitos - 1;	/* cantidad que se pueden pasar */
		if(tropas > 2) {		/* En verdad son 3, pero ya se le paso 1 */
			tropas =2;
		}

		pJ_src->estado = PLAYER_STATUS_TROPAS;
		pJ_src->country_src = src;
		pJ_src->country_dst = dst;

		pJ_dst->tot_countries--;

		g_countries[dst].numjug = pJ_src->numjug;

		/* updated statistics */
		pJ_src->player_stats.countries_won ++;
	}

	/* update the scores */
	stats_score(&pJ_src->player_stats, g_conts);
	stats_score(&pJ_dst->player_stats, g_conts);

	/* tell everybody the result of the attack */

	memset(buffer, 0, sizeof(buffer));

	if(! g_game.fog_of_war) {
		netall_printf("%s=%d,%d,%d,%d,%d,%d,%d,%d\n", TOKEN_DADOS,
		              src, d_src[0], d_src[1], d_src[2], dst, d_dst[0], d_dst[1], d_dst[2]);
	} else {
		fow_2_netall_printf(src, dst, "%s=%s,%d,%d,%d,%s,%d,%d,%d\n"
		                    , TOKEN_DADOS
		                    , "%d", d_src[0], d_src[1], d_src[2]
		                    , "%d", d_dst[0], d_dst[1], d_dst[2]);
	}

	if(! g_game.fog_of_war) {
		netall_printf("%s=%d,%d,%d;%s=%d,%d,%d\n",
		              TOKEN_COUNTRY, src, g_countries[src].numjug, g_countries[src].ejercitos,
		              TOKEN_COUNTRY, dst, g_countries[dst].numjug, g_countries[dst].ejercitos
		             );
	} else {
		fow_netall_printf(src, "%s=%d,%d,%d\n", TOKEN_COUNTRY,
		                  src, g_countries[src].numjug, g_countries[src].ejercitos);

		fow_netall_printf(dst, "%s=%d,%d,%d\n", TOKEN_COUNTRY,
		                  dst, g_countries[dst].numjug, g_countries[dst].ejercitos);
	}

	if(conq == 1) {

		/* Did 'dst' player lose the game ? */
		if(player_is_lost(pJ_dst)) {
			con_text_out(M_INF, _("Player %s(%d) lost the game\n"), pJ_dst->name, pJ_dst->numjug);
			netall_printf("%s=%d\n", TOKEN_LOST, pJ_dst->numjug);
			player_poner_perdio(pJ_dst);
		}

		/* Did 'src' player win the game ? */
		if(mission_chequear(pJ_src) == TEG_STATUS_GAMEOVER || game_is_finished()) {
			con_text_out(M_INF, _("Player %s(%d) is the winner! Game Over\n"), pJ_src->name, pJ_src->numjug);
			pJ_src->estado = PLAYER_STATUS_GAMEOVER;
			game_end(pJ_src);
			return TEG_STATUS_SUCCESS;
		}

		net_printf(fd, "%s=%d,%d,%d\n", TOKEN_TROPAS, src, dst, tropas);


		/* in FOW show the boundaries countries */
		if(g_game.fog_of_war) {
			char buffer[2048];

			memset(buffer, 0, sizeof(buffer));
			if(fow_fill_with_boundaries(dst, buffer, sizeof(buffer)) == TEG_STATUS_SUCCESS) {
				net_printf(fd, "%s\n", buffer);
			}
		}
	}

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_ATAQUE "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Place armies in the conquered country */
STATIC TEG_STATUS token_tropas(int fd, char *str)
{
	PARSER p{str};
	int src, dst, cant;
	PSPLAYER pJ;

	PLAY_DEBUG("token_tropas()\n");

	if(!SPLAYER_TROPAS_P(fd, &pJ) || strlen(str)==0) {
		goto error;
	}

	if(p.parse_fragment()) {
		src = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_fragment()) {
		dst = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_everything()) {
		cant = atoi(p.token);
	} else {
		goto error;
	}

	if(src >= COUNTRIES_CANT || src < 0 || dst >= COUNTRIES_CANT || dst < 0) {
		goto error;
	}

	if(src != pJ->country_src || dst != pJ->country_dst) {
		goto error;
	}

	if(cant > g_countries[src].ejercitos-1 || cant > 3) {
		goto error;
	}

	if(cant > 0) {

		g_countries[src].ejercitos -= cant;
		g_countries[dst].ejercitos += cant;

		pJ->estado=PLAYER_STATUS_ATAQUE;
		pJ->country_src = pJ->country_dst = -1;

		if(! g_game.fog_of_war)
			netall_printf("%s=%d,%d,%d;%s=%d,%d,%d\n",
			              TOKEN_COUNTRY, src, g_countries[src].numjug, g_countries[src].ejercitos,
			              TOKEN_COUNTRY, dst, g_countries[dst].numjug, g_countries[dst].ejercitos
			             );
		else {
			fow_netall_printf(src, "%s=%d,%d,%d\n",
			                  TOKEN_COUNTRY, src, g_countries[src].numjug, g_countries[src].ejercitos);
			fow_netall_printf(dst, "%s=%d,%d,%d\n",
			                  TOKEN_COUNTRY, dst, g_countries[dst].numjug, g_countries[dst].ejercitos);
		}
	}

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_TROPAS "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Ask for a card after the attack */
STATIC TEG_STATUS token_card(int fd, char *str)
{
	PSPLAYER pJ;
	PCOUNTRY pP;

	PLAY_DEBUG("token_card()\n");


	/* Veo si puede sacar una tarjeta... */
	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(pJ->estado < PLAYER_STATUS_TURNOSTART || pJ->estado >= PLAYER_STATUS_TARJETA) {
		goto error;
	}

	if(pJ->tot_cards >= maximum_country_cards) {
		goto error;
	}

	if(pJ->turno_conq < 1) {
		goto error;
	}

	if(pJ->tot_exchanges > 3 && pJ->turno_conq < 2) {
		goto error;
	}


	/* Puede sacar tarjeta */
	pJ->tot_cards++;
	pP = get_random_country(tarjeta_es_libre);

	pJ->estado = PLAYER_STATUS_TARJETA;

	tarjeta_sacar(&pP->tarjeta, pJ->numjug);

	/*
	 * Me fijo si el player es dueño del país que dice la tarjeta. Si es así
	 * le agrego 2 fichas automaticamente como dice el reglamento.
	 */
	if(pP->numjug == pJ->numjug) {
		pP->ejercitos += 2;
		pJ->tot_armies += 2;
		tarjeta_usar(&pP->tarjeta);
		if(! g_game.fog_of_war) {
			netall_printf("%s=%d,%d,%d\n", TOKEN_COUNTRY, pP->id, pP->numjug, pP->ejercitos);
		} else {
			fow_netall_printf(pP->id, "%s=%d,%d,%d\n", TOKEN_COUNTRY, pP->id, pP->numjug, pP->ejercitos);
		}
	}
	net_printf(fd, "%s=%d,%d\n", TOKEN_TARJETA, pP->id, pP->tarjeta.usada);

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_TARJETA "\n");
	return TEG_STATUS_ERROR;
}

/* request which cards do the players has */
STATIC TEG_STATUS token_enum_cards(int fd, char *str)
{
	PSPLAYER pJ;
	int i, first;
	char buffer[1024];

	PLAY_DEBUG("token_enum_cards()\n");


	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	memset(buffer, 0, sizeof(buffer));

	first=1;
	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].tarjeta.numjug == pJ->numjug) {
			char buffy[512];

			memset(buffy, 0, sizeof(buffy));

			if(first) {
				snprintf(buffy, sizeof(buffy)-1, "%d:%d", i, g_countries[i].tarjeta.usada);
				strncat(buffer, buffy, sizeof(buffer)-1);
				first = 0;
			} else {
				snprintf(buffy, sizeof(buffy)-1, ",%d:%d", i, g_countries[i].tarjeta.usada);
				strncat(buffer, buffy, sizeof(buffer)-1);
			}
		}
	}

	net_printf(fd, "%s=%s\n", TOKEN_ENUM_CARDS, buffer);

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_ENUM_CARDS "\n");
	return TEG_STATUS_ERROR;
}

/* request who started the round, and which round number */
STATIC TEG_STATUS token_new_round(int fd, char *str)
{
	PSPLAYER pJ;

	PLAY_DEBUG("token_new_round()\n");

	if(!JUEGO_EMPEZADO) {
		goto error;
	}

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	net_printf(fd, "%s=%d,%d\n", TOKEN_NEW_ROUND
	           , g_game.turno->numjug 	/* who starts the new turn */
	           , g_game.round_number	/* the round number */
	          );

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_NEW_ROUND "\n");
	return TEG_STATUS_ERROR;
}

/* Place 2 armies in the card's country. The player must own the country */
STATIC TEG_STATUS token_ejer2(int fd, char *str)
{
	PARSER p{str};
	int country;
	PSPLAYER j;

	PLAY_DEBUG("token_ejer2()\n");

	if(strlen(str)==0) {
		goto error;
	}

	if(player_whoisfd(fd, &j) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(j->estado != PLAYER_STATUS_TARJETA) {
		goto error;
	}

	if(p.parse_everything()) {
		country = atoi(p.token);
	} else {
		goto error;
	}

	if(country >= COUNTRIES_CANT || country < 0) {
		goto error;
	}

	if(tarjeta_es_usada(&g_countries[ country ].tarjeta)) {
		goto error;
	}

	if(g_countries[ country ].numjug == j->numjug && g_countries[ country ].tarjeta.numjug == j->numjug) {
		g_countries[ country ].ejercitos += 2;
		j->tot_armies += 2;
		tarjeta_usar(&g_countries[ country ].tarjeta);

		if(! g_game.fog_of_war)
			netall_printf("%s=%d,%d,%d\n", TOKEN_COUNTRY
			              , country, g_countries[ country ].numjug, g_countries[ country ].ejercitos);
		else
			fow_netall_printf(country, "%s=%d,%d,%d\n", TOKEN_COUNTRY
			                  , country, g_countries[ country ].numjug, g_countries[ country ].ejercitos);
	}

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_EJER2 "\n");
	return TEG_STATUS_PARSEERROR;
}


/* To exchange cards for armies */
STATIC TEG_STATUS token_canje(int fd, char *str)
{
	PARSER p{str};
	PSPLAYER pJ;
	int t1, t2, t3;
	int canj_ejer;

	PLAY_DEBUG("token_canje()\n");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(pJ->estado != PLAYER_STATUS_FICHASC) {
		goto error;
	}

	if(p.parse_fragment()) {
		t1 = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_fragment()) {
		t2 = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_everything()) {
		t3 = atoi(p.token);
	} else {
		goto error;
	}

	/* se puede hacer el canje ? */
	if(!tarjeta_puedocanje(pJ->numjug, t1, t2, t3)) {
		goto error;
	}

	pJ->estado = PLAYER_STATUS_CANJE;

	pJ->hizo_canje = true;
	pJ->tot_exchanges++;
	pJ->tot_cards -= 3;

	canj_ejer = cards_for_this_exchange(pJ->tot_exchanges);

	/* quitarle las tarjetas al player | mark the cards as unused*/
	tarjeta_poner(&g_countries[t1].tarjeta);
	tarjeta_poner(&g_countries[t2].tarjeta);
	tarjeta_poner(&g_countries[t3].tarjeta);


	netall_printf("%s=%d,%d,%d,%d,%d\n", TOKEN_CANJE,
	              pJ->numjug, canj_ejer, t1, t2, t3);
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_CANJE "\n");
	return TEG_STATUS_PARSEERROR;
}


/* Player is regrouping its armies */
STATIC TEG_STATUS token_regroup(int fd, char *str)
{
	PARSER p{str};
	int src, dst, cant;
	int ejer_disp;
	PSPLAYER pJ;

	PLAY_DEBUG("token_regroup()\n");


	if(strlen(str)==0) {
		goto error;
	}

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(pJ->estado < PLAYER_STATUS_TURNOSTART || pJ->estado > PLAYER_STATUS_REAGRUPE) {
		goto error;
	}

	if(p.parse_fragment()) {
		src = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_fragment()) {
		dst = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_everything()) {
		cant = atoi(p.token);
	} else {
		goto error;
	}

	if(src >= COUNTRIES_CANT || src < 0 || dst >= COUNTRIES_CANT || dst < 0 || cant <= 0) {
		goto error;
	}

	if(g_countries[src].numjug != pJ->numjug || g_countries[dst].numjug != pJ->numjug) {
		goto error;
	}

	if(!countries_eslimitrofe(src, dst)) {
		goto error;
	}

	ejer_disp = g_countries[src].ejercitos - g_countries[src].ejer_reagrupe - 1;
	if(cant > ejer_disp) {
		goto error;
	}

	pJ->estado = PLAYER_STATUS_REAGRUPE;

	g_countries[dst].ejercitos += cant;
	g_countries[dst].ejer_reagrupe += cant;
	g_countries[src].ejercitos -= cant;

	if(! g_game.fog_of_war) {
		netall_printf("%s=%d,%d,%d;%s=%d,%d,%d\n",
		              TOKEN_COUNTRY, src, g_countries[src].numjug, g_countries[src].ejercitos,
		              TOKEN_COUNTRY, dst, g_countries[dst].numjug, g_countries[dst].ejercitos);
	} else {
		fow_netall_printf(src, "%s=%d,%d,%d\n",
		                  TOKEN_COUNTRY, src, g_countries[src].numjug, g_countries[src].ejercitos);
		fow_netall_printf(dst, "%s=%d,%d,%d\n",
		                  TOKEN_COUNTRY, dst, g_countries[dst].numjug, g_countries[dst].ejercitos);
	}

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_REAGRUPE "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Show the status of all the players */
STATIC TEG_STATUS token_status(int fd, char *unused)
{
	char strout[PROT_MAX_LEN + max_playername_length * maximum_player_count + 200];

	PLAY_DEBUG("token_status()\n");

	if(!SPLAYER_CONNECTED(fd)) {
		goto error;
	}

	if(aux_token_stasta(strout, sizeof(strout) -1) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	net_printf(fd, "%s=%s\n", TOKEN_STATUS, strout);

	return TEG_STATUS_SUCCESS;

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_STATUS "\n");
	return TEG_STATUS_PARSEERROR;
}

/* send the hi-scores to the player */
STATIC TEG_STATUS token_scores(int fd, char *unused)
{
	char strout[PROT_MAX_LEN + max_playername_length * maximum_player_count + 200];

	PLAY_DEBUG("token_scores()\n");

	if(!SPLAYER_CONNECTED(fd)) {
		goto error;
	}

	if(scores_dump(strout, sizeof(strout) -1) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	net_printf(fd, "%s=%s\n", TOKEN_SCORES, strout);

	return TEG_STATUS_SUCCESS;

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_SCORES "\n");
	return TEG_STATUS_PARSEERROR;
}


/* Enums the players'countries , or of all players if player is -1 */
STATIC TEG_STATUS token_countries(int fd, char *str)
{
	PARSER p{str};
	int i;
	PSPLAYER pJ;
	char strout[PROT_MAX_LEN];

	PLAY_DEBUG("token_countries()\n");

	if(!JUEGO_EMPEZADO) {
		goto error;
	}

	if(strlen(str)==0) {
		goto error;
	}

	if(p.parse_everything()) {
		i = atoi(p.token);
	} else {
		goto error;
	}

	if(i != -1 && player_whois(i, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(i!=-1) {
		/* ask just for 1 player */
		aux_token_countries(pJ, strout, sizeof(strout)-1);
		net_printf(fd, "%s=%d/%s\n", TOKEN_COUNTRIES, pJ->numjug, strout);
	} else  {
		/* ask for all the players */

		player_map([&strout, fd](PSPLAYER pJ) {
			aux_token_countries(pJ, strout, sizeof(strout)-1);
			net_printf(fd, "%s=%d/%s\n", TOKEN_COUNTRIES, pJ->numjug, strout);
		});

		if(g_game.fog_of_war) {
			SPLAYER j;

			j.numjug = -1;

			aux_token_countries(&j, strout, sizeof(strout)-1);
			net_printf(fd, "%s=%d/%s\n", TOKEN_COUNTRIES, j.numjug, strout);
		}
	}
	return TEG_STATUS_SUCCESS;

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_COUNTRIES "\n");
	return TEG_STATUS_PARSEERROR;
}

/* To start the game */
TEG_STATUS token_start(int fd, char*)
{
	char strout[PROT_MAX_LEN + max_playername_length  * maximum_player_count + 200];
	PSPLAYER pJ;
	PLAY_DEBUG("token_start()\n");

	if(JUEGO_EMPEZADO || g_game.players < 2) {
		goto error;
	}

	if(g_server.with_console && fd != CONSOLE_FD) {
		if(!SPLAYER_HABILITADO_P(fd, &pJ) || !pJ->is_player) {
			goto error;
		}
	}

	JUEGO_EN_EMPEZAR;

	g_game.playing = g_game.players;

	con_text_out(M_INF, _("Starting game number: %d with seed: %u\n"), g_game.gamenumber, g_game.seed);

	player_all_set_status(PLAYER_STATUS_START);
	countries_repartir();

	if(turno_init() != TEG_STATUS_SUCCESS) {
		con_text_out(M_ERR, _("Error, cant initilize a new turn\n"));
		goto error;
	}

	JUEGO_EN_FICHAS;

	g_game.turno->estado = PLAYER_STATUS_FICHAS;

	aux_token_stasta(strout, sizeof(strout) -1);

	netall_printf("%s=%s;%s=%d,%d;%s=%d,%d,%d,%d;%s=%d,%d\n"
	              , TOKEN_START
	              , strout			/* available players */
	              , TOKEN_NEW_ROUND
	              , g_game.turno->numjug 	/* who starts the new turn */
	              , g_game.round_number	/* the round number */
	              , TOKEN_MODALIDAD
	              , g_game.mission	/* play with missions ? */
	              , g_game.cmission	/* play with common mission */
	              , g_game.fog_of_war	/* play with fog of war */
	              , g_game.reglas		/* which rules ? */
	              , TOKEN_FICHAS
	              , g_game.turno->numjug,	/* who starts ? */
	              g_game.fichas);	/* how many armies to place */
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_START "\n");
	return TEG_STATUS_PARSEERROR;
}

/* sends a player the type of game it is being played */
TEG_STATUS token_typeofgame(int fd, char *str)
{
	PSPLAYER pJ;
	PLAY_DEBUG("token_typeofgame()\n");

	if(JUEGO_EMPEZADO) {
		goto error;
	}

	if(!SPLAYER_HABILITADO_P(fd, &pJ) || !pJ->is_player) {
		goto error;
	}

	netall_printf("%s=%d,%d,%d,%d;\n"
	              , TOKEN_MODALIDAD
	              , g_game.mission	/* play with missions ? */
	              , g_game.cmission	/* play with common mission */
	              , g_game.fog_of_war	/* play with fog of war */
	              , g_game.reglas		/* which rules ? */
	             );
	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_MODALIDAD "\n");
	return TEG_STATUS_PARSEERROR;
}

/* For testing only */
STATIC TEG_STATUS token_test(int fd, char *unused)
{
	PLAY_DEBUG("token_test()\n");

	return TEG_STATUS_SUCCESS;
}

/* Server version */
STATIC TEG_STATUS token_sversion(int fd, char *unused)
{
	PLAY_DEBUG("token_sversion()\n");

	net_printf(fd, "%s=%s %s\n", TOKEN_SVERSION, _("TEG server version "), VERSION);
	return TEG_STATUS_SUCCESS;
}

/* Protocol version */
STATIC TEG_STATUS token_pversion(int fd, char *str)
{
	PSPLAYER pJ;
	PARSER p{str};
	int hi;

	PLAY_DEBUG("token_pversion()\n");


	if(strlen(str)==0) {
		goto error;
	}

	if(p.parse_fragment()) {
		hi = atoi(p.token);
	} else {
		goto error;
	}

	if(p.parse_everything()) {
		// We don't use the this integer value
	} else {
		goto error;
	}

	net_printf(fd, "%s=%i,%i\n", TOKEN_PVERSION, PROTOCOL_HIVER, PROTOCOL_LOVER);

	if(hi != PROTOCOL_HIVER) {
		con_text_out(M_ERR, _("Client with incompatible protocol version (server:%d , client:%d)\n"), PROTOCOL_HIVER, hi);

		if(player_whoisfd(fd, &pJ) == TEG_STATUS_SUCCESS) {
			player_del_hard(pJ);
		} else {
			fd_remove(fd);
		}
		return TEG_STATUS_CONNCLOSED;
	}

	return TEG_STATUS_SUCCESS;

error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_PVERSION "\n");
	return TEG_STATUS_PARSEERROR;
}

TEG_STATUS token_exit(int fd, char*)
{
	PSPLAYER pJ;
	PLAY_DEBUG("token_exit\n");

	if(player_whoisfd(fd, &pJ) == TEG_STATUS_SUCCESS) {
		player_del_hard(pJ);
	} else {
		fd_remove(fd);
	}

	return TEG_STATUS_CONNCLOSED;
}

/* Puts the player in Game Over state */
TEG_STATUS token_surrender(int fd, char *unused)
{
	PSPLAYER pJ;
	PLAY_DEBUG("token_surrender\n");

	if(player_whoisfd(fd, &pJ) != TEG_STATUS_SUCCESS) {
		goto error;
	}

	if(!pJ->is_player) {
		goto error;
	}

	if(pJ->estado < PLAYER_STATUS_HABILITADO) {
		goto error;
	}

	con_text_out(M_INF, _("Player %s(%d) abandoned the game\n"), pJ->name, pJ->numjug);

	netall_printf("%s=%d\n", TOKEN_SURRENDER, pJ->numjug);

	player_del_soft(pJ);

	return TEG_STATUS_SUCCESS;
error:
	net_print(fd, TOKEN_ERROR "=" TOKEN_SURRENDER "\n");
	return TEG_STATUS_PARSEERROR;
}

/* Ask for help */
STATIC TEG_STATUS token_help(int fd, char *unused)
{
	for(unsigned i=0; i<NTOKENS; i++) {
		if(tokens[i].func)
			net_printf(fd, "%s='%s' %s\n", TOKEN_REM,
			           tokens[i].label, _(tokens[i].help));
	}
	return TEG_STATUS_SUCCESS;
}

/* Parses the tokens */
STATIC TEG_STATUS token_lookup(int fd, PARSER *p)
{
	for(unsigned i=0; i<NTOKENS; i++) {
		if(strcasecmp(p->token, tokens[i].label)==0) {
			if(tokens[i].func) {
				return((tokens[i].func)(fd, p->value));
			}
			return TEG_STATUS_TOKENNULL;
		}
	}
	PLAY_DEBUG("Token '%s' no encontrado\n", p->token);
	return TEG_STATUS_TOKENNOTFOUND;
}

/* Read the file descriptor and call the apropiate function */
TEG_STATUS play_teg(int fd)
{
	if(g_game.fog_of_war) {
		g_game.player_fow = NULL;
	}

	char str[PROT_MAX_LEN];
	str[0]=0;

	if(net_readline(fd, str, PROT_MAX_LEN)<1) {
		PSPLAYER pJ;
		if(player_whoisfd(fd, &pJ) == TEG_STATUS_SUCCESS) {
			player_del_hard(pJ);
		} else {
			fd_remove(fd);
		}

		return TEG_STATUS_CONNCLOSED;
	}

	if(g_game.fog_of_war) {
		if(player_whoisfd(fd, &g_game.player_fow) != TEG_STATUS_SUCCESS) {
			g_game.player_fow = NULL;
		}
	}

	PARSER p{str, '=', ';'};

	do {
		if(!p.parse()) {
			break;
		}

		if(token_lookup(fd, &p) == TEG_STATUS_CONNCLOSED) {
			return TEG_STATUS_CONNCLOSED;
		}
	} while(p.can_continue());

	return TEG_STATUS_SUCCESS;
}

}