Fix bug in setting up w3 and w4 games
[capablanca.git] / lasker-2.2.3 / src / gics.c
1 /*
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6    
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11    
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17 /*
18  * FICS-Extensions used at GICS chess.unix-ag.uni-kl.de 5000
19  * (c) 1995/1996 by Ulrich Schlechte <Ulrich.Schlechte@ipp.tu-clausthal.de>
20  *                  Klaus Knopper <Knopper@unix-ag.uni-kl.de>
21  *
22  *
23 */
24
25 #include "includes.h"
26
27
28 void init_userstat(void)
29 {
30         FILE *fp;
31         int i;
32
33         if ((fp = fopen_s(STATS_DIR "/user.stats", "r")) == NULL) {
34                 bzero((char *) &gics_globals.userstat, sizeof(gics_globals.userstat));
35                 return;
36         }
37
38         for (i = 0; i < 48; i++) {
39                 fscanf(fp, "%d\n", &gics_globals.userstat.users[i]);
40         }
41         fscanf(fp, "%d\n", &gics_globals.userstat.usermax);
42         fscanf(fp, "%d\n", (int *) &gics_globals.userstat.usermaxtime);
43         fscanf(fp, "%d\n", &gics_globals.userstat.logins);
44         fscanf(fp, "%d\n", &gics_globals.userstat.games);
45         fscanf(fp, "%d\n", &gics_globals.userstat.gamemax);
46         fscanf(fp, "%d\n", (int *) &gics_globals.userstat.gamemaxtime);
47         fclose(fp);
48 }
49
50 void save_userstat(void)
51 {
52         FILE *fp;
53         int i;
54         
55         if ((fp = fopen_s(STATS_DIR "/user.stats", "w")) == NULL)
56                 return;
57         for (i = 0; i < 48; i++)
58                 fprintf(fp, "%d\n", gics_globals.userstat.users[i]);
59         fprintf(fp, "%d\n%d\n%d\n", 
60                 gics_globals.userstat.usermax, gics_globals.userstat.usermaxtime, gics_globals.userstat.logins);
61         fprintf(fp, "%d\n%d\n%d\n", 
62                 gics_globals.userstat.games, gics_globals.userstat.gamemax, gics_globals.userstat.gamemaxtime);
63         fclose(fp);
64 }
65
66 #if 0
67 int com_ping(int p, param_list param)
68 {
69   int p1;
70   struct timeval timeState;
71   struct timezone tz;
72
73   if ((p1 = player_find_bylogin(param[0].val.word)) < 0) {
74     pprintf(p, "Player \"%s\" not logged on!\n", param[0].val.word);
75     return COM_OK;
76   }
77   if ((!con[player_globals.parray[p1].socket].timeseal) || (con[player_globals.parray[p1].socket].method)) {
78     pprintf(p, "%s is not using timeseal or uses an old client.\n", player_globals.parray[p1].name);
79     return COM_OK;
80   }
81   pprintf(p1, "\n[P]\n");
82   gettimeofday(&timeState, &tz);
83   con[player_globals.parray[p1].socket].pingtime = (((int) timeState.tv_sec % 10000) * 1000) + ((int) timeState.tv_usec / 1000);
84   con[player_globals.parray[p1].socket].pingplayer = p;
85   return COM_OK;
86 }
87 #endif
88
89 void game_save_playerratio(char *file, char *Opponent, int Result, int rated)
90 {
91   FILE *fp, *fp1;
92   char Name[32], file1[256];
93   int win, loss, draw, flag = 0, inc = 1;
94
95   if (!rated)
96     inc = 100000;
97
98   sprintf(file1, "%s.new", file);
99   if ((fp = fopen_s(file, "r")) != NULL) {
100     fp1 = fopen_s(file1, "w");
101     while (!feof(fp)) {
102       if (fscanf(fp, "%s %d %d %d\n", Name, &win, &draw, &loss) != 4) {
103         break;
104       }
105       if (strcasecmp(Name, Opponent) == 0) {
106         if (Result == 1)
107           win += inc;
108         if (Result == 0)
109           loss += inc;
110         if (Result == -1)
111           draw += inc;
112         flag++;
113       }
114       fprintf(fp1, "%s %d %d %d\n", Name, win, draw, loss);
115     }
116     fclose(fp);
117     if (!flag) {
118       win = loss = draw = 0;
119       if (Result == 1)
120         win += inc;
121       if (Result == 0)
122         loss += inc;
123       if (Result == -1)
124         draw += inc;
125       fprintf(fp1, "%s %d %d %d\n", Opponent, win, draw, loss);
126     }
127     fclose(fp1);
128     unlink(file);
129     if (rename(file1, file) != 0) {
130       d_printf("CHESSD: Problems rename %s to %s : %d\n", file1, file, errno);
131       return;
132     }
133     return;
134   } else {
135     fp = fopen_s(file, "w");
136     if(fp == NULL) {
137         perror("fopen");
138         d_printf("Unable to open %s - very bad, info not saved\n",file);
139         return;
140     }
141     win = loss = draw = 0;
142     if (Result == 1)
143       win += inc;
144     if (Result == 0)
145       loss += inc;
146     if (Result == -1)
147       draw += inc;
148     fprintf(fp, "%s %d %d %d\n", Opponent, win, draw, loss);
149     fclose(fp);
150   }
151 }
152
153 int com_pstat(int p, param_list param)
154 {
155   struct player *pp = &player_globals.parray[p];
156   int p1 = p, p2, ptemp, pflag=0;
157   int p1_connected = 1, p2_connected = 1;
158   int win = 0, draw = 0, loss = 0, flag = 0;
159   FILE *fp;
160   char Name[32], fname[256];
161
162   if (param[0].type == TYPE_NULL) {
163     if (pp->game < 0) {
164       pprintf(p, "You are not playing a game.\n");
165       return COM_OK;
166     }
167     if (game_globals.garray[pp->game].status == GAME_EXAMINE) {
168       pprintf(p, "You are examining a game please specify variables.\n");
169       return COM_OK;
170     }
171     p2 = pp->opponent;
172   } else {
173     if (!FindPlayer(p, param[0].val.word, &p2, &p2_connected)) {
174       pprintf(p, "No user named \"%s\" is logged in.\n", param[0].val.word);
175       return COM_OK;
176     }
177     if (param[1].type != TYPE_NULL) {
178       if (!FindPlayer(p, param[1].val.word, &p1, &p1_connected)) {
179         pprintf(p, "No user named \"%s\" is logged in.\n", param[1].val.word);
180         if (!p2_connected)
181           player_remove(p2);
182         return COM_OK;
183       }
184       ptemp = p2;
185       p2 = p1;
186       p1 = ptemp;
187       pflag = 1;
188     }
189   }
190   if (p1 == p2) {
191     pprintf(p, "You have not played against yourself ;-)\n");
192     if (!p1_connected)
193       player_remove(p1);
194     if (!p2_connected)
195       player_remove(p2);
196     return COM_OK;
197   }
198   sprintf(fname, "%s/player_data/%c/%s.gstats", STATS_DIR, player_globals.parray[p1].login[0], player_globals.parray[p1].login);
199   if ((fp = fopen_s(fname, "r")) != NULL) {
200     while (!feof(fp)) {
201       if (fscanf(fp, "%s %d %d %d\n", Name, &win, &draw, &loss) != 4) {
202         win = loss = draw = 0;
203         break;
204       }
205       if (strcasecmp(Name, player_globals.parray[p2].name) == 0) {
206         flag++;
207         break;
208       }
209     }
210     fclose(fp);
211   }
212   if (!flag)
213     win = loss = draw = 0;
214   pprintf(p, "Record for %s vs. %s:\n", player_globals.parray[p1].name, player_globals.parray[p2].name);
215   pprintf(p, "                            wins     losses     draws\n");
216   pprintf(p, "                rated       %4d       %4d      %4d\n", win % 100000, loss % 100000, draw % 100000);
217   pprintf(p, "              unrated       %4d       %4d      %4d\n", win / 100000, loss / 100000, draw / 100000);
218   /*  pprintf(p, "Ratio of %s vs. %s (r/u): %d/%d wins %d/%d draws %d/%d losses\n", player_globals.parray[p1].name, player_globals.parray[p2].name,
219    *      win % 100000, win / 100000, draw % 100000, draw / 100000, loss % 100000, loss / 100000);
220    */
221   if (pflag) {
222     ptemp = p1;
223     p1 = p2;
224     p2 = ptemp;
225   }
226   if (!p1_connected)
227     player_remove(p1);
228   if (!p2_connected)
229     player_remove(p2);
230   return COM_OK;
231 }
232
233
234
235
236
237
238
239
240