home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / letters / highscore.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-02  |  3.6 KB  |  181 lines

  1. /*
  2.  * read and update high score file for Letter Invaders
  3.  *
  4.  * copyright 1991 by Larry Moss (lm03_cif@uhura.cc.rochester.edu)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #ifdef AMIGA
  9. # include <stdlib.h>
  10. # include <string.h>
  11. #else
  12. # include <pwd.h>
  13. #endif
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include "config.h"
  17. #include "term.h"
  18. #include "terms.h"
  19. #include "kinput.h"
  20.  
  21. struct score_rec {
  22.     char    name[9];
  23.     int    level, words, score;
  24. };
  25.  
  26. static char highscores[] = HIGHSCORES;
  27.  
  28. extern unsigned score, word_count, level;
  29.  
  30. static struct score_rec high_scores[10];
  31. static struct stat    s_buf;
  32. time_t    readtime;
  33.  
  34. void read_scores() {
  35.     int         i;
  36.     FILE         *fp;
  37.  
  38.     /*
  39.      * get the last modified time so we know later if we have to reread
  40.      * the scores before saving a new file.
  41.      */
  42.     if(stat(highscores, &s_buf) == -1) {
  43.         fprintf(stderr, "Cannot stat %s: ", highscores);
  44.         perror("");
  45.         exit(1);
  46.     }
  47.  
  48.     readtime = s_buf.st_mtime;
  49.  
  50.     if((fp = fopen(highscores, "r")) == NULL) {
  51.         fprintf(stderr, "Cannot open %s: ", highscores);
  52.         perror("");
  53.         exit(1);
  54.     }
  55.  
  56.     for(i = 0; i < 10; i++) {
  57.         fscanf(fp, "%s%d%d%d", high_scores[i].name,
  58.                &high_scores[i].level, &high_scores[i].words,
  59.                &high_scores[i].score);
  60.     }
  61.  
  62.     fclose(fp);
  63. }
  64.  
  65. int write_scores() {
  66.     int    i;
  67.     FILE    *fp;
  68.  
  69.     /*
  70.      * check to make sure the high score list has not been modified
  71.      * since we read it.
  72.      */
  73.     if(stat(highscores, &s_buf) == -1) {
  74.         fprintf(stderr, "Cannot stat %s: ", highscores);
  75.         perror("");
  76.         setterm(ORIG);
  77.         exit(1);
  78.     }
  79.  
  80.     if(s_buf.st_mtime > readtime)
  81.         return -1;
  82.  
  83.     if((fp = fopen(highscores, "w")) == NULL) {
  84.         fprintf(stderr, "Cannot write to %s: ", highscores);
  85.         perror("");
  86.         setterm(ORIG);
  87.         exit(1);
  88.     }
  89.  
  90.     for(i = 0; i < 10; i++) {
  91.         fprintf(fp, "%s %d %d %d", high_scores[i].name,
  92.                high_scores[i].level, high_scores[i].words,
  93.                high_scores[i].score);
  94.     }
  95.  
  96.     fclose(fp);
  97. }
  98.  
  99.  
  100.  
  101. void update_scores() {
  102.     int i, j;
  103. #ifdef AMIGA
  104.     char *u;
  105. #else
  106.     struct passwd *p;
  107. #endif
  108.  
  109.     for(i = 0; i < 10; i++)
  110.         if(score > high_scores[i].score) {
  111.             for(j = 10; j > i; j--) {
  112.                 strcpy(high_scores[j].name, high_scores[j-1].name);
  113.                 high_scores[j].words = high_scores[j-1].words;
  114.                 high_scores[j].score = high_scores[j-1].score;
  115.                 high_scores[j].level = high_scores[j-1].level;
  116.             }
  117. #ifdef AMIGA
  118.                         if((u = getenv("USER")) == NULL) {
  119.                 char buffer[128],*p;
  120.                 clrdisp();
  121.                 gotoxy(18, 5);
  122.                 cooked(stdin);
  123.                 fputs("Enter your name (8 char. max, no spaces): ",stdout);
  124.                 fflush(stdout);
  125.                 gets(buffer);
  126.                 if((p=strchr(buffer,' ')))
  127.                     *p='\0';
  128.                 if(!buffer[0])
  129.                     strcpy(buffer,"nobody");
  130.                 strncpy(high_scores[i].name, buffer, 8);
  131.                 raw(stdin);
  132.                         } else
  133.                                 strcpy(high_scores[i].name, u);
  134. #else
  135.             if((p = getpwuid(getuid())) == NULL)
  136.                                 strcpy(high_scores[i].name, "nobody");
  137.             else
  138.                 strcpy(high_scores[i].name, p->pw_name);
  139. #endif
  140.             high_scores[i].score = score;
  141.             high_scores[i].words = word_count;
  142.             high_scores[i].level = level;
  143.             if(write_scores() == -1) {
  144.                 read_scores();
  145.                 update_scores();
  146.             }
  147.             break;
  148.         }
  149. }
  150.  
  151. void show_scores() {
  152.     int i;
  153.  
  154.     clrdisp();
  155.     gotoxy(18, 5);
  156.     highlight(1);
  157.     printf("Top Ten Scores for Letter Invaders");
  158.     highlight(0);
  159.     gotoxy(20, 7);
  160.     underline(1);
  161.     printf("  name      level words score");
  162.     underline(0);
  163.     
  164.     for(i = 0; i < 10; i++) {
  165.         gotoxy(18, 8 + i);
  166.         printf("%3d %-10s%5d%6d%6d", i+1, high_scores[i].name,
  167.                high_scores[i].level, high_scores[i].words,
  168.                high_scores[i].score);
  169.     }
  170.  
  171.     printf("\n");
  172. #ifdef AMIGA
  173.     {
  174.         char bogus;
  175.         fputs("\nPress RETURN... ",stdout);
  176.         fflush(stdout);
  177.         bogus=getchar();
  178.     }
  179. #endif
  180. }
  181.