1 /* $NetBSD: score.c,v 1.21 2009/07/20 06:39:06 dholland Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: score.c,v 1.21 2009/07/20 06:39:06 dholland Exp $");
50 #include "pathnames.h"
52 const char *Scorefile
= _PATH_SCORE
;
54 int Max_per_uid
= MAX_PER_UID
;
56 static SCORE Top
[MAXSCORES
];
58 static uint32_t numscores
, max_uid
;
62 * Read the score file in MI format
69 if (read(inf
, &max_uid
, sizeof max_uid
) == sizeof max_uid
) {
70 max_uid
= ntohl(max_uid
);
72 read(inf
, Top
, sizeof Top
);
73 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++) {
74 scp
->s_uid
= ntohl(scp
->s_uid
);
75 scp
->s_score
= ntohl(scp
->s_score
);
76 scp
->s_auto
= ntohl(scp
->s_auto
);
77 scp
->s_level
= ntohl(scp
->s_level
);
81 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
83 max_uid
= Max_per_uid
;
89 * Write the score file in MI format
96 lseek(inf
, 0L, SEEK_SET
);
98 max_uid
= htonl(max_uid
);
99 write(inf
, &max_uid
, sizeof max_uid
);
101 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++) {
102 scp
->s_uid
= htonl(scp
->s_uid
);
103 scp
->s_score
= htonl(scp
->s_score
);
104 scp
->s_auto
= htonl(scp
->s_auto
);
105 scp
->s_level
= htonl(scp
->s_level
);
108 write(inf
, Top
, sizeof Top
);
114 * Post the player's score, if reasonable, and then print out the
123 bool done_show
= false;
132 if (Top
[MAXSCORES
-1].s_score
<= Score
) {
134 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
135 if ((scp
->s_uid
== uid
&& ++numscores
== max_uid
)) {
136 if (scp
->s_score
> Score
)
138 scp
->s_score
= Score
;
140 scp
->s_auto
= Auto_bot
;
141 scp
->s_level
= Level
;
146 if (scp
== &Top
[MAXSCORES
]) {
147 Top
[MAXSCORES
-1].s_score
= Score
;
148 Top
[MAXSCORES
-1].s_uid
= uid
;
149 Top
[MAXSCORES
-1].s_auto
= Auto_bot
;
150 Top
[MAXSCORES
-1].s_level
= Level
;
151 set_name(&Top
[MAXSCORES
-1]);
155 qsort(Top
, MAXSCORES
, sizeof Top
[0], cmp_sc
);
160 lseek(inf
, 0, SEEK_SET
);
167 printw("%5.5s %5.5s %-9.9s %-8.8s %5.5s", "Rank", "Score", "User",
170 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++) {
171 if (scp
->s_score
== 0)
173 move((scp
- Top
) + 2, 15);
174 if (!done_show
&& scp
->s_uid
== uid
&& scp
->s_score
== Score
)
176 printw("%5ld %5d %-8.8s %-9.9s %5d",
177 (long)(scp
- Top
) + 1, scp
->s_score
, scp
->s_name
,
178 scp
->s_auto
? "(autobot)" : "", scp
->s_level
);
179 if (!done_show
&& scp
->s_uid
== uid
&& scp
->s_score
== Score
) {
184 Num_scores
= scp
- Top
;
190 lseek(inf
, 0, SEEK_SET
);
198 if ((pp
= getpwuid(scp
->s_uid
)) == NULL
)
199 strncpy(scp
->s_name
, "???", MAXNAME
);
201 strncpy(scp
->s_name
, pp
->pw_name
, MAXNAME
);
206 * Compare two scores.
209 cmp_sc(const void *s1
, const void *s2
)
211 return ((const SCORE
*)s2
)->s_score
- ((const SCORE
*)s1
)->s_score
;
216 * Show the score list for the '-s' option.
224 if ((inf
= open(Scorefile
, O_RDONLY
)) < 0) {
225 warn("opening `%s'", Scorefile
);
232 printf("%5.5s %5.5s %-9.9s %-8.8s %5.5s\n", "Rank", "Score", "User",
234 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
235 if (scp
->s_score
> 0)
236 printf("%5d %5d %-8.8s %-9.9s %5d\n",
237 inf
++, scp
->s_score
, scp
->s_name
,
238 scp
->s_auto
? "(autobot)" : "", scp
->s_level
);