Beispiel #1
0
/*	check_score checks the score global variable against the high score
	table, and if it makes it, gets the player's name and saves the new
	high scores in the file.
*/
void	check_score()
{
	register int	i, spot;
	char			buf[MAXNAMELENGTH];

	spot = 0;
	flock(scorefd,LOCK_EX);
	read_scores();
	flock(scorefd,LOCK_UN);
	for(i=0; i<numhighs; i++) {
		if(score > highscore[i]) {
			get_high_name(buf);
			spot = 1;
			break;
		}
	}
	if(spot == 0) {
		if(numhighs < MAXHIGHSCORES) get_high_name(buf);
		else return;
	}
	flock(scorefd,LOCK_EX);
	read_scores();
	for(i=0; i<numhighs; i++) {
		if(score > highscore[i]) {
			shift_highs(i);
			highscore[i] = score;
			highlevel[i] = level;
			sprintf(highstring[i], "%-20s     %-3d       %-9d", buf,
					level, score);
			write_scores();
			flock(scorefd,LOCK_UN);
			return;
		}
	}
	/*	Didn't beat anyone else, but if the scoreboard isn't full, let it
		in.
	*/
	if(numhighs < MAXHIGHSCORES) {
		highscore[numhighs] = score;
		highlevel[numhighs] = level;
		sprintf(highstring[numhighs], "%-20s     %-3d       %-9d", buf,
				level, score);
		numhighs++;
		write_scores();
		flock(scorefd,LOCK_UN);
		return;
	}
	/* Sorry, you got bumped off while you entered your name */
}
Beispiel #2
0
/*	open_scores tries to open the score file, and if it can't it tries to
	create one.
*/
void	open_scores()
{
	numhighs = 0;
	scorefd = open(SCOREPATH, O_RDWR);
	if(scorefd == -1) {
		scorefd = open(SCOREPATH, O_RDWR | O_CREAT, 0444);
		if(scorefd == -1) {
			scorefd = open("jetpack.scores", O_RDWR);
			if(scorefd == -1) {
				scorefd = open("jetpack.scores", O_RDWR | O_CREAT, 0444);
				if(scorefd == -1) {
					fprintf(stderr,"Jetpack : Couldn't open scorefile.\n");
					exit(1);
				} else {
					flock(scorefd,LOCK_EX);
					scorefile = fdopen(scorefd,"r+");
					write_scores();
					flock(scorefd,LOCK_UN);
					return;
				}
			} else {
				flock(scorefd,LOCK_EX);
				scorefile = fdopen(scorefd,"r+");
				read_scores();
				flock(scorefd,LOCK_UN);
				return;
			}
		} else {
			flock(scorefd,LOCK_EX);
			scorefile = fdopen(scorefd,"r+");
			write_scores();
			flock(scorefd,LOCK_UN);
			return;
		}
	} else {
		flock(scorefd,LOCK_EX);
		scorefile = fdopen(scorefd,"r+");
		read_scores();
		flock(scorefd,LOCK_UN);
		return;
	}
}
Beispiel #3
0
int
main (int argc, char **argv)
{
  int c;
  bool running_suid;
  void *lockstate;
  char *scorefile;
  char *nl;
  const char *prefix, *user_prefix = NULL;
  struct stat buf;
  struct score_entry *scores;
  struct score_entry newscore;
  bool reverse = false;
  ptrdiff_t scorecount, scorealloc;
  ptrdiff_t max_scores = MAX_SCORES;

  srand (time (0));

  while ((c = getopt (argc, argv, "hrm:d:")) != -1)
    switch (c)
      {
      case 'h':
	usage (EXIT_SUCCESS);
	break;
      case 'd':
	user_prefix = optarg;
	break;
      case 'r':
	reverse = 1;
	break;
      case 'm':
	{
	  intmax_t m = strtoimax (optarg, 0, 10);
	  if (m < 0)
	    usage (EXIT_FAILURE);
	  max_scores = min (m, MAX_SCORES);
	}
	break;
      default:
	usage (EXIT_FAILURE);
      }

  if (argc - optind != 3)
    usage (EXIT_FAILURE);

  running_suid = (getuid () != geteuid ());

  prefix = get_prefix (running_suid, user_prefix);

  scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
  if (!scorefile)
    lose_syserr ("Couldn't allocate score file");

  strcpy (scorefile, prefix);
  strcat (scorefile, "/");
  strcat (scorefile, argv[optind]);

  newscore.score = strtoimax (argv[optind + 1], 0, 10);

  newscore.data = argv[optind + 2];
  if (strlen (newscore.data) > MAX_DATA_LEN)
    newscore.data[MAX_DATA_LEN] = '\0';
  nl = strchr (newscore.data, '\n');
  if (nl)
    *nl = '\0';

  newscore.username = get_user_id ();
  if (! newscore.username)
    lose_syserr ("Couldn't determine user id");

  if (stat (scorefile, &buf) < 0)
    lose_syserr ("Failed to access scores file");

  if (lock_file (scorefile, &lockstate) < 0)
    lose_syserr ("Failed to lock scores file");

  if (read_scores (scorefile, &scores, &scorecount, &scorealloc) < 0)
    {
      unlock_file (scorefile, lockstate);
      lose_syserr ("Failed to read scores file");
    }
  if (push_score (&scores, &scorecount, &scorealloc, &newscore) < 0)
    {
      unlock_file (scorefile, lockstate);
      lose_syserr ("Failed to add score");
    }
  sort_scores (scores, scorecount, reverse);
  /* Limit the number of scores.  If we're using reverse sorting, then
     also increment the beginning of the array, to skip over the
     *smallest* scores.  Otherwise, just decrementing the number of
     scores suffices, since the smallest is at the end. */
  if (scorecount > max_scores)
    {
      if (reverse)
	scores += scorecount - max_scores;
      scorecount = max_scores;
    }
  if (write_scores (scorefile, scores, scorecount) < 0)
    {
      unlock_file (scorefile, lockstate);
      lose_syserr ("Failed to write scores file");
    }
  if (unlock_file (scorefile, lockstate) < 0)
    lose_syserr ("Failed to unlock scores file");
  exit (EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
	char arg_string[2000];
	int nsub,n_new_sub,real_nsub;
	float *score,p;
	int max_cc[2],s,n_non_mendelian;
	non_mendelian *non_mendelians;
	char *non_mendelian_report;
	par_info pi;
	sa_par_info spi;
	subject **sub,**new_sub,**real_sub;
	pi.use_cc=1;
	printf("%s v%s\n",PROGRAM,SAVERSION);
	printf("MAX_LOCI=%d\nMAX_SUB=%d\n",MAX_LOCI,MAX_SUB);

	assert(sub=(subject **)calloc(MAX_SUB,sizeof(subject*)));
	for (s=0;s<MAX_SUB;++s)
		assert(sub[s]=(subject *)calloc(1,sizeof(subject)));
	assert(score=(float *)calloc(MAX_SUB,sizeof(float)));
	max_cc[0]=max_cc[1]=0;
	read_all_args(argv,argc, &pi, &spi);
	// make_arg_string(arg_string,argc,argv);
	// parse_arg_string(arg_string,&pi,&spi,&pspi);
	process_options(&pi,&spi);
	if (spi.df[FILTERFILE].fp)
		initExclusions(spi.df[FILTERFILE].fp);
	read_all_data(&pi,&spi,sub,&nsub,names,comments,func_weight);
if (spi.use_trios)
{
	if (atoi(comments[0])>22 || toupper(comments[0][0]) == 'X' || toupper(comments[0][0]) == 'Y' ||
		toupper(comments[0][0]) == 'C'&&toupper(comments[0][1]) == 'H'&&toupper(comments[0][2]) == 'R' && (atoi(comments[0] + 3) > 22 || toupper(comments[0][3]) == 'X' || toupper(comments[0][3]) == 'Y'))
	{
		error("Cannot at present use trios for genes on X or Y chromosome", "");
		return 1;
	}
}

if (spi.use_trios)
	{
		assert(new_sub=(subject **)calloc(nsub,sizeof(subject*)));
		for (s=0;s<nsub;++s)
			assert(new_sub[s]=(subject *)calloc(1,sizeof(subject)));
		assert(non_mendelians=(non_mendelian *)calloc(MAX_SUB,sizeof(non_mendelian)));
		if ((n_new_sub=sort_trios(sub,nsub,&pi,new_sub,non_mendelians,&n_non_mendelian,long_line))==0)
			exit(1);
		real_sub=sub;
		sub=new_sub;
		real_nsub=nsub;
		nsub=n_new_sub;
		assert(non_mendelian_report=(char*)malloc(strlen(long_line)+1));
		strcpy(non_mendelian_report,long_line);
	}
	fprintf(spi.df[OUTFILE].fp,"pscoreassoc output\n"
"Locus                   controls     frequency        cases          frequency   frequency allele  weight\n"
"                     AA  :   AB  :  BB                  AA  :   AB  :  BB                     \n");
get_freqs(sub,nsub,&pi,&spi,cc_freq,cc_count,cc_genocount);
applyExclusions(&pi);
set_weights(spi.df[OUTFILE].fp,weight,missing_score,rarer,sub,nsub,&pi,&spi,func_weight,cc_freq,cc_count,max_cc,names,comments);
get_scores(score,weight,missing_score,rarer,sub,nsub,&pi,&spi);
p=do_score_onetailed_ttest(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer);
if (spi.df[SCOREFILE].fp)
	write_scores(spi.df[SCOREFILE].fp,sub,nsub,score);
if (spi.do_recessive_test)
{
if (atoi(comments[0]+3)>22 || toupper(comments[0][4])=='X' || toupper(comments[0][4])=='Y')
// simple trick for now to avoid X and Y genes
	fprintf(spi.df[OUTFILE].fp,"\nCannot do recessive test for genes on X or Y chromosome.\n");
else if (spi.use_haplotypes)
	do_recessive_HWE_test_with_haplotypes(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer,names);
else 
	do_recessive_HWE_test(spi.df[OUTFILE].fp,score,sub,nsub,&pi,&spi,cc_freq,cc_count,max_cc,weight,missing_score,rarer,names);
}

stateExclusions(spi.df[OUTFILE].fp);
printf("\nProgram run completed OK\n");
return 0;

}
Beispiel #5
0
void do_highscores(int score)
{
   read_scores();

   ResourceManager& rm = ResourceManager::getInstance();
   Input *input = (Input *)rm.getData(RES_INPUT);
   ALLEGRO_FONT *sm_font = (ALLEGRO_FONT *)rm.getData(RES_SMALLFONT);
   ALLEGRO_FONT *big_font = (ALLEGRO_FONT *)rm.getData(RES_LARGEFONT);
   
   bool is_high = score >= highScores[NUM_SCORES-1].score;
   bool entering = is_high;
   double bail_time = al_get_time() + 8;
   int letter_num = 0;
   char name[4] = "   ";
   int character = 0;
   double next_input = al_get_time();
   double spin_start = 0;
   int spin_dir = 1;

   for (;;) {
      /* Catch close button presses */
      ALLEGRO_EVENT_QUEUE *events = ((DisplayResource *)rm.getResource(RES_DISPLAY))->getEventQueue();
      while (!al_is_event_queue_empty(events)) {
         ALLEGRO_EVENT event;
         al_get_next_event(events, &event);
#ifdef ALLEGRO_IPHONE
         if (event.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING || event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
            switch_game_out(event.type == ALLEGRO_EVENT_DISPLAY_HALT_DRAWING);
         }
         else if (event.type == ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING || event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN) {
            switch_game_in();
         }
#else
         if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
            exit(0);
#endif
      }

      input->poll();
      
      if (entering && al_get_time() > next_input) {
         float lr = input->lr();

         if (lr != 0) {
            if (lr < 0) {
               character--;
               if (character < 0)
                  character = 25;
               spin_dir = 1;
            }
            else if (lr > 0) {
               character++;
               if (character >= 26)
                  character = 0;
               spin_dir = -1;
            }
            next_input = al_get_time()+0.2;
            my_play_sample(RES_FIRESMALL);
            spin_start = al_get_time();
         }

         if (input->b1() && letter_num < 3) {
            name[letter_num] = 'A' + character;
            letter_num++;
            if (letter_num >= 3) {
               entering = false;
               bail_time = al_get_time()+8;
               insert_score(name, score);
               write_scores();
            }
            next_input = al_get_time()+0.2;
            my_play_sample(RES_FIRELARGE);
         }
      }
      else if (!entering) {
         if (al_get_time() > bail_time) {
            return;
         }
         else if (input->b1() && al_get_time() > next_input) {
            al_rest(0.250);
            return;
         }
      }

      al_rest(0.010);

#ifdef ALLEGRO_IPHONE
      if (switched_out) {
         continue;
      }
#endif

      al_clear_to_color(al_map_rgb(0, 0, 0));

      if (entering) {
         float a = ALLEGRO_PI*3/2;
         float ainc = ALLEGRO_PI*2 / 26;
         double elapsed = al_get_time() - spin_start;
         if (elapsed < 0.1) {
            a += (elapsed / 0.1) * ainc * spin_dir;
         }
         float scrh = BB_H / 2 - 32;
         float h = al_get_font_line_height(sm_font);
         for (int i = 0; i < 26; i++) {
            int c = character + i;
            if (c >= 26)
               c -= 26;
            char s[2];
            s[1] = 0;
            s[0] = 'A' +  c;
            int x = BB_W/2 + (cos(a) * scrh) - al_get_text_width(sm_font, s);
            int y = BB_H/2 + (sin(a) * scrh) - h/2;
            al_draw_textf(sm_font, i == 0 ? al_map_rgb(255, 255, 0) : al_map_rgb(200, 200, 200), x, y, 0, "%s", s);
            a += ainc;
         }
         char tmp[4] = { 0, };
         for (int i = 0; i < 3 && name[i] != ' '; i++) {
            tmp[i] = name[i];
         }
         al_draw_textf(big_font, al_map_rgb(0, 255, 0), BB_W/2, BB_H/2-20, ALLEGRO_ALIGN_CENTRE, "%s", tmp);
         al_draw_text(sm_font, al_map_rgb(200, 200, 200), BB_W/2, BB_H/2-20+5+al_get_font_line_height(big_font), ALLEGRO_ALIGN_CENTRE, "high score!");
      }
      else {
         int yy = BB_H/2 - al_get_font_line_height(big_font)*NUM_SCORES/2;
         for (int i = 0; i < NUM_SCORES; i++) {
            al_draw_textf(big_font, al_map_rgb(255, 255, 255), BB_W/2-10, yy, ALLEGRO_ALIGN_RIGHT, "%s", highScores[i].name);
            al_draw_textf(big_font, al_map_rgb(255, 255, 0), BB_W/2+10, yy, ALLEGRO_ALIGN_LEFT, "%d", highScores[i].score);
            yy += al_get_font_line_height(big_font);
         }
      }

#ifdef ALLEGRO_IPHONE
      input->draw();
#endif

      al_flip_display();
   }
}
Beispiel #6
0
int
main (int argc, char **argv)
{
  int c;
  bool running_suid, running_sgid;
  void *lockstate;
  char *scorefile;
  char *end, *nl, *user, *data;
  const char *prefix, *user_prefix = NULL;
  struct score_entry *scores;
  struct score_entry newscore;
  bool reverse = false;
  ptrdiff_t scorecount, scorealloc;
  ptrdiff_t max_scores = MAX_SCORES;

  srand (time (0));

  while ((c = getopt (argc, argv, "hrm:d:")) != -1)
    switch (c)
      {
      case 'h':
	usage (EXIT_SUCCESS);
	break;
      case 'd':
	user_prefix = optarg;
	break;
      case 'r':
	reverse = 1;
	break;
      case 'm':
	{
	  intmax_t m = strtoimax (optarg, &end, 10);
	  if (optarg == end || *end || m < 0)
	    usage (EXIT_FAILURE);
	  max_scores = min (m, MAX_SCORES);
	}
	break;
      default:
	usage (EXIT_FAILURE);
      }

  if (argc - optind != 3)
    usage (EXIT_FAILURE);

  running_suid = (getuid () != geteuid ());
  running_sgid = (getgid () != getegid ());
  if (running_suid && running_sgid)
    lose ("This program can run either suid or sgid, but not both.");

  prefix = get_prefix (running_suid || running_sgid, user_prefix);

  scorefile = malloc (strlen (prefix) + strlen (argv[optind]) + 2);
  if (!scorefile)
    lose_syserr ("Couldn't allocate score file");

  char *z = stpcpy (scorefile, prefix);
  *z++ = '/';
  strcpy (z, argv[optind]);

  newscore.score = normalize_integer (argv[optind + 1]);
  if (! newscore.score)
    {
      fprintf (stderr, "%s: Invalid score\n", argv[optind + 1]);
      return EXIT_FAILURE;
    }

  user = get_user_id ();
  if (! user)
    lose_syserr ("Couldn't determine user id");
  data = argv[optind + 2];
  if (strlen (data) > MAX_DATA_LEN)
    data[MAX_DATA_LEN] = '\0';
  nl = strchr (data, '\n');
  if (nl)
    *nl = '\0';
  newscore.user_data = malloc (strlen (user) + 1 + strlen (data) + 1);
  if (! newscore.user_data
      || sprintf (newscore.user_data, "%s %s", user, data) < 0)
    lose_syserr ("Memory exhausted");

  if (lock_file (scorefile, &lockstate) < 0)
    lose_syserr ("Failed to lock scores file");

  if (read_scores (scorefile, &scores, &scorecount, &scorealloc) < 0)
    {
      unlock_file (scorefile, lockstate);
      lose_syserr ("Failed to read scores file");
    }
  if (push_score (&scores, &scorecount, &scorealloc, &newscore) < 0)
    {
      unlock_file (scorefile, lockstate);
      lose_syserr ("Failed to add score");
    }
  sort_scores (scores, scorecount, reverse);
  /* Limit the number of scores.  If we're using reverse sorting, then
     also increment the beginning of the array, to skip over the
     *smallest* scores.  Otherwise, just decrementing the number of
     scores suffices, since the smallest is at the end. */
  if (scorecount > max_scores)
    {
      if (reverse)
	scores += scorecount - max_scores;
      scorecount = max_scores;
    }
  if (write_scores (scorefile, running_sgid ? 0664 : 0644,
		    scores, scorecount) < 0)
    {
      unlock_file (scorefile, lockstate);
      lose_syserr ("Failed to write scores file");
    }
  if (unlock_file (scorefile, lockstate) < 0)
    lose_syserr ("Failed to unlock scores file");
  exit (EXIT_SUCCESS);
}