DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T s

⟦2592fb9ae⟧ TextFile

    Length: 5337 (0x14d9)
    Types: TextFile
    Names: »score.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/X/Xrobots/score.c« 

TextFile

/*
 * score.c  --  xrobots v1.0
 *
 * flock() is used to stop a race condition within... if you don't 
 * have a bsd-like flock(), you could probably comment it out.
 * The race condition (multiple users writing to the score file) is 
 * probably rare.
 */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Box.h>
#include <X11/Command.h>
#include <X11/Label.h>
#include <stdio.h>
#include <sys/file.h>
#include "score.h"
#include "game.h"

/*----------------------------------------------------------------------*/

typedef struct {
  char 	score[6], 
	name[26];
} SCORE;

static SCORE scores[MAXSCORES];

void 	show_scores(), 
	new_high_score(), 
	load_scores(), 
	write_out_scores();

static FILE *scorefile = 0;

/*----------------------------------------------------------------------*/

void
check_score(current_score)
  int current_score;
{

  load_scores();

  if(current_score > atoi(scores[MAXSCORES-1].score)) {
    new_high_score(current_score);
    write_out_scores();
  }
  if(scorefile) {
    flock(scorefile->_file, LOCK_UN);
    fclose(scorefile);
    show_scores();
  }
}


static void
load_scores()
{
  int i = 0;

  if( !(scorefile = fopen(SCORE_FILE,"r+")) ) {
    scorefile = fopen(SCORE_FILE, "w");
    return;
  }
  flock(scorefile->_file, LOCK_EX);
  while( fgets(scores[i].score,6,scorefile) 	/* get score */
      && fgets(scores[i].name,26,scorefile) 	/* get name */
      && fgetc(scorefile))			/* and newline */
  {
    i++;
    if( i > MAXSCORES ) break;
  }
}


static void
new_high_score(current_score)
  int current_score;
{
  int i;
  char textscore[6],
       name[26];

  sprintf(textscore,"%5d",current_score);
  strncpy(name,getenv("USER"),25);

  for(i=MAXSCORES-2;i>=0;i--)
    if( current_score < atoi(scores[i].score) ) {
         /* move new score into i+1 slot */
      strcpy(scores[i+1].score,textscore);
      strcpy(scores[i+1].name,name);
      return;
    } else {
      strcpy(scores[i+1].score,scores[i].score);
      strcpy(scores[i+1].name,scores[i].name);
    }
  /* if it got here, there is a new number 1 score */
  strcpy(scores[0].score,textscore);
  strcpy(scores[0].name,name);

}


static void
write_out_scores()
{
  int i;

  if( !scorefile )
    return;
  rewind(scorefile);
  for(i=0;i<MAXSCORES;i++)
    fprintf(scorefile,"%5s%25s\n",scores[i].score,scores[i].name);
}


/*----------------------------------------------------------------------*/


Widget score_popup;
Widget score_labels[MAXSCORES];

static Arg arglist_score_title[] = {
  {  XtNborderWidth, (XtArgVal) 0  },
  {  XtNresize, (XtArgVal) False  },
  {  XtNwidth, (XtArgVal) 300   },
  {  XtNheight, (XtArgVal) 30   },
  {  XtNlabel, (XtArgVal) "High Scores"  },
  {  XtNjustify, (XtArgVal) XtJustifyCenter  },
};

static Arg arglist_score_label[] = {
  {  XtNlabel, (XtArgVal) 0  },
  {  XtNborderWidth, (XtArgVal) 0  },
  {  XtNresize, (XtArgVal) False  },
  {  XtNwidth, (XtArgVal) 300   },
  {  XtNjustify, (XtArgVal) XtJustifyCenter  },
};

static Arg arglist_popdown[] = {
/*  {  XtNborderWidth, (XtArgVal) 2  },*/
  {  XtNresize, (XtArgVal) False  },
  {  XtNwidth, (XtArgVal) 300   },
  {  XtNlabel, (XtArgVal) "Pop Down"  },
  {  XtNjustify, (XtArgVal) XtJustifyCenter  },
};


/*ARGSUSED*/
static XtCallbackProc 
popdown_callback(w, closure, call_data)
  Widget w;
  caddr_t closure;
  caddr_t call_data;
{
  XtPopdown(score_popup);
}


void
create_high_score_popup(parent)
  Widget parent;
{
  int i;
  Widget score_box, popdown;

  score_popup = XtCreatePopupShell(
                                   "score_popup",
                                   transientShellWidgetClass,
                                   parent, 0,0);

  score_box = XtCreateManagedWidget(
                                    "score_box",
                                    boxWidgetClass,
                                    score_popup,
                                    0,0);

  (void)XtCreateManagedWidget(
                                    "score_title",
                                    labelWidgetClass,
                                    score_box,
                                    arglist_score_title,
                                    XtNumber(arglist_score_title));

  for(i=0;i<MAXSCORES;i++) {
    score_labels[i] = XtCreateManagedWidget(
                                    "alabel",
                                    labelWidgetClass,
                                    score_box,
                                    arglist_score_label,
                                    XtNumber(arglist_score_label));
  }

  popdown = XtCreateManagedWidget(
                                    "popdown",
                                    commandWidgetClass,
                                    score_box,
                                    arglist_popdown,
                                    XtNumber(arglist_popdown));
  XtAddCallback(popdown,XtNcallback,popdown_callback,0);
}



void
show_scores()
{
  int i;
  char tmp_str[31];
  Arg tmp_arg;

  for(i = 0;i<MAXSCORES;i++) {
    sprintf(tmp_str,"%5s  %25s", scores[i].score, scores[i].name);
    XtSetArg(tmp_arg,XtNlabel,tmp_str);
    XtSetValues(score_labels[i],&tmp_arg,1);
  }
  XtPopup(score_popup, XtGrabNone);
}


/*----------------------------------------------------------------------*/