Exemple #1
0
char player_getc(player *p){
    char ch;
    int resp = 0;
    size_t i;

    /* 
    * TODO: make this configurable
    * 60/3*8 = (min/h)/(socket timeout)(8 hours) = 160 number of iterations
    * before you get to 8 hours, no one needs to  spectate for 8 hours
    */
    for(i=0; i<160; i++){
      if ( read(p->fd, &ch, 1) != -1 ) return ch;

      if ( errno != EAGAIN  ){
        /* something bad? */
        server_log(ERROR, "%s:%d [player_getc] Timeout passed and socketerr for player \"%s\":%p:", 
          __FILE__, __LINE__,
          (p->name != NULL) ? p->name : "", p
        );
        return 0x00;
      }

      /* please respond */
      player_write(p, 
        "\xff\xfb\xf6" /* IAC WILL AYT*/
      ); 

      if ( read(p->fd, &resp, 3) < 0 )
        return 0x00; /* enough waiting */

      /* did they answer politely? */
      if ( resp != 0xf6feff ){
        server_log(ERROR, "%s:%d [player_getc] Player \"%s\":%p did not respond to AYT properly (resp = 0x%0x)!", 
          __FILE__, __LINE__,
          (p->name != NULL) ? p->name : "", p, resp
        );
        return 0x00;
      }
    }
    server_log(ERROR, "%s:%d [player_getc] Player \"%s\":%p was hanging out far too long %zu iterations", 
      __FILE__, __LINE__,
      (p->name != NULL) ? p->name : "", p,
      i
    );

    return 0x00;
}
Exemple #2
0
void destroy_player(player *p){
  /*
   * called twice, per snake to exit, once per each thread first call marks
   * snake as dead, second call cleans up the mess
   *
   * only called once for spectators and bots
  */

  /* it is a snake, and is alive. kill it and exit thread */
  if ( ( p->flags & ( DEAD | SPECTATOR ) ) == 0 ){
    server_log(DEBUG, "part1 destroying player %p:%s", p,p->name);
    pthread_mutex_lock(&p->lock);
    p->flags |= DEAD;

    /* get a response to kill controle thread if it is still there*/
    player_write(p, 
      "\xff\xfb\xf6" /* IAC WILL AYT*/
    ); 

    pthread_mutex_unlock(&p->lock);
    pthread_exit(0);
    return;
  }

  /* snake is dead...
   * you are the only one who remembers him
   * clean him up and exit
   * it is what he would of wanted.
   * at least he won't be in our memory
  */

  if ( p->score > 0 ){
      server_log(INFO, "Player \"%s\" %p %s:%d scored %d", 
        p->name, p,
        inet_ntoa(p->addr.sin_addr), ntohs(p->addr.sin_port), 
        p->score
      );
    if ( serv_check_highscore(p) )
      serv_notify_all(p->color, "NEW HIGH SCORE %s: %d", p->name, p->score);
  }else{
    if ( p->flags & SPECTATOR )
      serv_notify_all(1, "A watcher has decided to leave...");
  }

  if ( p->name == NULL ) 
    server_log(FATAL, "%s line %d p->name == NULL", __FILE__, __LINE__);
  
  pthread_mutex_destroy(&p->lock);

  /* delete player from server list */
  if ( serv_del_player(p) == -1 && serv_get_flags() & RANDOM_MODES  )
    serv_set_flags( serv_get_flags() & ( ~ ALL_MODES  ) );

  if ( p->flags & BOT )
    server_log(INFO, "FILTHY STINKING BOT %p:%s destroyed", p, p->name);
  else if ( p->flags & SPECTATOR )
    server_log(INFO, "Spectator %p:%s destroyed", p, p->name);
  else
    server_log(INFO, "Player %p:%s destroyed", p, p->name);

  free(p->name);
  close(p->fd);
  free(p->pix);
  free(p);

  pthread_exit(0);
}
Exemple #3
0
int make_charactor(struct player *p)
{
	char buf[1024];
	char *name = p->name;
	int *age = &(p->age);
	int *sex = &(p->sex);
	int *job = &(p->job);
	
	fflush(stdin);
	
	// 所持金を 0 に初期化
	p->money = 0;
	// レベルを 1 に初期化
	p->level = 1;
	
	/* 名前の入力 */
	printf("キャラクターを作成します\n");
	printf("名前を教えてね♪ > ");
	fgets(buf, sizeof(buf), stdin);
	strncpy(name, buf, strlen(buf) - 1);
	name[strlen(buf) -1] = '\0';

	printf("%s かぁ〜!!! いい名前だね♪\n", name);

	/* 年齢の入力 */
	for(;;) {
		printf("何歳なのっっ♪ > ");
		fgets(buf, sizeof(buf), stdin);
		*age = atoi(buf);
		if(*age > 0 && *age < 130) break;
		printf("いやいや、%dとかありえないでしょー\n秘密にするから私にだけ教えて!\n", *age);
	}

	printf("%d歳なんだー\n", *age);
	
	/* 性別の入力 */
	for(;;) {
		printf("どっちかなぁ?\n 1:男 2:女の子 > ");
		fgets(buf, sizeof(buf), stdin);
		*sex = atoi(buf);
		if(*sex == 1 || *sex == 2) break;
		printf("ごめんね!よく聞こえなかった、もう一度教えて!!\n");
	}

	/* 職業の入力 */
	for(;;) {
		printf("職業をえらんで\n"
			"\t1: 勇者\n"
			"\t2: 魔王(シューベルト作)\n"
			"\t3: 徳川家光\n"
			"\t4: 学生(のふりをしたスパイ)\n"
			"\t5: 勇者に憧れたナイト\n"
			"\t6: たこ焼き職人\n"
			"\t7: スーパーハッカー藤原(スパコン84台所持)\n"
			"\t8: 狂気な目をした鳥(以下略)\n"
			"\t9: たこ焼き(そのもの)\n"
			"\t10: 親の脛をかじり続けて早24年の独身男性(ps.童貞)。\n"
			"\t11: 会社員(課長になりたいと願い続けて13年)のようなニート\n"
			"\t12: 脱サラしてコンビニの店長になったけど病気を患ってやめて、今はニート\n"
			"\t0: 松佐(さまつ)川 航\n");
		fgets(buf, sizeof(buf), stdin);
		*job = atoi(buf);
		if(*job >= 0 && *job <= 12) break;
		printf("選択は無効です(^ ^;;\n再入力してください\n");
	}

	player_write(*p);
	
	return 0;
}