DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T e

⟦739071b0c⟧ TextFile

    Length: 6294 (0x1896)
    Types: TextFile
    Names: »end_game.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/General/Tetris/end_game.c« 

TextFile

/*
** written by adam margulies vespa@ssyx.ucsc.edu
**                           {...}!ucbvax!ucscc!ssyx!vespa
**
** permission is granted to freely distribute this code provided that you:
**
** 1) don't charge for it
** 2) leave my name and header on it
** 3) clearly document your changes and place your name on them
**
*/
/* Tetris: end_game()                                                      */
/*                                                                         */
/*  When the player quits or loses this function is called.                */
/*  It updates the high-score file and then exits after resetting the      */
/*  tty.                                                                   */
#include "tetris.h"
#include "sys/file.h"

void end_game()
{
	struct sgttyb *thistty;
    int i,counter, flag=0; /* i and counter are counters, flag is used
							  to show that the current user's score has been
							  written and should not be written again */
	int ncp;
    FILE *fp, *fopen();
    long clock;
    int tmp;
    char *ctime();
	char c;
	char monster[10], pts[20];
	char levelstr[15], aan[5];
	struct tm *tmstr;
	int year;
	tmstr = (struct tm *)malloc(sizeof(struct tm));
	thistty = (struct sgttyb *)malloc(sizeof(struct sgttyb));

    SIGHOLD(14);

    time(&clock);
	tmstr = localtime(&clock);
	year = tmstr->tm_year;

	unlink(rcd_file);

	if (!strcmp(username, "\0")) {
		if (!getenv("NAME"))
			sprintf(username, "doofus");
		else
			sprintf(username,"%s", getenv("NAME"));
	}

	if ((fp = fopen(HIGHSCOREFILE, "r+")) == NULL){
		SIGHOLD(14);
		cls();
		csr(23,0);
		resetty();
		fprintf(stderr, "%s\n", HIGHSCOREFILE);
		perror("opening highscore file");
		exit(3);
	}

	flock(fileno(fp), LOCK_EX);
    for(counter=0;counter<15;counter++){
        fscanf(fp, "%d\n", &high_score[counter]);
        fscanf(fp, "%s\n", high_user[counter]);
        fgets(high_name[counter], 255, fp);
		for (i=0; i<255; i++)
			if (high_name[counter][i] == '\n')
				high_name[counter][i] = '\0';	
    }

	rewind(fp);

    for(counter=0;counter<15;counter++){
		if (points > high_score[counter]) {
			fprintf(fp, "%d\n%s\n%s\n", points, user_info->pw_name,
									  username);

			for (i=counter; i<15; i++)
				if (strcmp(high_user[i], user_info->pw_name) != 0)
					fprintf(fp, "%d\n%s\n%s\n", high_score[i], high_user[i],
											  high_name[i]);
			counter=15;
		}
		else  fprintf(fp, "%d\n%s\n%s\n", high_score[counter],
								high_user[counter], high_name[counter]);
		if (strcmp(high_user[counter] ,user_info->pw_name) == 0) {
			for (i=counter+1; i<15; i++)
				fprintf(fp, "%d\n%s\n%s\n", high_score[i], high_user[i],
										  high_name[i]);
			counter=15;
		} 
    }
	flock(fileno(fp), LOCK_UN);
    fclose(fp);

	if ((fp = fopen(HIGHSCOREFILE, "r")) == NULL){
		SIGHOLD(14);
		cls();
		csr(23,0);
		resetty();
		fprintf(stderr, "%s\n", HIGHSCOREFILE);
		perror("opening highscore file");
		exit(3);
	}

    for(counter=0;counter<10;counter++){
        fscanf(fp, "%d\n", &high_score[counter]);
        fscanf(fp, "%s\n", high_user[counter]);
        fgets(high_name[counter], 255, fp);
		for (i=0; i<255; i++)
			if (high_name[counter][i] == '\n')
				high_name[counter][i] = '\0';	
        move(10+counter,0);
        printc("%2.2d. %7.7d (%s)    ", counter+1, high_score[counter],
                high_user[counter]);
    }
    fclose(fp);
    csr_draw(10,0,20,30);

    for (i=2; i<12; i++) {
        mvaddstr(i,35,"**********");
        mvaddstr(23-i,35,"**********");
        csr_draw(2,35,21,44);
    }
    mvaddstr(11,35,"GAME  OVER");
    csr_draw(11,35,11,44);

    if (getuid() != MASTERUID) {
		if ((fp = fopen(MONITORFILE, "a")) == NULL){
			SIGHOLD(14);
			cls();
			csr(23,0);
			resetty();
			fprintf(stderr, "%s\n", MONITORFILE);
			perror("opening monitor file");
			exit(3);
		}

		flock(fileno(fp), LOCK_EX);
        fprintf(fp, "%s %d %s", user_info->pw_name, points, ctime(&clock));
		flock(fileno(fp), LOCK_UN);
        fclose(fp);
    }

	if(disp_tomb) {
		csr(0,0);

		cls();
			sprintf(pts, "%d Pts", points);
			sprintf(levelstr, "on level %d", level);
			switch(current->shape) {
				case 0:
					sprintf(monster,"bar");
					sprintf(aan, "a");
					break;
				case 1:
					sprintf(monster,"block");
					sprintf(aan, "a");
					break;
				case 2:
					sprintf(monster,"tee");
					sprintf(aan, "a");
					break;
				case 3:
					sprintf(monster,"zee");
					sprintf(aan, "a");
					break;
				case 4:
					sprintf(monster,"ess");
					sprintf(aan, "an");
					break;
				case 5:
					sprintf(monster,"elle");
					sprintf(aan, "an");
					break;
				case 6:
					sprintf(monster,"belle");
					sprintf(aan, "a");
					break;
				case 7:
					sprintf(monster,"Q key");
					sprintf(aan, "a");
			}
				   mvaddstr(8,15,"__________");
				 mvaddstr( 9,14,"/          \\");
				mvaddstr(10,13,"/    REST    \\");
			   mvaddstr(11,12,"/      IN      \\");
			  mvaddstr(12,11,"/     PEACE      \\");
			 mvaddstr(13,10,"/                  \\");
			 mvaddstr(14,10,"|                  |");
			 mvaddstr(15,10,"|                  |");
			 mvaddstr(16,10,"|                  |");
			 mvaddstr(17,10,"|                  |");
			 mvaddstr(18,10,"|                  |");
			 mvaddstr(19,10,"|                  |");
			 mvaddstr(20,9,"*|     *  *  *      | *");
	 mvaddstr(21,1,"________)/\\\\_//(\\/(/\\)/\\//\\/|_)________");
		mvaddstr(14,19 - (strlen(user_info->pw_name)/2),user_info->pw_name);
		mvaddstr(15,19 - (strlen(pts)/2), pts);
		move(16,15 - strlen(aan) + 1);
		printc("Killed by %s", aan);
		mvaddstr(17,19 - (strlen(monster)/2), monster);
		mvaddstr(18,19 - (strlen(levelstr)/2), levelstr);
		move(19,18);
		printc("19%d", year);
		csr_draw(0,0,23,79);

			csr(22,0);
		printc("[Viciously strike a poor, helpless key]");
		csr_draw(22,0,22,79);
		getchar();
		clear(22,0,22,79);
		csr_draw(22,0,22,79);
	}


	if(!disp_high)
		display_high();
	csr(23,0);
	resetty();

	/* make writable again */
	if(nowrite & 2) {
		ttystat->st_mode |= 16;
		chmod(ttyname(0), ttystat->st_mode);
	}

	if(addict) {
		do
			ioctl(1, TIOCOUTQ, &ncp);
		while(ncp != 0);
		signal(SIGHUP, SIG_IGN);
		signal(SIGALRM, SIG_IGN);
		thistty->sg_ispeed = 0;
		thistty->sg_ospeed = 0;
		ioctl(0, TIOCSETP, thistty);
		exit(0);
	}
	else
		exit(0);
}