Subversion Repositories HelenOS

Rev

Rev 3150 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3150 Rev 4348
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
/** @addtogroup tetris
38
/** @addtogroup tetris
39
 * @{
39
 * @{
40
 */
40
 */
41
/** @file
41
/** @file
42
 */
42
 */
43
 
43
 
44
/*
44
/*
45
 * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu)
45
 * Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu)
46
 * modified 22 January 1992, to limit the number of entries any one
46
 * modified 22 January 1992, to limit the number of entries any one
47
 * person has.
47
 * person has.
48
 *
48
 *
49
 * Major whacks since then.
49
 * Major whacks since then.
50
 */
50
 */
51
#include <errno.h>
51
#include <errno.h>
52
/* #include <err.h> */
52
/* #include <err.h> */
53
/* #include <fcntl.h> */
53
/* #include <fcntl.h> */
54
/* #include <pwd.h> */
54
/* #include <pwd.h> */
55
#include <stdio.h>
55
#include <stdio.h>
56
/* #include <stdlib.h> */
56
/* #include <stdlib.h> */
57
#include <string.h>
57
#include <string.h>
-
 
58
#include <kbd/kbd.h>
-
 
59
#include <kbd/keycode.h>
-
 
60
#include <stdlib.h>
58
/* #include <time.h> */
61
/* #include <time.h> */
59
/* #include <term.h> */
62
/* #include <term.h> */
60
/* #include <unistd.h> */
63
/* #include <unistd.h> */
61
/* #include <sys/param.h> */
64
/* #include <sys/param.h> */
62
/* #include <sys/stat.h> */
65
/* #include <sys/stat.h> */
63
/* #include <sys/types.h> */
66
/* #include <sys/types.h> */
64
 
67
 
65
#include "pathnames.h"
68
#include "pathnames.h"
66
#include "screen.h"
69
#include "screen.h"
67
#include "tetris.h"
70
#include "tetris.h"
68
#include "scores.h"
71
#include "scores.h"
69
 
72
 
70
/*
73
/*
71
 * Within this code, we can hang onto one extra "high score", leaving
74
 * Within this code, we can hang onto one extra "high score", leaving
72
 * room for our current score (whether or not it is high).
75
 * room for our current score (whether or not it is high).
73
 *
76
 *
74
 * We also sometimes keep tabs on the "highest" score on each level.
77
 * We also sometimes keep tabs on the "highest" score on each level.
75
 * As long as the scores are kept sorted, this is simply the first one at
78
 * As long as the scores are kept sorted, this is simply the first one at
76
 * that level.
79
 * that level.
77
 */
80
 */
78
#define NUMSPOTS (MAXHISCORES + 1)
81
#define NUMSPOTS (MAXHISCORES + 1)
79
#define NLEVELS (MAXLEVEL + 1)
82
#define NLEVELS (MAXLEVEL + 1)
80
 
83
 
81
/* static time_t now; */
84
/* static time_t now; */
82
/* static int nscores; */
85
/* static int nscores; */
83
/* static int gotscores; */
86
/* static int gotscores; */
84
/* static struct highscore scores[NUMSPOTS]; */
87
/* static struct highscore scores[NUMSPOTS]; */
85
static struct highscore scores[NUMSPOTS];
88
static struct highscore scores[NUMSPOTS];
86
 
89
 
87
/* static int checkscores(struct highscore *, int); */
90
/* static int checkscores(struct highscore *, int); */
88
/* static int cmpscores(const void *, const void *); */
91
/* static int cmpscores(const void *, const void *); */
89
/* static void getscores(FILE **); */
92
/* static void getscores(FILE **); */
90
/* static void printem(int, int, struct highscore *, int, const char *); */
93
/* static void printem(int, int, struct highscore *, int, const char *); */
91
/* static char *thisuser(void); */
94
/* static char *thisuser(void); */
92
 
95
 
