score.c revision 1.13
1/*	$OpenBSD: score.c,v 1.13 2015/12/04 16:40:09 tb 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#include	"robots.h"
34
35char	Scorefile[PATH_MAX];
36
37#ifndef MAX_PER_UID
38#define MAX_PER_UID	5
39#endif
40
41int	Max_per_uid = MAX_PER_UID;
42
43static SCORE	Top[MAXSCORES];
44
45/*
46 * score:
47 *	Post the player's score, if reasonable, and then print out the
48 *	top list.
49 */
50void
51score(int score_wfd)
52{
53	int	inf = score_wfd;
54	SCORE	*scp;
55	uid_t	uid;
56	bool	done_show = FALSE;
57	static int	numscores, max_uid;
58
59	Newscore = FALSE;
60	if (inf < 0)
61		return;
62
63	if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid)
64		read(inf, Top, sizeof Top);
65	else {
66		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
67			scp->s_score = -1;
68		max_uid = Max_per_uid;
69	}
70
71	uid = getuid();
72	if (Top[MAXSCORES-1].s_score <= Score) {
73		numscores = 0;
74		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
75			if (scp->s_score < 0 ||
76			    (scp->s_uid == uid && ++numscores == max_uid)) {
77				if (scp->s_score > Score)
78					break;
79				scp->s_score = Score;
80				scp->s_uid = uid;
81				set_name(scp);
82				Newscore = TRUE;
83				break;
84			}
85		if (scp == &Top[MAXSCORES]) {
86			Top[MAXSCORES-1].s_score = Score;
87			Top[MAXSCORES-1].s_uid = uid;
88			set_name(&Top[MAXSCORES-1]);
89			Newscore = TRUE;
90		}
91		if (Newscore)
92			qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
93	}
94
95	if (!Newscore) {
96		Full_clear = FALSE;
97		fsync(inf);
98		lseek(inf, 0, SEEK_SET);
99		return;
100	}
101	else
102		Full_clear = TRUE;
103
104	for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
105		if (scp->s_score < 0)
106			break;
107		move((scp - Top) + 1, 15);
108		if (!done_show && scp->s_uid == uid && scp->s_score == Score)
109			standout();
110		printw(" %d\t%d\t%-*s ", (scp - Top) + 1, scp->s_score,
111			(int)(sizeof scp->s_name), scp->s_name);
112		if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
113			standend();
114			done_show = TRUE;
115		}
116	}
117	Num_scores = scp - Top;
118	refresh();
119
120	if (Newscore) {
121		lseek(inf, 0L, SEEK_SET);
122		write(inf, &max_uid, sizeof max_uid);
123		write(inf, Top, sizeof Top);
124	}
125	fsync(inf);
126	lseek(inf, 0, SEEK_SET);
127}
128
129void
130set_name(SCORE *scp)
131{
132	const char	*name;
133
134	name = getenv("LOGNAME");
135	if (name == NULL || *name == '\0')
136		name = getenv("USER");
137	if (name == NULL || *name == '\0')
138		name = getlogin();
139	if (name == NULL || *name == '\0')
140		name = "  ???";
141
142	strlcpy(scp->s_name, name, LOGIN_NAME_MAX);
143}
144
145/*
146 * cmp_sc:
147 *	Compare two scores.
148 */
149int
150cmp_sc(const void *s1, const void *s2)
151{
152	return ((SCORE *)s2)->s_score - ((SCORE *)s1)->s_score;
153}
154
155/*
156 * show_score:
157 *	Show the score list for the '-s' option.
158 */
159void
160show_score(void)
161{
162	SCORE	*scp;
163	int	inf;
164	static int	max_score;
165
166	if ((inf = open(Scorefile, O_RDONLY)) < 0) {
167		perror(Scorefile);
168		return;
169	}
170
171	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
172		scp->s_score = -1;
173
174	read(inf, &max_score, sizeof max_score);
175	read(inf, Top, sizeof Top);
176	close(inf);
177	inf = 1;
178	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
179		if (scp->s_score >= 0)
180			printf("%d\t%d\t%.*s\n", inf++, scp->s_score,
181				(int)(sizeof scp->s_name), scp->s_name);
182}
183