Home | History | Annotate | Line # | Download | only in robots
score.c revision 1.8
      1 /*	$NetBSD: score.c,v 1.8 1999/09/08 21:17:57 jsm Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     22  *
     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
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)score.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: score.c,v 1.8 1999/09/08 21:17:57 jsm Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 # include	"robots.h"
     46 # include	"pathnames.h"
     47 
     48 const char	*Scorefile = _PATH_SCORE;
     49 
     50 int	Max_per_uid = MAX_PER_UID;
     51 
     52 static SCORE	Top[MAXSCORES];
     53 
     54 static u_int32_t	numscores, max_uid;
     55 
     56 static void read_score __P((int));
     57 static void write_score __P((int));
     58 
     59 /*
     60  * read_score:
     61  *	Read the score file in MI format
     62  */
     63 static void
     64 read_score(inf)
     65 	int inf;
     66 {
     67 	SCORE	*scp;
     68 
     69 	if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid) {
     70 		max_uid = ntohl(max_uid);
     71 
     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);
     78 		}
     79 	}
     80 	else {
     81 		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
     82 			scp->s_score = 0;
     83 		max_uid = Max_per_uid;
     84 	}
     85 }
     86 
     87 /*
     88  * write_score:
     89  *	Write the score file in MI format
     90  */
     91 static void
     92 write_score(inf)
     93 	int inf;
     94 {
     95 	SCORE	*scp;
     96 
     97 	lseek(inf, 0L, 0);
     98 
     99 	max_uid = htonl(max_uid);
    100 	write(inf, &max_uid, sizeof max_uid);
    101 
    102 	for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
    103 		 scp->s_uid = htonl(scp->s_uid);
    104 		 scp->s_score = htonl(scp->s_score);
    105 		 scp->s_auto = htonl(scp->s_auto);
    106 		 scp->s_level = htonl(scp->s_level);
    107 	}
    108 
    109 	write(inf, Top, sizeof Top);
    110 }
    111 
    112 
    113 /*
    114  * score:
    115  *	Post the player's score, if reasonable, and then print out the
    116  *	top list.
    117  */
    118 void
    119 score()
    120 {
    121 	int			inf;
    122 	SCORE			*scp;
    123 	int			uid;
    124 	bool			done_show = FALSE;
    125 
    126 	Newscore = FALSE;
    127 	if ((inf = open(Scorefile, O_RDWR)) < 0) {
    128 		warn("opening `%s'", Scorefile);
    129 		return;
    130 	}
    131 
    132 	read_score(inf);
    133 
    134 	uid = getuid();
    135 	if (Top[MAXSCORES-1].s_score <= Score) {
    136 		numscores = 0;
    137 		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
    138 			if (scp->s_score < 0 ||
    139 			    (scp->s_uid == uid && ++numscores == max_uid)) {
    140 				if (scp->s_score > Score)
    141 					break;
    142 				scp->s_score = Score;
    143 				scp->s_uid = uid;
    144 				scp->s_auto = Auto_bot;
    145 				scp->s_level = Level;
    146 				set_name(scp);
    147 				Newscore = TRUE;
    148 				break;
    149 			}
    150 		if (scp == &Top[MAXSCORES]) {
    151 			Top[MAXSCORES-1].s_score = Score;
    152 			Top[MAXSCORES-1].s_uid = uid;
    153 			Top[MAXSCORES-1].s_auto = Auto_bot;
    154 			Top[MAXSCORES-1].s_level = Level;
    155 			set_name(&Top[MAXSCORES-1]);
    156 			Newscore = TRUE;
    157 		}
    158 		if (Newscore)
    159 			qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
    160 	}
    161 
    162 	if (!Newscore) {
    163 		Full_clear = FALSE;
    164 		close(inf);
    165 		return;
    166 	}
    167 	else
    168 		Full_clear = TRUE;
    169 
    170 	move(1, 15);
    171 	printw("%5.5s %5.5s %-9.9s %-8.8s %5.5s", "Rank", "Score", "User",
    172 	    " ", "Level");
    173 
    174 	for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
    175 		if (scp->s_score == 0)
    176 			break;
    177 		move((scp - Top) + 2, 15);
    178 		if (!done_show && scp->s_uid == uid && scp->s_score == Score)
    179 			standout();
    180 		printw("%5.5d %5.5d %-8.8s %-9.9s %5.5d",
    181 		    (scp - Top) + 1, scp->s_score, scp->s_name,
    182 		    Auto_bot ? "(autobot)" : "", scp->s_level);
    183 		if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
    184 			standend();
    185 			done_show = TRUE;
    186 		}
    187 	}
    188 	Num_scores = scp - Top;
    189 	refresh();
    190 
    191 	if (Newscore) {
    192 		write_score(inf);
    193 	}
    194 	close(inf);
    195 }
    196 
    197 void
    198 set_name(scp)
    199 	SCORE	*scp;
    200 {
    201 	PASSWD	*pp;
    202 	static char unknown[] = "???";
    203 
    204 	if ((pp = getpwuid(scp->s_uid)) == NULL)
    205 		pp->pw_name = unknown;
    206 	strncpy(scp->s_name, pp->pw_name, MAXNAME);
    207 }
    208 
    209 /*
    210  * cmp_sc:
    211  *	Compare two scores.
    212  */
    213 int
    214 cmp_sc(s1, s2)
    215 	const void *s1, *s2;
    216 {
    217 	return ((const SCORE *)s2)->s_score - ((const SCORE *)s1)->s_score;
    218 }
    219 
    220 /*
    221  * show_score:
    222  *	Show the score list for the '-s' option.
    223  */
    224 void
    225 show_score()
    226 {
    227 	SCORE		*scp;
    228 	int		inf;
    229 
    230 	if ((inf = open(Scorefile, O_RDONLY)) < 0) {
    231 		warn("opening `%s'", Scorefile);
    232 		return;
    233 	}
    234 
    235 	read_score(inf);
    236 	close(inf);
    237 	inf = 1;
    238 	printf("%5.5s %5.5s %-9.9s %-8.8s %5.5s\n", "Rank", "Score", "User",
    239 	    " ", "Level");
    240 	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
    241 		if (scp->s_score > 0)
    242 			printf("%5.5d %5.5d %-8.8s %-9.9s %5.5d\n",
    243 			    inf++, scp->s_score, scp->s_name,
    244 			    scp->s_auto ? "(autobot)" :  "", scp->s_level);
    245 }
    246