Rev 1466 | Rev 1653 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1466 | Rev 1590 | ||
---|---|---|---|
1 | /* $OpenBSD: scores.c,v 1.11 2006/04/20 03:25:36 ray Exp $ */ |
1 | /* $OpenBSD: scores.c,v 1.11 2006/04/20 03:25:36 ray Exp $ */ |
2 | /* $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $ */ |
2 | /* $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $ */ |
3 | 3 | ||
4 | /*- |
4 | /*- |
5 | * Copyright (c) 1992, 1993 |
5 | * Copyright (c) 1992, 1993 |
6 | * The Regents of the University of California. All rights reserved. |
6 | * The Regents of the University of California. All rights reserved. |
7 | * |
7 | * |
8 | * This code is derived from software contributed to Berkeley by |
8 | * This code is derived from software contributed to Berkeley by |
9 | * Chris Torek and Darren F. Provine. |
9 | * Chris Torek and Darren F. Provine. |
10 | * |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. Neither the name of the University nor the names of its contributors |
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 |
20 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. |
21 | * without specific prior written permission. |
22 | * |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
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 |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
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 |
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 |
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 |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | * SUCH DAMAGE. |
33 | * SUCH DAMAGE. |
34 | * |
34 | * |
35 | * @(#)scores.c 8.1 (Berkeley) 5/31/93 |
35 | * @(#)scores.c 8.1 (Berkeley) 5/31/93 |
36 | */ |
36 | */ |
37 | 37 | ||
38 | /* |
38 | /* |
39 | * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu) |
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 |
40 | * modified 22 January 1992, to limit the number of entries any one |
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 "tetris.h" |
61 | #include "tetris.h" |
62 | #include "scores.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 | * |
68 | * We also sometimes keep tabs on the "highest" score on each level. |
68 | * We also sometimes keep tabs on the "highest" score on each level. |
69 | * As long as the scores are kept sorted, this is simply the first one at |
69 | * As long as the scores are kept sorted, this is simply the first one at |
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 | static struct highscore scores[NUMSPOTS]; |
|
79 | 80 | ||
80 | /* static int checkscores(struct highscore *, int); */ |
81 | /* static int checkscores(struct highscore *, int); */ |
81 | /* static int cmpscores(const void *, const void *); */ |
82 | /* static int cmpscores(const void *, const void *); */ |
82 | /* static void getscores(FILE **); */ |
83 | /* static void getscores(FILE **); */ |
83 | /* static void printem(int, int, struct highscore *, int, const char *); */ |
84 | /* static void printem(int, int, struct highscore *, int, const char *); */ |
84 | /* static char *thisuser(void); */ |
85 | /* static char *thisuser(void); */ |
85 | 86 | ||
- | 87 | void showscores(int firstgame) |
|
- | 88 | { |
|
- | 89 | int i; |
|
- | 90 | ||
- | 91 | clear_screen(); |
|
- | 92 | moveto(10, 0); |
|
- | 93 | printf("\tRank \tLevel \tName\t points\n"); |
|
- | 94 | printf("\t========================================================\n"); |
|
- | 95 | for (i = 0; i < NUMSPOTS - 1; i++) { |
|
- | 96 | printf("\t%6d %6d %-16s %20d\n", i+1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score); |
|
- | 97 | } |
|
- | 98 | if (!firstgame) { |
|
- | 99 | printf("\t========================================================\n"); |
|
- | 100 | printf("\t Last %6d %-16s %20d\n", scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score); |
|
- | 101 | } |
|
- | 102 | printf("\n\n\n\n\tPress any key to return to main menu."); |
|
- | 103 | getchar(); |
|
- | 104 | } |
|
- | 105 | ||
- | 106 | /** Copy from hiscore table score with index src to dest |
|
- | 107 | * |
|
- | 108 | */ |
|
- | 109 | static void copyhiscore(int dest, int src) |
|
- | 110 | { |
|
- | 111 | strcpy(scores[dest].hs_name, scores[src].hs_name); |
|
- | 112 | scores[dest].hs_score = scores[src].hs_score; |
|
- | 113 | scores[dest].hs_level = scores[src].hs_level; |
|
- | 114 | } |
|
- | 115 | ||
- | 116 | void insertscore(int score, int level) |
|
- | 117 | { |
|
- | 118 | int i,j; |
|
- | 119 | int key; |
|
- | 120 | ||
- | 121 | ||
- | 122 | clear_screen(); |
|
- | 123 | moveto(10 , 10); |
|
- | 124 | puts("Insert your name: "); |
|
- | 125 | strncpy(scores[NUMSPOTS - 1].hs_name, "Player", MAXLOGNAME); |
|
- | 126 | i = 6; |
|
- | 127 | ||
- | 128 | moveto(10 , 28); |
|
- | 129 | printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................"); |
|
- | 130 | key = getchar(); |
|
- | 131 | while(key != '\n') { |
|
- | 132 | if (key == '\b') { |
|
- | 133 | if (i > 0) |
|
- | 134 | scores[NUMSPOTS - 1].hs_name[--i] = '\0'; |
|
- | 135 | } else { |
|
- | 136 | if (i < (MAXLOGNAME - 1)) |
|
- | 137 | scores[NUMSPOTS - 1].hs_name[i++] = key; |
|
- | 138 | scores[NUMSPOTS - 1].hs_name[i] = '\0'; |
|
- | 139 | } |
|
- | 140 | ||
- | 141 | moveto(10 , 28); |
|
- | 142 | printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................"); |
|
- | 143 | ||
- | 144 | key = getchar(); |
|
- | 145 | } |
|
- | 146 | ||
- | 147 | scores[NUMSPOTS - 1].hs_score = score; |
|
- | 148 | scores[NUMSPOTS - 1].hs_level = level; |
|
- | 149 | ||
- | 150 | i = NUMSPOTS-1; |
|
- | 151 | while ((i > 0) && (scores[i - 1].hs_score < score)) |
|
- | 152 | i--; |
|
- | 153 | ||
- | 154 | for (j = NUMSPOTS - 2; j > i; j--) { |
|
- | 155 | copyhiscore(j,j-1); |
|
- | 156 | } |
|
- | 157 | copyhiscore(i, NUMSPOTS - 1); |
|
- | 158 | } |
|
- | 159 | ||
- | 160 | void initscores(void) |
|
- | 161 | { |
|
- | 162 | int i; |
|
- | 163 | for(i = 0; i < NUMSPOTS; i++) { |
|
- | 164 | strncpy(scores[i].hs_name, "HelenOS Team", MAXLOGNAME); |
|
- | 165 | scores[i].hs_score = (NUMSPOTS - i) * 200; |
|
- | 166 | scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1); |
|
- | 167 | } |
|
- | 168 | } |
|
- | 169 | ||
86 | /* |
170 | /* |
87 | * Read the score file. Can be called from savescore (before showscores) |
171 | * Read the score file. Can be called from savescore (before showscores) |
88 | * or showscores (if savescore will not be called). If the given pointer |
172 | * 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 |
173 | * 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 |
174 | * 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. |
175 | * file is locked with LOCK_SH for the read and closed before return. |
92 | * |
176 | * |
93 | * Note, we assume closing the stdio file releases the lock. |
177 | * Note, we assume closing the stdio file releases the lock. |
94 | */ |
178 | */ |
95 | /* static void */ |
179 | /* static void */ |
96 | /* getscores(FILE **fpp) */ |
180 | /* getscores(FILE **fpp) */ |
97 | /* { */ |
181 | /* { */ |
98 | /* int sd, mint, lck, mask, i; */ |
182 | /* int sd, mint, lck, mask, i; */ |
99 | /* char *mstr, *human; */ |
183 | /* char *mstr, *human; */ |
100 | /* FILE *sf; */ |
184 | /* FILE *sf; */ |
101 | 185 | ||
102 | /* if (fpp != NULL) { */ |
186 | /* if (fpp != NULL) { */ |
103 | /* mint = O_RDWR | O_CREAT; */ |
187 | /* mint = O_RDWR | O_CREAT; */ |
104 | /* mstr = "r+"; */ |
188 | /* mstr = "r+"; */ |
105 | /* human = "read/write"; */ |
189 | /* human = "read/write"; */ |
106 | /* lck = LOCK_EX; */ |
190 | /* lck = LOCK_EX; */ |
107 | /* } else { */ |
191 | /* } else { */ |
108 | /* mint = O_RDONLY; */ |
192 | /* mint = O_RDONLY; */ |
109 | /* mstr = "r"; */ |
193 | /* mstr = "r"; */ |
110 | /* human = "reading"; */ |
194 | /* human = "reading"; */ |
111 | /* lck = LOCK_SH; */ |
195 | /* lck = LOCK_SH; */ |
112 | /* } */ |
196 | /* } */ |
113 | /* setegid(egid); */ |
197 | /* setegid(egid); */ |
114 | /* mask = umask(S_IWOTH); */ |
198 | /* mask = umask(S_IWOTH); */ |
115 | /* sd = open(_PATH_SCOREFILE, mint, 0666); */ |
199 | /* sd = open(_PATH_SCOREFILE, mint, 0666); */ |
116 | /* (void)umask(mask); */ |
200 | /* (void)umask(mask); */ |
117 | /* setegid(gid); */ |
201 | /* setegid(gid); */ |
118 | /* if (sd < 0) { */ |
202 | /* if (sd < 0) { */ |
119 | /* if (fpp == NULL) { */ |
203 | /* if (fpp == NULL) { */ |
120 | /* nscores = 0; */ |
204 | /* nscores = 0; */ |
121 | /* return; */ |
205 | /* return; */ |
122 | /* } */ |
206 | /* } */ |
123 | /* err(1, "cannot open %s for %s", _PATH_SCOREFILE, human); */ |
207 | /* err(1, "cannot open %s for %s", _PATH_SCOREFILE, human); */ |
124 | /* } */ |
208 | /* } */ |
125 | /* setegid(egid); */ |
209 | /* setegid(egid); */ |
126 | /* if ((sf = fdopen(sd, mstr)) == NULL) */ |
210 | /* if ((sf = fdopen(sd, mstr)) == NULL) */ |
127 | /* err(1, "cannot fdopen %s for %s", _PATH_SCOREFILE, human); */ |
211 | /* err(1, "cannot fdopen %s for %s", _PATH_SCOREFILE, human); */ |
128 | /* setegid(gid); */ |
212 | /* setegid(gid); */ |
129 | 213 | ||
130 | /* /\* */ |
214 | /* /\* */ |
131 | /* * Grab a lock. */ |
215 | /* * Grab a lock. */ |
132 | /* *\/ */ |
216 | /* *\/ */ |
133 | /* if (flock(sd, lck)) */ |
217 | /* if (flock(sd, lck)) */ |
134 | /* warn("warning: score file %s cannot be locked", */ |
218 | /* warn("warning: score file %s cannot be locked", */ |
135 | /* _PATH_SCOREFILE); */ |
219 | /* _PATH_SCOREFILE); */ |
136 | 220 | ||
137 | /* nscores = fread(scores, sizeof(scores[0]), MAXHISCORES, sf); */ |
221 | /* nscores = fread(scores, sizeof(scores[0]), MAXHISCORES, sf); */ |
138 | /* if (ferror(sf)) */ |
222 | /* if (ferror(sf)) */ |
139 | /* err(1, "error reading %s", _PATH_SCOREFILE); */ |
223 | /* err(1, "error reading %s", _PATH_SCOREFILE); */ |
140 | /* for (i = 0; i < nscores; i++) */ |
224 | /* for (i = 0; i < nscores; i++) */ |
141 | /* if (scores[i].hs_level < MINLEVEL || */ |
225 | /* if (scores[i].hs_level < MINLEVEL || */ |
142 | /* scores[i].hs_level > MAXLEVEL) */ |
226 | /* scores[i].hs_level > MAXLEVEL) */ |
143 | /* errx(1, "scorefile %s corrupt", _PATH_SCOREFILE); */ |
227 | /* errx(1, "scorefile %s corrupt", _PATH_SCOREFILE); */ |
144 | 228 | ||
145 | /* if (fpp) */ |
229 | /* if (fpp) */ |
146 | /* *fpp = sf; */ |
230 | /* *fpp = sf; */ |
147 | /* else */ |
231 | /* else */ |
148 | /* (void)fclose(sf); */ |
232 | /* (void)fclose(sf); */ |
149 | /* } */ |
233 | /* } */ |
150 | 234 | ||
151 | void |
235 | void |
152 | savescore(int level) |
236 | savescore(int level) |
153 | { |
237 | { |
154 | return; |
238 | return; |
155 | } |
239 | } |
156 | /* struct highscore *sp; */ |
240 | /* struct highscore *sp; */ |
157 | /* int i; */ |
241 | /* int i; */ |
158 | /* int change; */ |
242 | /* int change; */ |
159 | /* FILE *sf; */ |
243 | /* FILE *sf; */ |
160 | /* const char *me; */ |
244 | /* const char *me; */ |
161 | 245 | ||
162 | /* getscores(&sf); */ |
246 | /* getscores(&sf); */ |
163 | /* gotscores = 1; */ |
247 | /* gotscores = 1; */ |
164 | /* (void)time(&now); */ |
248 | /* (void)time(&now); */ |
165 | 249 | ||
166 | /* /\* */ |
250 | /* /\* */ |
167 | /* * Allow at most one score per person per level -- see if we */ |
251 | /* * Allow at most one score per person per level -- see if we */ |
168 | /* * can replace an existing score, or (easiest) do nothing. */ |
252 | /* * can replace an existing score, or (easiest) do nothing. */ |
169 | /* * Otherwise add new score at end (there is always room). */ |
253 | /* * Otherwise add new score at end (there is always room). */ |
170 | /* *\/ */ |
254 | /* *\/ */ |
171 | /* change = 0; */ |
255 | /* change = 0; */ |
172 | /* me = thisuser(); */ |
256 | /* me = thisuser(); */ |
173 | /* for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { */ |
257 | /* for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { */ |
174 | /* if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0) */ |
258 | /* if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0) */ |
175 | /* continue; */ |
259 | /* continue; */ |
176 | /* if (score > sp->hs_score) { */ |
260 | /* if (score > sp->hs_score) { */ |
177 | /* (void)printf("%s bettered %s %d score of %d!\n", */ |
261 | /* (void)printf("%s bettered %s %d score of %d!\n", */ |
178 | /* "\nYou", "your old level", level, */ |
262 | /* "\nYou", "your old level", level, */ |
179 | /* sp->hs_score * sp->hs_level); */ |
263 | /* sp->hs_score * sp->hs_level); */ |
180 | /* sp->hs_score = score; /\* new score *\/ */ |
264 | /* sp->hs_score = score; /\* new score *\/ */ |
181 | /* sp->hs_time = now; /\* and time *\/ */ |
265 | /* sp->hs_time = now; /\* and time *\/ */ |
182 | /* change = 1; */ |
266 | /* change = 1; */ |
183 | /* } else if (score == sp->hs_score) { */ |
267 | /* } else if (score == sp->hs_score) { */ |
184 | /* (void)printf("%s tied %s %d high score.\n", */ |
268 | /* (void)printf("%s tied %s %d high score.\n", */ |
185 | /* "\nYou", "your old level", level); */ |
269 | /* "\nYou", "your old level", level); */ |
186 | /* sp->hs_time = now; /\* renew it *\/ */ |
270 | /* sp->hs_time = now; /\* renew it *\/ */ |
187 | /* change = 1; /\* gotta rewrite, sigh *\/ */ |
271 | /* change = 1; /\* gotta rewrite, sigh *\/ */ |
188 | /* } /\* else new score < old score: do nothing *\/ */ |
272 | /* } /\* else new score < old score: do nothing *\/ */ |
189 | /* break; */ |
273 | /* break; */ |
190 | /* } */ |
274 | /* } */ |
191 | /* if (i >= nscores) { */ |
275 | /* if (i >= nscores) { */ |
192 | /* strlcpy(sp->hs_name, me, sizeof sp->hs_name); */ |
276 | /* strlcpy(sp->hs_name, me, sizeof sp->hs_name); */ |
193 | /* sp->hs_level = level; */ |
277 | /* sp->hs_level = level; */ |
194 | /* sp->hs_score = score; */ |
278 | /* sp->hs_score = score; */ |
195 | /* sp->hs_time = now; */ |
279 | /* sp->hs_time = now; */ |
196 | /* nscores++; */ |
280 | /* nscores++; */ |
197 | /* change = 1; */ |
281 | /* change = 1; */ |
198 | /* } */ |
282 | /* } */ |
199 | 283 | ||
200 | /* if (change) { */ |
284 | /* if (change) { */ |
201 | /* /\* */ |
285 | /* /\* */ |
202 | /* * Sort & clean the scores, then rewrite. */ |
286 | /* * Sort & clean the scores, then rewrite. */ |
203 | /* *\/ */ |
287 | /* *\/ */ |
204 | /* nscores = checkscores(scores, nscores); */ |
288 | /* nscores = checkscores(scores, nscores); */ |
205 | /* rewind(sf); */ |
289 | /* rewind(sf); */ |
206 | /* if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores || */ |
290 | /* if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores || */ |
207 | /* fflush(sf) == EOF) */ |
291 | /* fflush(sf) == EOF) */ |
208 | /* warnx("error writing %s: %s\n\t-- %s", */ |
292 | /* warnx("error writing %s: %s\n\t-- %s", */ |
209 | /* _PATH_SCOREFILE, strerror(errno), */ |
293 | /* _PATH_SCOREFILE, strerror(errno), */ |
210 | /* "high scores may be damaged"); */ |
294 | /* "high scores may be damaged"); */ |
211 | /* } */ |
295 | /* } */ |
212 | /* (void)fclose(sf); /\* releases lock *\/ */ |
296 | /* (void)fclose(sf); /\* releases lock *\/ */ |
213 | /* } */ |
297 | /* } */ |
214 | 298 | ||
215 | /* |
299 | /* |
216 | * Get login name, or if that fails, get something suitable. |
300 | * Get login name, or if that fails, get something suitable. |
217 | * The result is always trimmed to fit in a score. |
301 | * The result is always trimmed to fit in a score. |
218 | */ |
302 | */ |
219 | /* static char * */ |
303 | /* static char * */ |
220 | /* thisuser(void) */ |
304 | /* thisuser(void) */ |
221 | /* { */ |
305 | /* { */ |
222 | /* const char *p; */ |
306 | /* const char *p; */ |
223 | /* struct passwd *pw; */ |
307 | /* struct passwd *pw; */ |
224 | /* static char u[sizeof(scores[0].hs_name)]; */ |
308 | /* static char u[sizeof(scores[0].hs_name)]; */ |
225 | 309 | ||
226 | /* if (u[0]) */ |
310 | /* if (u[0]) */ |
227 | /* return (u); */ |
311 | /* return (u); */ |
228 | /* p = getlogin(); */ |
312 | /* p = getlogin(); */ |
229 | /* if (p == NULL || *p == '\0') { */ |
313 | /* if (p == NULL || *p == '\0') { */ |
230 | /* pw = getpwuid(getuid()); */ |
314 | /* pw = getpwuid(getuid()); */ |
231 | /* if (pw != NULL) */ |
315 | /* if (pw != NULL) */ |
232 | /* p = pw->pw_name; */ |
316 | /* p = pw->pw_name; */ |
233 | /* else */ |
317 | /* else */ |
234 | /* p = " ???"; */ |
318 | /* p = " ???"; */ |
235 | /* } */ |
319 | /* } */ |
236 | /* strlcpy(u, p, sizeof(u)); */ |
320 | /* strlcpy(u, p, sizeof(u)); */ |
237 | /* return (u); */ |
321 | /* return (u); */ |
238 | /* } */ |
322 | /* } */ |
239 | 323 | ||
240 | /* |
324 | /* |
241 | * Score comparison function for qsort. |
325 | * Score comparison function for qsort. |
242 | * |
326 | * |
243 | * If two scores are equal, the person who had the score first is |
327 | * If two scores are equal, the person who had the score first is |
244 | * listed first in the highscore file. |
328 | * listed first in the highscore file. |
245 | */ |
329 | */ |
246 | /* static int */ |
330 | /* static int */ |
247 | /* cmpscores(const void *x, const void *y) */ |
331 | /* cmpscores(const void *x, const void *y) */ |
248 | /* { */ |
332 | /* { */ |
249 | /* const struct highscore *a, *b; */ |
333 | /* const struct highscore *a, *b; */ |
250 | /* long l; */ |
334 | /* long l; */ |
251 | 335 | ||
252 | /* a = x; */ |
336 | /* a = x; */ |
253 | /* b = y; */ |
337 | /* b = y; */ |
254 | /* l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score; */ |
338 | /* l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score; */ |
255 | /* if (l < 0) */ |
339 | /* if (l < 0) */ |
256 | /* return (-1); */ |
340 | /* return (-1); */ |
257 | /* if (l > 0) */ |
341 | /* if (l > 0) */ |
258 | /* return (1); */ |
342 | /* return (1); */ |
259 | /* if (a->hs_time < b->hs_time) */ |
343 | /* if (a->hs_time < b->hs_time) */ |
260 | /* return (-1); */ |
344 | /* return (-1); */ |
261 | /* if (a->hs_time > b->hs_time) */ |
345 | /* if (a->hs_time > b->hs_time) */ |
262 | /* return (1); */ |
346 | /* return (1); */ |
263 | /* return (0); */ |
347 | /* return (0); */ |
264 | /* } */ |
348 | /* } */ |
265 | 349 | ||
266 | /* |
350 | /* |
267 | * If we've added a score to the file, we need to check the file and ensure |
351 | * If we've added a score to the file, we need to check the file and ensure |
268 | * that this player has only a few entries. The number of entries is |
352 | * that this player has only a few entries. The number of entries is |
269 | * controlled by MAXSCORES, and is to ensure that the highscore file is not |
353 | * controlled by MAXSCORES, and is to ensure that the highscore file is not |
270 | * monopolised by just a few people. People who no longer have accounts are |
354 | * monopolised by just a few people. People who no longer have accounts are |
271 | * only allowed the highest score. Scores older than EXPIRATION seconds are |
355 | * only allowed the highest score. Scores older than EXPIRATION seconds are |
272 | * removed, unless they are someone's personal best. |
356 | * removed, unless they are someone's personal best. |
273 | * Caveat: the highest score on each level is always kept. |
357 | * Caveat: the highest score on each level is always kept. |
274 | */ |
358 | */ |
275 | /* static int */ |
359 | /* static int */ |
276 | /* checkscores(struct highscore *hs, int num) */ |
360 | /* checkscores(struct highscore *hs, int num) */ |
277 | /* { */ |
361 | /* { */ |
278 | /* struct highscore *sp; */ |
362 | /* struct highscore *sp; */ |
279 | /* int i, j, k, numnames; */ |
363 | /* int i, j, k, numnames; */ |
280 | /* int levelfound[NLEVELS]; */ |
364 | /* int levelfound[NLEVELS]; */ |
281 | /* struct peruser { */ |
365 | /* struct peruser { */ |
282 | /* char *name; */ |
366 | /* char *name; */ |
283 | /* int times; */ |
367 | /* int times; */ |
284 | /* } count[NUMSPOTS]; */ |
368 | /* } count[NUMSPOTS]; */ |
285 | /* struct peruser *pu; */ |
369 | /* struct peruser *pu; */ |
286 | 370 | ||
287 | /* /\* */ |
371 | /* /\* */ |
288 | /* * Sort so that highest totals come first. */ |
372 | /* * Sort so that highest totals come first. */ |
289 | /* * */ |
373 | /* * */ |
290 | /* * levelfound[i] becomes set when the first high score for that */ |
374 | /* * levelfound[i] becomes set when the first high score for that */ |
291 | /* * level is encountered. By definition this is the highest score. */ |
375 | /* * level is encountered. By definition this is the highest score. */ |
292 | /* *\/ */ |
376 | /* *\/ */ |
293 | /* qsort((void *)hs, nscores, sizeof(*hs), cmpscores); */ |
377 | /* qsort((void *)hs, nscores, sizeof(*hs), cmpscores); */ |
294 | /* for (i = MINLEVEL; i < NLEVELS; i++) */ |
378 | /* for (i = MINLEVEL; i < NLEVELS; i++) */ |
295 | /* levelfound[i] = 0; */ |
379 | /* levelfound[i] = 0; */ |
296 | /* numnames = 0; */ |
380 | /* numnames = 0; */ |
297 | /* for (i = 0, sp = hs; i < num;) { */ |
381 | /* for (i = 0, sp = hs; i < num;) { */ |
298 | /* /\* */ |
382 | /* /\* */ |
299 | /* * This is O(n^2), but do you think we care? */ |
383 | /* * This is O(n^2), but do you think we care? */ |
300 | /* *\/ */ |
384 | /* *\/ */ |
301 | /* for (j = 0, pu = count; j < numnames; j++, pu++) */ |
385 | /* for (j = 0, pu = count; j < numnames; j++, pu++) */ |
302 | /* if (strcmp(sp->hs_name, pu->name) == 0) */ |
386 | /* if (strcmp(sp->hs_name, pu->name) == 0) */ |
303 | /* break; */ |
387 | /* break; */ |
304 | /* if (j == numnames) { */ |
388 | /* if (j == numnames) { */ |
305 | /* /\* */ |
389 | /* /\* */ |
306 | /* * Add new user, set per-user count to 1. */ |
390 | /* * Add new user, set per-user count to 1. */ |
307 | /* *\/ */ |
391 | /* *\/ */ |
308 | /* pu->name = sp->hs_name; */ |
392 | /* pu->name = sp->hs_name; */ |
309 | /* pu->times = 1; */ |
393 | /* pu->times = 1; */ |
310 | /* numnames++; */ |
394 | /* numnames++; */ |
311 | /* } else { */ |
395 | /* } else { */ |
312 | /* /\* */ |
396 | /* /\* */ |
313 | /* * Two ways to keep this score: */ |
397 | /* * Two ways to keep this score: */ |
314 | /* * - Not too many (per user), still has acct, & */ |
398 | /* * - Not too many (per user), still has acct, & */ |
315 | /* * score not dated; or */ |
399 | /* * score not dated; or */ |
316 | /* * - High score on this level. */ |
400 | /* * - High score on this level. */ |
317 | /* *\/ */ |
401 | /* *\/ */ |
318 | /* if ((pu->times < MAXSCORES && */ |
402 | /* if ((pu->times < MAXSCORES && */ |
319 | /* getpwnam(sp->hs_name) != NULL && */ |
403 | /* getpwnam(sp->hs_name) != NULL && */ |
320 | /* sp->hs_time + EXPIRATION >= now) || */ |
404 | /* sp->hs_time + EXPIRATION >= now) || */ |
321 | /* levelfound[sp->hs_level] == 0) */ |
405 | /* levelfound[sp->hs_level] == 0) */ |
322 | /* pu->times++; */ |
406 | /* pu->times++; */ |
323 | /* else { */ |
407 | /* else { */ |
324 | /* /\* */ |
408 | /* /\* */ |
325 | /* * Delete this score, do not count it, */ |
409 | /* * Delete this score, do not count it, */ |
326 | /* * do not pass go, do not collect $200. */ |
410 | /* * do not pass go, do not collect $200. */ |
327 | /* *\/ */ |
411 | /* *\/ */ |
328 | /* num--; */ |
412 | /* num--; */ |
329 | /* for (k = i; k < num; k++) */ |
413 | /* for (k = i; k < num; k++) */ |
330 | /* hs[k] = hs[k + 1]; */ |
414 | /* hs[k] = hs[k + 1]; */ |
331 | /* continue; */ |
415 | /* continue; */ |
332 | /* } */ |
416 | /* } */ |
333 | /* } */ |
417 | /* } */ |
334 | /* levelfound[sp->hs_level] = 1; */ |
418 | /* levelfound[sp->hs_level] = 1; */ |
335 | /* i++, sp++; */ |
419 | /* i++, sp++; */ |
336 | /* } */ |
420 | /* } */ |
337 | /* return (num > MAXHISCORES ? MAXHISCORES : num); */ |
421 | /* return (num > MAXHISCORES ? MAXHISCORES : num); */ |
338 | /* } */ |
422 | /* } */ |
339 | 423 | ||
340 | /* |
424 | /* |
341 | * Show current scores. This must be called after savescore, if |
425 | * Show current scores. This must be called after savescore, if |
342 | * savescore is called at all, for two reasons: |
426 | * savescore is called at all, for two reasons: |
343 | * - Showscores munches the time field. |
427 | * - Showscores munches the time field. |
344 | * - Even if that were not the case, a new score must be recorded |
428 | * - Even if that were not the case, a new score must be recorded |
345 | * before it can be shown anyway. |
429 | * before it can be shown anyway. |
346 | */ |
430 | */ |
- | 431 | /* |
|
347 | void |
432 | void |
348 | showscores(int level) |
433 | showscores(int level) |
349 | { |
434 | { |
350 | return; |
435 | return; |
351 | } |
436 | } |
352 | 437 | */ |
|
353 | /* struct highscore *sp; */ |
438 | /* struct highscore *sp; */ |
354 | /* int i, n, c; */ |
439 | /* int i, n, c; */ |
355 | /* const char *me; */ |
440 | /* const char *me; */ |
356 | /* int levelfound[NLEVELS]; */ |
441 | /* int levelfound[NLEVELS]; */ |
357 | 442 | ||
358 | /* if (!gotscores) */ |
443 | /* if (!gotscores) */ |
359 | /* getscores((FILE **)NULL); */ |
444 | /* getscores((FILE **)NULL); */ |
360 | /* (void)printf("\n\t\t Tetris High Scores\n"); */ |
445 | /* (void)printf("\n\t\t Tetris High Scores\n"); */ |
361 | 446 | ||
362 | /* /\* */ |
447 | /* /\* */ |
363 | /* * If level == 0, the person has not played a game but just asked for */ |
448 | /* * 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 */ |
449 | /* * 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. */ |
450 | /* * mode. If SOstr is null, we can't do highlighting anyway. */ |
366 | /* *\/ */ |
451 | /* *\/ */ |
367 | /* me = level && SOstr ? thisuser() : NULL; */ |
452 | /* me = level && SOstr ? thisuser() : NULL; */ |
368 | 453 | ||
369 | /* /\* */ |
454 | /* /\* */ |
370 | /* * Set times to 0 except for high score on each level. */ |
455 | /* * Set times to 0 except for high score on each level. */ |
371 | /* *\/ */ |
456 | /* *\/ */ |
372 | /* for (i = MINLEVEL; i < NLEVELS; i++) */ |
457 | /* for (i = MINLEVEL; i < NLEVELS; i++) */ |
373 | /* levelfound[i] = 0; */ |
458 | /* levelfound[i] = 0; */ |
374 | /* for (i = 0, sp = scores; i < nscores; i++, sp++) { */ |
459 | /* for (i = 0, sp = scores; i < nscores; i++, sp++) { */ |
375 | /* if (levelfound[sp->hs_level]) */ |
460 | /* if (levelfound[sp->hs_level]) */ |
376 | /* sp->hs_time = 0; */ |
461 | /* sp->hs_time = 0; */ |
377 | /* else { */ |
462 | /* else { */ |
378 | /* sp->hs_time = 1; */ |
463 | /* sp->hs_time = 1; */ |
379 | /* levelfound[sp->hs_level] = 1; */ |
464 | /* levelfound[sp->hs_level] = 1; */ |
380 | /* } */ |
465 | /* } */ |
381 | /* } */ |
466 | /* } */ |
382 | 467 | ||
383 | /* /\* */ |
468 | /* /\* */ |
384 | /* * Page each screenful of scores. */ |
469 | /* * Page each screenful of scores. */ |
385 | /* *\/ */ |
470 | /* *\/ */ |
386 | /* for (i = 0, sp = scores; i < nscores; sp += n) { */ |
471 | /* for (i = 0, sp = scores; i < nscores; sp += n) { */ |
387 | /* n = 20; */ |
472 | /* n = 20; */ |
388 | /* if (i + n > nscores) */ |
473 | /* if (i + n > nscores) */ |
389 | /* n = nscores - i; */ |
474 | /* n = nscores - i; */ |
390 | /* printem(level, i + 1, sp, n, me); */ |
475 | /* printem(level, i + 1, sp, n, me); */ |
391 | /* if ((i += n) < nscores) { */ |
476 | /* if ((i += n) < nscores) { */ |
392 | /* (void)printf("\nHit RETURN to continue."); */ |
477 | /* (void)printf("\nHit RETURN to continue."); */ |
393 | /* (void)fflush(stdout); */ |
478 | /* (void)fflush(stdout); */ |
394 | /* while ((c = getchar()) != '\n') */ |
479 | /* while ((c = getchar()) != '\n') */ |
395 | /* if (c == EOF) */ |
480 | /* if (c == EOF) */ |
396 | /* break; */ |
481 | /* break; */ |
397 | /* (void)printf("\n"); */ |
482 | /* (void)printf("\n"); */ |
398 | /* } */ |
483 | /* } */ |
399 | /* } */ |
484 | /* } */ |
400 | 485 | ||
401 | /* if (nscores == 0) */ |
486 | /* if (nscores == 0) */ |
402 | /* printf("\t\t\t - none to date.\n"); */ |
487 | /* printf("\t\t\t - none to date.\n"); */ |
403 | /* } */ |
488 | /* } */ |
404 | 489 | ||
405 | /* static void */ |
490 | /* static void */ |
406 | /* printem(int level, int offset, struct highscore *hs, int n, const char *me) */ |
491 | /* printem(int level, int offset, struct highscore *hs, int n, const char *me) */ |
407 | /* { */ |
492 | /* { */ |
408 | /* struct highscore *sp; */ |
493 | /* struct highscore *sp; */ |
409 | /* int row, highlight, i; */ |
494 | /* int row, highlight, i; */ |
410 | /* char buf[100]; */ |
495 | /* char buf[100]; */ |
411 | /* #define TITLE "Rank Score Name (points/level)" */ |
496 | /* #define TITLE "Rank Score Name (points/level)" */ |
412 | /* #define TITL2 "==========================================================" */ |
497 | /* #define TITL2 "==========================================================" */ |
413 | 498 | ||
414 | /* printf("%s\n%s\n", TITLE, TITL2); */ |
499 | /* printf("%s\n%s\n", TITLE, TITL2); */ |
415 | 500 | ||
416 | /* highlight = 0; */ |
501 | /* highlight = 0; */ |
417 | 502 | ||
418 | /* for (row = 0; row < n; row++) { */ |
503 | /* for (row = 0; row < n; row++) { */ |
419 | /* sp = &hs[row]; */ |
504 | /* sp = &hs[row]; */ |
420 | /* (void)snprintf(buf, sizeof(buf), */ |
505 | /* (void)snprintf(buf, sizeof(buf), */ |
421 | /* "%3d%c %6d %-31s (%6d on %d)\n", */ |
506 | /* "%3d%c %6d %-31s (%6d on %d)\n", */ |
422 | /* row + offset, sp->hs_time ? '*' : ' ', */ |
507 | /* row + offset, sp->hs_time ? '*' : ' ', */ |
423 | /* sp->hs_score * sp->hs_level, */ |
508 | /* sp->hs_score * sp->hs_level, */ |
424 | /* sp->hs_name, sp->hs_score, sp->hs_level); */ |
509 | /* sp->hs_name, sp->hs_score, sp->hs_level); */ |
425 | /* /\* Print leaders every three lines *\/ */ |
510 | /* /\* Print leaders every three lines *\/ */ |
426 | /* if ((row + 1) % 3 == 0) { */ |
511 | /* if ((row + 1) % 3 == 0) { */ |
427 | /* for (i = 0; i < sizeof(buf); i++) */ |
512 | /* for (i = 0; i < sizeof(buf); i++) */ |
428 | /* if (buf[i] == ' ') */ |
513 | /* if (buf[i] == ' ') */ |
429 | /* buf[i] = '_'; */ |
514 | /* buf[i] = '_'; */ |
430 | /* } */ |
515 | /* } */ |
431 | /* /\* */ |
516 | /* /\* */ |
432 | /* * Highlight if appropriate. This works because */ |
517 | /* * Highlight if appropriate. This works because */ |
433 | /* * we only get one score per level. */ |
518 | /* * we only get one score per level. */ |
434 | /* *\/ */ |
519 | /* *\/ */ |
435 | /* if (me != NULL && */ |
520 | /* if (me != NULL && */ |
436 | /* sp->hs_level == level && */ |
521 | /* sp->hs_level == level && */ |
437 | /* sp->hs_score == score && */ |
522 | /* sp->hs_score == score && */ |
438 | /* strcmp(sp->hs_name, me) == 0) { */ |
523 | /* strcmp(sp->hs_name, me) == 0) { */ |
439 | /* putpad(SOstr); */ |
524 | /* putpad(SOstr); */ |
440 | /* highlight = 1; */ |
525 | /* highlight = 1; */ |
441 | /* } */ |
526 | /* } */ |
442 | /* (void)printf("%s", buf); */ |
527 | /* (void)printf("%s", buf); */ |
443 | /* if (highlight) { */ |
528 | /* if (highlight) { */ |
444 | /* putpad(SEstr); */ |
529 | /* putpad(SEstr); */ |
445 | /* highlight = 0; */ |
530 | /* highlight = 0; */ |
446 | /* } */ |
531 | /* } */ |
447 | /* } */ |
532 | /* } */ |
448 | /* } */ |
533 | /* } */ |
449 | 534 |