]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/scores.c
1 /* $NetBSD: scores.c,v 1.14 2006/06/01 16:12:27 drochner Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)scores.c 8.1 (Berkeley) 5/31/93
38 * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu)
39 * modified 22 January 1992, to limit the number of entries any one
42 * Major whacks since then.
56 #include "pathnames.h"
62 * Within this code, we can hang onto one extra "high score", leaving
63 * room for our current score (whether or not it is high).
65 * We also sometimes keep tabs on the "highest" score on each level.
66 * As long as the scores are kept sorted, this is simply the first one at
69 #define NUMSPOTS (MAXHISCORES + 1)
70 #define NLEVELS (MAXLEVEL + 1)
75 static struct highscore scores
[NUMSPOTS
];
77 static int checkscores(struct highscore
*, int);
78 static int cmpscores(const void *, const void *);
79 static void getscores(FILE **);
80 static void printem(int, int, struct highscore
*, int, const char *);
81 static char *thisuser(void);
84 * Read the score file. Can be called from savescore (before showscores)
85 * or showscores (if savescore will not be called). If the given pointer
86 * is not NULL, sets *fpp to an open file pointer that corresponds to a
87 * read/write score file that is locked with LOCK_EX. Otherwise, the
88 * file is locked with LOCK_SH for the read and closed before return.
90 * Note, we assume closing the stdio file releases the lock.
98 const char *mstr
, *human
;
102 mint
= O_RDWR
| O_CREAT
;
104 human
= "read/write";
113 mask
= umask(S_IWOTH
);
114 sd
= open(_PATH_SCOREFILE
, mint
, 0666);
122 err(1, "cannot open %s for %s", _PATH_SCOREFILE
, human
);
124 if ((sf
= fdopen(sd
, mstr
)) == NULL
) {
125 err(1, "cannot fdopen %s for %s", _PATH_SCOREFILE
, human
);
133 warn("warning: score file %s cannot be locked",
136 nscores
= fread(scores
, sizeof(scores
[0]), MAXHISCORES
, sf
);
138 err(1, "error reading %s", _PATH_SCOREFILE
);
151 struct highscore
*sp
;
162 * Allow at most one score per person per level -- see if we
163 * can replace an existing score, or (easiest) do nothing.
164 * Otherwise add new score at end (there is always room).
168 for (i
= 0, sp
= &scores
[0]; i
< nscores
; i
++, sp
++) {
169 if (sp
->hs_level
!= level
|| strcmp(sp
->hs_name
, me
) != 0)
171 if (score
> sp
->hs_score
) {
172 (void)printf("%s bettered %s %d score of %d!\n",
173 "\nYou", "your old level", level
,
174 sp
->hs_score
* sp
->hs_level
);
175 sp
->hs_score
= score
; /* new score */
176 sp
->hs_time
= now
; /* and time */
178 } else if (score
== sp
->hs_score
) {
179 (void)printf("%s tied %s %d high score.\n",
180 "\nYou", "your old level", level
);
181 sp
->hs_time
= now
; /* renew it */
182 change
= 1; /* gotta rewrite, sigh */
183 } /* else new score < old score: do nothing */
187 strcpy(sp
->hs_name
, me
);
188 sp
->hs_level
= level
;
189 sp
->hs_score
= score
;
197 * Sort & clean the scores, then rewrite.
199 nscores
= checkscores(scores
, nscores
);
201 if (fwrite(scores
, sizeof(*sp
), nscores
, sf
) != (size_t)nscores
||
203 warnx("error writing %s: %s -- %s",
204 _PATH_SCOREFILE
, strerror(errno
),
205 "high scores may be damaged");
207 (void)fclose(sf
); /* releases lock */
211 * Get login name, or if that fails, get something suitable.
212 * The result is always trimmed to fit in a score.
220 static char u
[sizeof(scores
[0].hs_name
)];
225 if (p
== NULL
|| *p
== '\0') {
226 pw
= getpwuid(getuid());
241 * Score comparison function for qsort.
243 * If two scores are equal, the person who had the score first is
244 * listed first in the highscore file.
250 const struct highscore
*a
, *b
;
255 l
= (long)b
->hs_level
* b
->hs_score
- (long)a
->hs_level
* a
->hs_score
;
260 if (a
->hs_time
< b
->hs_time
)
262 if (a
->hs_time
> b
->hs_time
)
268 * If we've added a score to the file, we need to check the file and ensure
269 * that this player has only a few entries. The number of entries is
270 * controlled by MAXSCORES, and is to ensure that the highscore file is not
271 * monopolised by just a few people. People who no longer have accounts are
272 * only allowed the highest score. Scores older than EXPIRATION seconds are
273 * removed, unless they are someone's personal best.
274 * Caveat: the highest score on each level is always kept.
278 struct highscore
*hs
;
281 struct highscore
*sp
;
282 int i
, j
, k
, numnames
;
283 int levelfound
[NLEVELS
];
291 * Sort so that highest totals come first.
293 * levelfound[i] becomes set when the first high score for that
294 * level is encountered. By definition this is the highest score.
296 qsort((void *)hs
, nscores
, sizeof(*hs
), cmpscores
);
297 for (i
= MINLEVEL
; i
< NLEVELS
; i
++)
300 for (i
= 0, sp
= hs
; i
< num
;) {
302 * This is O(n^2), but do you think we care?
304 for (j
= 0, pu
= count
; j
< numnames
; j
++, pu
++)
305 if (strcmp(sp
->hs_name
, pu
->name
) == 0)
309 * Add new user, set per-user count to 1.
311 pu
->name
= sp
->hs_name
;
316 * Two ways to keep this score:
317 * - Not too many (per user), still has acct, &
318 * score not dated; or
319 * - High score on this level.
321 if ((pu
->times
< MAXSCORES
&&
322 getpwnam(sp
->hs_name
) != NULL
&&
323 sp
->hs_time
+ EXPIRATION
>= now
) ||
324 levelfound
[sp
->hs_level
] == 0)
328 * Delete this score, do not count it,
329 * do not pass go, do not collect $200.
332 for (k
= i
; k
< num
; k
++)
337 if (sp
->hs_level
< NLEVELS
&& sp
->hs_level
>= 0)
338 levelfound
[sp
->hs_level
] = 1;
341 return (num
> MAXHISCORES
? MAXHISCORES
: num
);
345 * Show current scores. This must be called after savescore, if
346 * savescore is called at all, for two reasons:
347 * - Showscores munches the time field.
348 * - Even if that were not the case, a new score must be recorded
349 * before it can be shown anyway.
355 struct highscore
*sp
;
358 int levelfound
[NLEVELS
];
361 getscores((FILE **)NULL
);
362 (void)printf("\n\t\t\t Tetris High Scores\n");
365 * If level == 0, the person has not played a game but just asked for
366 * the high scores; we do not need to check for printing in highlight
367 * mode. If SOstr is null, we can't do highlighting anyway.
369 me
= level
&& SOstr
? thisuser() : NULL
;
372 * Set times to 0 except for high score on each level.
374 for (i
= MINLEVEL
; i
< NLEVELS
; i
++)
376 for (i
= 0, sp
= scores
; i
< nscores
; i
++, sp
++) {
377 if (sp
->hs_level
< NLEVELS
&& sp
->hs_level
>= 0) {
378 if (levelfound
[sp
->hs_level
])
382 levelfound
[sp
->hs_level
] = 1;
388 * Page each screenful of scores.
390 for (i
= 0, sp
= scores
; i
< nscores
; sp
+= n
) {
394 printem(level
, i
+ 1, sp
, n
, me
);
395 if ((i
+= n
) < nscores
) {
396 (void)printf("\nHit RETURN to continue.");
397 (void)fflush(stdout
);
398 while ((c
= getchar()) != '\n')
407 printem(level
, offset
, hs
, n
, me
)
409 struct highscore
*hs
;
413 struct highscore
*sp
;
414 int nrows
, row
, col
, item
, i
, highlight
;
416 #define TITLE "Rank Score Name (points/level)"
419 * This makes a nice two-column sort with headers, but it's a bit
422 printf("%s %s\n", TITLE
, n
> 1 ? TITLE
: "");
427 for (row
= 0; row
< nrows
; row
++) {
428 for (col
= 0; col
< 2; col
++) {
429 item
= col
* nrows
+ row
;
432 * Can only occur on trailing columns.
438 (void)snprintf(buf
, sizeof(buf
),
439 "%3d%c %6d %-11s (%6d on %d)",
440 item
+ offset
, sp
->hs_time
? '*' : ' ',
441 sp
->hs_score
* sp
->hs_level
,
442 sp
->hs_name
, sp
->hs_score
, sp
->hs_level
);
444 * Highlight if appropriate. This works because
445 * we only get one score per level.
448 sp
->hs_level
== level
&&
449 sp
->hs_score
== score
&&
450 strcmp(sp
->hs_name
, me
) == 0) {
454 (void)printf("%s", buf
);
460 /* fill in spaces so column 1 lines up */
462 for (i
= 40 - strlen(buf
); --i
>= 0;)