aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/makerank.c
blob: 85938ce72baad65a80bd64ad8a680e7cd886bacb (plain)
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
/*
 * Revised by maxxe 23/12/14
 */

#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if __linux__
#include <bsd/string.h>
#endif

#include "common.h"
#include "makerank.h"

static ENTRY	**list;
static ENTRY	**sortme;

static char	*rnames[] = { "std", "blitz", "wild", "lightning" };
static int	 rtype;

static int
GetPlayerInfo(char *fileName, ENTRY *e)
{
	FILE	*fp = NULL;
	char	 NameWithCase[30] = { '\0' };
	char	 field[20] = { '\0' };
	char	 line[100] = { '\0' };
	int	 done = 0;

	e->computer = 0;

	for (size_t i = 0; i < ARRAY_SIZE(e->r); i++) {
		e->r[i].num	= 0;
		e->r[i].rating	= 0;
	}

	if ((fp = fopen(fileName, "r")) == NULL ||
	    fgets(line, sizeof line, fp) == NULL ||
	    feof(fp)) {
		if (fp)
			fclose(fp);
		return 0;
	}

	if (!strcmp(line, "v 1\n")) {
		if (fgets(line, sizeof line, fp) == NULL ||
		    sscanf(line, "%s", e->name) != 1) {
			warnx("%s: fgets() or sscanf() error", __func__);
			fclose(fp);
			return 0;
		} else if (fgets(line, sizeof line, fp) == NULL ||
			   fgets(line, sizeof line, fp) == NULL ||
			   fgets(line, sizeof line, fp) == NULL) {
			warnx("%s: fgets() error", __func__);
//			fclose(fp);
//			return 0;
		}

		if (fscanf(fp, "%d %*u %*u %*u %d %*u %*u %*u %*u %d %*u %*u "
		    "%*u %d %*u %*u %*u %*u %d %*u %*u %*u %d %*u %*u %*u %*u "
		    "%d %*u %*u %*u %d %*u %*u %*u %*u",
		    &(e->r[0].num),
		    &(e->r[0].rating),
		    &(e->r[1].num),
		    &(e->r[1].rating),
		    &(e->r[2].num),
		    &(e->r[2].rating),
		    &(e->r[3].num),
		    &(e->r[3].rating)) != 8) {
			fprintf(stderr, "OOPS: couldn't parse player file %s."
			    "\n", fileName);
			fclose(fp);
			return 0;
		}
	} else {
		do {
			sscanf(line, "%s", field);

			if (!strcmp(field, "Name:")) {
				sscanf(line, "%*s %s", NameWithCase);

				if (strcasecmp(e->name, NameWithCase)) {
					printf("TROUBLE: %s's handle is "
					    "listed as %s.\n", e->name,
					    NameWithCase);
				} else if (strlcpy(e->name, NameWithCase,
				    sizeof e->name) >= sizeof e->name) {
					fprintf(stderr, "%s: warning: "
					    "strlcpy() truncated\n", __func__);
				}
			} else if (!strcmp(field, "S_NUM:")) {
				sscanf(line, "%*s %d", &(e->r[0].num));
			} else if (!strcmp(field, "B_NUM:")) {
				sscanf(line, "%*s %d", &(e->r[1].num));
			} else if (!strcmp(field, "W_NUM:")) {
				sscanf(line, "%*s %d", &(e->r[2].num));
			} else if (!strcmp(field, "L_NUM:")) {
				sscanf(line, "%*s %d", &(e->r[3].num));
			} else if (!strcmp(field, "S_RATING:")) {
				sscanf(line, "%*s %d", &(e->r[0].rating));
			} else if (!strcmp(field, "B_RATING:")) {
				sscanf(line, "%*s %d", &(e->r[1].rating));
			} else if (!strcmp(field, "W_RATING:")) {
				sscanf(line, "%*s %d", &(e->r[2].rating));
			} else if (!strcmp(field, "L_RATING:")) {
				sscanf(line, "%*s %d", &(e->r[3].rating));
			} else if (!strcmp(field, "Network:")) {
				done = 1;
			}

			fgets(line, sizeof line, fp);
		} while (!done && !feof(fp));
	}

	fclose(fp);
	return 1;
}

