/* * Show current scores. This must be called after savescore, if * savescore is called at all, for two reasons: * - Showscores munches the time field. * - Even if that were not the case, a new score must be recorded * before it can be shown anyway. */ void showscores(int level) { struct highscore *sp; int i, n, c; const char *me; int levelfound[NLEVELS]; if (!gotscores) getscores((FILE **)NULL); (void)printf("\n\t\t Tetris High Scores\n"); /* * If level == 0, the person has not played a game but just asked for * the high scores; we do not need to check for printing in highlight * mode. If SOstr is null, we can't do highlighting anyway. */ me = level && SOstr ? thisuser() : NULL; /* * Set times to 0 except for high score on each level. */ for (i = MINLEVEL; i < NLEVELS; i++) levelfound[i] = 0; for (i = 0, sp = scores; i < nscores; i++, sp++) { if (levelfound[sp->hs_level]) sp->hs_time = 0; else { sp->hs_time = 1; levelfound[sp->hs_level] = 1; } } /* * Page each screenful of scores. */ for (i = 0, sp = scores; i < nscores; sp += n) { n = 20; if (i + n > nscores) n = nscores - i; printem(level, i + 1, sp, n, me); if ((i += n) < nscores) { (void)printf("\nHit RETURN to continue."); (void)fflush(stdout); while ((c = getchar()) != '\n') if (c == EOF) break; (void)printf("\n"); } } if (nscores == 0) printf("\t\t\t - none to date.\n"); }
void process(char *userid,char *platform,char *instrument) { int where=-1,i,r,j,k; char *p=buffer,*q,*u,*v; static char s[MAXSTR]; static char username[MAXSTR]; FILE *f; memset(entry,0,sizeof(entry)); /* warning, string parsing code ahead */ /* find percentage by finding top score for each song */ memset(topscore,0,sizeof(topscore)); memset(topwhere,0,sizeof(topwhere)); for(i=2;i<5;i++) { grabtopscores(i,instrument); p=buffer; while((p=strstr2(p,"tr height"))) { q=strstr2(p,"class=\""); if(!q) error("expected class"); if(!strncmp(q,"headrow",7)) continue; if(!strncmp(q,"tier1",5)) { q=strstr2(q,">"); if(!q) error("expected >"); grabuntils(s,MAXSTR-1,q,"<("); while(s[0] && s[strlen(s)-1]==' ') s[strlen(s)-1]=0; if(!strcmp(s,"Warmup")) where=0; else if(!strcmp(s,"Apprentice")) where=0; else if(!strcmp(s,"Solid")) where=0; else if(!strcmp(s,"Moderate")) where=0; else if(!strcmp(s,"Challenging")) where=0; else if(!strcmp(s,"Nightmare")) where=0; else if(!strcmp(s,"Impossible")) where=0; else if(!strcmp(s,"Rock Band Blitz Soundtrack")) where=1; else if(!strcmp(s,"Downloaded Songs - Rock Band 3")) where=2; else printf("section [%s] not supported, put as dlc\n",s),where=2; } else if(!strncmp(q,"whitecol",8)) { u=strstr2(q,"<tr height"); v=strstr2(q,"<span class=\"error\">NO SCORES SUBMITTED</span>"); if((u || v) && (!u || (v && v<u))) continue; /* get song id */ q=strstr2(q,"song="); if(!q) error("expected entry"); j=grabnumuntil(q,'&'); if(j<0 || j>=MAXID) error("id too high, inclease MAXID and recompile"); q=strstr2(q,"col\">"); if(!q) error("expected entry"); /* get score */ if(q[0]=='<') q=strstr2(q,"_blank\">"); k=grabnumuntil(q,'<'); if(topscore[j]<k) topscore[j]=k,topwhere[j]=where; } } } getscores(userid,platform,instrument); /* find username */ p=strstr2(buffer,"name=\"username\" value=\""); grabuntil(username,MAXSTR-1,p,'\"'); /* find scores and stats */ p=buffer; i=0; while((p=strstr2(p,"tr height"))) { q=strstr2(p,"class=\""); if(!q) error("expected class"); if(!strncmp(q,"headrow",7)) continue; if(!strncmp(q,"tier1",5)) { q=strstr2(q,">"); if(!q) error("expected >"); grabuntils(s,MAXSTR-1,q,"<("); while(s[0] && s[strlen(s)-1]==' ') s[strlen(s)-1]=0; if(!strcmp(s,"Warmup")) where=0; else if(!strcmp(s,"Apprentice")) where=0; else if(!strcmp(s,"Solid")) where=0; else if(!strcmp(s,"Moderate")) where=0; else if(!strcmp(s,"Challenging")) where=0; else if(!strcmp(s,"Nightmare")) where=0; else if(!strcmp(s,"Impossible")) where=0; else if(!strcmp(s,"Rock Band Blitz Soundtrack")) where=1; else if(!strcmp(s,"Downloaded Songs - Rock Band 3")) where=2; else printf("section [%s] not supported, put as dlc\n",s),where=2; } else if(!strncmp(q,"whitecol",8)) { u=strstr2(q,"<tr height"); v=strstr2(q,"<span class=\"error\">NO SCORES SUBMITTED</span>"); if((u || v) && (!u || (v && v<u))) continue; entry[where][2]++; q=strstr2(q,"col\">"); if(!q) error("expected entry"); q=strstr2(q,"col\">"); if(!q) error("expected entry"); q=strstr2(q,"col\">"); if(!q) error("expected entry"); q=strstr2(q,"col\">"); if(!q) error("expected entry"); q=strstr2(q,"col\">"); if(!q) error("expected entry"); /* get score */ if(q[0]=='<') q=strstr2(q,"_blank\">"); entry[where][1]+=grabnumuntil(q,'<'); /* get star rating */ q=strstr2(q,"col\">"); if(!q) error("expected entry"); if(strncmp(q+10,"/images/rating",14)) r=0; else r=q[25]-'0'; if(r>=3) entry[where][3]++; if(r>=4) entry[where][4]++; if(r>=5) entry[where][5]++; if(r>=6) entry[where][6]++; /* get percentage */ q=strstr2(q,"col\">"); if(!q) error("expected entry"); u=strstr2(q,"percent"); if(u && *u=='1') entry[where][7]++; } } /* find rank */ for(i=0;i<3;i++) entry[i][0]=1,highest[i]=0; for(i=2;i<5;i++) for(j=0;j<3;j++) { /* for each platform, for each subrank */ grablist(instrument,i,j); p=buffer; while((p=strstr2(p,"tr height"))) { q=strstr2(p,"class=\""); if(!q) error("expected class"); if(!strncmp(q,"headrow",7)) continue; q=strstr2(q,"center\">"); if(!q) break; /* none submitted */ q=strstr2(q,"center\">"); if(!q) break; q=strstr2(q,"center\">"); if(!q) break; r=grabnumuntil(q,'<'); if(r>entry[j][1]) entry[j][0]++; } } /* find highest */ for(j=0;j<3;j++) highest[j]=0; for(j=0;j<MAXID;j++) highest[topwhere[j]]+=topscore[j]; /* output! */ if(!(f=fopen("stats.txt","w"))) error("couldn't open temp file for writing"); fprintf(f,"%s %s\n",username,instrument); for(i=0;i<3;i++) { for(j=0;j<8;j++) fprintf(f,"%d ",entry[i][j]); fprintf(f,"%d\n",highest[i]); } if(fclose(f)) error("something bad happened when writing to temp file"); }
void savescore(int level) { struct highscore *sp; int i; int change; FILE *sf; const char *me; getscores(&sf); gotscores = 1; (void)time(&now); /* * Allow at most one score per person per level -- see if we * can replace an existing score, or (easiest) do nothing. * Otherwise add new score at end (there is always room). */ change = 0; me = thisuser(); for (i = 0, sp = &scores[0]; i < nscores; i++, sp++) { if (sp->hs_level != level || strcmp(sp->hs_name, me) != 0) continue; if (score > sp->hs_score) { (void)printf("%s bettered %s %d score of %d!\n", "\nYou", "your old level", level, sp->hs_score * sp->hs_level); sp->hs_score = score; /* new score */ sp->hs_time = now; /* and time */ change = 1; } else if (score == sp->hs_score) { (void)printf("%s tied %s %d high score.\n", "\nYou", "your old level", level); sp->hs_time = now; /* renew it */ change = 1; /* gotta rewrite, sigh */ } /* else new score < old score: do nothing */ break; } if (i >= nscores) { strlcpy(sp->hs_name, me, sizeof sp->hs_name); sp->hs_level = level; sp->hs_score = score; sp->hs_time = now; nscores++; change = 1; } if (change) { /* * Sort & clean the scores, then rewrite. */ nscores = checkscores(scores, nscores); rewind(sf); if (fwrite(scores, sizeof(*sp), nscores, sf) != nscores || fflush(sf) == EOF) warnx("error writing %s: %s\n\t-- %s", _PATH_SCOREFILE, strerror(errno), "high scores may be damaged"); } (void)fclose(sf); /* releases lock */ }
main() { int i; #ifdef BILL BillMode = 1; #endif terminit(); for(i=0;i<MAXGAMES;i++) { Highscore[i] = 0; MaxWave[i] = 0; } Lastscore = -1; #ifdef HOSTCODE getscores(); #endif VolumeControl = 8; Abort = FALSE; Silent = FALSE; Running = TRUE; Paused = FALSE; Newgame = 0; /* * For each game played... */ while ( Running ) { /* * For each new wave... */ newgame(); while ( Pacmen > 0 && Running ) { newwave(); /* * For each game scan... */ while ( Dotsrem > 0 ) { /* * If the Pacman died, start a new one. */ if ( !Pacman.alive ) { addmen(-1); if ( Pacmen <= 0 ) break; elinit(); if ( Wallsoff ) { compwalls(); Wallsoff = FALSE; } pacinit(); moninit(); fruitinit(FALSE); } do { kbpoll(); } while ( Paused && Newgame==0); if ( Abort || Newgame ) break; elpoll(); } soundoff(); if ( Abort || Newgame ) break; paccomp(); moncomp(); fruitcomp(); soundoff(); } if (Game != 0) { #ifdef HOSTCODE sndhost ("%s%s%d\n%s%D %D\n", SYNCSTRING, GAMESTRING, Game, SCORESTRING, (long)Score, (long)KEY); #endif Lastscore = Score; if ( Score > Highscore[Game-1] ) { Highscore[Game-1] = Score; MaxWave[Game-1] = Wave; } } if ( Abort ) break; } #ifdef HOSTCODE sndhost ("%s", EXITSTRING); #endif termwrapup(); }