93
void showscores(int firstgame)
96
void showscores(int firstgame)
94
{
97
{
95
    int i;
98
    int i;
96
   
99
   
97
    clear_screen();
100
    clear_screen();
98
    moveto(10, 0);
101
    moveto(10, 0);
99
    printf("\tRank \tLevel \tName\t                     points\n");
102
    printf("\tRank \tLevel \tName\t                     points\n");
100
    printf("\t========================================================\n");
103
    printf("\t========================================================\n");
101
    for (i = 0; i < NUMSPOTS - 1; i++) {
104
    for (i = 0; i < NUMSPOTS - 1; i++) {
102
        printf("\t%6d %6d %-16s %20d\n", i+1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
105
        printf("\t%6d %6d %-16s %20d\n", i+1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
103
    }
106
    }
104
    if (!firstgame) {
107
    if (!firstgame) {
105
        printf("\t========================================================\n");
108
        printf("\t========================================================\n");
106
        printf("\t  Last %6d %-16s %20d\n", scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
109
        printf("\t  Last %6d %-16s %20d\n", scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
107
    }
110
    }
108
    printf("\n\n\n\n\tPress any key to return to main menu.");
111
    printf("\n\n\n\n\tPress any key to return to main menu.");
109
    getchar();
112
    getchar();
110
}
113
}
111
 
114
 
112
/** Copy from hiscore table score with index src to dest
115
/** Copy from hiscore table score with index src to dest
113
 *
116
 *
114
 */
117
 */
115
static void copyhiscore(int dest, int src)
118
static void copyhiscore(int dest, int src)
116
{
119
{
117
    strcpy(scores[dest].hs_name, scores[src].hs_name);
120
    str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
-
 
121
        scores[src].hs_name);
118
    scores[dest].hs_score = scores[src].hs_score;  
122
    scores[dest].hs_score = scores[src].hs_score;  
119
    scores[dest].hs_level = scores[src].hs_level;
123
    scores[dest].hs_level = scores[src].hs_level;
120
}
124
}
121
 
125
 
122
void insertscore(int score, int level)
126
void insertscore(int score, int level)
123
{
127
{
124
    int i,j;
128
    int i,j;
125
    int key;
129
    size_t off;
126
 
130
    kbd_event_t ev;
127
   
131
   
128
    clear_screen();
132
    clear_screen();
129
    moveto(10 , 10);
133
    moveto(10 , 10);
130
    puts("Insert your name: ");
134
    puts("Insert your name: ");
131
    strncpy(scores[NUMSPOTS - 1].hs_name, "Player", MAXLOGNAME);
135
    str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
-
 
136
        "Player");
132
    i = 6;
137
    i = 6; off = 6;
133
 
138
 
134
    moveto(10 , 28);
139
    moveto(10 , 28);
135
    printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
140
    printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
-
 
141
 
-
 
142
    while (1) {
136
    key = getchar();   
143
        fflush(stdout);
-
 
144
        if (kbd_get_event(&ev) != EOK)
-
 
145
            exit(1);
-
 
146
 
137
    while(key != '\n') {
147
        if (ev.type == KE_RELEASE)
-
 
148
            continue;
-
 
149
 
-
 
150
        if (ev.key == KC_ENTER || ev.key == KC_NENTER)
-
 
151
            break;
-
 
152
 
138
        if (key == '\b') {
153
        if (ev.key == KC_BACKSPACE) {
139
            if (i > 0)
154
            if (i > 0) {
-
 
155
                wchar_t uc;
-
 
156
 
-
 
157
                --i;
-
 
158
                while (off > 0) {
-
 
159
                    --off;
-
 
160
                    size_t otmp = off;
-
 
161
                    uc = str_decode(scores[NUMSPOTS - 1].hs_name,
-
 
162
                        &otmp, STR_BOUNDS(MAXLOGNAME) + 1);
-
 
163
                    if (uc != U_SPECIAL)
-
 
164
                        break;
-
 
165
                }
-
 
166
 
140
                scores[NUMSPOTS - 1].hs_name[--i] = '\0';  
167
                scores[NUMSPOTS - 1].hs_name[off] = '\0';
-
 
168
            }
141
        } else {
169
        } else if (ev.c != '\0') {
142
            if (i < (MAXLOGNAME - 1))
170
            if (i < (MAXLOGNAME - 1)) {
143
                scores[NUMSPOTS - 1].hs_name[i++] = key;   
171
                if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name,
-
 
172
                    &off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
-
 
173
                    ++i;
-
 
174
                }
144
                scores[NUMSPOTS - 1].hs_name[i] = '\0';
175
                scores[NUMSPOTS - 1].hs_name[off] = '\0';
-
 
176
            }
145
        }
177
        }
146
       
178
       
147
        moveto(10 , 28);
179
        moveto(10 , 28);
148
        printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
180
        printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
149
   
-
 
150
        key = getchar();   
-
 
151
    }
181
    }
152
   
182
   
153
    scores[NUMSPOTS - 1].hs_score = score; 
183
    scores[NUMSPOTS - 1].hs_score = score; 
154
    scores[NUMSPOTS - 1].hs_level = level;
184
    scores[NUMSPOTS - 1].hs_level = level;
155
   
185
   
156
    i = NUMSPOTS-1;
186
    i = NUMSPOTS-1;
157
    while ((i > 0) && (scores[i - 1].hs_score < score))
187
    while ((i > 0) && (scores[i - 1].hs_score < score))
158
        i--;
188
        i--;
159
 
189
 
160
    for (j = NUMSPOTS - 2; j > i; j--) {
190
    for (j = NUMSPOTS - 2; j > i; j--) {
161
        copyhiscore(j,j-1);
191
        copyhiscore(j,j-1);
162
    }
192
    }
163
    copyhiscore(i, NUMSPOTS - 1);  
193
    copyhiscore(i, NUMSPOTS - 1);  
164
}
194
}
165
 
195
 
166
void initscores(void)
196
void initscores(void)
167
{
197
{
168
    int i;
198
    int i;
169
    for(i = 0; i < NUMSPOTS; i++) {
199
    for(i = 0; i < NUMSPOTS; i++) {
170
        strncpy(scores[i].hs_name, "HelenOS Team", MAXLOGNAME);
200
        str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
171
        scores[i].hs_score = (NUMSPOTS - i) * 200; 
201
        scores[i].hs_score = (NUMSPOTS - i) * 200; 
172
        scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1);
202
        scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1);
173
    }
203
    }
174
}
204
}
175
 
