2 Copyright (c) 1993 Richard V. Nash.
3 Copyright (c) 2000 Dan Papasian
4 Copyright (C) Andrew Tridgell 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static int check_kings(struct game_state_t *gs);
24 static int get_empty_slot(void);
25 static int ReadGameAttrs_common(FILE *fp, int g, int version);
26 static long OldestHistGame(char *login);
27 static void RemoveHistGame(char *file, int maxlines);
28 static void write_g_out(int g, char *file, int maxlines, int isDraw, char *EndSymbol, char *name, time_t *now);
29 static int game_zero(int g);
31 const char *TypeStrings[NUM_GAMETYPES] = {"untimed", "blitz", "standard",
32 "nonstandard", "wild", "lightning",
35 /* this method is awful! how about allocation as we need it and freeing
37 static int get_empty_slot(void)
41 for (i = 0; i < game_globals.g_num; i++) {
42 if (game_globals.garray[i].status == GAME_EMPTY)
46 game_globals.garray = (struct game *)realloc(game_globals.garray, sizeof(struct game) * game_globals.g_num);
47 /* yeah great, bet this causes lag! - DAV*/
48 /* I have serious doubt of the truth to the above client- bugg */
49 game_globals.garray[game_globals.g_num - 1].status = GAME_EMPTY;
50 return game_globals.g_num - 1;
55 int new = get_empty_slot();
60 static int game_zero(int g)
62 ZERO_STRUCT(game_globals.garray[g]);
64 game_globals.garray[g].white = -1;
65 game_globals.garray[g].black = -1;
67 game_globals.garray[g].status = GAME_NEW;
68 game_globals.garray[g].link = -1;
69 game_globals.garray[g].result = END_NOTENDED;
70 game_globals.garray[g].type = TYPE_UNTIMED;
71 game_globals.garray[g].game_state.gameNum = g;
72 game_globals.garray[g].wInitTime = 300; /* 5 minutes */
73 game_globals.garray[g].wIncrement = 0;
74 game_globals.garray[g].bInitTime = 300; /* 5 minutes */
75 game_globals.garray[g].bIncrement = 0;
76 game_globals.garray[g].flag_pending = FLAG_NONE;
77 strcpy(game_globals.garray[g].FENstartPos,INITIAL_FEN);
83 FREE(game_globals.garray[g].moveList);
84 FREE(game_globals.garray[g].examMoveList);
85 game_globals.garray[g].moveList = NULL;
86 game_globals.garray[g].examMoveList = NULL;
87 game_globals.garray[g].moveListSize = 0;
88 game_globals.garray[g].examMoveListSize = 0;
92 int game_remove(int g)
94 /* Should remove game from players observation list */
97 game_globals.garray[g].status = GAME_EMPTY;
101 /* old moves not stored now - uses smoves */
102 int game_finish(int g)
104 player_game_ended(g); /* Alert playerdb that game ended */
109 void MakeFENpos (int g, char *FEN)
111 strcpy(FEN, boardToFEN(g));
114 static char *game_time_str(int wt, int winc, int bt, int binc)
116 static char tstr[50];
118 if ((!wt) && (!winc)) { /* Untimed */
122 if ((wt == bt) && (winc == binc)) {
123 sprintf(tstr, " %d %d", wt, winc);
125 sprintf(tstr, " %d %d : %d %d", wt, winc, bt, binc);
130 const char *bstr[] = {"untimed", "blitz", "standard", "non-standard", "wild", "lightning", "Bughouse"};
132 const char *rstr[] = {"unrated", "rated"};
134 char *game_str(int rated, int wt, int winc, int bt, int binc,
135 char *cat, char *board)
137 static char tstr[200];
139 if (cat && cat[0] && board && board[0] &&
140 (strcmp(cat, "standard") || strcmp(board, "standard"))) {
141 sprintf(tstr, "%s %s%s Loaded from %s/%s",
143 bstr[game_isblitz(wt / 60, winc, bt / 60, binc, cat, board)],
144 game_time_str(wt / 60, winc, bt / 60, binc),
147 sprintf(tstr, "%s %s%s",
149 bstr[game_isblitz(wt / 60, winc, bt / 60, binc, cat, board)],
150 game_time_str(wt / 60, winc, bt / 60, binc));
155 int game_isblitz(int wt, int winc, int bt, int binc,
156 char *cat, char *board)
161 if (!strcmp(cat, "bughouse"))
162 return TYPE_BUGHOUSE;
163 if (board && board[0]) {
164 if (!strcmp(cat, "wild"))
166 if (strcmp(cat, "standard") || strcmp(board, "standard"))
167 return TYPE_NONSTANDARD;
171 if ((wt == 0) || (bt == 0))
172 /* nonsense if one is timed and one is not */
175 if ((wt != bt) || (winc != binc))
176 return TYPE_NONSTANDARD;
177 total = wt * 60 + winc * 40;
178 if (total < 180) /* 3 minute */
180 if (total >= 900) /* 15 minutes */
186 void send_board_to(int g, int p)
188 struct player *pp = &player_globals.parray[p];
193 /* since we know g and p, figure out our relationship to this game */
196 if ((game_globals.garray[g].status == GAME_EXAMINE) || (game_globals.garray[g].status == GAME_SETUP)) {
205 relation = ((side == game_globals.garray[g].game_state.onMove) ? 1 : -1);
211 if (CheckPFlag(p, PFLAG_FLIP)) { /* flip board? */
218 b = board_to_string(game_globals.garray[g].white_name,
219 game_globals.garray[g].black_name,
220 game_globals.garray[g].wTime,
221 game_globals.garray[g].bTime,
222 &game_globals.garray[g].game_state,
223 (game_globals.garray[g].status == GAME_EXAMINE || game_globals.garray[g].status == GAME_SETUP) ?
224 game_globals.garray[g].examMoveList : game_globals.garray[g].moveList,
229 if (pp->game == g && net_globals.con[pp->socket]->timeseal) {
230 pprintf_noformat(p, "\n%s\n[G]\n", b);
232 pprintf_noformat(p, "\n%s", b);
235 if (p != command_globals.commanding_player) {
240 void send_boards(int g)
243 struct simul_info_t *simInfo = player_globals.parray[game_globals.garray[g].white].simul_info;
244 int which_board = -1;
246 /* Begin code added 3/28/96 - Figbert */
248 if ((game_globals.garray[g].status != GAME_SETUP) &&
249 (check_kings(&game_globals.garray[g].game_state))) {
250 d_printf( "Game has invalid amount of kings. Aborting...\n");
253 for (p = 0; p < player_globals.p_num; p++) {
254 struct player *pp = &player_globals.parray[p];
255 if (pp->status == PLAYER_EMPTY)
261 if (p1 >= 0 && player_globals.parray[p1].game == g) {
262 pprintf (p1, "Disconnecting you from game number %d.\n", g+1);
263 player_globals.parray[p1].game = -1;
266 pprintf (p, "Disconnecting you from game number %d.\n", g+1);
273 /* End code added 3/28/96 - Figbert */
276 which_board = simInfo->boards[simInfo->onBoard];
278 if ((simInfo == NULL) || (which_board == g)) {
279 for (p = 0; p < player_globals.p_num; p++) {
280 struct player *pp = &player_globals.parray[p];
281 if (pp->status == PLAYER_EMPTY)
283 if (player_is_observe(p, g) || (pp->game == g))
289 void game_update_time(int g)
292 unsigned now, timesince;
298 gg = &game_globals.garray[g];
300 /* no update on first move */
301 if (gg->game_state.moveNum == 1)
303 if (gg->clockStopped)
305 if (gg->type == TYPE_UNTIMED)
308 timesince = now - gg->lastDecTime;
309 if (gg->game_state.onMove == WHITE) {
310 gg->wTime -= timesince;
312 gg->bTime -= timesince;
314 gg->lastDecTime = now;
318 static void game_update_times(void)
322 for (g = 0; g < game_globals.g_num; g++) {
323 if (game_globals.garray[g].status != GAME_ACTIVE)
325 if (game_globals.garray[g].clockStopped)
332 char *EndString(int g, int personal)
334 static char endstr[200];
335 char *blackguy, *whiteguy;
336 static char blackstr[] = "Black";
337 static char whitestr[] = "White";
339 blackguy = (personal ? game_globals.garray[g].black_name : blackstr);
340 whiteguy = (personal ? game_globals.garray[g].white_name : whitestr);
342 switch (game_globals.garray[g].result) {
344 sprintf(endstr, "%s checkmated",
345 game_globals.garray[g].winner == WHITE ? blackguy : whiteguy);
348 sprintf(endstr, "%s resigned",
349 game_globals.garray[g].winner == WHITE ? blackguy : whiteguy);
352 sprintf(endstr, "%s ran out of time",
353 game_globals.garray[g].winner == WHITE ? blackguy : whiteguy);
356 sprintf(endstr, "Game drawn by mutual agreement");
359 sprintf(endstr, "Game drawn because both players ran out of time");
362 sprintf(endstr, "Game drawn by repetition");
365 sprintf(endstr, "Draw by the 50 move rule");
368 sprintf(endstr, "Game adjourned by mutual agreement");
370 case END_LOSTCONNECTION:
371 sprintf(endstr, "%s lost connection, game adjourned",
372 game_globals.garray[g].winner == WHITE ? whiteguy : blackguy);
375 sprintf(endstr, "Game aborted by mutual agreement");
378 sprintf(endstr, "Stalemate.");
381 sprintf(endstr, "Still in progress");
384 sprintf(endstr, "Game courtesyaborted by %s",
385 game_globals.garray[g].winner == WHITE ? whiteguy : blackguy);
387 case END_COURTESYADJOURN:
388 sprintf(endstr, "Game courtesyadjourned by %s",
389 game_globals.garray[g].winner == WHITE ? whiteguy : blackguy);
392 sprintf(endstr, "Game drawn because neither player has mating material");
394 case END_FLAGNOMATERIAL:
395 sprintf(endstr, "%s ran out of time and %s has no material to mate",
396 game_globals.garray[g].winner == WHITE ? blackguy : whiteguy,
397 game_globals.garray[g].winner == WHITE ? whiteguy : blackguy);
400 sprintf(endstr, "Game drawn by adjudication");
403 sprintf(endstr, "%s wins by adjudication",
404 game_globals.garray[g].winner == WHITE ? whiteguy : blackguy);
407 sprintf(endstr, "Game aborted by adjudication");
410 sprintf(endstr, "???????");
417 const char *EndSym(int g)
419 static const char *symbols[] = {"1-0", "0-1", "1/2-1/2", "*"};
421 switch (game_globals.garray[g].result) {
426 return ((game_globals.garray[g].winner == WHITE) ? symbols[0] : symbols[1]);
434 case END_FLAGNOMATERIAL:
445 /* This should be enough to hold any game up to at least 8000 moves
446 * If we overwrite this, the server will crash :-).
448 /* 8000? who you trying to kid? this is awful - enough for 600 halfs :) -DAV*/
449 #define GAME_STRING_LEN 19000
450 static char gameString[GAME_STRING_LEN];
451 char *movesToString(int g, int pgn)
457 struct move_t *moves;
459 if (game_globals.garray[g].status == GAME_EXAMINE || game_globals.garray[g].status == GAME_SETUP)
460 moves = game_globals.garray[g].examMoveList;
461 else moves = game_globals.garray[g].moveList;
463 wr = game_globals.garray[g].white_rating;
464 br = game_globals.garray[g].black_rating;
467 curTime = untenths(game_globals.garray[g].timeOfStart);
471 "\n[Event \"%s %s %s game\"]\n"
472 "[Site \"%s, %s\"]\n",
473 config_get_tmp("SERVER_NAME"),
474 rstr[game_globals.garray[g].rated], bstr[game_globals.garray[g].type],
475 config_get_tmp("SERVER_NAME"),
476 config_get_tmp("SERVER_LOCATION"));
477 strftime(tmp, sizeof(tmp),
478 "[Date \"%Y.%m.%d\"]\n"
479 "[Time \"%H:%M:%S\"]\n",
480 localtime((time_t *) &curTime));
481 strcat(gameString, tmp);
486 "[WhiteElo \"%d\"]\n"
487 "[BlackElo \"%d\"]\n",
488 game_globals.garray[g].white_name, game_globals.garray[g].black_name, wr, br);
489 strcat(gameString, tmp);
491 "[TimeControl \"%d+%d\"]\n"
493 "[Result \"%s\"]\n\n",
494 game_globals.garray[g].wInitTime / 10, game_globals.garray[g].wIncrement / 10, EndSym(g));
495 strcat(gameString, tmp);
498 for (i = 0; i < game_globals.garray[g].numHalfMoves; i++) {
499 if (moves[i].color == WHITE) {
500 if ((col += sprintf(tmp, "%d. ", (i+1) / 2 + 1)) > 70) {
501 strcat(gameString, "\n");
504 strcat(gameString, tmp);
506 strcat (tmp, "1. ... ");
509 if ((col += sprintf(tmp, "%s ", moves[i].algString)) > 70) {
510 strcat(gameString, "\n");
513 strcat(gameString, tmp);
515 strcat(gameString, "\n");
519 sprintf(gameString, "\n%s ", game_globals.garray[g].white_name);
521 sprintf(tmp, "(%d) ", wr);
523 sprintf(tmp, "(UNR) ");
525 strcat(gameString, tmp);
526 sprintf(tmp, "vs. %s ", game_globals.garray[g].black_name);
527 strcat(gameString, tmp);
529 sprintf(tmp, "(%d) ", br);
531 sprintf(tmp, "(UNR) ");
533 strcat(gameString, tmp);
534 strcat(gameString, "--- ");
535 strcat(gameString, strltime(&curTime));
536 if (game_globals.garray[g].rated) {
537 strcat(gameString, "\nRated ");
539 strcat(gameString, "\nUnrated ");
541 strcat (gameString, TypeStrings[game_globals.garray[g].type]);
542 strcat(gameString, " match, initial time: ");
543 if ((game_globals.garray[g].bInitTime != game_globals.garray[g].wInitTime) || (game_globals.garray[g].wIncrement != game_globals.garray[g].bIncrement)) { /* different starting times */
544 sprintf(tmp, "%d minutes, increment: %d seconds AND %d minutes, increment: %d seconds.\n\n", game_globals.garray[g].wInitTime / 600, game_globals.garray[g].wIncrement / 10, game_globals.garray[g].bInitTime / 600, game_globals.garray[g].bIncrement / 10);
546 sprintf(tmp, "%d minutes, increment: %d seconds.\n\n", game_globals.garray[g].wInitTime / 600, game_globals.garray[g].wIncrement / 10);
548 strcat(gameString, tmp);
549 sprintf(tmp, "Move %-19s%-19s\n", game_globals.garray[g].white_name, game_globals.garray[g].black_name);
550 strcat(gameString, tmp);
551 strcat(gameString, "---- ---------------- ----------------\n");
553 for (i = 0; i < game_globals.garray[g].numHalfMoves; i += 2) {
554 if (i==0 && (moves[i].color == BLACK)) {
555 sprintf(tmp, "%3d. %-16s %-16s\n", (i+1)/2 + 1, "...",
556 move_and_time(&moves[i]));
558 } else if (i + 1 < game_globals.garray[g].numHalfMoves) {
559 sprintf(tmp, "%3d. %-16s ", (i+1)/2 + 1, move_and_time(&moves[i]));
560 strcat(gameString, tmp);
561 sprintf(tmp, "%-16s\n", move_and_time(&moves[i+1]));
563 sprintf(tmp, "%3d. %-16s\n", (i+1)/2 + 1, move_and_time(&moves[i]));
565 strcat(gameString, tmp);
566 if (strlen(gameString) > GAME_STRING_LEN - 100) { /* Bug out if getting
567 close to filling this
573 strcat(gameString, " ");
576 sprintf(tmp, "{%s} %s\n", EndString(g, 0), EndSym(g));
577 strcat(gameString, tmp);
582 void game_disconnect(int g, int p)
584 game_ended(g, (game_globals.garray[g].white == p) ? WHITE : BLACK, END_LOSTCONNECTION);
587 int CharToPiece(char c)
619 char PieceToChar(int piece)
622 case W_PAWN:return 'P';
651 write a new style generic game file
653 static void WriteGameFile_v100(FILE * fp, int g)
655 struct game gg = game_globals.garray[g];
658 /* zero any elements we don't want to save */
659 memset(&gg, 0, offsetof(struct game, not_saved_marker));
660 gg.game_state.gameNum = 0;
662 /* marshall it into a string */
663 s = marshall_game(&gg);
665 d_printf("Unable to marshall game structure!\n");
670 fprintf(fp, "v 100\n%s\n", s);
676 read a game file using the new generic and extensible format
678 static int ReadGameAttrs_v100(FILE *fp, int g)
681 struct game *gg = &game_globals.garray[g];
684 s = fd_load(fileno(fp), NULL);
686 d_printf("Error reading game file!\n");
690 /* skip first line */
691 s2 = strchr(s, '\n');
693 /* remember the game state for later */
696 /* the marshaller doesn't save zero elements, but some elements don't
697 default to zero. Make sure they get the right value */
698 memset(&gg->not_saved_marker, 0,
699 sizeof(struct game) - offsetof(struct game, not_saved_marker));
700 gg->game_state.gameNum = g;
702 if (!s2 || unmarshall_game(gg, s2) != 0) {
703 d_printf("Error unmarshalling game data!\n");
709 /* when examining we are not supposed to restore the game
710 state, so put it back here */
711 if (g1.status == GAME_EXAMINE || g1.status == GAME_SETUP) {
712 gg->game_state = g1.game_state;
714 gg->status = g1.status;
716 /* cope with continuing a game with timeseal that was started without it */
717 gg->wRealTime = gg->wTime * 100;
718 gg->bRealTime = gg->bTime * 100;
724 static int ReadGameAttrs_common(FILE * fp, int g,int version)
726 if (version == 9 || version == 100) {
727 return ReadGameAttrs_v100(fp, g);
730 return ReadGameAttrs_old(fp, g, version);
733 int ReadGameAttrs_exam(FILE * fp, int g)
736 char line[MAX_GLINE_SIZE];
738 fgets(line, MAX_GLINE_SIZE - 1, fp);
740 if (line[0] == 'v') {
741 sscanf(line, "%*c %d", &version);
746 return ReadGameAttrs_common(fp,g,version);
752 int ReadGameAttrs(FILE * fp, int g)
755 char line[MAX_GLINE_SIZE];
757 fgets(line, MAX_GLINE_SIZE - 1, fp);
759 if (line[0] == 'v') {
760 sscanf(line, "%*c %d", &version);
762 return ReadGameAttrs_common(fp,g,version);
765 int game_read(int g, int wp, int bp)
770 game_globals.garray[g].white = wp;
771 game_globals.garray[g].black = bp;
772 /* game_globals.garray[g].old_white = -1;
773 game_globals.garray[g].old_black = -1;
775 game_globals.garray[g].moveListSize = 0;
776 game_globals.garray[g].game_state.gameNum = g;
777 strcpy(game_globals.garray[g].white_name, player_globals.parray[wp].name);
778 strcpy(game_globals.garray[g].black_name, player_globals.parray[bp].name);
779 if (game_globals.garray[g].type == TYPE_BLITZ) {
780 game_globals.garray[g].white_rating = player_globals.parray[wp].b_stats.rating;
781 game_globals.garray[g].black_rating = player_globals.parray[bp].b_stats.rating;
782 } else if (game_globals.garray[g].type == TYPE_WILD) {
783 game_globals.garray[g].white_rating = player_globals.parray[wp].w_stats.rating;
784 game_globals.garray[g].black_rating = player_globals.parray[bp].w_stats.rating;
785 } else if (game_globals.garray[g].type == TYPE_LIGHT) {
786 game_globals.garray[g].white_rating = player_globals.parray[wp].l_stats.rating;
787 game_globals.garray[g].black_rating = player_globals.parray[bp].l_stats.rating;
788 } else if (game_globals.garray[g].type == TYPE_BUGHOUSE) {
789 game_globals.garray[g].white_rating = player_globals.parray[wp].bug_stats.rating;
790 game_globals.garray[g].black_rating = player_globals.parray[bp].bug_stats.rating;
792 game_globals.garray[g].white_rating = player_globals.parray[wp].s_stats.rating;
793 game_globals.garray[g].black_rating = player_globals.parray[bp].s_stats.rating;
795 fp = fopen_p("%s/%c/%s-%s", "r", ADJOURNED_DIR, player_globals.parray[wp].login[0],
796 player_globals.parray[wp].login, player_globals.parray[bp].login);
800 for (piece=PAWN; piece <= QUEEN; piece++) {
801 game_globals.garray[g].game_state.holding[0][piece-PAWN]
802 = game_globals.garray[g].game_state.holding[1][piece-PAWN] = 0;
804 if (ReadGameAttrs(fp, g) < 0) {
810 if (game_globals.garray[g].result == END_ADJOURN
811 || game_globals.garray[g].result == END_COURTESYADJOURN)
812 game_globals.garray[g].result = END_NOTENDED;
813 game_globals.garray[g].status = GAME_ACTIVE;
814 game_globals.garray[g].startTime = tenth_secs();
815 game_globals.garray[g].lastMoveTime = game_globals.garray[g].startTime;
816 game_globals.garray[g].lastDecTime = game_globals.garray[g].startTime;
818 /* cope with continuing a game with timeseal that was started without it */
819 game_globals.garray[g].wRealTime = game_globals.garray[g].wTime * 100;
820 game_globals.garray[g].bRealTime = game_globals.garray[g].bTime * 100;
822 /* Need to do notification and pending cleanup */
826 int game_delete(int wp, int bp)
828 char fname[MAX_FILENAME_SIZE];
829 char lname[MAX_FILENAME_SIZE];
831 sprintf(fname, "%s/%c/%s-%s", ADJOURNED_DIR, player_globals.parray[wp].login[0],
832 player_globals.parray[wp].login, player_globals.parray[bp].login);
833 sprintf(lname, "%s/%c/%s-%s", ADJOURNED_DIR, player_globals.parray[bp].login[0],
834 player_globals.parray[wp].login, player_globals.parray[bp].login);
843 struct player *wp, *bp;
844 struct game *gg = &game_globals.garray[g];
847 wp = &player_globals.parray[gg->white];
848 bp = &player_globals.parray[gg->black];
849 asprintf(&fname, "%s/%c/%s-%s", ADJOURNED_DIR, wp->login[0],
850 wp->login, bp->login);
851 fp = fopen_p("%s", "w", fname);
853 d_printf("CHESSD: Problem opening adjourn file %s-%s for write\n",
854 wp->login, bp->login);
858 WriteGameFile_v100(fp, g);
860 /* Create link for easier stored game finding */
861 if (bp->login[0] != wp->login[0]) {
863 asprintf(&lname, "%s/%c/%s-%s", ADJOURNED_DIR, bp->login[0],
864 wp->login, bp->login);
872 static long OldestHistGame(char *login)
877 fp = fopen_p("%s/player_data/%c/%s.%s", "r", STATS_DIR,
878 login[0], login, STATS_GAMES);
880 fp = fopen_p("%s/player_data/%c/.rem.%s.%s", "r", STATS_DIR,
881 login[0], login, STATS_GAMES);
885 fscanf(fp, "%*d %*c %*d %*c %*d %*s %*s %*d %*d %*d %*d %*s %*s %ld",
891 static void RemoveHistGame(char *file, int maxlines)
894 char GameFile[MAX_FILENAME_SIZE];
895 char Opponent[MAX_LOGIN_NAME];
896 char line[MAX_LINE_SIZE];
900 fp = fopen_p("%s", "r", file);
904 fgets(line, MAX_LINE_SIZE - 1, fp);
905 sscanf(line, "%*d %*c %*d %*c %*d %s %*s %*d %*d %*d %*d %*s %*s %ld",
910 fgets(line, MAX_LINE_SIZE - 1, fp);
917 if (count > maxlines) {
918 truncate_file(file, maxlines);
920 oppWhen = OldestHistGame(Opponent);
921 if (oppWhen > When || oppWhen <= 0L) {
922 sprintf(GameFile, "%s/%ld/%ld", HISTORY_DIR, When % 100, When);
928 void RemHist(char *who)
931 char Opp[MAX_LOGIN_NAME];
934 fp = fopen_p("%s/player_data/%c/%s.%s", "r",
936 who[0], who, STATS_GAMES);
941 fscanf(fp, "%*d %*c %*d %*c %*d %s %*s %*d %*d %*d %*d %*s %*s %ld",
944 oppWhen = OldestHistGame(Opp);
945 if (oppWhen > When || oppWhen <= 0L) {
947 asprintf(&fName, "%s/%ld/%ld", HISTORY_DIR, When % 100, When);
955 static void write_g_out(int g, char *file, int maxlines, int isDraw,
956 char *EndSymbol, char *name, time_t *now)
968 wp = game_globals.garray[g].white;
969 bp = game_globals.garray[g].black;
971 if (game_globals.garray[g].private) {
976 if (game_globals.garray[g].type == TYPE_BLITZ) {
977 wr = player_globals.parray[wp].b_stats.rating;
978 br = player_globals.parray[bp].b_stats.rating;
980 } else if (game_globals.garray[g].type == TYPE_WILD) {
981 wr = player_globals.parray[wp].w_stats.rating;
982 br = player_globals.parray[bp].w_stats.rating;
984 } else if (game_globals.garray[g].type == TYPE_STAND) {
985 wr = player_globals.parray[wp].s_stats.rating;
986 br = player_globals.parray[bp].s_stats.rating;
988 } else if (game_globals.garray[g].type == TYPE_LIGHT) {
989 wr = player_globals.parray[wp].l_stats.rating;
990 br = player_globals.parray[bp].l_stats.rating;
992 } else if (game_globals.garray[g].type == TYPE_BUGHOUSE) {
993 wr = player_globals.parray[wp].bug_stats.rating;
994 br = player_globals.parray[bp].bug_stats.rating;
999 if (game_globals.garray[g].type == TYPE_NONSTANDARD)
1004 if (game_globals.garray[g].rated) {
1011 fp = fopen_s(file, "r");
1014 fgets(tmp, 1024, fp);
1015 sscanf(ptmp, "%d", &count);
1018 count = (count + 1) % 100;
1020 fp = fopen_s(file, "a");
1026 /* Counter Result MyRating MyColor OppRating OppName [pbr 2 12 2 12] ECO End Date */
1027 if (name == player_globals.parray[wp].name) {
1030 else if (game_globals.garray[g].winner == WHITE)
1035 fprintf(fp, "%d %c %d W %d %s %s %d %d %d %d %s %s %ld\n",
1036 count, cResult, wr, br, player_globals.parray[bp].name, type,
1037 game_globals.garray[g].wInitTime, game_globals.garray[g].wIncrement,
1038 game_globals.garray[g].bInitTime, game_globals.garray[g].bIncrement,
1045 else if (game_globals.garray[g].winner == BLACK)
1050 fprintf(fp, "%d %c %d B %d %s %s %d %d %d %d %s %s %ld\n",
1051 count, cResult, br, wr, player_globals.parray[wp].name, type,
1052 game_globals.garray[g].wInitTime, game_globals.garray[g].wIncrement,
1053 game_globals.garray[g].bInitTime, game_globals.garray[g].bIncrement,
1060 RemoveHistGame(file, maxlines);
1063 /* Test if entry is present - 1 yes 0 no */
1064 /* returns -1 if an error */
1066 char get_journalgame_type(int p,char* fname,char slot)
1069 struct player *pp = &player_globals.parray[p];
1073 FILE* fp = fopen_s(fname,"r");
1075 d_printf("Corrupt journal file! %s\n",fname);
1076 pprintf (p, "The journal file is corrupt! See an admin.\n");
1081 if (fscanf(fp, "%c %*s %*d %*s %*d %s %*d %*d %*s %*s %*s\n",
1082 &cur_slot,type) != 2) {
1083 d_printf( "CHESSD: Error in journal info format for player %s.\n",
1085 pprintf(p, "The journal file is corrupt! Error in internal format.\n");
1088 if (cur_slot == slot) {
1101 /* Returns 1 if successful */
1103 int removejournalitem(int p, char slot,FILE* fp,char* fname, int* empty)
1111 fp_new = fopen_s(fname, "w");
1113 d_printf("Can't write to journal %s!\n",fname);
1114 pprintf (p, "Was unable to write to the file! See an admin.\n");
1117 j = (struct journal*) malloc(sizeof(struct journal));
1119 if (fscanf(fp, "%c %s %d %s %d %s %d %d %s %s %s\n",
1130 d_printf( "CHESSD: Error in journal info format. %s\n", fname);
1131 pprintf(p, "The journal file is corrupt! Error in internal format.\n");
1136 if (slot != j->slot) {
1138 fprintf(fp_new, "%c %s %d %s %d %s %d %d %s %s %s\n",
1157 /* Find from_spot in journal list - return 0 if corrupted */
1158 int journal_get_info(int p, char from_spot,struct journal* j, char *fname)
1162 fp = fopen_s(fname, "r");
1164 d_printf("Corrupt journal file! %s\n",fname);
1165 pprintf (p, "The journal file is corrupt! See an admin.\n");
1169 if (fscanf(fp, "%c %s %d %s %d %s %d %d %s %s %s\n",
1180 d_printf( "CHESSD: Error in journal info format. %s\n", fname);
1181 pprintf(p, "The journal file is corrupt! Error in internal format.\n");
1185 if (tolower(j->slot) == from_spot) {
1194 void addjournalitem(int p,struct journal* j_add, char* fname)
1196 struct journal* j_cur;
1198 char fname2[MAX_FILENAME_SIZE];
1202 strcpy (fname2,fname);
1203 strcat (fname2,".w");
1204 fp2 = fopen_s(fname2, "w");
1206 d_printf( "CHESSD: Problem opening file %s for write\n", fname);
1207 pprintf (p, "Couldn't update journal! Report this to an admin.\n");
1210 fp = fopen_s(fname, "r");
1211 if (!fp) { /* Empty? */
1212 fprintf(fp2, "%c %s %d %s %d %s %d %d %s %s %s\n",
1213 j_add->slot, j_add->WhiteName, j_add->WhiteRating,
1214 j_add->BlackName, j_add->BlackRating,
1215 j_add->type, j_add->t, j_add->i, j_add->eco, j_add->ending,
1218 rename (fname2, fname);
1221 j_cur = (struct journal*) malloc(sizeof(struct journal));
1223 if (fscanf(fp, "%c %s %d %s %d %s %d %d %s %s %s\n",
1226 &j_cur->WhiteRating,
1228 &j_cur->BlackRating,
1230 &j_cur->t, &j_cur->i,
1233 j_cur->result) != 11) {
1234 d_printf( "CHESSD: Error in journal info format - aborting. %s\n", fname);
1240 if ((j_cur->slot >= j_add->slot) && (!have_output)) {
1242 fprintf(fp2, "%c %s %d %s %d %s %d %d %s %s %s\n",
1256 if (j_cur->slot != j_add->slot) {
1258 fprintf(fp2, "%c %s %d %s %d %s %d %d %s %s %s\n",
1272 if (!have_output) { /* Haven't written yet */
1273 fprintf(fp2, "%c %s %d %s %d %s %d %d %s %s %s\n",
1288 rename(fname2, fname);
1293 int pjournal(int p, int p1, char *fname)
1298 fp = fopen_s(fname, "r");
1300 pprintf(p, "Sorry, no journal information available.\n");
1304 j = (struct journal*) malloc(sizeof(struct journal));
1305 pprintf(p, "Journal for %s:\n", player_globals.parray[p1].name);
1306 pprintf(p, " White Rating Black Rating Type ECO End Result\n");
1308 if (fscanf(fp, "%c %s %d %s %d %s %d %d %s %s %s\n",
1319 d_printf( "CHESSD: Error in journal info format. %s\n", fname);
1324 j->WhiteName[13] = '\0'; /* only first 13 chars in name */
1325 j->BlackName[13] = '\0';
1326 pprintf(p, "%c: %-13s %4d %-13s %4d [%3s%3d%4d] %s %3s %-7s\n",
1327 j->slot, j->WhiteName, j->WhiteRating,
1328 j->BlackName, j->BlackRating,
1329 j->type, j->t / 600, j->i / 10, j->eco, j->ending,
1337 int pgames(int p, int p1, char *fname)
1341 int MyRating, OppRating;
1343 char OppName[MAX_LOGIN_NAME + 1];
1351 fp = fopen_s(fname, "r");
1353 pprintf(p, "Sorry, no game information available.\n");
1356 pprintf(p, "History for %s:\n", player_globals.parray[p1].name);
1357 pprintf(p, " Opponent Type ECO End Date\n");
1359 if (fscanf(fp, "%d %s %d %s %d %s %s %d %d %d %d %s %s %ld\n",
1371 (long *) &t) != 14) {
1372 d_printf( "CHESSD: Error in games info format. %s\n", fname);
1376 OppName[13] = '\0'; /* only first 13 chars in name */
1377 pprintf(p, "%2d: %s %4d %s %4d %-13s [%3s%3d%4d] %s %3s %s",
1378 count, result, MyRating, MyColor,
1380 type, wt / 600, wi / 10, eco, ending,
1387 void game_write_complete(int g, int isDraw, char *EndSymbol)
1389 char fname[MAX_FILENAME_SIZE];
1390 int wp = game_globals.garray[g].white, bp = game_globals.garray[g].black;
1391 time_t now = time(NULL);
1400 sprintf(fname, "%s/%ld/%ld", HISTORY_DIR, (long) now % 100, (long) now);
1401 fp = fopen_s(fname, "r");
1402 } while (fp); /* terminates when the file doesn't exist */
1404 fp = fopen_s(fname, "w");
1407 WriteGameFile_v100(fp, g);
1410 d_printf( "Trouble writing history file %s", fname);
1413 sprintf(fname, "%s/player_data/%c/%s.%s", STATS_DIR,
1414 player_globals.parray[wp].login[0], player_globals.parray[wp].login, STATS_GAMES);
1415 write_g_out(g, fname, 10, isDraw, EndSymbol, player_globals.parray[wp].name, &now);
1416 sprintf(fname, "%s/player_data/%c/%s.%s", STATS_DIR,
1417 player_globals.parray[bp].login[0], player_globals.parray[bp].login, STATS_GAMES);
1418 write_g_out(g, fname, 10, isDraw, EndSymbol, player_globals.parray[bp].name, &now);
1422 else if (game_globals.garray[g].winner == WHITE)
1426 sprintf(fname,"%s/player_data/%c/%s.gstats", STATS_DIR, player_globals.parray[game_globals.garray[g].white].login[0], player_globals.parray[game_globals.garray[g].white].login);
1427 if ((CheckPFlag(bp, PFLAG_REG)) && (CheckPFlag(wp, PFLAG_REG)) && (game_globals.garray[g].type != TYPE_WILD))
1428 game_save_playerratio(fname,player_globals.parray[game_globals.garray[g].black].name,Result,game_globals.garray[g].rated);
1431 else if (game_globals.garray[g].winner == BLACK)
1435 sprintf(fname,"%s/player_data/%c/%s.gstats", STATS_DIR, player_globals.parray[game_globals.garray[g].black].login[0], player_globals.parray[game_globals.garray[g].black].login);
1436 if ((CheckPFlag(bp, PFLAG_REG)) && (CheckPFlag(wp, PFLAG_REG)) && (game_globals.garray[g].type != TYPE_WILD))
1437 game_save_playerratio(fname,player_globals.parray[game_globals.garray[g].white].name,Result,game_globals.garray[g].rated);
1440 int game_count(void)
1444 for (g = 0; g < game_globals.g_num; g++) {
1445 if ((game_globals.garray[g].status == GAME_ACTIVE) || (game_globals.garray[g].status == GAME_EXAMINE)
1446 || (game_globals.garray[g].status == GAME_SETUP))
1449 if (count > command_globals.game_high)
1450 command_globals.game_high = count;
1454 static int check_kings(struct game_state_t *gs)
1456 /* Function written 3/28/96 by Figbert to check for 1 king each side only! */
1457 int blackking = 0, whiteking = 0;
1461 for (f = 0; f < 8; f++) {
1462 for (r = 0; r < 8; r++) {
1463 if (gs->board[f][r] == B_KING) blackking++;
1464 if (gs->board[f][r] == W_KING) whiteking++;
1468 if (blackking == 1 && whiteking == 1) return 0; /* Perfect! */