From 375d551487c2c4d80402b2ed145bdf48d6e6c334 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 24 Apr 2021 13:11:43 +0430 Subject: [PATCH] improved handling of the scorefiles --- common.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 12 +++----- darrt.c | 76 +++++++++------------------------------------------ fisher.c | 78 ++++++++++------------------------------------------ jewels.c | 76 ++++++++++----------------------------------------- miketron.c | 76 ++++++++------------------------------------------- muncher.c | 75 ++++++++------------------------------------------ pipes.c | 80 +++++++++--------------------------------------------- 8 files changed, 159 insertions(+), 390 deletions(-) create mode 100644 common.h diff --git a/common.h b/common.h new file mode 100644 index 0000000..1ef37c1 --- /dev/null +++ b/common.h @@ -0,0 +1,76 @@ +/* +Authored by abakh +To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. + +You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . + + +*/ +#include +#include +#include +#include +#include +#include "config.h" +#define FOPEN_FAIL -10 +typedef signed char byte; +FILE* score_file; +byte score_write(const char* path, long wscore, byte save_to_num){// only saves the top 10, returns the place in the chart + score_file=fopen(path,"r"); + if(!score_file){ + score_file=fopen(path,"w"); + if(!score_file){ + return FOPEN_FAIL; + } + } + + char name_buff[save_to_num][60]; + long score_buff[save_to_num]; + + memset(name_buff,0,save_to_num*60*sizeof(char) ); + memset(score_buff,0,save_to_num*sizeof(long) ); + + long scanned_score =0; + char scanned_name[60]={0}; + byte location=0; + + while( fscanf(score_file,"%59s : %ld\n",scanned_name,&scanned_score) == 2 && location=scores_count || wscore>=score_buff[i]) ){ + fprintf(score_file,"%s : %ld\n",getenv("USER"),wscore); + ret=i; + wrote_it=1; + } + if(i #include #include "config.h" +#include "common.h" #define SAVE_TO_NUM 11 #define LEN 24 #define HLEN LEN/2 @@ -46,7 +47,6 @@ int usleep(long usec) { chtype colors[3]={0}; long score=0; -char error [150]={0}; FILE* scorefile; chtype background[LEN][WID]; @@ -288,68 +288,18 @@ void end_scene(){ refresh(); } -byte scorewrite(void){// only saves the top 10, returns the place in the chart - bool deforno; - if( !getenv("DRT_SCORES") && (scorefile= fopen(DRT_SCORES,"r")) ){ - deforno=1; - } - else{ - deforno=0; - if( !(scorefile = fopen(getenv("DRT_SCORES"),"r")) ){ - sprintf(error,"No accessible score files found. You can make an empty text file in %s or set DRT_SCORES to such a file to solve this.",DRT_SCORES); - return -1; - } - } +byte save_score(void){ + return fallback_to_home("darrt_scores",score,SAVE_TO_NUM); - char namebuff[SAVE_TO_NUM][60]; - long scorebuff[SAVE_TO_NUM]; - - memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) ); - memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) ); - - long fuckingscore =0; - char fuckingname[60]={0}; - byte location=0; - - while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location=scorebuff[location] ){ - fprintf(scorefile,"%s : %ld\n",getenv("USER"),score); - ret=location; - wroteit=1; - } - if(location0){ + rewind(score_file); + fscanf(score_file,"%*s : %*d"); + if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){ byte a = (LEN-9)/2; star_line(1); star_line(LEN-2); @@ -387,10 +337,10 @@ void showscores(byte playerrank){ char pname[60] = {0}; long pscore=0; byte rank=0; - rewind(scorefile); + rewind(score_file); mvaddstr(1,WID/2-4,"HIGH SCORES"); attron(colors[3]); - while( rank #include #include "config.h" +#include "common.h" #define SAVE_TO_NUM 11 #define HOOKS 10 #define LEN 24 @@ -49,9 +50,7 @@ unsigned int count[10]={0}; unsigned long score=0; const char sym[]="~:=!@+><;&"; byte hook=0, hooknum=0; -char error [150]={0}; byte clb,clbtime=0; -FILE* scorefile; int input; byte digit_count(int num){ @@ -185,68 +184,19 @@ void draw(void){ attroff(colors[1]); } -byte scorewrite(void){// only saves the top 10, returns the place in the chart - bool deforno; - if( !getenv("FSH_SCORES") && (scorefile= fopen(FSH_SCORES,"r")) ){ - deforno=1; - } - else{ - deforno=0; - if( !(scorefile = fopen(getenv("FSH_SCORES"),"r")) ){ - sprintf(error,"No accessible score files found. You can make an empty text file in %s or set FSH_SCORES to such a file to solve this.",FSH_SCORES); - return -1; - } - } +byte save_score(void){ + return fallback_to_home("fisher_scores",score,SAVE_TO_NUM); - char namebuff[SAVE_TO_NUM][60]; - long scorebuff[SAVE_TO_NUM]; - - memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) ); - memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) ); - - long fuckingscore =0; - char fuckingname[60]={0}; - byte location=0; - - while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location=itreached || score>=scorebuff[location]) ){ - fprintf(scorefile,"%s : %ld\n",getenv("USER"),score); - ret=location; - wroteit=1; - } - if(location0){ + rewind(score_file); + fscanf(score_file,"%*s : %*d"); + if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){ byte a = (LEN-9)/2; star_line(1); star_line(LEN-2); @@ -284,10 +234,10 @@ void showscores(byte playerrank){ char pname[60] = {0}; long pscore=0; byte rank=0; - rewind(scorefile); + rewind(score_file); mvaddstr(1,WID/2-4,"HIGH SCORES"); attron(colors[3]); - while( rank #include #include "config.h" +#include "common.h" #define LEN 17 #define WID 19 #define DELAY 2 @@ -32,70 +33,22 @@ byte jx,jy; //first jewel's position byte kx,ky;//second jewel's position in relation to that of j long score=0; char* controls = "j,l-Move k-Rotate p-Pause q-Quit"; -FILE* scorefile; -byte scorewrite(long score){// only saves the top 10 - bool deforno; - if( !getenv("JW_SCORES") && (scorefile= fopen(JW_SCORES,"r")) ){ - deforno=1; - } - else{ - deforno=0; - if( !(scorefile = fopen(getenv("JW_SCORES"),"r")) ){ - fprintf(stderr,"\nNo accessible score files found. You can make an empty text file in %s or set JW_SCORES to such a file to solve this. \n",JW_SCORES); - exit(EXIT_SUCCESS); - } - } - char namebuff[SAVE_TO_NUM][60]; - long scorebuff[SAVE_TO_NUM]; +byte save_score(void){ + return fallback_to_home("jewels_scores",score,SAVE_TO_NUM); - memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) ); - memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) ); - - long fuckingscore =0; - char fuckingname[60]={0}; - byte location=0; - - while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location=itreached || score>=scorebuff[location]) ){ - fprintf(scorefile,"%s : %ld\n",getenv("USER"),score); - ret=location; - wroteit=1; - } - if(location*>*>Top %d<*<*<\n",SAVE_TO_NUM); - while( rank>>"); printf("%d) %s : %ld\n",rank+1,pname,pscore); ++rank; } putchar('\n'); + fclose(score_file); } //apply gravity bool fall(void){ @@ -386,6 +340,6 @@ int main(void){ endwin(); printf("%s _Jewels_ %s\n",jwstr,jwstr); printf("You have scored %ld points.\n",score); - showscores(scorewrite(score)); + show_scores(save_score()); return EXIT_SUCCESS; } diff --git a/miketron.c b/miketron.c index bb8f4cb..dd07b1f 100644 --- a/miketron.c +++ b/miketron.c @@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include #include #include "config.h" +#include "common.h" #define SAVE_TO_NUM 10 #define MINLEN 10 #define MAXLEN 24 @@ -57,7 +58,6 @@ byte pse_msg=100; //no need to a epilepsy variable like in muncher as zeroing the colors suffices FILE *scorefile; -char error[150]={0}; void move_tron(void){ switch(direction){ @@ -87,68 +87,16 @@ void logo(void){ mvaddstr(1,0,"|\\/|"); mvaddstr(2,0,"| |IKETRON"); } -byte scorewrite(void){// only saves the top 10, returns the place in the chart - bool deforno; - if( !getenv("MT_SCORES") && (scorefile= fopen(MT_SCORES,"r")) ){ - deforno=1; - } - else{ - deforno=0; - if( !(scorefile = fopen(getenv("MT_SCORES"),"r")) ){ - sprintf(error,"No accessible score files found. You can make an empty text file in %s or set MT_SCORES to such a file to solve this.",MT_SCORES); - return -1; - } - } +byte save_score(void){ + return fallback_to_home("miketron_scores",score,SAVE_TO_NUM); - char namebuff[SAVE_TO_NUM][60]; - long scorebuff[SAVE_TO_NUM]; - - memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) ); - memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) ); - - long fuckingscore=0; - char fuckingname[60]={0}; - byte location=0; - - while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location=itreached || score>=scorebuff[location]) ){ - fprintf(scorefile,"%s : %ld\n",getenv("USER"),score); - ret=location; - wroteit=1; - } - if(location*>*>Top %d<*<*<\n",SAVE_TO_NUM); - while( rank>>"); printw("%d) %s : %ld\n",rank+1,pname,pscore); @@ -568,7 +516,7 @@ int main(int argc, char** argv){ refresh(); mvprintw(len+5,0,"Game over! Press a key to see the high scores:"); getch(); - showscores(scorewrite()); + show_scores(save_score()); printw("Game over! Wanna play again?(y/n)"); curs_set(1); input=getch(); diff --git a/muncher.c b/muncher.c index 79b4949..adda5b3 100644 --- a/muncher.c +++ b/muncher.c @@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include #include #include "config.h" +#include "common.h" #define SAVE_TO_NUM 10 #define MINLEN 10 #define MAXLEN 24 @@ -60,74 +61,22 @@ long score; chtype colors[6]={0}; FILE* scorefile; -char error[150]={0}; void logo(void){ mvaddstr(1,0,"|\\/|"); mvaddstr(2,0,"| |UNCHER"); } -byte scorewrite(void){// only saves the top 10, returns the place in the chart - bool deforno; - if( !getenv("MNCH_SCORES") && (scorefile= fopen(MNCH_SCORES,"r")) ){ - deforno=1; - } - else{ - deforno=0; - if( !(scorefile = fopen(getenv("MNCH_SCORES"),"r")) ){ - sprintf(error,"No accessible score files found. You can make an empty text file in %s or set MNCH_SCORES to such a file to solve this.",MNCH_SCORES); - return -1; - } - } - char namebuff[SAVE_TO_NUM][60]; - long scorebuff[SAVE_TO_NUM]; +byte save_score(void){ + return fallback_to_home("muncher_scores",score,SAVE_TO_NUM); - memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) ); - memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) ); - - long fuckingscore =0; - char fuckingname[60]={0}; - byte location=0; - - while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location=itreached || score>=scorebuff[location]) ){ - fprintf(scorefile,"%s : %ld\n",getenv("USER"),score); - ret=location; - wroteit=1; - } - if(location*>*>Top %d<*<*<\n",SAVE_TO_NUM); - while( rank>>"); printw("%d) %s : %ld\n",rank+1,pname,pscore); @@ -498,7 +447,7 @@ int main(int argc, char** argv){ refresh(); mvprintw(len+5,0,"Game over! Press a key to see the high scores:"); getch(); - showscores(scorewrite()); + show_scores(save_score()); printw("Game over! Wanna play again?(y/n)"); curs_set(1); input=getch(); diff --git a/pipes.c b/pipes.c index 91c13d3..92745e8 100644 --- a/pipes.c +++ b/pipes.c @@ -18,6 +18,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include #include #include "config.h" +#include "common.h" #define UP 1 #define RIGHT 2 #define DOWN 4 @@ -44,8 +45,6 @@ int py,px,fy,fx;//p: pointer f: fluid bitbox tocome[5]={0};//the row of pipes in the left side chtype green=A_BOLD;//will use bold font instead of green if colors are not available long score; -FILE* scorefile; -char error [150]={0}; void logo(void){ mvprintw(0,0," _ "); @@ -53,68 +52,16 @@ void logo(void){ mvprintw(2,0,"| IPES"); } -byte scorewrite(void){// only saves the top 10, returns the place in the chart - bool deforno; - if( !getenv("PP_SCORES") && (scorefile= fopen(PP_SCORES,"r")) ){ - deforno=1; - } - else{ - deforno=0; - if( !(scorefile = fopen(getenv("PP_SCORES"),"r")) ){ - sprintf(error,"No accessible score files found. You can make an empty text file in %s or set PP_SCORES to such a file to solve this.",PP_SCORES); - return -1; - } - } +byte save_score(void){ + return fallback_to_home("pipes_scores",score,SAVE_TO_NUM); - char namebuff[SAVE_TO_NUM][60]; - long scorebuff[SAVE_TO_NUM]; - - memset(namebuff,0,SAVE_TO_NUM*60*sizeof(char) ); - memset(scorebuff,0,SAVE_TO_NUM*sizeof(long) ); - - long fuckingscore =0; - char fuckingname[60]={0}; - byte location=0; - - while( fscanf(scorefile,"%59s : %ld\n",fuckingname,&fuckingscore) == 2 && location=itreached || score>=scorebuff[location]) ){ - fprintf(scorefile,"%s : %ld\n",getenv("USER"),score); - ret=location; - wroteit=1; - } - if(location0){ + rewind(score_file); + fscanf(score_file,"%*s : %*d"); + if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){ byte a = (len-9)/2; attron(A_BOLD); mvprintw(SY,SX, "**** ***"); @@ -159,8 +106,8 @@ void showscores(byte playerrank){ char pname[60] = {0}; long pscore=0; byte rank=0; - rewind(scorefile); - while( rank