home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume4 / xrobots / part01 / score.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-04  |  5.2 KB  |  225 lines

  1. /*
  2.  * score.c  --  xrobots v1.0
  3.  *
  4.  * flock() is used to stop a race condition within... if you don't 
  5.  * have a bsd-like flock(), you could probably comment it out.
  6.  * The race condition (multiple users writing to the score file) is 
  7.  * probably rare.
  8.  */
  9.  
  10. #include <X11/Intrinsic.h>
  11. #include <X11/StringDefs.h>
  12. #include <X11/Shell.h>
  13. #include <X11/Box.h>
  14. #include <X11/Command.h>
  15. #include <X11/Label.h>
  16. #include <stdio.h>
  17. #include <sys/file.h>
  18. #include "score.h"
  19. #include "game.h"
  20.  
  21. /*----------------------------------------------------------------------*/
  22.  
  23. typedef struct {
  24.   char     score[6], 
  25.     name[26];
  26. } SCORE;
  27.  
  28. static SCORE scores[MAXSCORES];
  29.  
  30. void     show_scores(), 
  31.     new_high_score(), 
  32.     load_scores(), 
  33.     write_out_scores();
  34.  
  35. static FILE *scorefile = 0;
  36.  
  37. /*----------------------------------------------------------------------*/
  38.  
  39. void
  40. check_score(current_score)
  41.   int current_score;
  42. {
  43.  
  44.   load_scores();
  45.  
  46.   if(current_score > atoi(scores[MAXSCORES-1].score)) {
  47.     new_high_score(current_score);
  48.     write_out_scores();
  49.   }
  50.   if(scorefile) {
  51.     flock(scorefile->_file, LOCK_UN);
  52.     fclose(scorefile);
  53.     show_scores();
  54.   }
  55. }
  56.  
  57.  
  58. static void
  59. load_scores()
  60. {
  61.   int i = 0;
  62.  
  63.   if( !(scorefile = fopen(SCORE_FILE,"r+")) ) {
  64.     scorefile = fopen(SCORE_FILE, "w");
  65.     return;
  66.   }
  67.   flock(scorefile->_file, LOCK_EX);
  68.   while( fgets(scores[i].score,6,scorefile)     /* get score */
  69.       && fgets(scores[i].name,26,scorefile)     /* get name */
  70.       && fgetc(scorefile))            /* and newline */
  71.   {
  72.     i++;
  73.     if( i > MAXSCORES ) break;
  74.   }
  75. }
  76.  
  77.  
  78. static void
  79. new_high_score(current_score)
  80.   int current_score;
  81. {
  82.   int i;
  83.   char textscore[6],
  84.        name[26];
  85.  
  86.   sprintf(textscore,"%5d",current_score);
  87.   strncpy(name,getenv("USER"),25);
  88.  
  89.   for(i=MAXSCORES-2;i>=0;i--)
  90.     if( current_score < atoi(scores[i].score) ) {
  91.          /* move new score into i+1 slot */
  92.       strcpy(scores[i+1].score,textscore);
  93.       strcpy(scores[i+1].name,name);
  94.       return;
  95.     } else {
  96.       strcpy(scores[i+1].score,scores[i].score);
  97.       strcpy(scores[i+1].name,scores[i].name);
  98.     }
  99.   /* if it got here, there is a new number 1 score */
  100.   strcpy(scores[0].score,textscore);
  101.   strcpy(scores[0].name,name);
  102.  
  103. }
  104.  
  105.  
  106. static void
  107. write_out_scores()
  108. {
  109.   int i;
  110.  
  111.   if( !scorefile )
  112.     return;
  113.   rewind(scorefile);
  114.   for(i=0;i<MAXSCORES;i++)
  115.     fprintf(scorefile,"%5s%25s\n",scores[i].score,scores[i].name);
  116. }
  117.  
  118.  
  119. /*----------------------------------------------------------------------*/
  120.  
  121.  
  122. Widget score_popup;
  123. Widget score_labels[MAXSCORES];
  124.  
  125. static Arg arglist_score_title[] = {
  126.   {  XtNborderWidth, (XtArgVal) 0  },
  127.   {  XtNresize, (XtArgVal) False  },
  128.   {  XtNwidth, (XtArgVal) 300   },
  129.   {  XtNheight, (XtArgVal) 30   },
  130.   {  XtNlabel, (XtArgVal) "High Scores"  },
  131.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  132. };
  133.  
  134. static Arg arglist_score_label[] = {
  135.   {  XtNlabel, (XtArgVal) 0  },
  136.   {  XtNborderWidth, (XtArgVal) 0  },
  137.   {  XtNresize, (XtArgVal) False  },
  138.   {  XtNwidth, (XtArgVal) 300   },
  139.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  140. };
  141.  
  142. static Arg arglist_popdown[] = {
  143. /*  {  XtNborderWidth, (XtArgVal) 2  },*/
  144.   {  XtNresize, (XtArgVal) False  },
  145.   {  XtNwidth, (XtArgVal) 300   },
  146.   {  XtNlabel, (XtArgVal) "Pop Down"  },
  147.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  148. };
  149.  
  150.  
  151. /*ARGSUSED*/
  152. static XtCallbackProc 
  153. popdown_callback(w, closure, call_data)
  154.   Widget w;
  155.   caddr_t closure;
  156.   caddr_t call_data;
  157. {
  158.   XtPopdown(score_popup);
  159. }
  160.  
  161.  
  162. void
  163. create_high_score_popup(parent)
  164.   Widget parent;
  165. {
  166.   int i;
  167.   Widget score_box, popdown;
  168.  
  169.   score_popup = XtCreatePopupShell(
  170.                                    "score_popup",
  171.                                    transientShellWidgetClass,
  172.                                    parent, 0,0);
  173.  
  174.   score_box = XtCreateManagedWidget(
  175.                                     "score_box",
  176.                                     boxWidgetClass,
  177.                                     score_popup,
  178.                                     0,0);
  179.  
  180.   (void)XtCreateManagedWidget(
  181.                                     "score_title",
  182.                                     labelWidgetClass,
  183.                                     score_box,
  184.                                     arglist_score_title,
  185.                                     XtNumber(arglist_score_title));
  186.  
  187.   for(i=0;i<MAXSCORES;i++) {
  188.     score_labels[i] = XtCreateManagedWidget(
  189.                                     "alabel",
  190.                                     labelWidgetClass,
  191.                                     score_box,
  192.                                     arglist_score_label,
  193.                                     XtNumber(arglist_score_label));
  194.   }
  195.  
  196.   popdown = XtCreateManagedWidget(
  197.                                     "popdown",
  198.                                     commandWidgetClass,
  199.                                     score_box,
  200.                                     arglist_popdown,
  201.                                     XtNumber(arglist_popdown));
  202.   XtAddCallback(popdown,XtNcallback,popdown_callback,0);
  203. }
  204.  
  205.  
  206.  
  207. void
  208. show_scores()
  209. {
  210.   int i;
  211.   char tmp_str[31];
  212.   Arg tmp_arg;
  213.  
  214.   for(i = 0;i<MAXSCORES;i++) {
  215.     sprintf(tmp_str,"%5s  %25s", scores[i].score, scores[i].name);
  216.     XtSetArg(tmp_arg,XtNlabel,tmp_str);
  217.     XtSetValues(score_labels[i],&tmp_arg,1);
  218.   }
  219.   XtPopup(score_popup, XtGrabNone);
  220. }
  221.  
  222.  
  223. /*----------------------------------------------------------------------*/
  224.  
  225.