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.
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.
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.
20 #define COMPUTER_FILE LISTS_DIR "/computer"
22 static char *rnames[] = {"std", "blitz", "wild", "lightning"};
24 typedef struct _ratings {
31 typedef struct _Entry {
32 char name[MAX_LOGIN_NAME+2];
38 static ENTRY **sortme;
40 unsigned chessd_reload_flag;
42 static void copy_stats(ratings *r, struct statistics *s)
45 r->rating = s->rating;
51 read a player file using the new generic and extensible format
53 static int GetPlayerInfo(char *fileName, ENTRY *e)
59 s = file_load(fileName, NULL);
61 d_printf("Error reading player file '%s'\n", fileName);
65 if (sscanf(s, "v %u", &version) != 1 ||
66 (version != 100 && version != 9)) {
76 if (!s2 || unmarshall_player(&player, s2) != 0) {
77 d_printf("Error unmarshalling player data in '%s'!\n", fileName);
83 strcpy(e->name, player.name);
84 copy_stats(&e->r[0], &player.s_stats);
85 copy_stats(&e->r[1], &player.b_stats);
86 copy_stats(&e->r[2], &player.w_stats);
87 copy_stats(&e->r[3], &player.l_stats);
94 static int LoadEntries(void)
105 list = malloc(sizeof(ENTRY *)*listsize);
107 for (letter1 = 'a'; letter1 <= 'z'; letter1++) {
108 printf("Loading %c's.\n", letter1);
109 sprintf(pathInput, "%s/%c", PLAYER_DIR, letter1);
110 asprintf(&command, "ls -1 %s", pathInput);
111 fpPlayerList = popen(command, "r");
114 if (fpPlayerList == NULL)
117 fgets(e.name, MAX_LOGIN_NAME, fpPlayerList);
118 if (feof(fpPlayerList))
120 len = strlen(e.name);
121 e.name[len - 1] = '\0';
122 if (e.name[0] != letter1)
123 printf("File %c/%s: wrong directory.\n", letter1, e.name);
125 sprintf(pathInput, "%s/%c/%s", PLAYER_DIR, letter1, e.name);
126 if (GetPlayerInfo(pathInput, &e)) {
127 if ((list[n] = malloc(sizeof(ENTRY))) == NULL) {
128 d_printf( "malloc() failed!\n");
130 memcpy(list[n], &e, sizeof(ENTRY));
134 list = realloc(list, listsize*sizeof(ENTRY *));
140 pclose(fpPlayerList);
145 static int SetComputers(int n)
149 char *line, comp[30];
151 asprintf(&line, "sort -f %s", COMPUTER_FILE);
152 fpComp = popen(line, "r");
158 fgets(comp, 29, fpComp);
161 comp[strlen(comp) - 1] = '\0';
163 while (i < n && strcasecmp(list[i]->name, comp) < 0)
166 if (i < n && strcasecmp(list[i]->name, comp) == 0) {
167 list[i++]->computer = 1;
176 static int sortfunc(const void *i, const void *j)
178 int n = (*(ENTRY **)j)->r[rtype].rating - (*(ENTRY **)i)->r[rtype].rating;
179 return n ? n : strcasecmp((*(ENTRY **)i)->name, (*(ENTRY **)j)->name);
182 static void makerank(void)
184 int sortnum, sortmesize, i, n;
187 long now = time(NULL);
189 printf("Loading players\n");
191 printf("Found %d players.\n", n);
192 printf("Setting computers.\n");
195 for (rtype = 0; rtype < 4; rtype++) {
196 sortnum = 0; sortmesize = 100;
197 sortme = malloc(sizeof(ENTRY *)*sortmesize);
199 for (i = 0; i < n; i++) {
200 if (list[i]->r[rtype].rating) {
201 sortme[sortnum++] = list[i];
202 if (sortnum == sortmesize) {
204 sortme = realloc(sortme, sortmesize*sizeof(ENTRY *));
208 printf("Sorting %d %s.\n", sortnum, rnames[rtype]);
209 qsort(sortme, sortnum, sizeof(ENTRY *), sortfunc);
210 printf("Saving to file.\n");
211 asprintf(&fName, "%s/rank.%s", STATS_DIR, rnames[rtype]);
212 fp = fopen_s(fName, "w");
215 for (i = 0; i < sortnum; i++)
216 fprintf(fp, "%s %d %d %d %f\n", sortme[i]->name,
217 sortme[i]->r[rtype].rating, sortme[i]->r[rtype].num,
218 sortme[i]->computer, current_sterr(sortme[i]->r[rtype].rd,
219 now - sortme[i]->r[rtype].ltime));
225 int main(int argc, char **argv)
228 printf("usage: %s.\n", argv[0]);