scores.c revision 1.6 1 1.6 jsm /* $NetBSD: scores.c,v 1.6 1999/09/12 09:02:23 jsm Exp $ */
2 1.2 cgd
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1992, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Chris Torek and Darren F. Provine.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.1 cgd * @(#)scores.c 8.1 (Berkeley) 5/31/93
39 1.1 cgd */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Score code for Tetris, by Darren Provine (kilroy (at) gboro.glassboro.edu)
43 1.1 cgd * modified 22 January 1992, to limit the number of entries any one
44 1.1 cgd * person has.
45 1.1 cgd *
46 1.1 cgd * Major whacks since then.
47 1.1 cgd */
48 1.1 cgd #include <errno.h>
49 1.1 cgd #include <fcntl.h>
50 1.1 cgd #include <pwd.h>
51 1.1 cgd #include <stdio.h>
52 1.1 cgd #include <stdlib.h>
53 1.1 cgd #include <string.h>
54 1.6 jsm #include <sys/stat.h>
55 1.1 cgd #include <time.h>
56 1.4 lukem #include <termcap.h>
57 1.1 cgd #include <unistd.h>
58 1.1 cgd
59 1.1 cgd #include "pathnames.h"
60 1.1 cgd #include "screen.h"
61 1.1 cgd #include "scores.h"
62 1.1 cgd #include "tetris.h"
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * Within this code, we can hang onto one extra "high score", leaving
66 1.1 cgd * room for our current score (whether or not it is high).
67 1.1 cgd *
68 1.1 cgd * We also sometimes keep tabs on the "highest" score on each level.
69 1.1 cgd * As long as the scores are kept sorted, this is simply the first one at
70 1.1 cgd * that level.
71 1.1 cgd */
72 1.1 cgd #define NUMSPOTS (MAXHISCORES + 1)
73 1.1 cgd #define NLEVELS (MAXLEVEL + 1)
74 1.1 cgd
75 1.1 cgd static time_t now;
76 1.1 cgd static int nscores;
77 1.1 cgd static int gotscores;
78 1.1 cgd static struct highscore scores[NUMSPOTS];
79 1.1 cgd
80 1.1 cgd static int checkscores __P((struct highscore *, int));
81 1.1 cgd static int cmpscores __P((const void *, const void *));
82 1.1 cgd static void getscores __P((FILE **));
83 1.1 cgd static void printem __P((int, int, struct highscore *, int, const char *));
84 1.1 cgd static char *thisuser __P((void));
85 1.1 cgd
86 1.1 cgd /*
87 1.1 cgd * Read the score file. Can be called from savescore (before showscores)
88 1.1 cgd * or showscores (if savescore will not be called). If the given pointer
89 1.1 cgd * is not NULL, sets *fpp to an open file pointer that corresponds to a
90 1.1 cgd * read/write score file that is locked with LOCK_EX. Otherwise, the
91 1.1 cgd * file is locked with LOCK_SH for the read and closed before return.
92 1.1 cgd *
93 1.1 cgd * Note, we assume closing the stdio file releases the lock.
94 1.1 cgd */
95 1.1 cgd static void
96 1.1 cgd getscores(fpp)
97 1.1 cgd FILE **fpp;
98 1.1 cgd {
99 1.1 cgd int sd, mint, lck;
100 1.6 jsm mode_t mask;
101 1.5 jsm const char *mstr, *human;
102 1.1 cgd FILE *sf;
103 1.1 cgd
104 1.1 cgd if (fpp != NULL) {
105 1.1 cgd mint = O_RDWR | O_CREAT;
106 1.1 cgd mstr = "r+";
107 1.1 cgd human = "read/write";
108 1.1 cgd lck = LOCK_EX;
109 1.1 cgd } else {
110 1.1 cgd mint = O_RDONLY;
111 1.1 cgd mstr = "r";
112 1.1 cgd human = "reading";
113 1.1 cgd lck = LOCK_SH;
114 1.1 cgd }
115 1.6 jsm setegid(egid);
116 1.6 jsm mask = umask(S_IWOTH);
117 1.1 cgd sd = open(_PATH_SCOREFILE, mint, 0666);
118 1.6 jsm (void)umask(mask);
119 1.1 cgd if (sd < 0) {
120 1.1 cgd if (fpp == NULL) {
121 1.1 cgd nscores = 0;
122 1.6 jsm setegid(gid);
123 1.1 cgd return;
124 1.1 cgd }
125 1.1 cgd (void)fprintf(stderr, "tetris: cannot open %s for %s: %s\n",
126 1.1 cgd _PATH_SCOREFILE, human, strerror(errno));
127 1.1 cgd exit(1);
128 1.1 cgd }
129 1.1 cgd if ((sf = fdopen(sd, mstr)) == NULL) {
130 1.1 cgd (void)fprintf(stderr, "tetris: cannot fdopen %s for %s: %s\n",
131 1.1 cgd _PATH_SCOREFILE, human, strerror(errno));
132 1.1 cgd exit(1);
133 1.1 cgd }
134 1.6 jsm setegid(gid);
135 1.1 cgd
136 1.1 cgd /*
137 1.1 cgd * Grab a lock.
138 1.1 cgd */
139 1.1 cgd if (flock(sd, lck))
140 1.1 cgd (void)fprintf(stderr,
141 1.1 cgd "tetris: warning: score file %s cannot be locked: %s\n",
142 1.1 cgd _PATH_SCOREFILE, strerror(errno));
143 1.1 cgd
144 1.1 cgd nscores = fread(scores, sizeof(scores[0]), MAXHISCORES, sf);
145 1.1 cgd if (ferror(sf)) {
146 1.1 cgd (void)fprintf(stderr, "tetris: error reading %s: %s\n",
147 1.1 cgd _PATH_SCOREFILE, strerror(errno));
148 1.1 cgd exit(1);
149 1.1 cgd }
150 1.1 cgd
151 1.1 cgd if (fpp)
152 1.1 cgd *fpp = sf;
153 1.1 cgd else
154 1.1 cgd (void)fclose(sf);
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd void
158 1.1 cgd savescore(level)
159 1.1 cgd int level;
160 1.1 cgd {
161 1.1 cgd register struct highscore *sp;
162 1.1 cgd register int i;
163 1.1 cgd int change;
164 1.1 cgd FILE *sf;
165 1.1 cgd const char *me;
166 1.1 cgd
167 1.1 cgd getscores(&sf);
168 1.1 cgd gotscores = 1;
169 1.1 cgd (void)time(&now);
170 1.1 cgd
171 1.1 cgd /*
172 1.1 cgd * Allow at most one score per person per level -- see if we
173 1.1 cgd * can replace an existing score, or (easiest) do nothing.
174 1.1 cgd * Otherwise add new score at end (there is always room).
175 1.1 cgd */
176 1.1 cgd change = 0;
177 1.1 cgd me = thisuser();
178 1.1 cgd for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) {
179 1.1 cgd if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0)
180 1.1 cgd continue;
181 1.1 cgd if (score > sp->hs_score) {
182 1.1 cgd (void)printf("%s bettered %s %d score of %d!\n",
183 1.1 cgd "\nYou", "your old level", level,
184 1.1 cgd sp->hs_score * sp->hs_level);
185 1.1 cgd sp->hs_score = score; /* new score */
186 1.1 cgd sp->hs_time = now; /* and time */
187 1.1 cgd change = 1;
188 1.1 cgd } else if (score == sp->hs_score) {
189 1.1 cgd (void)printf("%s tied %s %d high score.\n",
190 1.1 cgd "\nYou", "your old level", level);
191 1.1 cgd sp->hs_time = now; /* renew it */
192 1.1 cgd change = 1; /* gotta rewrite, sigh */
193 1.1 cgd } /* else new score < old score: do nothing */
194 1.1 cgd break;
195 1.1 cgd }
196 1.1 cgd if (i >= nscores) {
197 1.1 cgd strcpy(sp->hs_name, me);
198 1.1 cgd sp->hs_level = level;
199 1.1 cgd sp->hs_score = score;
200 1.1 cgd sp->hs_time = now;
201 1.1 cgd nscores++;
202 1.1 cgd change = 1;
203 1.1 cgd }
204 1.1 cgd
205 1.1 cgd if (change) {
206 1.1 cgd /*
207 1.1 cgd * Sort & clean the scores, then rewrite.
208 1.1 cgd */
209 1.1 cgd nscores = checkscores(scores, nscores);
210 1.1 cgd rewind(sf);
211 1.1 cgd if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores ||
212 1.1 cgd fflush(sf) == EOF)
213 1.1 cgd (void)fprintf(stderr,
214 1.1 cgd "tetris: error writing %s: %s -- %s\n",
215 1.1 cgd _PATH_SCOREFILE, strerror(errno),
216 1.1 cgd "high scores may be damaged");
217 1.1 cgd }
218 1.1 cgd (void)fclose(sf); /* releases lock */
219 1.1 cgd }
220 1.1 cgd
221 1.1 cgd /*
222 1.1 cgd * Get login name, or if that fails, get something suitable.
223 1.1 cgd * The result is always trimmed to fit in a score.
224 1.1 cgd */
225 1.1 cgd static char *
226 1.1 cgd thisuser()
227 1.1 cgd {
228 1.1 cgd register const char *p;
229 1.1 cgd register struct passwd *pw;
230 1.1 cgd register size_t l;
231 1.1 cgd static char u[sizeof(scores[0].hs_name)];
232 1.1 cgd
233 1.1 cgd if (u[0])
234 1.1 cgd return (u);
235 1.1 cgd p = getlogin();
236 1.1 cgd if (p == NULL || *p == '\0') {
237 1.1 cgd pw = getpwuid(getuid());
238 1.1 cgd if (pw != NULL)
239 1.1 cgd p = pw->pw_name;
240 1.1 cgd else
241 1.1 cgd p = " ???";
242 1.1 cgd }
243 1.1 cgd l = strlen(p);
244 1.1 cgd if (l >= sizeof(u))
245 1.1 cgd l = sizeof(u) - 1;
246 1.3 tls memcpy(u, p, l);
247 1.1 cgd u[l] = '\0';
248 1.1 cgd return (u);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd /*
252 1.1 cgd * Score comparison function for qsort.
253 1.1 cgd *
254 1.1 cgd * If two scores are equal, the person who had the score first is
255 1.1 cgd * listed first in the highscore file.
256 1.1 cgd */
257 1.1 cgd static int
258 1.1 cgd cmpscores(x, y)
259 1.1 cgd const void *x, *y;
260 1.1 cgd {
261 1.1 cgd register const struct highscore *a, *b;
262 1.1 cgd register long l;
263 1.1 cgd
264 1.1 cgd a = x;
265 1.1 cgd b = y;
266 1.1 cgd l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score;
267 1.1 cgd if (l < 0)
268 1.1 cgd return (-1);
269 1.1 cgd if (l > 0)
270 1.1 cgd return (1);
271 1.1 cgd if (a->hs_time < b->hs_time)
272 1.1 cgd return (-1);
273 1.1 cgd if (a->hs_time > b->hs_time)
274 1.1 cgd return (1);
275 1.1 cgd return (0);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd /*
279 1.1 cgd * If we've added a score to the file, we need to check the file and ensure
280 1.1 cgd * that this player has only a few entries. The number of entries is
281 1.1 cgd * controlled by MAXSCORES, and is to ensure that the highscore file is not
282 1.1 cgd * monopolised by just a few people. People who no longer have accounts are
283 1.1 cgd * only allowed the highest score. Scores older than EXPIRATION seconds are
284 1.1 cgd * removed, unless they are someone's personal best.
285 1.1 cgd * Caveat: the highest score on each level is always kept.
286 1.1 cgd */
287 1.1 cgd static int
288 1.1 cgd checkscores(hs, num)
289 1.1 cgd register struct highscore *hs;
290 1.1 cgd int num;
291 1.1 cgd {
292 1.1 cgd register struct highscore *sp;
293 1.1 cgd register int i, j, k, numnames;
294 1.1 cgd int levelfound[NLEVELS];
295 1.1 cgd struct peruser {
296 1.1 cgd char *name;
297 1.1 cgd int times;
298 1.1 cgd } count[NUMSPOTS];
299 1.1 cgd register struct peruser *pu;
300 1.1 cgd
301 1.1 cgd /*
302 1.1 cgd * Sort so that highest totals come first.
303 1.1 cgd *
304 1.1 cgd * levelfound[i] becomes set when the first high score for that
305 1.1 cgd * level is encountered. By definition this is the highest score.
306 1.1 cgd */
307 1.1 cgd qsort((void *)hs, nscores, sizeof(*hs), cmpscores);
308 1.1 cgd for (i = MINLEVEL; i < NLEVELS; i++)
309 1.1 cgd levelfound[i] = 0;
310 1.1 cgd numnames = 0;
311 1.1 cgd for (i = 0, sp = hs; i < num;) {
312 1.1 cgd /*
313 1.1 cgd * This is O(n^2), but do you think we care?
314 1.1 cgd */
315 1.1 cgd for (j = 0, pu = count; j < numnames; j++, pu++)
316 1.1 cgd if (strcmp(sp->hs_name, pu->name) == 0)
317 1.1 cgd break;
318 1.1 cgd if (j == numnames) {
319 1.1 cgd /*
320 1.1 cgd * Add new user, set per-user count to 1.
321 1.1 cgd */
322 1.1 cgd pu->name = sp->hs_name;
323 1.1 cgd pu->times = 1;
324 1.1 cgd numnames++;
325 1.1 cgd } else {
326 1.1 cgd /*
327 1.1 cgd * Two ways to keep this score:
328 1.1 cgd * - Not too many (per user), still has acct, &
329 1.1 cgd * score not dated; or
330 1.1 cgd * - High score on this level.
331 1.1 cgd */
332 1.1 cgd if ((pu->times < MAXSCORES &&
333 1.1 cgd getpwnam(sp->hs_name) != NULL &&
334 1.1 cgd sp->hs_time + EXPIRATION >= now) ||
335 1.1 cgd levelfound[sp->hs_level] == 0)
336 1.1 cgd pu->times++;
337 1.1 cgd else {
338 1.1 cgd /*
339 1.1 cgd * Delete this score, do not count it,
340 1.1 cgd * do not pass go, do not collect $200.
341 1.1 cgd */
342 1.1 cgd num--;
343 1.1 cgd for (k = i; k < num; k++)
344 1.1 cgd hs[k] = hs[k + 1];
345 1.1 cgd continue;
346 1.1 cgd }
347 1.1 cgd }
348 1.1 cgd levelfound[sp->hs_level] = 1;
349 1.1 cgd i++, sp++;
350 1.1 cgd }
351 1.1 cgd return (num > MAXHISCORES ? MAXHISCORES : num);
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd /*
355 1.1 cgd * Show current scores. This must be called after savescore, if
356 1.1 cgd * savescore is called at all, for two reasons:
357 1.1 cgd * - Showscores munches the time field.
358 1.1 cgd * - Even if that were not the case, a new score must be recorded
359 1.1 cgd * before it can be shown anyway.
360 1.1 cgd */
361 1.1 cgd void
362 1.1 cgd showscores(level)
363 1.1 cgd int level;
364 1.1 cgd {
365 1.1 cgd register struct highscore *sp;
366 1.1 cgd register int i, n, c;
367 1.1 cgd const char *me;
368 1.1 cgd int levelfound[NLEVELS];
369 1.1 cgd
370 1.1 cgd if (!gotscores)
371 1.1 cgd getscores((FILE **)NULL);
372 1.1 cgd (void)printf("\n\t\t\t Tetris High Scores\n");
373 1.1 cgd
374 1.1 cgd /*
375 1.1 cgd * If level == 0, the person has not played a game but just asked for
376 1.1 cgd * the high scores; we do not need to check for printing in highlight
377 1.1 cgd * mode. If SOstr is null, we can't do highlighting anyway.
378 1.1 cgd */
379 1.1 cgd me = level && SOstr ? thisuser() : NULL;
380 1.1 cgd
381 1.1 cgd /*
382 1.1 cgd * Set times to 0 except for high score on each level.
383 1.1 cgd */
384 1.1 cgd for (i = MINLEVEL; i < NLEVELS; i++)
385 1.1 cgd levelfound[i] = 0;
386 1.1 cgd for (i = 0, sp = scores; i < nscores; i++, sp++) {
387 1.1 cgd if (levelfound[sp->hs_level])
388 1.1 cgd sp->hs_time = 0;
389 1.1 cgd else {
390 1.1 cgd sp->hs_time = 1;
391 1.1 cgd levelfound[sp->hs_level] = 1;
392 1.1 cgd }
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd /*
396 1.1 cgd * Page each screenful of scores.
397 1.1 cgd */
398 1.1 cgd for (i = 0, sp = scores; i < nscores; sp += n) {
399 1.1 cgd n = 40;
400 1.1 cgd if (i + n > nscores)
401 1.1 cgd n = nscores - i;
402 1.1 cgd printem(level, i + 1, sp, n, me);
403 1.1 cgd if ((i += n) < nscores) {
404 1.1 cgd (void)printf("\nHit RETURN to continue.");
405 1.1 cgd (void)fflush(stdout);
406 1.1 cgd while ((c = getchar()) != '\n')
407 1.1 cgd if (c == EOF)
408 1.1 cgd break;
409 1.1 cgd (void)printf("\n");
410 1.1 cgd }
411 1.1 cgd }
412 1.1 cgd }
413 1.1 cgd
414 1.1 cgd static void
415 1.1 cgd printem(level, offset, hs, n, me)
416 1.1 cgd int level, offset;
417 1.1 cgd register struct highscore *hs;
418 1.1 cgd register int n;
419 1.1 cgd const char *me;
420 1.1 cgd {
421 1.1 cgd register struct highscore *sp;
422 1.1 cgd int nrows, row, col, item, i, highlight;
423 1.1 cgd char buf[100];
424 1.1 cgd #define TITLE "Rank Score Name (points/level)"
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * This makes a nice two-column sort with headers, but it's a bit
428 1.1 cgd * convoluted...
429 1.1 cgd */
430 1.1 cgd printf("%s %s\n", TITLE, n > 1 ? TITLE : "");
431 1.1 cgd
432 1.1 cgd highlight = 0;
433 1.1 cgd nrows = (n + 1) / 2;
434 1.1 cgd
435 1.1 cgd for (row = 0; row < nrows; row++) {
436 1.1 cgd for (col = 0; col < 2; col++) {
437 1.1 cgd item = col * nrows + row;
438 1.1 cgd if (item >= n) {
439 1.1 cgd /*
440 1.1 cgd * Can only occur on trailing columns.
441 1.1 cgd */
442 1.1 cgd (void)putchar('\n');
443 1.1 cgd continue;
444 1.1 cgd }
445 1.1 cgd (void)printf(item + offset < 10 ? " " : " ");
446 1.1 cgd sp = &hs[item];
447 1.1 cgd (void)sprintf(buf,
448 1.1 cgd "%d%c %6d %-11s (%d on %d)",
449 1.1 cgd item + offset, sp->hs_time ? '*' : ' ',
450 1.1 cgd sp->hs_score * sp->hs_level,
451 1.1 cgd sp->hs_name, sp->hs_score, sp->hs_level);
452 1.1 cgd /*
453 1.1 cgd * Highlight if appropriate. This works because
454 1.1 cgd * we only get one score per level.
455 1.1 cgd */
456 1.1 cgd if (me != NULL &&
457 1.1 cgd sp->hs_level == level &&
458 1.1 cgd sp->hs_score == score &&
459 1.1 cgd strcmp(sp->hs_name, me) == 0) {
460 1.1 cgd putpad(SOstr);
461 1.1 cgd highlight = 1;
462 1.1 cgd }
463 1.1 cgd (void)printf("%s", buf);
464 1.1 cgd if (highlight) {
465 1.1 cgd putpad(SEstr);
466 1.1 cgd highlight = 0;
467 1.1 cgd }
468 1.1 cgd
469 1.1 cgd /* fill in spaces so column 1 lines up */
470 1.1 cgd if (col == 0)
471 1.1 cgd for (i = 40 - strlen(buf); --i >= 0;)
472 1.1 cgd (void)putchar(' ');
473 1.1 cgd else /* col == 1 */
474 1.1 cgd (void)putchar('\n');
475 1.1 cgd }
476 1.1 cgd }
477 1.1 cgd }
478