Subversion Repositories HelenOS-historic

Rev

Rev 1590 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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