home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / xap / xgames / xtetris-.6 / xtetris- / xtetris-2.6 / score.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  5KB  |  188 lines

  1. #include "defs.h"
  2.  
  3. #define HIGH_TABLE_SIZE 10      /* size of high score table */
  4. static struct score_table {
  5.     char    name[80];
  6.     int     score;
  7.     int     rows;
  8.     int     level;
  9.     char hostname[80];
  10.     char    date[80];
  11. }       high_scores[HIGH_TABLE_SIZE];
  12.  
  13. update_highscore_table()
  14. {
  15.   /* This version only allows 1 entry in the HIGH SCORE TABLE per user */
  16.   int     i, j;
  17.   long    when;
  18.   extern char *ctime();
  19.   extern long time();
  20.   char    hostname[BUFSIZ];
  21.   char    buf[BUFSIZ];
  22.   char      padname[25];
  23.   
  24.   strcpy( padname, "                    ");
  25.   strncpy( padname, name, strlen(name) );    /* get padded name. */
  26.  
  27.   if (!resources.usescorefile) return;
  28.   
  29.   /*    re-read high-score table in case someone else on the network is
  30.    *    playing at the same time 
  31.    */
  32.   read_high_scores();
  33.  
  34.   /* Check for previous best score */
  35.   for (i = 0; i < HIGH_TABLE_SIZE; i++)
  36.   {
  37.     if (strcmp(padname, high_scores[i].name) == 0)
  38.       break;
  39.   }
  40.   if (i < HIGH_TABLE_SIZE) {
  41.     /*
  42.      *    We have a previous best score. 
  43.      */
  44.     if (high_scores[i].score >= score)
  45.       return;         /* Same/worse score - no update */
  46.     for (j = i; j > 0; j--) /* Remove previous best */
  47.       high_scores[j] = high_scores[j - 1];
  48.   }
  49.   /* Next line finds score greater than current one */
  50.   for (i = 0;
  51.        i < HIGH_TABLE_SIZE && score >= high_scores[i].score; 
  52.        i++);
  53.   i--;
  54.   if (i >= 0) {
  55.     for (j = 0; j < i; j++)
  56.       high_scores[j] = high_scores[j + 1];
  57.     strcpy(high_scores[i].name, name);
  58.     high_scores[i].score = score;
  59.     high_scores[i].rows = rows;
  60.     high_scores[i].level = rows / 10;
  61.     if (_XGetHostname(hostname, BUFSIZ) == -1)
  62.       strncpy(high_scores[i].hostname, "unknown-host",sizeof(high_scores[i].hostname)-1);
  63.     else
  64.       strncpy(high_scores[i].hostname, hostname,sizeof(high_scores[i].hostname)-1);
  65.     high_scores[i].hostname[sizeof(high_scores[i].hostname)-1] = 0;
  66.     time(&when);
  67.     strncpy(high_scores[i].date, ctime(&when), 24); 
  68.     high_scores[i].date[24] = 0;
  69.     write_high_scores();
  70.   }
  71. }
  72.  
  73.  
  74. read_high_scores()
  75. {
  76.   FILE   *fp;
  77.   int     i;
  78.   char   buf[BUFSIZ];
  79.   
  80.   if (!resources.usescorefile) return;
  81.   
  82.   if ((fp = fopen(resources.scorefile, "r")) == NULL) {
  83.     write_high_scores();
  84.     if ((fp = fopen(resources.scorefile, "r")) == NULL) {
  85.       resources.usescorefile = False;
  86.       fprintf( stderr, "%s: No High score file.  Use '-noscore' to avoid this message.\n",
  87.           programname );
  88.       return;
  89.     }
  90.   }
  91.   for (i = 0; i < HIGH_TABLE_SIZE; i++) {
  92.     struct score_table *score = &(high_scores[i]);
  93.     if (6 != fscanf( fp, "%20[^,],%7d,%5d,%4d,%12[^,],%24[^,]\n",
  94.             score->name, 
  95.             &score->score, 
  96.             &score->level,
  97.             &score->rows, 
  98.             score->hostname, 
  99.             score->date ))
  100.     {
  101.       strcpy( score->name, "No name" );
  102.       strcpy( score->hostname, "No host" );
  103.       strcpy( score->date, "No date" );
  104.       score->rows = score->score = score->level = 0;
  105.     }
  106.   }
  107.   fclose(fp);
  108. }
  109.  
  110. write_high_scores()
  111. {
  112.   FILE   *fp;
  113.   int     i;
  114.   
  115.   if ((fp = fopen(resources.scorefile, "w")) == NULL) {
  116.     fprintf(stderr, "%s: Couldn't open high score file %s\n", programname, resources.scorefile );
  117.     return;
  118.   }
  119.   for (i = 0; i < HIGH_TABLE_SIZE; i++)
  120.     fprintf( fp, "%-20s,%7d,%5d,%4d,%-12s,%-24s\n",
  121.        high_scores[i].name, 
  122.        high_scores[i].score, 
  123.        high_scores[i].level,
  124.        high_scores[i].rows, 
  125.        high_scores[i].hostname, 
  126.        high_scores[i].date );
  127.   fclose(fp);
  128. }
  129.  
  130. void print_high_scores()
  131. {
  132.   int     i,j;
  133. static  char    buf[81*HIGH_TABLE_SIZE+100];
  134.   char * start;
  135.   int len;
  136.   
  137.   if (!resources.usescorefile) return;
  138.   
  139.   /* re-read high-score table in case someone else on the network is
  140.    * playing at the same time */
  141.  
  142.   read_high_scores();
  143.  
  144.   sprintf( buf, "\
  145. Name                 Score   Level Rows Host         Date\n\
  146. ----                 -----   ----- ---- ----         ----\n" );
  147.   
  148.   len = strlen(buf);
  149.   start = buf+len;
  150.   
  151.   for (i = HIGH_TABLE_SIZE-1; i >= 0; i--)
  152.   {
  153.     if (high_scores[i].score != 0)
  154.     {
  155.       int chars;
  156.       sprintf( start, "%-20s %-7d %-5d %-4d %-12s %-24s\n",
  157.           high_scores[i].name, 
  158.           high_scores[i].score, 
  159.           high_scores[i].level,
  160.           high_scores[i].rows, 
  161.           high_scores[i].hostname, 
  162.           high_scores[i].date );
  163.       chars = strlen(start);
  164.       start += chars;
  165.       len += chars;
  166.     }
  167.   }
  168.   XtVaSetValues( score_text, 
  169.         XtNstring, (XtArgVal)buf,
  170.         XtNlength, (XtArgVal)len+1, NULL );
  171.   XtPopupSpringLoaded(score_frame);
  172. }
  173.  
  174. /*
  175.   emacs mode: indented-text
  176.   
  177.   emacs Local Variables: 
  178.   emacs mode: c 
  179.   emacs c-indent-level: 2
  180.   emacs c-continued-statement-offset: 2
  181.   emacs c-continued-brace-offset: -2
  182.   emacs c-tab-always-indent: nil
  183.   emacs c-brace-offset: 0 
  184.   emacs tab-width: 8
  185.   emacs tab-stop-list: (2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84)
  186.   emacs End:
  187.   */
  188.