/*
 *  Kevin P. Smith      12/05/88
 *
 *  Takes the output of scores A and generates the player file.
 *  (Also creates the .GLOBAL file)
 *  This program reads stdin for its data.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <pwd.h>
#include "defs.h"
#include INC_FCNTL
#include "data.h"
#include "struct.h"

struct statentry play_entry;
struct rawdesc *output=NULL;
char mode;

trimblanks2(str)
char *str;
{
    *str=0;
    str--;
    while (*str==' ') {
	*str=0;
	str--;
    }
    if (*str=='_') *str=0;
}

trimblanks(str)
char *str;
{
    *str=0;
    str--;
    while (*str==' ') {
	*str=0;
	str--;
    }
}

main(argc, argv)
int argc;
char **argv;
{
    int fd;
    struct player j;
    int i;
    char buf[512];
    int harsh=10;	/* How strict we will be with player trimming */

    if (argc==2) {
	harsh=atoi(argv[1]);
	if (harsh<=0) usage();
    }
    if (argc>2) usage();
    getpath();
    fprintf(stderr,"Warning:  If you do not know how to use this program, break it now!\n");
    status=(struct status *) malloc(sizeof(struct status));
    scanf("%10ld %10d %10d %10d %10d %10ld\n", 
	&status->time, 
	&status->planets, 
	&status->armsbomb, 
	&status->kills, 
	&status->losses,
	&status->timeprod);

#ifdef DOGLOBAL
    fd = open(Global, O_WRONLY|O_CREAT|O_TRUNC, 0600);
    if (fd < 0) {
	printf("Cannot open the global file!\n");
	exit(0);
    }
    write(fd, (char *) status, sizeof(struct status));
    close(fd);
#endif

    fd = open(PlayerFile, O_WRONLY|O_CREAT|O_TRUNC, 0644);
    if (fd < 0) {
	printf("Cannot open player file\n");
    }

    i=0;
    while (gets(buf)) {
	trimblanks2(buf+16);
	trimblanks(buf+33);
	trimblanks(buf+129);
	strcpy(play_entry.name, buf);
	strcpy(play_entry.password, buf+17);
	strcpy(play_entry.stats.st_keymap, buf+34);
#ifdef GENO_COUNT
	sscanf(buf+130, " %d %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %lf %ld %d %d\n",
#else
	sscanf(buf+130, " %d %lf %d %d %d %d %d %d %d %d %d %d %d %d %d %lf %ld %d\n",
#endif
	    &play_entry.stats.st_rank,
	    &play_entry.stats.st_maxkills,
	    &play_entry.stats.st_kills,
	    &play_entry.stats.st_losses,
	    &play_entry.stats.st_armsbomb,
	    &play_entry.stats.st_planets,
	    &play_entry.stats.st_ticks,
	    &play_entry.stats.st_tkills,
	    &play_entry.stats.st_tlosses,
	    &play_entry.stats.st_tarmsbomb,
	    &play_entry.stats.st_tplanets,
	    &play_entry.stats.st_tticks,
	    &play_entry.stats.st_sbkills,
	    &play_entry.stats.st_sblosses,
	    &play_entry.stats.st_sbticks,
	    &play_entry.stats.st_sbmaxkills,
	    &play_entry.stats.st_lastlogin,
#ifdef GENO_COUNT
	    &play_entry.stats.st_flags,
	    &play_entry.stats.st_genos);
#else
	    &play_entry.stats.st_flags);
#endif
	/* Player 0 is always saved. */
	/* This formula reads:
	 * If (deadtime - (10 + rank^2 + playtime/2.4)*n days > 0, nuke him.
	 */
	if (i!=0 && harsh<100 && ((time(NULL) - play_entry.stats.st_lastlogin - 864000*harsh) - 
	    play_entry.stats.st_rank * play_entry.stats.st_rank * harsh * 86400 -
	    (play_entry.stats.st_tticks + play_entry.stats.st_ticks + 
	     play_entry.stats.st_sbticks) * harsh > 0)) {
	    j.p_stats = play_entry.stats;
	    printf("%-16.16s %7.2f   %4d %5.2f  %4d %5.2f  %4d %5.2f  %4d %5.2f\n",
		play_entry.name,
		play_entry.stats.st_tticks / 36000.0,
		play_entry.stats.st_tplanets,
		planetRating(&j),
		play_entry.stats.st_tarmsbomb,
		bombingRating(&j),
		play_entry.stats.st_tkills,
		offenseRating(&j),
		play_entry.stats.st_tlosses,
		defenseRating(&j));
	    continue;
	}
	if (fd>=0) {
	    write(fd, (char *) &play_entry, sizeof(struct statentry));
	}
	i++;
    }
    if (fd>=0) {
	close(fd);
    }
    return 1;		/* satisfy lint */
}

usage()
{
    printf("Usage: trimscores n < scoredb\n");
    printf("This program takes input of the form generated by the 'scores A'\n");
    printf(" command.  It then recreates the database, throwing away characters\n");
    printf(" which haven't been used recently.  The n tells it how nice to be\n");
    printf(" when determining who to throw away.  The default n is 10.\n");
    printf(" Any character destroyed must have not played for the last n*10 days\n");
    printf(" and then some consideration is given to how valued he is likely to be.\n");
    printf("It also outputs a quick description of the characters who were\n");
    printf(" nuked.\n");
    exit(0);
}