205
 
176
/*
206
/*
177
 * Read the score file.  Can be called from savescore (before showscores)
207
 * Read the score file.  Can be called from savescore (before showscores)
178
 * or showscores (if savescore will not be called).  If the given pointer
208
 * or showscores (if savescore will not be called).  If the given pointer
179
 * is not NULL, sets *fpp to an open file pointer that corresponds to a
209
 * is not NULL, sets *fpp to an open file pointer that corresponds to a
180
 * read/write score file that is locked with LOCK_EX.  Otherwise, the
210
 * read/write score file that is locked with LOCK_EX.  Otherwise, the
181
 * file is locked with LOCK_SH for the read and closed before return.
211
 * file is locked with LOCK_SH for the read and closed before return.
182
 *
212
 *
183
 * Note, we assume closing the stdio file releases the lock.
213
 * Note, we assume closing the stdio file releases the lock.
184
 */
214
 */
185
/* static void */
215
/* static void */
186
/* getscores(FILE **fpp) */
216
/* getscores(FILE **fpp) */
187
/* { */
217
/* { */
188
/*  int sd, mint, lck, mask, i; */
218
/*  int sd, mint, lck, mask, i; */
189
/*  char *mstr, *human; */
219
/*  char *mstr, *human; */
190
/*  FILE *sf; */
220
/*  FILE *sf; */
191
 
221
 
192
/*  if (fpp != NULL) { */
222
/*  if (fpp != NULL) { */
193
/*      mint = O_RDWR | O_CREAT; */
223
/*      mint = O_RDWR | O_CREAT; */
194
/*      mstr = "r+"; */
224
/*      mstr = "r+"; */
195
/*      human = "read/write"; */
225
/*      human = "read/write"; */
196
/*      lck = LOCK_EX; */
226
/*      lck = LOCK_EX; */
197
/*  } else { */
227
/*  } else { */
198
/*      mint = O_RDONLY; */
228
/*      mint = O_RDONLY; */
199
/*      mstr = "r"; */
229
/*      mstr = "r"; */
200
/*      human = "reading"; */
230
/*      human = "reading"; */
201
/*      lck = LOCK_SH; */
231
/*      lck = LOCK_SH; */
202
/*  } */
232
/*  } */
203
/*  setegid(egid); */
233
/*  setegid(egid); */
204
/*  mask = umask(S_IWOTH); */
234
/*  mask = umask(S_IWOTH); */
205
/*  sd = open(_PATH_SCOREFILE, mint, 0666); */
235
/*  sd = open(_PATH_SCOREFILE, mint, 0666); */
206
/*  (void)umask(mask); */
236
/*  (void)umask(mask); */
207
/*  setegid(gid); */
237
/*  setegid(gid); */
208
/*  if (sd < 0) { */
238
/*  if (sd < 0) { */
209
/*      if (fpp == NULL) { */
239
/*      if (fpp == NULL) { */
210
/*          nscores = 0; */
240
/*          nscores = 0; */
211
/*          return; */
241
/*          return; */
212
/*      } */
242
/*      } */
213
/*      err(1, "cannot open %s for %s", _PATH_SCOREFILE, human); */
243
/*      err(1, "cannot open %s for %s", _PATH_SCOREFILE, human); */
214
/*  } */
244
/*  } */
215
/*  setegid(egid); */
245
/*  setegid(egid); */
216
/*  if ((sf = fdopen(sd, mstr)) == NULL) */
246
/*  if ((sf = fdopen(sd, mstr)) == NULL) */
217
/*      err(1, "cannot fdopen %s for %s", _PATH_SCOREFILE, human); */
247
/*      err(1, "cannot fdopen %s for %s", _PATH_SCOREFILE, human); */
218
/*  setegid(gid); */
248
/*  setegid(gid); */
219
 
249
 
220
/*  /\* */
250
/*  /\* */
221
/*   * Grab a lock. */
251
/*   * Grab a lock. */
222
/*   *\/ */
252
/*   *\/ */
223
/*  if (flock(sd, lck)) */
253
/*  if (flock(sd, lck)) */
224
/*      warn("warning: score file %s cannot be locked", */
254
/*      warn("warning: score file %s cannot be locked", */
225
/*          _PATH_SCOREFILE); */
255
/*          _PATH_SCOREFILE); */
226
 
256
 
