score.c revision 1.8
1/*	$OpenBSD: score.c,v 1.8 2003/06/03 03:01:41 millert Exp $	*/
2/*	$NetBSD: score.c,v 1.3 1995/04/22 10:09:12 cgd Exp $	*/
3
4/*
5 * Copyright (c) 1980, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)score.c	8.1 (Berkeley) 5/31/93";
36#else
37static char rcsid[] = "$OpenBSD: score.c,v 1.8 2003/06/03 03:01:41 millert Exp $";
38#endif
39#endif /* not lint */
40
41#include	"robots.h"
42#include	"pathnames.h"
43
44char	*Scorefile = _PATH_SCORE;
45
46#ifndef MAX_PER_UID
47#define MAX_PER_UID	5
48#endif
49
50int	Max_per_uid = MAX_PER_UID;
51
52static SCORE	Top[MAXSCORES];
53
54/*
55 * score:
56 *	Post the player's score, if reasonable, and then print out the
57 *	top list.
58 */
59void
60score(score_wfd)
61     int score_wfd;
62{
63	int	inf = score_wfd;
64	SCORE	*scp;
65	uid_t	uid;
66	bool	done_show = FALSE;
67	static int	numscores, max_uid;
68
69	Newscore = FALSE;
70	if (inf < 0)
71		return;
72
73	if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid)
74		read(inf, Top, sizeof Top);
75	else {
76		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
77			scp->s_score = -1;
78		max_uid = Max_per_uid;
79	}
80
81	uid = getuid();
82	if (Top[MAXSCORES-1].s_score <= Score) {
83		numscores = 0;
84		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
85			if (scp->s_score < 0 ||
86			    (scp->s_uid == uid && ++numscores == max_uid)) {
87				if (scp->s_score > Score)
88					break;
89				scp->s_score = Score;
90				scp->s_uid = uid;
91				set_name(scp);
92				Newscore = TRUE;
93				break;
94			}
95		if (scp == &Top[MAXSCORES]) {
96			Top[MAXSCORES-1].s_score = Score;
97			Top[MAXSCORES-1].s_uid = uid;
98			set_name(&Top[MAXSCORES-1]);
99			Newscore = TRUE;
100		}
101		if (Newscore)
102			qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
103	}
104
105	if (!Newscore) {
106		Full_clear = FALSE;
107		fsync(inf);
108		lseek(inf, 0, SEEK_SET);
109		return;
110	}
111	else
112		Full_clear = TRUE;
113
114	for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
115		if (scp->s_score < 0)
116			break;
117		move((scp - Top) + 1, 15);
118		if (!done_show && scp->s_uid == uid && scp->s_score == Score)
119			standout();
120		printw(" %d\t%d\t%-*s ", (scp - Top) + 1, scp->s_score,
121			(int)(sizeof scp->s_name), scp->s_name);
122		if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
123			standend();
124			done_show = TRUE;
125		}
126	}
127	Num_scores = scp - Top;
128	refresh();
129
130	if (Newscore) {
131		lseek(inf, 0L, SEEK_SET);
132		write(inf, &max_uid, sizeof max_uid);
133		write(inf, Top, sizeof Top);
134	}
135	fsync(inf);
136	lseek(inf, 0, SEEK_SET);
137}
138
139void
140set_name(scp)
141	SCORE	*scp;
142{
143	PASSWD	*pp;
144
145	if ((pp = getpwuid(scp->s_uid)) == NULL)
146		pp->pw_name = "???";
147	strlcpy(scp->s_name, pp->pw_name, MAXLOGNAME);
148}
149
150/*
151 * cmp_sc:
152 *	Compare two scores.
153 */
154int
155cmp_sc(s1, s2)
156	const void	*s1, *s2;
157{
158	return ((SCORE *)s2)->s_score - ((SCORE *)s1)->s_score;
159}
160
161/*
162 * show_score:
163 *	Show the score list for the '-s' option.
164 */
165void
166show_score()
167{
168	SCORE	*scp;
169	int	inf;
170	static int	max_score;
171
172	if ((inf = open(Scorefile, O_RDONLY)) < 0) {
173		perror(Scorefile);
174		return;
175	}
176
177	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
178		scp->s_score = -1;
179
180	read(inf, &max_score, sizeof max_score);
181	read(inf, Top, sizeof Top);
182	close(inf);
183	inf = 1;
184	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
185		if (scp->s_score >= 0)
186			printf("%d\t%d\t%.*s\n", inf++, scp->s_score,
187				(int)(sizeof scp->s_name), scp->s_name);
188}
189