home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume19 / xtmines / part02 / hiscore.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-28  |  6.1 KB  |  236 lines

  1. /* xtmines: game where you try to cross a minefield */
  2. /* hiscore.c: */
  3. /* Written by Timothy Tsai  April 13, 1992 */
  4.  
  5. #include <sys/file.h>
  6. #include <pwd.h>
  7. #include <time.h>
  8. #include "xtmines.h"
  9.  
  10. #define MAXSCORES        10
  11. #define MAX_ANGEL_STARTS    5
  12. #define HISCORE_FILENAME    "/home/bach3/ttsai/games/xtmines/xtmines.hiscores"
  13.  
  14. typedef struct {
  15.     int        uid, score;
  16.     ranktype    final_level,start_level;
  17.     long        timestamp;
  18. } score_t;
  19.  
  20. void show_final_score()
  21. {
  22.     printf("Your final score was %d\n",num_score);
  23.     printf("Your final rank was %s\n",num_rank_to_words(level));
  24. }
  25.  
  26. int add_hiscore()
  27. {
  28.     int    fd;
  29.     score_t
  30.         current, last, mine;
  31.     int    occ = 0;        /* num times current user */
  32.                     /*    is on hiscore list  */
  33.     int    angelocc = 0;        /* num times current user is on list */
  34.                     /*    and started on level angel */
  35.     int    trickle = FALSE;
  36.     int    myuid;
  37.     int    added = FALSE;        /* current score added yet? */
  38.  
  39.     myuid = getuid();    /* current user's uid */
  40.     mine.uid = myuid;
  41.     mine.score = num_score;
  42.     mine.final_level = level;
  43.     mine.start_level = start_level;
  44.     mine.timestamp = time(0);
  45.  
  46.     if ((fd=open(HISCORE_FILENAME,O_RDWR,0666)) < 0) {
  47.        if ((fd=open(HISCORE_FILENAME,O_CREAT|O_WRONLY,0666)) < 0) {
  48.         fprintf(stderr,"Error: Can't open %s\n",HISCORE_FILENAME);
  49.         exit(5);
  50.        }
  51.        write(fd, (char *) &mine, sizeof(score_t));
  52.        close(fd);
  53.        return(0);
  54.     }
  55.  
  56.  
  57.     while (read(fd,(char *) ¤t,sizeof(score_t))==sizeof(score_t)) {
  58.         if ((!added) && (num_score > current.score) &&
  59.             ((angelocc < MAX_ANGEL_STARTS) ||
  60.              (angelocc == MAX_ANGEL_STARTS) && (start_level != angel))){
  61.         /* add to hiscore list */
  62.             last = mine;
  63.             trickle = TRUE;
  64.             added = TRUE;
  65.             occ++;
  66.             if (mine.start_level == angel) angelocc++;
  67.         }
  68.         if (trickle) {
  69.             lseek(fd, (long) -sizeof(score_t), L_INCR);
  70.             write(fd, (char *) &last, sizeof(score_t));
  71.             last = current;
  72.             if (myuid == current.uid) {
  73.                 occ++;
  74.                 if (current.start_level == angel) angelocc++;
  75.                 if ((occ > MAXSCORES) ||
  76.                     ((angelocc > MAX_ANGEL_STARTS) &&
  77.                      (mine.start_level == angel))) {
  78.                     trickle = FALSE;
  79.                     if (occ > MAXSCORES) break;
  80.                 }
  81.             }
  82.         }
  83.         else {
  84.             if (myuid == current.uid) {
  85.                 occ++;
  86.                 if (current.start_level == angel) angelocc++;
  87.                 if (occ == MAXSCORES)
  88.                     break;
  89.             }
  90.         }
  91.     }
  92.     if (trickle)
  93.         write(fd, (char *) &last, sizeof(score_t));
  94.     if ((!added) && (occ < MAXSCORES) && (angelocc < MAX_ANGEL_STARTS))
  95.         write(fd, (char *) &mine, sizeof(score_t));
  96.     close(fd);
  97. }
  98.  
  99. #define LINE_SEP    2    /* separation between lines in hswind */
  100. #define LEFT_MARGIN    3    /* pixel sep from left edge of hswind */
  101. int show_hiscores(dox)
  102. int dox;    /* boolean:  do Xwindow show or just text */
  103. {
  104.     int    fd;
  105.     int    lastuid = -1;
  106.     int    i = 0;
  107.     score_t    curscore;
  108.     int    pcount = MAXSCORES;
  109.     int    myuid;
  110.     struct passwd *user;
  111.     char    *name;
  112.     int    lines=0,width,height;
  113.     Window    hswind;        /* high score window */
  114.     char    str[MAXSTRLEN];
  115.     int    px=LEFT_MARGIN,    /* pixel positions for next line */
  116.         py=fontmh;
  117.     XEvent                  event;
  118.     XButtonPressedEvent     *eventbp;
  119.     XKeyPressedEvent        *eventkp;
  120.     int                     wait = TRUE;
  121.  
  122.     if ((fd=open(HISCORE_FILENAME,O_RDONLY,0666)) < 0) {
  123.         fprintf(stderr,"Error: Can't open %s\n",HISCORE_FILENAME);
  124.         exit(5);
  125.     }
  126.  
  127.     myuid = getuid();
  128.     printf("Rank User          Score Final level  Start level  Time\n");
  129.     lines++;
  130.  
  131.     while (read(fd,(char *)&curscore,sizeof(score_t)) == sizeof(score_t)) {
  132.         i++;
  133.         if (pcount || curscore.uid == myuid) {
  134.             if (lastuid != curscore.uid) {
  135.                 user = getpwuid(curscore.uid);
  136.                 lastuid = curscore.uid;
  137.             }
  138.             name = user->pw_name;
  139.             /* no \n is needed because ctime puts one there */
  140.             printf("%4d %-9s %9d %-11s  %-11s  %24s",
  141.                 i,name,curscore.score,
  142.                 num_rank_to_words(curscore.final_level),
  143.                 ((curscore.start_level<grunt) ||
  144.                  (curscore.start_level>god)) ?
  145.                 " " : num_rank_to_words(curscore.start_level),
  146.                 ctime(&curscore.timestamp));
  147.             lines++;
  148.             if (pcount) pcount--;
  149.         }
  150.     }
  151.     close(fd);
  152.     printf("There are %d scores to date\n", i);
  153.     lines++;
  154.  
  155.     if (dox == NOX)
  156.         return(SUCCESS);
  157.  
  158.     /* create new window */
  159.     lastuid = -1;
  160.     i = 0;
  161.     pcount = MAXSCORES;
  162.     width = fontmw*40;
  163.     height = (fontmh+LINE_SEP)*(lines+1);
  164.     hswind = XCreateSimpleWindow(disp,frame,(w-width)/2,(h-height)/2,
  165.         width,height,bw,black,white);
  166.     XSelectInput(disp,hswind,KeyPressMask|ButtonPressMask);
  167.     XMapRaised(disp,hswind);
  168.     XSetForeground(disp,gct,color[font_color].pixel);
  169.     XSetBackground(disp,gct,color[background_color].pixel);
  170.     XFlush(disp);
  171.  
  172.     /* print in new window */
  173.     if ((fd=open(HISCORE_FILENAME,O_RDONLY,0666)) < 0) {
  174.         fprintf(stderr,"Error: Can't open %s\n",HISCORE_FILENAME);
  175.         exit(5);
  176.     }
  177.     sprintf(str,"Rank User          Score  Level");
  178.     XDrawImageString(disp,hswind,gct,px,py,str,strlen(str));
  179.     XFlush(disp);
  180.     py += (fontmh + LINE_SEP);
  181.     while (read(fd,(char *)&curscore,sizeof(score_t)) == sizeof(score_t)){
  182.         i++;
  183.         if (pcount || curscore.uid == myuid) {
  184.             if (lastuid != curscore.uid) {
  185.                 user = getpwuid(curscore.uid);
  186.                 lastuid = curscore.uid;
  187.             }
  188.             name = user->pw_name;
  189.             sprintf(str,"%4d %-9s %9d %s",i,name,curscore.score,
  190.                 num_rank_to_words(curscore.final_level));
  191.             XDrawImageString(disp,hswind,gct,px,py,
  192.                 str,strlen(str));
  193.             XFlush(disp);
  194.             py += (fontmh + LINE_SEP);
  195.             if (pcount) pcount--;
  196.         }
  197.     }
  198.     close(fd);
  199.     sprintf(str,"There are %d scores to date", i);
  200.     XDrawImageString(disp,hswind,gct,px,py,str,strlen(str));
  201.     py += (fontmh + LINE_SEP);
  202.     sprintf(str,"Press key or click button to end");
  203.     XDrawImageString(disp,hswind,gct,px,py,str,strlen(str));
  204.     XFlush(disp);
  205.  
  206.     /* Wait for keypress or button click */
  207.     while (wait == TRUE) {
  208.        while (!XPending(disp))
  209.         usleep(XPENDING_UDELAY);
  210.        XNextEvent(disp,&event);
  211.        if (event.type==ButtonPress) {
  212.         eventbp = (XButtonPressedEvent *) &event;
  213.         if ((eventbp->window == hswind) ||
  214.             (eventbp->window == field) ||
  215.             (eventbp->window == wind[quit_wind]))
  216.                     wait = FALSE;
  217.        }
  218.        else if (event.type == KeyPress) {
  219.         eventkp = (XKeyPressedEvent *) &event;
  220.         if ((eventkp->window == hswind) ||
  221.             (eventkp->window == field))
  222.             wait = FALSE;
  223.        }
  224.     }
  225.  
  226.     return (SUCCESS);
  227. }
  228.  
  229. void show_and_add_scores()
  230. {
  231.     show_final_score();
  232.     add_hiscore();
  233.     show_hiscores(DOX);
  234.     exit(SUCCESS);
  235. }
  236.