227
/*  nscores = fread(scores, sizeof(scores[0]), MAXHISCORES, sf); */
257
/*  nscores = fread(scores, sizeof(scores[0]), MAXHISCORES, sf); */
228
/*  if (ferror(sf)) */
258
/*  if (ferror(sf)) */
229
/*      err(1, "error reading %s", _PATH_SCOREFILE); */
259
/*      err(1, "error reading %s", _PATH_SCOREFILE); */
230
/*  for (i = 0; i < nscores; i++) */
260
/*  for (i = 0; i < nscores; i++) */
231
/*      if (scores[i].hs_level < MINLEVEL || */
261
/*      if (scores[i].hs_level < MINLEVEL || */
232
/*          scores[i].hs_level > MAXLEVEL) */
262
/*          scores[i].hs_level > MAXLEVEL) */
233
/*          errx(1, "scorefile %s corrupt", _PATH_SCOREFILE); */
263
/*          errx(1, "scorefile %s corrupt", _PATH_SCOREFILE); */
234
 
264
 
235
/*  if (fpp) */
265
/*  if (fpp) */
236
/*      *fpp = sf; */
266
/*      *fpp = sf; */
237
/*  else */
267
/*  else */
238
/*      (void)fclose(sf); */
268
/*      (void)fclose(sf); */
239
/* } */
269
/* } */
240
 
270
 
241
void
271
void
242
savescore(int level)
272
savescore(int level)
243
{
273
{
244
    return;
274
    return;
245
}
275
}
246
/*  struct highscore *sp; */
276
/*  struct highscore *sp; */
247
/*  int i; */
277
/*  int i; */
248
/*  int change; */
278
/*  int change; */
249
/*  FILE *sf; */
279
/*  FILE *sf; */
250
/*  const char *me; */
280
/*  const char *me; */
251
 
281
 
252
/*  getscores(&sf); */
282
/*  getscores(&sf); */
253
/*  gotscores = 1; */
283
/*  gotscores = 1; */
254
/*  (void)time(&now); */
284
/*  (void)time(&now); */
255
 
285
 
256
/*  /\* */
286
/*  /\* */
257
/*   * Allow at most one score per person per level -- see if we */
287
/*   * Allow at most one score per person per level -- see if we */
258
/*   * can replace an existing score, or (easiest) do nothing. */
288
/*   * can replace an existing score, or (easiest) do nothing. */
259
/*   * Otherwise add new score at end (there is always room). */
289
/*   * Otherwise add new score at end (there is always room). */
260
/*   *\/ */
290
/*   *\/ */
261
/*  change = 0; */
291
/*  change = 0; */
262
/*  me = thisuser(); */
292
/*  me = thisuser(); */
263
/*  for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { */
293
/*  for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { */
264
/*      if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0) */
294
/*      if (sp->hs_level != level || str_cmp(sp->hs_name, me) != 0) */
265
/*          continue; */
295
/*          continue; */
266
/*      if (score > sp->hs_score) { */
296
/*      if (score > sp->hs_score) { */
267
/*          (void)printf("%s bettered %s %d score of %d!\n", */
297
/*          (void)printf("%s bettered %s %d score of %d!\n", */
268
/*              "\nYou", "your old level", level, */
298
/*              "\nYou", "your old level", level, */
269
/*              sp->hs_score * sp->hs_level); */
299
/*              sp->hs_score * sp->hs_level); */
270
/*          sp->hs_score = score;   /\* new score *\/ */
300
/*          sp->hs_score = score;   /\* new score *\/ */
271
/*          sp->hs_time = now;  /\* and time *\/ */
301
/*          sp->hs_time = now;  /\* and time *\/ */
272
/*          change = 1; */
302
/*          change = 1; */
273
/*      } else if (score == sp->hs_score) { */
303
/*      } else if (score == sp->hs_score) { */
274
/*          (void)printf("%s tied %s %d high score.\n", */
304
/*          (void)printf("%s tied %s %d high score.\n", */
275
/*              "\nYou", "your old level", level); */
305
/*              "\nYou", "your old level", level); */
276
/*          sp->hs_time = now;  /\* renew it *\/ */
306
/*          sp->hs_time = now;  /\* renew it *\/ */
277
/*          change = 1;     /\* gotta rewrite, sigh *\/ */
307
/*          change = 1;     /\* gotta rewrite, sigh *\/ */
278
/*      } /\* else new score < old score: do nothing *\/ */
308
/*      } /\* else new score < old score: do nothing *\/ */
279
/*      break; */
309
/*      break; */
280
/*  } */
310
/*  } */
281
/*  if (i >= nscores) { */
311
/*  if (i >= nscores) { */
282
/*      strlcpy(sp->hs_name, me, sizeof sp->hs_name); */
312
/*      strlcpy(sp->hs_name, me, sizeof sp->hs_name); */
283
/*      sp->hs_level = level; */
313
/*      sp->hs_level = level; */
284
/*      sp->hs_score = score; */
314
/*      sp->hs_score = score; */
285
/*      sp->hs_time = now; */
315
/*      sp->hs_time = now; */
286
/*      nscores++; */
316
/*      nscores++; */
287
/*      change = 1; */
317
/*      change = 1; */
288
/*  } */
318
/*  } */
289
 
