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