/***********************************************************************
 * 
 *  G F O ! 
 *  by Chee Chew
 *      and 
 *     Ken Duda 
 *
 *  (c) 1990
 *
 *  This software is free for distribution.  This notice, however, may 
 *  not be altered.
 *  
 *  Enjoy! (and punt... )
 *
 ***********************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include "gfo.h"
#define HSTITLE BITMAP_PATH"hstitle"

#define NUMHIGHS 25

typedef struct high_s {
  char username[9];
  char fullname[76];
  int round;
  int score;
} High;

High highs[NUMHIGHS];
High enchighs[NUMHIGHS];

char filename[1024];

is_high(int score)
{
  int n;

  load_highs();
  for (n = 0; n < NUMHIGHS && (highs[n].score >= score); n++);
  return (n < NUMHIGHS);
}

add_high(char *username, char *fullname, int score, int round)
{
  int n, m;

  load_highs();
  for (n = 0; n < NUMHIGHS && (strcmp(username, highs[n].username)); n++);
  if (n < NUMHIGHS) 
    if (highs[n].score >= score)
      return;			/* Old score was higher */
    else
      for (; n < NUMHIGHS-1; n++)
	highs[n] = highs[n+1];	/* remove old score */
  for (n = 0; n < NUMHIGHS && (highs[n].score >= score); n++);
  if (n < NUMHIGHS) {
    for (m = NUMHIGHS-1; m > n; m--)
      highs[m] = highs[m-1];
    strcpy(highs[n].username, username);
    strcpy(highs[n].fullname, fullname);
    highs[n].score = score;
    highs[n].round = round;
    save_highs();
  }
}

make_high_window(XC *xc)
{
  xc->hswin = XCreateSimpleWindow(xc->dsp, xc->mwin, 
				  xc->width/2 - xc->hswidth/2,
				  xc->height/2 - xc->hsheight/2,
				  xc->hswidth, xc->hsheight, 5,
				  WhitePixel(xc->dsp,xc->scr),
				  BlackPixel(xc->dsp,xc->scr));
  XSelectInput(xc->dsp, xc->hswin,
	       ButtonPressMask |
	       KeyPressMask |
	       ExposureMask |
	       StructureNotifyMask | FocusChangeMask);
}



SetupHigh(XC *xc)
{
  int i;
  char file[1024];
  
  xc->hswidth = 200;
  xc->hsheight = 500;
  make_high_window(xc);
  xc->hsdraw_gc = XCreateGC(xc->dsp, xc->hswin, 0, 0);
  XSetForeground(xc->dsp, xc->hsdraw_gc, WhitePixel(xc->dsp,xc->scr));
  XSetBackground(xc->dsp, xc->hsdraw_gc, BlackPixel(xc->dsp,xc->scr));
  xc->hstitle = read_pixmap(xc, xc->hswin, HSTITLE, &xc->hstitlewidth, &xc->hstitleheight);
  for (i = 0; i < 3; i++) {
    sprintf(file,"%s%d", HSRANK, i+1);
    xc->hsrank[i] = read_pixmap(xc, xc->hswin, file, &xc->hsrankwidth[i], &xc->hsrankheight[i]);
  }
  xc->hsno = read_pixmap(xc, xc->hswin, HSNO, &xc->hsnowidth, &xc->hsnoheight);
  if ((xc->hsfont = XLoadQueryFont(xc->dsp, HSFONT)) == NULL) {
    fprintf (stderr, "Can't open font %s\n",HSFONT);
    exit (1);
  }
  XSetFont (xc->dsp, xc->hsdraw_gc, xc->hsfont->fid);
  destroy_hs(xc);
}

destroy_hs(XC *xc)
{
  XDestroyWindow(xc->dsp,xc->hswin);
  xc->hswin = NULL;
}

hs_wait(XC *xc)
{
  XEvent e;
  int waiting = 1; 
  char buffer[2];

  do {
    XNextEvent (xc->dsp, &e);
    switch ( e.type ) {
    case KeyPress: 
      (void) XLookupString(&e.xkey, buffer, 2, NULL,NULL) ;
      waiting = 0; /*buffer[0] != 32;*/
      break; 
    case Expose: 
      refresh_hs(xc);
      break;
    }
  } while (waiting);
  ClearMainWin(xc);
}

show_high_scores(XC *xc)
{
  if (!xc->hswin) 
    make_high_window(xc);
  XMapWindow(xc->dsp, xc->hswin);
  load_highs();
  hs_wait(xc); 
  
  destroy_hs(xc);
}


refresh_hs(XC *xc)
{
  int y, i;
  char line[128];
  XCopyArea(xc->dsp, xc->hstitle, xc->hswin, xc->copy_gc, 
	    0 ,0, xc->hstitlewidth, xc->hstitleheight, 0 ,0);
  
  for(i = 0, y = xc->hstitleheight + 10; i < NUMHIGHS; y+= 15, i++) {
    XDrawString(xc->dsp, xc->hswin, xc->hsdraw_gc, 5, y, 
		highs[i].username, strlen(highs[i].username));
/*    XDrawString(xc->dsp, xc->hswin, xc->hsdraw_gc, 60, y, 
		highs[i].fullname, strlen(highs[i].fullname));*/
    sprintf(line,"%5d %2d", highs[i].score, highs[i].round);
    XDrawString(xc->dsp, xc->hswin, xc->hsdraw_gc, 100, y, 
		line, strlen(line));
  }		
}





/*
 *  This Code is provided by Steve Smoot 
 *  (primarily because we were too lazy to make a duplicate set of 
 * encrypted high score procedures. 
 *
 *  Thanks Steve! 
 *  -Chee 
 */
 

/*
	 A primitive mechanism would be to insert system calls to encrypt,
   but i didnt bother...
*/

#define HSFILE "/n/hermes/b/users/smoot/games/gfo/gfo.scores"

/* ... */


 load_highs()
{
  int   i;
  FILE *s;

  s = fopen(HSFILE, "r");
  if (s == NULL)
  {
    warning("unable to open score file; creating new one");

    for (i=0; i<NUMHIGHS; i++)
    {
      strcpy(highs[i].username, "noone");
      strcpy(highs[i].fullname, "nobody");
      highs[i].score = -1;
      highs[i].round = -1;
    }

    s = fopen(HSFILE, "w");
    if (s == NULL)
      fatal("unable to create score file");

    save_highs();
    fclose(s);

    if (chmod(HSFILE, 0777) != 0)
    {
      unlink(HSFILE);
      fatal("unable to set score file mode");
    }
  }
  else
  {
    for (i=0; i<NUMHIGHS; i++)
      if (fscanf(s, "%s ! %s ! %d %d\n", highs[i].username, highs[i].fullname, 
	    &highs[i].score, &highs[i].round) !=4)
	fatal("incomplete score file read");
    fclose(s);
  }
}


 save_highs()
{
  int   i;
  FILE *s;
  
  s = fopen(HSFILE, "w");
  if (s == NULL)
    fatal("unable to open score file");

  for (i=0; i<NUMHIGHS; i++)
    fprintf(s, "%s ! %s ! %d %d\n", highs[i].username, highs[i].fullname, 
	    highs[i].score, highs[i].round);

  fclose(s);
}

fatal(msg)
     char *msg;
{
  fflush(stdout);
  fprintf(stderr, "gfo: %s\n", msg);
  exit(1);
}

warning(msg)
     char *msg;
{
  fflush(stdout);
  fprintf(stderr, "gfo: warning! %s\n", msg);
  fflush(stderr);
}