319
 
290
/*  if (change) { */
320
/*  if (change) { */
291
/*      /\* */
321
/*      /\* */
292
/*       * Sort & clean the scores, then rewrite. */
322
/*       * Sort & clean the scores, then rewrite. */
293
/*       *\/ */
323
/*       *\/ */
294
/*      nscores = checkscores(scores, nscores); */
324
/*      nscores = checkscores(scores, nscores); */
295
/*      rewind(sf); */
325
/*      rewind(sf); */
296
/*      if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores || */
326
/*      if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores || */
297
/*          fflush(sf) == EOF) */
327
/*          fflush(sf) == EOF) */
298
/*          warnx("error writing %s: %s\n\t-- %s", */
328
/*          warnx("error writing %s: %s\n\t-- %s", */
299
/*              _PATH_SCOREFILE, strerror(errno), */
329
/*              _PATH_SCOREFILE, strerror(errno), */
300
/*              "high scores may be damaged"); */
330
/*              "high scores may be damaged"); */
301
/*  } */
331
/*  } */
302
/*  (void)fclose(sf);   /\* releases lock *\/ */
332
/*  (void)fclose(sf);   /\* releases lock *\/ */
303
/* } */
333
/* } */
304
 
334
 
305
/*
335
/*
306
 * Get login name, or if that fails, get something suitable.
336
 * Get login name, or if that fails, get something suitable.
307
 * The result is always trimmed to fit in a score.
337
 * The result is always trimmed to fit in a score.
308
 */
338
 */
309
/* static char * */
339
/* static char * */
310
/* thisuser(void) */
340
/* thisuser(void) */
311
/* { */
341
/* { */
312
/*  const char *p; */
342
/*  const char *p; */
313
/*  struct passwd *pw; */
343
/*  struct passwd *pw; */
314
/*  static char u[sizeof(scores[0].hs_name)]; */
344
/*  static char u[sizeof(scores[0].hs_name)]; */
315
 
345
 
316
/*  if (u[0]) */
346
/*  if (u[0]) */
317
/*      return (u); */
347
/*      return (u); */
318
/*  p = getlogin(); */
348
/*  p = getlogin(); */
319
/*  if (p == NULL || *p == '\0') { */
349
/*  if (p == NULL || *p == '\0') { */
320
/*      pw = getpwuid(getuid()); */
350
/*      pw = getpwuid(getuid()); */
321
/*      if (pw != NULL) */
351
/*      if (pw != NULL) */
322
/*          p = pw->pw_name; */
352
/*          p = pw->pw_name; */
323
/*      else */
353
/*      else */
324
/*          p = "  ???"; */
354
/*          p = "  ???"; */
325
/*  } */
355
/*  } */
326
/*  strlcpy(u, p, sizeof(u)); */
356
/*  strlcpy(u, p, sizeof(u)); */
327
/*  return (u); */
357
/*  return (u); */
328
/* } */
358
/* } */
329
 
359
 
330
/*
360
/*
331
 * Score comparison function for qsort.
361
 * Score comparison function for qsort.
332
 *
362
 *
333
 * If two scores are equal, the person who had the score first is
363
 * If two scores are equal, the person who had the score first is
334
 * listed first in the highscore file.
364
 * listed first in the highscore file.
335
 */
365
 */
336
/* static int */
366
/* static int */
337
/* cmpscores(const void *x, const void *y) */
367
/* cmpscores(const void *x, const void *y) */
338
/* { */
368
/* { */
339
/*  const struct highscore *a, *b; */
369
/*  const struct highscore *a, *b; */
340
/*  long l; */
370
/*  long l; */
341
 
371
 
342
/*  a = x; */
372
/*  a = x; */
343
/*  b = y; */
373
/*  b = y; */
344
/*  l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score; */
374
/*  l = (long)b->hs_level * b->hs_score - (long)a->hs_level * a->hs_score; */
345
/*  if (l < 0) */
375
/*  if (l < 0) */
346
/*      return (-1); */
376
/*      return (-1); */
347
/*  if (l > 0) */
377
/*  if (l > 0) */
348
/*      return (1); */
378
/*      return (1); */
349
/*  if (a->hs_time < b->hs_time) */
379
/*  if (a->hs_time < b->hs_time) */
350
/*      return (-1); */
380
/*      return (-1); */
351
/*  if (a->hs_time > b->hs_time) */
381
/*  if (a->hs_time > b->hs_time) */
352
/*      return (1); */
382
/*      return (1); */
353
/*  return (0); */
383
/*  return (0); */
354
/* } */
384
/* } */
355
 
385
 
