]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - robots/score.c
1 /* $NetBSD: score.c,v 1.4 1997/10/12 14:10:05 lukem 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. 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
21 * without specific prior written permission.
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
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: score.c,v 1.4 1997/10/12 14:10:05 lukem Exp $");
46 # include "pathnames.h"
48 char *Scorefile
= _PATH_SCORE
;
50 int Max_per_uid
= MAX_PER_UID
;
52 static SCORE Top
[MAXSCORES
];
56 * Post the player's score, if reasonable, and then print out the
65 bool done_show
= FALSE
;
66 static int numscores
, max_uid
;
69 if ((inf
= open(Scorefile
, 2)) < 0) {
74 if (read(inf
, &max_uid
, sizeof max_uid
) == sizeof max_uid
)
75 read(inf
, Top
, sizeof Top
);
77 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
79 max_uid
= Max_per_uid
;
83 if (Top
[MAXSCORES
-1].s_score
<= Score
) {
85 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
86 if (scp
->s_score
< 0 ||
87 (scp
->s_uid
== uid
&& ++numscores
== max_uid
)) {
88 if (scp
->s_score
> Score
)
96 if (scp
== &Top
[MAXSCORES
]) {
97 Top
[MAXSCORES
-1].s_score
= Score
;
98 Top
[MAXSCORES
-1].s_uid
= uid
;
99 set_name(&Top
[MAXSCORES
-1]);
103 qsort(Top
, MAXSCORES
, sizeof Top
[0], cmp_sc
);
114 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++) {
115 if (scp
->s_score
< 0)
117 move((scp
- Top
) + 1, 15);
118 if (!done_show
&& scp
->s_uid
== uid
&& scp
->s_score
== Score
)
120 printw(" %d\t%d\t%-8.8s ", (scp
- Top
) + 1, scp
->s_score
, scp
->s_name
);
121 if (!done_show
&& scp
->s_uid
== uid
&& scp
->s_score
== Score
) {
126 Num_scores
= scp
- Top
;
131 write(inf
, &max_uid
, sizeof max_uid
);
132 write(inf
, Top
, sizeof Top
);
143 if ((pp
= getpwuid(scp
->s_uid
)) == NULL
)
145 strncpy(scp
->s_name
, pp
->pw_name
, MAXNAME
);
150 * Compare two scores.
156 return ((SCORE
*)s2
)->s_score
- ((SCORE
*)s1
)->s_score
;
161 * Show the score list for the '-s' option.
168 static int max_score
;
170 if ((inf
= open(Scorefile
, 0)) < 0) {
175 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
178 read(inf
, &max_score
, sizeof max_score
);
179 read(inf
, Top
, sizeof Top
);
182 for (scp
= Top
; scp
< &Top
[MAXSCORES
]; scp
++)
183 if (scp
->s_score
>= 0)
184 printf("%d\t%d\t%.*s\n", inf
++, scp
->s_score
,
185 (int)(sizeof(scp
->s_name
)), scp
->s_name
);