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