356
/*
386
/*
357
 * If we've added a score to the file, we need to check the file and ensure
387
 * If we've added a score to the file, we need to check the file and ensure
358
 * that this player has only a few entries.  The number of entries is
388
 * that this player has only a few entries.  The number of entries is
359
 * controlled by MAXSCORES, and is to ensure that the highscore file is not
389
 * controlled by MAXSCORES, and is to ensure that the highscore file is not
360
 * monopolised by just a few people.  People who no longer have accounts are
390
 * monopolised by just a few people.  People who no longer have accounts are
361
 * only allowed the highest score.  Scores older than EXPIRATION seconds are
391
 * only allowed the highest score.  Scores older than EXPIRATION seconds are
362
 * removed, unless they are someone's personal best.
392
 * removed, unless they are someone's personal best.
363
 * Caveat:  the highest score on each level is always kept.
393
 * Caveat:  the highest score on each level is always kept.
364
 */
394
 */
365
/* static int */
395
/* static int */
366
/* checkscores(struct highscore *hs, int num) */
396
/* checkscores(struct highscore *hs, int num) */
367
/* { */
397
/* { */
368
/*  struct highscore *sp; */
398
/*  struct highscore *sp; */
369
/*  int i, j, k, numnames; */
399
/*  int i, j, k, numnames; */
370
/*  int levelfound[NLEVELS]; */
400
/*  int levelfound[NLEVELS]; */
371
/*  struct peruser { */
401
/*  struct peruser { */
372
/*      char *name; */
402
/*      char *name; */
373
/*      int times; */
403
/*      int times; */
374
/*  } count[NUMSPOTS]; */
404
/*  } count[NUMSPOTS]; */
375
/*  struct peruser *pu; */
405
/*  struct peruser *pu; */
376
 
406
 
377
/*  /\* */
407
/*  /\* */
378
/*   * Sort so that highest totals come first. */
408
/*   * Sort so that highest totals come first. */
379
/*   * */
409
/*   * */
380
/*   * levelfound[i] becomes set when the first high score for that */
410
/*   * levelfound[i] becomes set when the first high score for that */
381
/*   * level is encountered.  By definition this is the highest score. */
411
/*   * level is encountered.  By definition this is the highest score. */
382
/*   *\/ */
412
/*   *\/ */
383
/*  qsort((void *)hs, nscores, sizeof(*hs), cmpscores); */
413
/*  qsort((void *)hs, nscores, sizeof(*hs), cmpscores); */
384
/*  for (i = MINLEVEL; i < NLEVELS; i++) */
414
/*  for (i = MINLEVEL; i < NLEVELS; i++) */
385
/*      levelfound[i] = 0; */
415
/*      levelfound[i] = 0; */
386
/*  numnames = 0; */
416
/*  numnames = 0; */
387
/*  for (i = 0, sp = hs; i < num;) { */
417
/*  for (i = 0, sp = hs; i < num;) { */
388
/*      /\* */
418
/*      /\* */
389
/*       * This is O(n^2), but do you think we care? */
419
/*       * This is O(n^2), but do you think we care? */
390
/*       *\/ */
420
/*       *\/ */
391
/*      for (j = 0, pu = count; j < numnames; j++, pu++) */
421
/*      for (j = 0, pu = count; j < numnames; j++, pu++) */
392
/*          if (strcmp(sp->hs_name, pu->name) == 0) */
422
/*          if (str_cmp(sp->hs_name, pu->name) == 0) */
393
/*              break; */
423
/*              break; */
394
/*      if (j == numnames) { */
424
/*      if (j == numnames) { */
395
/*          /\* */
425
/*          /\* */
396
/*           * Add new user, set per-user count to 1. */
426
/*           * Add new user, set per-user count to 1. */
397
/*           *\/ */
427
/*           *\/ */
398
/*          pu->name = sp->hs_name; */
428
/*          pu->name = sp->hs_name; */
399
/*          pu->times = 1; */
429
/*          pu->times = 1; */
400
/*          numnames++; */
430
/*          numnames++; */
401
/*      } else { */
431
/*      } else { */
402
/*          /\* */
432
/*          /\* */
403
/*           * Two ways to keep this score: */
433
/*           * Two ways to keep this score: */
404
/*           * - Not too many (per user), still has acct, & */
434
/*           * - Not too many (per user), still has acct, & */
405
/*           *  score not dated; or */
435
/*           *  score not dated; or */
406
/*           * - High score on this level. */
436
/*           * - High score on this level. */
407
/*           *\/ */
437
/*           *\/ */
408
/*          if ((pu->times < MAXSCORES && */
438
/*          if ((pu->times < MAXSCORES && */
409
/*               getpwnam(sp->hs_name) != NULL && */
439
/*               getpwnam(sp->hs_name) != NULL && */
410
/*               sp->hs_time + EXPIRATION >= now) || */
440
/*               sp->hs_time + EXPIRATION >= now) || */
411
/*              levelfound[sp->hs_level] == 0) */
441
/*              levelfound[sp->hs_level] == 0) */
412
/*              pu->times++; */
442
/*              pu->times++; */
413
/*          else { */
443
/*          else { */
414
/*              /\* */
444
/*              /\* */
415
/*               * Delete this score, do not count it, */
445
/*               * Delete this score, do not count it, */
416
/*               * do not pass go, do not collect $200. */
446
/*               * do not pass go, do not collect $200. */
417
/*               *\/ */
447
/*               *\/ */
418
/*              num--; */
448
/*              num--; */
419
/*              for (k = i; k < num; k++) */
449
/*              for (k = i; k < num; k++) */
420
/*                  hs[k] = hs[k + 1]; */
450
/*                  hs[k] = hs[k + 1]; */
421
/*              continue; */
451
/*              continue; */
422
/*          } */
452
/*          } */
423
/*      } */
453
/*      } */
424
/*      levelfound[sp->hs_level] = 1; */
454
/*      levelfound[sp->hs_level] = 1; */
425
/*      i++, sp++; */
455
/*      i++, sp++; */
426
/*  } */
456
/*  } */
427
/*  return (num > MAXHISCORES ? MAXHISCORES : num); */
457
/*  return (num > MAXHISCORES ? MAXHISCORES : num); */
428
/* } */
458
/* } */
429
 
