Beispiel #1
0
/*
 * Send C_FLASH text to everyone.
 * Format text to send using printf-style @format and optional
 * arguments.  It is assumed to be plain ASCII.
 * Prefix text it with a header suitable for broadcast from deity.
 * Initiate an output queue flush, but do not wait for it to complete.
 */
void
pr_wall(char *format, ...)
{
    time_t now;
    struct tm *tm;
    char buf[4096];		/* UTF-8 */
    int n;
    struct player *p;
    va_list ap;

    time(&now);
    tm = localtime(&now);
    n = sprintf(buf, "BROADCAST from %s @ %02d:%02d: ",
		getnatp(0)->nat_cnam, tm->tm_hour, tm->tm_min);

    va_start(ap, format);
    (void)vsprintf(buf + n, format, ap);
    va_end(ap);
    for (p = player_next(NULL); p; p = player_next(p)) {
	if (p->state != PS_PLAYING)
	    continue;
	pr_player(p, C_FLASH, buf);
	io_output(p->iop, 0);
    }
}
Beispiel #2
0
static int e_player_next(GtkWidget *widget, GdkEventButton *event, void *data)
{
	if(player_next() < 0)
	{
		/* fail */
		return TRUE;
	}

	/* refresh ui info */
	ui_update(NULL);

	return TRUE;
}
Beispiel #3
0
void
mlrate_errors(FILE *fdiag)
{
  player_t p;
  piter_t piter;

  player_start(&piter);
  while ((p = player_next(&piter)) != NO_PLAYER)
    if (player_get_rated(p) && !player_get_ignore(p))
    {
      irank_t r = player_get_rank(p);
      double plevel;
      irank_t low, lowleft, lowright, high, highleft, highright;

      lowright = highleft = r;
      plevel = 1.0/log(player_get_ratedgames(p) + 1.0) - players_P(p, r);
      lowleft = RANK_MINIMUM;
      highright = RANK_MAXIMUM;

      do {
	low = (lowleft + lowright)/2;
	high = (highleft + highright)/2;

	if (players_P(p, low) + plevel > 0)
	  lowright = low;	/* Root's somewhere to the left. */
	else
	  lowleft = low;	/* Root's somewhere to the right. */
	if (players_P(p, high) + plevel < 0)
	  highright = high;	/* Root's somewhere to the left. */
	else
	  highleft = high;	/* Root's somewhere to the right. */

      } while (lowright - lowleft > CLOSE_ENOUGH &&
	       highright - highleft > CLOSE_ENOUGH);

      player_set_low(p, low-r);
      player_set_high(p, high-r);
    }
}
Beispiel #4
0
int main(void)
{
     char player1 = CROSS;
     char player2 = NOUGHT;

     char winner;
     char currnt_player = player1;
     char board[NUM_ALL];
     char *br;
     bool ended = false;
     int nrounds = 0;
     int mode;

     int wplayer1 = 0;
     int wplayer2 = 0;
     int nreplays = 0;
     int nstalemates = 0;


     mode = select_mode();
     if (mode == 1) {
        max_depth = set_diff();
     }
     while (!ended) {

        if (nrounds == 0) {
           for (int i = 0; i < NUM_ALL; ++i) {
              board[i] = ' ';
            }
            br = &board[0];
            simulate(br);
        }
        if (mode == 1) {
           if (currnt_player == player1) {
              human_turn(br, currnt_player, &nrounds);
           } else {
              computer_turn(br, player1, currnt_player, &nrounds);
           }
        } else {
           if (currnt_player == player1) {
              human_turn(br, currnt_player, &nrounds);
           } else {
              human_turn(br, currnt_player, &nrounds);
           }
        }

        currnt_player = player_next(currnt_player, player1, player2);
        simulate(br);
        winner = game_winner(br, player1, player2);
        ended = game_ended(winner, nrounds);

        if (ended) {
           ++nreplays;
           if (winner == player1) {
              ++wplayer1;
           } else if (winner == player2) {
              ++wplayer2;
           } else {
              ++nstalemates;
           }
           rover_stats(winner, player1, player2, wplayer1,
                       wplayer2, nreplays, nstalemates, mode);
           if (restart()) {
              if (winner == CROSS) {
                 if (player1 == CROSS) {
                    currnt_player = player1;
                 } else {
                    currnt_player = player2;
                 }
              } else if (winner == NOUGHT) {
                 if (player1 == NOUGHT) {
                    player1 = CROSS;
                    player2 = NOUGHT;
                    currnt_player = player1;
                 } else {
                    player1 = NOUGHT;
                    player2 = CROSS;
                    currnt_player = player2;
                 }
              } else {
                 if (player1 == CROSS) {
                    currnt_player = player1;
                 } else {
                    currnt_player = player2;
                 }
              }
              nround_announce(winner, player1, player2, currnt_player);
              nrounds = 0;
              winner = NO_MATCH;
              ended = false;
           } else {
              gover_stats(wplayer1, wplayer2, nreplays, nstalemates, mode);
           }
        }
     }
     return 0;
}
Beispiel #5
0
/* the menu logic for the ncurses interface, much simpler than it looks...
 */
