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