459
 
430
/*
460
/*
431
 * Show current scores.  This must be called after savescore, if
461
 * Show current scores.  This must be called after savescore, if
432
 * savescore is called at all, for two reasons:
462
 * savescore is called at all, for two reasons:
433
 * - Showscores munches the time field.
463
 * - Showscores munches the time field.
434
 * - Even if that were not the case, a new score must be recorded
464
 * - Even if that were not the case, a new score must be recorded
435
 *   before it can be shown anyway.
465
 *   before it can be shown anyway.
436
 */
466
 */
437
/*
467
/*
438
void
468
void
439
showscores(int level)
469
showscores(int level)
440
{
470
{
441
    return;
471
    return;
442
}
472
}
443
*/
473
*/
444
/*  struct highscore *sp; */
474
/*  struct highscore *sp; */
445
/*  int i, n, c; */
475
/*  int i, n, c; */
446
/*  const char *me; */
476
/*  const char *me; */
447
/*  int levelfound[NLEVELS]; */
477
/*  int levelfound[NLEVELS]; */
448
 
478
 
449
/*  if (!gotscores) */
479
/*  if (!gotscores) */
450
/*      getscores((FILE **)NULL); */
480
/*      getscores((FILE **)NULL); */
451
/*  (void)printf("\n\t\t    Tetris High Scores\n"); */
481
/*  (void)printf("\n\t\t    Tetris High Scores\n"); */
452
 
482
 
453
/*  /\* */
483
/*  /\* */
454
/*   * If level == 0, the person has not played a game but just asked for */
484
/*   * If level == 0, the person has not played a game but just asked for */
455
/*   * the high scores; we do not need to check for printing in highlight */
485
/*   * the high scores; we do not need to check for printing in highlight */
456
/*   * mode.  If SOstr is null, we can't do highlighting anyway. */
486
/*   * mode.  If SOstr is null, we can't do highlighting anyway. */
457
/*   *\/ */
487
/*   *\/ */
458
/*  me = level && SOstr ? thisuser() : NULL; */
488
/*  me = level && SOstr ? thisuser() : NULL; */
459
 
489
 
460
/*  /\* */
490
/*  /\* */
461
/*   * Set times to 0 except for high score on each level. */
491
/*   * Set times to 0 except for high score on each level. */
462
/*   *\/ */
492
/*   *\/ */
463
/*  for (i = MINLEVEL; i < NLEVELS; i++) */
493
/*  for (i = MINLEVEL; i < NLEVELS; i++) */
464
/*      levelfound[i] = 0; */
494
/*      levelfound[i] = 0; */
465
/*  for (i = 0, sp = scores; i < nscores; i++, sp++) { */
495
/*  for (i = 0, sp = scores; i < nscores; i++, sp++) { */
466
/*      if (levelfound[sp->hs_level]) */
496
/*      if (levelfound[sp->hs_level]) */
467
/*          sp->hs_time = 0; */
497
/*          sp->hs_time = 0; */
468
/*      else { */
498
/*      else { */
469
/*          sp->hs_time = 1; */
499
/*          sp->hs_time = 1; */
470
/*          levelfound[sp->hs_level] = 1; */
500
/*          levelfound[sp->hs_level] = 1; */
471
/*      } */
501
/*      } */
472
/*  } */
502
/*  } */
473
 
503
 