void ninterface()
{
	menu_parameters artists_menu, albums_menu, tracks_menu;

	char *sections[] = {"Artists", "Albums", "Tracks" };  /* Section titles, used in the menu */

	artist art;
	album alb;
	track song;
	list np;

	int art_choice, alb_choice, trak_choice;

	/* ---===[ ARTISTS MENU ]===--- */
	artists_menu = (menu_parameters)malloc(sizeof(struct menu_parameters_str));
	artists_menu->list = get_artist_list();
	artists_menu->list_len = num_artists;
	artists_menu->title = "Artists:";
	artists_menu->sections = sections;
	artists_menu->num_sections = 3;
	artists_menu->curr_section = 1;
	artists_menu->commands = COMMANDS;
	artists_menu->select = 0;
	artists_menu->scroll = 0;
	artists_menu->height = LINES;
	artists_menu->width = COLS;

	while(1) {
		art_choice = nmenu(artists_menu);  /* draw artist menu */
		artists_menu->select = art_choice;

		if(artists_menu->entered == QUIT_BACK)  /* exit artist menu (and function) */
			break;
		else if(artists_menu->entered == APPEND_PLAYLIST || artists_menu->entered == REPLACE_PLAYLIST) {  /* play an artist (XMMS2) */
			if(artists_menu->entered == REPLACE_PLAYLIST) {
				player_stop();
				player_clear_playlist();
			}

			art = ((artist)(lookup(artists_menu->list[art_choice])->contents));

			player_add_artist(art);

			player_play();
		} else if(artists_menu->entered == TOGGLE_PLAY_PAUSE) {
			player_toggle();
		} else if(artists_menu->entered == NOWPLAYING) {
			now_playing();
		} else if(artists_menu->entered == NEXT) {
			player_next();
		} else if(artists_menu->entered == PREVIOUS) {
			player_previous();
		}

		/* ---===[ ALBUMS MENU ]===--- */
		else if(artists_menu->entered == '\n') { /* enter this menu */
			art = ((artist)(lookup(artists_menu->list[art_choice])->contents));

			albums_menu = (menu_parameters)malloc(sizeof(struct menu_parameters_str));
			albums_menu->list = get_album_list(art);
			albums_menu->list_len = art->num_albums;
			albums_menu->title = art->name;
			albums_menu->sections = sections;
			albums_menu->num_sections = 3;
			albums_menu->curr_section = 2;
			albums_menu->commands = COMMANDS;
			albums_menu->select = 0;
			albums_menu->scroll = 0;
			albums_menu->height = LINES;
			albums_menu->width = COLS;

			while(1) {
				alb_choice = nmenu(albums_menu);  /* draw album menu */

				if(albums_menu->entered == KEY_LEFT || albums_menu->entered == QUIT_BACK) /* exit albums menu */
					break;
				else if(albums_menu->entered == APPEND_PLAYLIST || albums_menu->entered == REPLACE_PLAYLIST)  {  /* play an album (XMMS2) */
					if(albums_menu->entered == REPLACE_PLAYLIST) {
						player_stop();
						player_clear_playlist();
					}


					for(np = art->albums; np != NULL; np = np->next)
						if(strcmp(albums_menu->list[alb_choice], np->name) == 0)
							break;

					alb = (album)np->contents;


					player_add_album(alb);
					player_play();
				} else if(albums_menu->entered == TOGGLE_PLAY_PAUSE) {
					player_toggle();
				} else if(albums_menu->entered == NOWPLAYING) {
					now_playing();
				} else if(albums_menu->entered == NEXT) {
					player_next();
				} else if(albums_menu->entered == PREVIOUS) {
					player_previous();
				}

				/* ---===[ TRACKS MENU ]===--- */
				else if(albums_menu->entered == '\n') { /* enter this menu */
					for(np = art->albums; np != NULL; np = np->next)
						if(strcmp(albums_menu->list[alb_choice], np->name) == 0)
							break;

					alb = (album)np->contents;

					tracks_menu = (menu_parameters)malloc(sizeof(struct menu_parameters_str));
					tracks_menu->list = get_track_list(alb);
					tracks_menu->list_len = alb->num_songs;
					tracks_menu->title = alb->name;
					tracks_menu->sections = sections;
					tracks_menu->num_sections = 3;
					tracks_menu->curr_section = 3;
					tracks_menu->commands = COMMANDS;
					tracks_menu->select = 0;
					tracks_menu->scroll = 0;
					tracks_menu->height = LINES;
					tracks_menu->width = COLS;

					while(1) {
						trak_choice = nmenu(tracks_menu);  /* draw track menu */

						/* play a track (XMMS2) */
						if(tracks_menu->entered == APPEND_PLAYLIST || tracks_menu->entered == REPLACE_PLAYLIST) {  /* play a track (XMMS2) */
							if(tracks_menu->entered == REPLACE_PLAYLIST) {
								player_stop();
								player_clear_playlist();
							}

							for(song = alb->songs; song != NULL; song = song->next)
								if(strcmp(tracks_menu->list[trak_choice], song->name) == 0)
									break;

							player_add_track(song);
							player_play();
						} else if(tracks_menu->entered == TOGGLE_PLAY_PAUSE) {
							player_toggle();
						} else if(tracks_menu->entered == NOWPLAYING) {
							now_playing();
						} else if(tracks_menu->entered == KEY_LEFT || tracks_menu->entered == QUIT_BACK) { /* exit tracks menu */
							break;
						} else if(tracks_menu->entered == NEXT) {
							player_next();
						} else if(tracks_menu->entered == PREVIOUS) {
							player_previous();
						}
					} /* drop out of tracks here */

					destroy_menu_params(tracks_menu);
				}
			} /* drop out of albums here */

			destroy_menu_params(albums_menu);
		 }
	} /* drop out of artists here */

	destroy_menu_params(artists_menu);

	return;
}
Beispiel #6
0
void
mlrate(double mean, FILE *fdiag)
{
  double maxchange;		/* The max change of one turn. */
  size_t pcount, gcount, rcount; /* Player, game, and removed counters. */
  size_t wcount, lcount;	/* Win/loss counters. */
  double wsum, lsum;		/* Weighted sums. */
  size_t globturns = 0;		/* Counts the turns of the outer loop. */
  size_t hcount[10];		/* Handicap game counters. */
  player_t p;			/* A player. */
  piter_t piter;		/* An iterator over players. */
  char *pflags;

  assert(0.0 < mean && mean < RANK_MAXIMUM);

  pflags = (char *)malloc(player_count());
  if (pflags == NULL)
    errex("malloc(%lu) failed", player_count());
  memset(pflags, 0, player_count());

  /* Assign a start rating for every player. */
  player_start(&piter);
  while ((p = player_next(&piter)) != NO_PLAYER)
    if (!player_get_rated(p) && !player_get_ignore(p))
    {
      player_set_rank(p, mean);
      player_set_rated(p, 1);
    }

  /* Remove players with no wins or no losses against other rated 
  ** players; then again and again, until no more can be removed.
  ** While we're at it, count some statistics as well.
  */
  do {
    pcount = gcount = rcount = 0;
    hcount[0] = hcount[1] = hcount[2] = hcount[3] = hcount[4] =
      hcount[5] = hcount[6] = hcount[7] = hcount[8] = hcount[9] = 0;
    player_start(&piter);
    while ((p = player_next(&piter)) != NO_PLAYER)
      if (player_get_rated(p) && !player_get_ignore(p))
      {
	game_t g;
	giter_t giter;
	unsigned oppcount = 0;

	wcount = lcount = 0;
	wsum = lsum = 0.0;

	player_games_start(p, &giter);
	while ((g = player_games_next(&giter)) != NO_GAME)
	  if (game_weight(g) > 0.0)
	  {
	    double a = game_advantage(g);

	    if (a >= 10.0)
	      hcount[0] += 1;
	    else if (a >= 1.0)
	      hcount[(unsigned)floor(a)] += 1;
	    if (p == game_winner(g) && player_get_rated(game_loser(g)))
	    {
	      wcount += 1;
	      wsum += game_weight(g);
	      if (!pflags[game_loser(g)])
	      {
		pflags[game_loser(g)] = 1;
		oppcount += 1;
	      }
	    }
	    else if (p == game_loser(g) && player_get_rated(game_winner(g)))
	    {
	      lcount += 1;
	      lsum += game_weight(g);
	      if (!pflags[game_winner(g)])
	      {
		pflags[game_winner(g)] = 1;
		oppcount += 1;
	      }
	    }
	    else
	      game_set_weight(g, 0.0);
	  }
	  else
	    game_set_weight(g, 0.0);

	if (wsum < 0.25 || lsum < 0.25 || oppcount < 3)
	{
	  player_set_rated(p, 0);
	  rcount += 1;
	}
	else
	{
	  pcount += 1;
	  gcount += wcount + lcount;
	}
	player_set_ratedgames(p, wcount, lcount);
	player_set_wratedgames(p, wsum, lsum);

	/* Clear flags. */
	player_games_start(p, &giter);
	while ((g = player_games_next(&giter)) != NO_GAME)
	  pflags[game_loser(g)] = pflags[game_winner(g)] = 0;
      }
  } while (rcount > 0);

  player_gc_games();

  if (fdiag)
  {
    int i;

    fprintf(fdiag, "\nRemaining:\n%6lu players\n%6lu games\n\n",
	    (unsigned long)pcount, (unsigned long)gcount/2);
    for (i = 1 ; i <= 9 ; i++)
      fprintf(fdiag, "Advantage   %2d: %5lu games\n",
	      i, (unsigned long)hcount[i]);
    fprintf(fdiag,   "Advantage >=10: %5lu games\n\n",
	    (unsigned long)hcount[0]);
  }
  if (pcount == 0 || gcount == 0)
    errex("No player or no games");

  /*
  ** The outer loop.
  */
  do {	/* while (maxchange > CHANGE_LIMIT && globturns < GLOBAL_TURNS_MAX); */

    int maxp = 0;
    pcount = 0;
    maxchange = 0.0;
    globturns += 1;

    /*
    **  Loop over all players.
    */
    player_start(&piter);
    while ((p = player_next(&piter)) != NO_PLAYER)
    {
      /*
      **  We use bisection to find the root of the derivative (the maximum).
      */
      irank_t r, oldrank;
      irank_t ileft = RANK_MINIMUM, iright = RANK_MAXIMUM;

      if (!player_get_rated(p) || player_get_ignore(p))
	continue;

      /*
      ** Inner (bisection) loop.
      */
      pcount += 1;
      r = oldrank = player_get_rank(p);
      do {			/*  while (iright - ileft > CLOSE_ENOUGH); */
	game_t g;
	double sum = 0.0;
	giter_t giter;

	player_games_start(p, &giter);
	while ((g = player_games_next(&giter)) != NO_GAME)
	{
	  player_t opp;
	  double diff;

	  if (p == game_winner(g))
	  {
	    opp = game_loser(g);

	    if (player_get_rated(opp))
	    {
	      diff = RANK_DIFF(r, player_get_rank(opp))
		+ game_advantage(g);
	      sum += dP(diff, 1) * game_weight(g);
	    }
	  }
	  else
	  {
	    opp = game_winner(g);

	    if (player_get_rated(opp))
	    {
	      diff = RANK_DIFF(r, player_get_rank(opp))
		- game_advantage(g);
	      sum += dP(diff, 0) * game_weight(g);
	    }
	  }
	}

	if (sum > 0.0)
	  iright = r;		/* Root's somewhere to the left. */
	else
	  ileft = r;		/* Root's somewhere to the right. */
	r = (iright + ileft)/2;

      } while (iright - ileft > CLOSE_ENOUGH);

      if (r > oldrank)
      {
	if (r - oldrank > maxchange)
	{
	  maxchange = r - oldrank;
	  maxp = p;
	}
      }
      else
      {
	if (oldrank - r > maxchange)
	{
	  maxchange = oldrank - r;
	  maxp = p;
	}
      }
      player_set_rank(p, r);

    }	/* while ((p = player_next())) */

#ifdef MAXP
    fprintf(stderr, "\n--- Maxp: %s rank=%g ww=%g wl=%g w=%u l=%u rg=%u\n",
	    player_get_name(maxp),
	    player_get_rank(maxp),
	    player_get_wwins(maxp),
	    player_get_wlosses(maxp),
	    player_get_wins(maxp),
	    player_get_losses(maxp),
	    player_get_ratedgames(maxp));
#endif

    if (globturns > 100)
      circular_check(maxp);

    if (fdiag)
    {
      fprintf(fdiag, " %3lu: %6.3f", (unsigned long)globturns, maxchange);
      if (globturns % 5)
	fflush(fdiag);
      else
	fputc('\n', fdiag);
    }

  } while (maxchange > CHANGE_LIMIT && globturns < GLOBAL_TURNS_MAX);

  if (fdiag)
  {
    if (globturns % 5)
      fputc('\n', fdiag);
    fputc('\n', fdiag);
  }
  if (globturns == GLOBAL_TURNS_MAX)
    errex("Aborted after maximum %u turns\n", GLOBAL_TURNS_MAX);

  if (pflags != NULL)
    free(pflags);
}
Beispiel #7
0
static void _read_parse(PlayerBackend * player, char const * buf)
{
	unsigned int u1;
	unsigned int u2;
	gdouble db;
	char str[256];
	time_t t;
	struct tm tm;

	if(sscanf(buf, "ANS_META_ALBUM='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_ALBUM, str);
	}
	else if(sscanf(buf, "ANS_META_ARTIST='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_ARTIST, str);
	}
	else if(sscanf(buf, "ANS_META_COMMENT='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_COMMENT, str);
	}
	else if(sscanf(buf, "ANS_META_GENRE='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_GENRE, str);
	}
	else if(sscanf(buf, "ANS_META_TITLE='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_TITLE, str);
	}
	else if(sscanf(buf, "ANS_META_TRACK='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_TRACK, str);
	}
	else if(sscanf(buf, "ANS_META_YEAR='%255[^'\n]\n", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		player_set_metadata(player->player, PM_YEAR, str);
	}
	else if(sscanf(buf, "ANS_PERCENT_POSITION=%u\n", &u1) == 1)
	{
		player_set_progress(player->player, u1);
		if(u1 == 100)
			player_next(player->player);
	}
	else if(sscanf(buf, "ANS_TIME_POSITION=%lf\n", &db) == 1)
	{
		t = db;
		gmtime_r(&t, &tm);
		strftime(str, sizeof(str), "%H:%M:%S", &tm);
		player_set_metadata(player->player, PM_LENGTH, str);
	}
	else if(sscanf(buf, "ANS_VIDEO_RESOLUTION='%u x %u'\n", &u1, &u2) == 2)
		_playerbackend_set_size(player, u1, u2);
	else if(sscanf(buf, "ID_AUDIO_BITRATE=%u\n", &u1) == 1)
		player->audio_bitrate = u1;
	else if(sscanf(buf, "ID_AUDIO_CODEC=%255[^\n]", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		if(player->audio_codec != NULL)
			free(player->audio_codec);
		player->audio_codec = strdup(str);
	}
	else if(sscanf(buf, "ID_AUDIO_NCH=%u\n", &u1) == 1)
		player->audio_channels = u1;
	else if(sscanf(buf, "ID_AUDIO_RATE=%u\n", &u1) == 1)
		player->audio_rate = u1;
	else if(sscanf(buf, "ID_CLIP_INFO_NAME%u=%255s", &u1, str) == 2)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		if(strcmp(str, "Album") == 0)
			player->album = u1;
		else if(strcmp(str, "Artist") == 0)
			player->artist = u1;
		else if(strcmp(str, "Title") == 0)
			player->title = u1;
	}
	else if(sscanf(buf, "ID_CLIP_INFO_VALUE%u=%255[^\n]", &u1, str) == 2)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		if(player->album >= 0 && (unsigned)player->album == u1)
			player_set_metadata(player->player, PM_ALBUM, str);
		else if(player->artist >= 0 && (unsigned)player->artist == u1)
			player_set_metadata(player->player, PM_ARTIST, str);
		else if(player->title >= 0 && (unsigned)player->title == u1)
			player_set_metadata(player->player, PM_TITLE, str);
		/* FIXME also update the duration */
	}
	else if(sscanf(buf, "ID_LENGTH=%lf\n", &db) == 1)
		player->length = db;
	else if(sscanf(buf, "ID_SEEKABLE=%u\n", &u1) == 1)
		player_set_seekable(player->player, u1 ? TRUE : FALSE);
	else if(sscanf(buf, "ID_VIDEO_ASPECT=%lf\n", &db) == 1)
		player->video_aspect = db;
	else if(sscanf(buf, "ID_VIDEO_BITRATE=%u\n", &u1) == 1)
		player->video_bitrate = u1;
	else if(sscanf(buf, "ID_VIDEO_CODEC=%255[^\n]", str) == 1)
	{
		str[sizeof(str) - 1] = '\0';
		string_rtrim(str, NULL);
		if(player->video_codec != NULL)
			free(player->video_codec);
		player->video_codec = strdup(str);
	}
	else if(sscanf(buf, "ID_VIDEO_FPS=%lf\n", &db) == 1)
		player->video_fps = db;
	else if(sscanf(buf, "ID_VIDEO_HEIGHT=%u\n", &u1) == 1)
		_playerbackend_set_size(player, -1, u1);
	else if(sscanf(buf, "ID_VIDEO_RATE=%u\n", &u1) == 1)
		player->video_rate = u1;
	else if(sscanf(buf, "ID_VIDEO_WIDTH=%u\n", &u1) == 1)
		_playerbackend_set_size(player, u1, -1);
#ifdef DEBUG
	else
		fprintf(stderr, "DEBUG: unknown output \"%s\"\n", buf);
#endif
}