static int
LoadEntries(void)
{
	ENTRY	 e;
	FILE	*fpPlayerList;
	char	 command[90];
	char	 letter1;
	char	 pathInput[80];
	int	 len, n = 0;
	int	 listsize;

	listsize	= 100;
	list		= reallocarray(NULL, sizeof(ENTRY *), listsize);

	if (list == NULL)
		err(1, "%s: reallocarray", __func__);

	for (letter1 = 'a'; letter1 <= 'z'; letter1++) {
		printf("Loading %c's.\n", letter1);

		snprintf(pathInput, sizeof pathInput, "%s/%c", DEFAULT_PLAYERS,
		    letter1);
		snprintf(command, sizeof command, "ls -1 %s", pathInput);

		if ((fpPlayerList = popen(command, "r")) == NULL)
			continue;

		while (1) {
			if (fgets(e.name, sizeof(e.name), fpPlayerList) == NULL ||
			    feof(fpPlayerList))
				break;

			len = strlen(e.name);
			e.name[len - 1] = '\0';

			if (e.name[0] != letter1) {
				printf("File %c/%s: wrong directory.\n",
				    letter1, e.name);
			} else {
				snprintf(pathInput, sizeof pathInput,
				    "%s/%c/%s", DEFAULT_PLAYERS, letter1,
				    e.name);

				if (GetPlayerInfo(pathInput, &e)) {
					if ((list[n] = malloc(sizeof(ENTRY))) ==
					    NULL)
						err(1, "%s: malloc", __func__);

					memcpy(list[n], &e, sizeof(ENTRY));

					if (++n == listsize) {
						listsize += 100;
						list = reallocarray(list,
						    listsize,
						    sizeof(ENTRY *));
						if (list == NULL)
							err(1, NULL);
					}
				}
			}
		} /* while (1) */

		pclose(fpPlayerList);
	} /* for */

	return n;
}

static int
SetComputers(int n)
{
	FILE	*fpComp;
	char	 comp[30];
	char	 line[100];
	int	 i = 0;

	if (snprintf(line, sizeof line, "sort -f %s", COMPUTER_FILE) >=
	    (int)sizeof line) {
		warnx("%s: snprintf truncated", __func__);
		return 0;
	} else if ((fpComp = popen(line, "r")) == NULL)
		return 0;

	while (i < n) {
		if (fgets(comp, sizeof comp, fpComp) == NULL ||
		    feof(fpComp))
			break;

		comp[strlen(comp) - 1] = '\0';

		while (i < n && strcasecmp(list[i]->name, comp) < 0)
			i++;
		if (i < n && strcasecmp(list[i]->name, comp) == 0)
			list[i++]->computer = 1;
	}

	pclose(fpComp);
	return 1;
}

static int
sortfunc(const void *i, const void *j)
{
	int n =	(*(ENTRY **)j)->r[rtype].rating -
		(*(ENTRY **)i)->r[rtype].rating;

	return n ? n : strcasecmp((*(ENTRY **)i)->name, (*(ENTRY **)j)->name);
}

static void
makerank(void)
{
	FILE	*fp;
	char	 fName[200];
	int	 sortnum, sortmesize, i, n;

	printf("Loading players\n");
	n = LoadEntries();
	printf("Found %d players.\n", n);
	printf("Setting computers.\n");
	SetComputers(n);

	for (rtype = 0; rtype < 4; rtype++) {
		sortnum		= 0;
		sortmesize	= 100;
		sortme		= reallocarray(NULL, sizeof(ENTRY *),
		    sortmesize);

		if (sortme == NULL)
			err(1, "%s: reallocarray", __func__);

		for (i = 0; i < n; i++) {
			if (list[i]->r[rtype].rating) {
				sortme[sortnum++] = list[i];

				if (sortnum == sortmesize) {
					sortmesize += 100;
					sortme = reallocarray(sortme,
					    sortmesize,
					    sizeof(ENTRY *));
					if (sortme == NULL)
						err(1, NULL);
				}
			}
		}

		printf("Sorting %d %s.\n", sortnum, rnames[rtype]);
		qsort(sortme, sortnum, sizeof(ENTRY *), sortfunc);

		printf("Saving to file.\n");
		snprintf(fName, sizeof fName, "%s/rank.%s", DEFAULT_STATS,
		    rnames[rtype]);

		if ((fp = fopen(fName, "w")) == NULL)
			err(1, "%s: fopen", __func__);

		for (i = 0; i < sortnum; i++) {
			fprintf(fp, "%s %d %d %d\n",
			    sortme[i]->name,
			    sortme[i]->r[rtype].rating,
			    sortme[i]->r[rtype].num,
			    sortme[i]->computer);
		}
		fclose(fp);
		free(sortme);
	}
}

int
main(int argc, char **argv)
{
	if (argc > 1) {
		fprintf(stderr, "usage: %s.\n", argv[0]);
		return EXIT_FAILURE;
	}

	makerank();
	return EXIT_SUCCESS;
}