474
/*  /\* */
504
/*  /\* */
475
/*   * Page each screenful of scores. */
505
/*   * Page each screenful of scores. */
476
/*   *\/ */
506
/*   *\/ */
477
/*  for (i = 0, sp = scores; i < nscores; sp += n) { */
507
/*  for (i = 0, sp = scores; i < nscores; sp += n) { */
478
/*      n = 20; */
508
/*      n = 20; */
479
/*      if (i + n > nscores) */
509
/*      if (i + n > nscores) */
480
/*          n = nscores - i; */
510
/*          n = nscores - i; */
481
/*      printem(level, i + 1, sp, n, me); */
511
/*      printem(level, i + 1, sp, n, me); */
482
/*      if ((i += n) < nscores) { */
512
/*      if ((i += n) < nscores) { */
483
/*          (void)printf("\nHit RETURN to continue."); */
513
/*          (void)printf("\nHit RETURN to continue."); */
484
/*          (void)fflush(stdout); */
514
/*          (void)fflush(stdout); */
485
/*          while ((c = getchar()) != '\n') */
515
/*          while ((c = getchar()) != '\n') */
486
/*              if (c == EOF) */
516
/*              if (c == EOF) */
487
/*                  break; */
517
/*                  break; */
488
/*          (void)printf("\n"); */
518
/*          (void)printf("\n"); */
489
/*      } */
519
/*      } */
490
/*  } */
520
/*  } */
491
 
521
 
492
/*  if (nscores == 0) */
522
/*  if (nscores == 0) */
493
/*      printf("\t\t\t      - none to date.\n"); */
523
/*      printf("\t\t\t      - none to date.\n"); */
494
/* } */
524
/* } */
495
 
525
 
496
/* static void */
526
/* static void */
497
/* printem(int level, int offset, struct highscore *hs, int n, const char *me) */
527
/* printem(int level, int offset, struct highscore *hs, int n, const char *me) */
498
/* { */
528
/* { */
499
/*  struct highscore *sp; */
529
/*  struct highscore *sp; */
500
/*  int row, highlight, i; */
530
/*  int row, highlight, i; */
501
/*  char buf[100]; */
531
/*  char buf[100]; */
502
/* #define  TITLE "Rank  Score   Name                          (points/level)" */
532
/* #define  TITLE "Rank  Score   Name                          (points/level)" */
503
/* #define  TITL2 "==========================================================" */
533
/* #define  TITL2 "==========================================================" */
504
 
534
 
505
/*  printf("%s\n%s\n", TITLE, TITL2); */
535
/*  printf("%s\n%s\n", TITLE, TITL2); */
506
 
536
 
507
/*  highlight = 0; */
537
/*  highlight = 0; */
508
 
538
 
509
/*  for (row = 0; row < n; row++) { */
539
/*  for (row = 0; row < n; row++) { */
510
/*      sp = &hs[row]; */
540
/*      sp = &hs[row]; */
511
/*      (void)snprintf(buf, sizeof(buf), */
541
/*      (void)snprintf(buf, sizeof(buf), */
512
/*          "%3d%c %6d  %-31s (%6d on %d)\n", */
542
/*          "%3d%c %6d  %-31s (%6d on %d)\n", */
513
/*          row + offset, sp->hs_time ? '*' : ' ', */
543
/*          row + offset, sp->hs_time ? '*' : ' ', */
514
/*          sp->hs_score * sp->hs_level, */
544
/*          sp->hs_score * sp->hs_level, */
515
/*          sp->hs_name, sp->hs_score, sp->hs_level); */
545
/*          sp->hs_name, sp->hs_score, sp->hs_level); */
516
/*      /\* Print leaders every three lines *\/ */
546
/*      /\* Print leaders every three lines *\/ */
517
/*      if ((row + 1) % 3 == 0) { */
547
/*      if ((row + 1) % 3 == 0) { */
518
/*          for (i = 0; i < sizeof(buf); i++) */
548
/*          for (i = 0; i < sizeof(buf); i++) */
519
/*              if (buf[i] == ' ') */
549
/*              if (buf[i] == ' ') */
520
/*                  buf[i] = '_'; */
550
/*                  buf[i] = '_'; */
521
/*      } */
551
/*      } */
522
/*      /\* */
552
/*      /\* */
523
/*       * Highlight if appropriate.  This works because */
553
/*       * Highlight if appropriate.  This works because */
524
/*       * we only get one score per level. */
554
/*       * we only get one score per level. */
525
/*       *\/ */
555
/*       *\/ */
526
/*      if (me != NULL && */
556
/*      if (me != NULL && */
527
/*          sp->hs_level == level && */
557
/*          sp->hs_level == level && */
528
/*          sp->hs_score == score && */
558
/*          sp->hs_score == score && */
529
/*          strcmp(sp->hs_name, me) == 0) { */
559
/*          str_cmp(sp->hs_name, me) == 0) { */
530
/*          putpad(SOstr); */
560
/*          putpad(SOstr); */
531
/*          highlight = 1; */
561
/*          highlight = 1; */
532
/*      } */
562
/*      } */
533
/*      (void)printf("%s", buf); */
563
/*      (void)printf("%s", buf); */
534
/*      if (highlight) { */
564
/*      if (highlight) { */
535
/*          putpad(SEstr); */
565
/*          putpad(SEstr); */
536
/*          highlight = 0; */
566
/*          highlight = 0; */
537
/*      } */
567
/*      } */
538
/*  } */
568
/*  } */
539
/* } */
569
/* } */
540
 
570
 
541
/** @}
571
/** @}
542
 */
572
 */
543
 
573
 
544
 
574