xref: /csrg-svn/games/robots/score.c (revision 41323)
121473Smckusick /*
221473Smckusick  * Copyright (c) 1980 Regents of the University of California.
333690Sbostic  * All rights reserved.
433690Sbostic  *
533690Sbostic  * Redistribution and use in source and binary forms are permitted
634797Sbostic  * provided that the above copyright notice and this paragraph are
734797Sbostic  * duplicated in all such forms and that any documentation,
834797Sbostic  * advertising materials, and other materials related to such
934797Sbostic  * distribution and use acknowledge that the software was developed
1034797Sbostic  * by the University of California, Berkeley.  The name of the
1134797Sbostic  * University may not be used to endorse or promote products derived
1234797Sbostic  * from this software without specific prior written permission.
1334797Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434797Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534797Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621473Smckusick  */
1721473Smckusick 
1821473Smckusick #ifndef lint
19*41323Sbostic static char sccsid[] = "@(#)score.c	5.5 (Berkeley) 05/02/90";
2033690Sbostic #endif /* not lint */
2121473Smckusick 
2221473Smckusick # include	"robots.h"
2337017Sbostic # include	<sys/types.h>
2421473Smckusick # include	<pwd.h>
25*41323Sbostic # include	"pathnames.h"
2621473Smckusick 
2721473Smckusick typedef struct {
2821473Smckusick 	int	s_uid;
2921473Smckusick 	int	s_score;
3021473Smckusick 	char	s_name[MAXNAME];
3121473Smckusick } SCORE;
3221473Smckusick 
3321473Smckusick typedef struct passwd	PASSWD;
3421473Smckusick 
35*41323Sbostic char	*Scorefile = _PATH_SCORE;
3621473Smckusick 
3721473Smckusick int	Max_per_uid = MAX_PER_UID;
3821473Smckusick 
3921473Smckusick static SCORE	Top[MAXSCORES];
4021473Smckusick 
4121473Smckusick /*
4221473Smckusick  * score:
4321473Smckusick  *	Post the player's score, if reasonable, and then print out the
4421473Smckusick  *	top list.
4521473Smckusick  */
4621473Smckusick score()
4721473Smckusick {
4821473Smckusick 	register int	inf;
4921473Smckusick 	register SCORE	*scp;
5021473Smckusick 	register int	uid;
5121473Smckusick 	register bool	done_show = FALSE;
5221473Smckusick 	static int	numscores, max_uid;
5321473Smckusick 
5421473Smckusick 	Newscore = FALSE;
5521473Smckusick 	if ((inf = open(Scorefile, 2)) < 0) {
5621473Smckusick 		perror(Scorefile);
5721473Smckusick 		return;
5821473Smckusick 	}
5921473Smckusick 
6021473Smckusick 	if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid)
6121473Smckusick 		read(inf, Top, sizeof Top);
6221473Smckusick 	else {
6321473Smckusick 		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
6421473Smckusick 			scp->s_score = -1;
6521473Smckusick 		max_uid = Max_per_uid;
6621473Smckusick 	}
6721473Smckusick 
6821473Smckusick 	uid = getuid();
6921473Smckusick 	if (Top[MAXSCORES-1].s_score <= Score) {
7021473Smckusick 		numscores = 0;
7121473Smckusick 		for (scp = Top; scp < &Top[MAXSCORES]; scp++)
7221473Smckusick 			if (scp->s_score < 0 ||
7321473Smckusick 			    (scp->s_uid == uid && ++numscores == max_uid)) {
7421473Smckusick 				if (scp->s_score > Score)
7521473Smckusick 					break;
7621473Smckusick 				scp->s_score = Score;
7721473Smckusick 				scp->s_uid = uid;
7821473Smckusick 				set_name(scp);
7921473Smckusick 				Newscore = TRUE;
8021473Smckusick 				break;
8121473Smckusick 			}
8221473Smckusick 		if (scp == &Top[MAXSCORES]) {
8321473Smckusick 			Top[MAXSCORES-1].s_score = Score;
8421473Smckusick 			Top[MAXSCORES-1].s_uid = uid;
8521473Smckusick 			set_name(&Top[MAXSCORES-1]);
8621473Smckusick 			Newscore = TRUE;
8721473Smckusick 		}
8821473Smckusick 		if (Newscore)
8921473Smckusick 			qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
9021473Smckusick 	}
9121473Smckusick 
9221473Smckusick 	if (!Newscore) {
9321473Smckusick 		Full_clear = FALSE;
9421473Smckusick 		close(inf);
9521473Smckusick 		return;
9621473Smckusick 	}
9721473Smckusick 	else
9821473Smckusick 		Full_clear = TRUE;
9921473Smckusick 
10021473Smckusick 	for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
10121473Smckusick 		if (scp->s_score < 0)
10221473Smckusick 			break;
10321473Smckusick 		move((scp - Top) + 1, 15);
10421473Smckusick 		if (!done_show && scp->s_uid == uid && scp->s_score == Score)
10521473Smckusick 			standout();
10621473Smckusick 		printw(" %d\t%d\t%-8.8s ", (scp - Top) + 1, scp->s_score, scp->s_name);
10721473Smckusick 		if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
10821473Smckusick 			standend();
10921473Smckusick 			done_show = TRUE;
11021473Smckusick 		}
11121473Smckusick 	}
11221473Smckusick 	Num_scores = scp - Top;
11321473Smckusick 	refresh();
11421473Smckusick 
11521473Smckusick 	if (Newscore) {
11621473Smckusick 		lseek(inf, 0L, 0);
11721473Smckusick 		write(inf, &max_uid, sizeof max_uid);
11821473Smckusick 		write(inf, Top, sizeof Top);
11921473Smckusick 	}
12021473Smckusick 	close(inf);
12121473Smckusick }
12221473Smckusick 
12321473Smckusick set_name(scp)
12421473Smckusick register SCORE	*scp;
12521473Smckusick {
12621473Smckusick 	register PASSWD	*pp;
12721473Smckusick 
12821473Smckusick 	if ((pp = getpwuid(scp->s_uid)) == NULL)
12921473Smckusick 		pp->pw_name = "???";
13021473Smckusick 	strncpy(scp->s_name, pp->pw_name, MAXNAME);
13121473Smckusick }
13221473Smckusick 
13321473Smckusick /*
13421473Smckusick  * cmp_sc:
13521473Smckusick  *	Compare two scores.
13621473Smckusick  */
13721473Smckusick cmp_sc(s1, s2)
13821473Smckusick register SCORE	*s1, *s2;
13921473Smckusick {
14021473Smckusick 	return s2->s_score - s1->s_score;
14121473Smckusick }
14221473Smckusick 
14321473Smckusick /*
14421473Smckusick  * show_score:
14521473Smckusick  *	Show the score list for the '-s' option.
14621473Smckusick  */
14721473Smckusick show_score()
14821473Smckusick {
14921473Smckusick 	register SCORE	*scp;
15021473Smckusick 	register int	inf;
15121473Smckusick 	static int	max_score;
15221473Smckusick 
15321473Smckusick 	if ((inf = open(Scorefile, 0)) < 0) {
15421473Smckusick 		perror(Scorefile);
15521473Smckusick 		return;
15621473Smckusick 	}
15721473Smckusick 
15821473Smckusick 	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
15921473Smckusick 		scp->s_score = -1;
16021473Smckusick 
16121473Smckusick 	read(inf, &max_score, sizeof max_score);
16221473Smckusick 	read(inf, Top, sizeof Top);
16321473Smckusick 	close(inf);
16421473Smckusick 	inf = 1;
16521473Smckusick 	for (scp = Top; scp < &Top[MAXSCORES]; scp++)
16621473Smckusick 		if (scp->s_score >= 0)
16721473Smckusick 			printf("%d\t%d\t%.*s\n", inf++, scp->s_score, sizeof scp->s_name, scp->s_name);
16821473Smckusick }
169