/** * Handle a connection attempt from a player : * - check the crc * - check server/user password (TODO) * - check if the player is already connected (TODO) * - initialize the player, add it to the pool * - notify the player if he has been accepted (or not TODO) * - notify the other players (TODO) * - move the player to the default channel (TODO) * * @param data the connection packet * @param len the length of the connection packet * @param cli_addr the adress of the client * @param cli_len the length of cli_addr */ void handle_player_connect(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len, struct server *s) { struct player *pl, *tmp_pl; char password[30]; char login[30]; struct registration *r; size_t iter; /* Check crc */ if (!packet_check_crc(data, len, 16)) return; /* Check if the IP is banned */ if (get_ban_by_ip(s, cli_addr->sin_addr) != NULL) { server_refuse_connection_ban(cli_addr, cli_len, s); logger(LOG_INFO, "PLAYER BANNED TRIED TO CONNECT"); return; } /* If registered, check if player exists, else check server password */ bzero(password, 30 * sizeof(char)); strncpy(password, data + 121, MIN(29, data[120])); bzero(login, 30 * sizeof(char)); strncpy(login, data + 91, MIN(29, data[90])); pl = new_player_from_data(data, len, cli_addr, cli_len); if (data[90] == 0) { /* no login = anonymous mode */ /* check password against server password */ if (strcmp(password, s->password) != 0) { destroy_player(pl); return; /* wrong server password */ } pl->global_flags = GLOBAL_FLAG_UNREGISTERED; } else { r = get_registration(s, login, password); if (r == NULL) { logger(LOG_INFO, "Invalid credentials for a registered player"); destroy_player(pl); return; /* nobody found with those credentials */ } pl->global_flags |= r->global_flags; pl->global_flags |= GLOBAL_FLAG_REGISTERED; pl->reg = r; } /* Add player to the pool */ add_player(s, pl); /* Send a message to the client indicating he has been accepted */ /* Send server information to the player (0xf4be0400) */ server_accept_connection(pl); /* Send a message to all players saying that a new player arrived (0xf0be6400) */ s_notify_new_player(pl); /* Send the new player the list of all the Voice Requests */ ar_each(struct player *, tmp_pl, iter, s->players) if (pl->player_attributes & PL_ATTR_REQUEST_VOICE) s_notify_player_requested_voice(tmp_pl, pl); ar_end_each; }
void test_handles_draw() { Board* board = draw_board(); Player* p1 = create_player('X', &fake_move); Player* p2 = create_player('O', &fake_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); assert(strstr(writer_log, "Draw")); }
void test_gets_valid_move() { Board* board = draw_board(); unset_move(board, 1); Player* p1 = create_player('X', &incrementing_move); Player* p2 = create_player('O', &fake_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); current_move = 0; assert(strstr(writer_log, "Player 1's turn")); assert(strstr(writer_log, "Invalid move")); assert(strstr(writer_log, "X wins!")); }
void test_handles_win() { Board* board = draw_board(); unset_move(board, 0); Player* p1 = create_player('O', &fake_move); Player* p2 = create_player('X', &fake_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); assert(strstr(writer_log, "1 O X O X O O X O")); assert(strstr(writer_log, "Player 1's turn")); assert(strstr(writer_log, "O O X O X O O X O")); assert(strstr(writer_log, "O wins!")); }
void test_switches_turns() { Board* board = draw_board(); unset_move(board, 0); unset_move(board, 1); Player* p1 = create_player('X', &incrementing_move); Player* p2 = create_player('O', &incrementing_move); start_game(board, p1, p2); destroy_board(board); destroy_player(p1); destroy_player(p2); current_move = 0; assert(strstr(writer_log, "Player 1's turn")); assert(strstr(writer_log, "Player 2's turn")); assert(strstr(writer_log, "Draw")); }
void end_in_game_state( game* _pGame ) { destroy_top_bar( ((in_game_state*)_pGame->p_current_state)->p_top_bar ); unload_image( ((in_game_state*)_pGame->p_current_state)->p_esc_menu_img ); unload_button( ((in_game_state*)_pGame->p_current_state)->p_continue ); unload_button( ((in_game_state*)_pGame->p_current_state)->p_main_menu ); unload_button( ((in_game_state*)_pGame->p_current_state)->p_exit ); destroy_map( ((in_game_state*)_pGame->p_current_state)->p_map ); for ( int i = 0; i < ((in_game_state*)_pGame->p_current_state)->NPC_count; i++ ) destroy_NPC( ((in_game_state*)_pGame->p_current_state)->pp_NPCs[i] ); free( ((in_game_state*)_pGame->p_current_state)->pp_NPCs ); destroy_player( ((in_game_state*)_pGame->p_current_state)->p_player ); destroy_timer( ((in_game_state*)_pGame->p_current_state)->game_ended_timer ); free( _pGame->p_current_state ); _pGame->p_current_state = NULL; }
void player_lock(player *p){ pthread_mutex_lock(&p->lock); if (p->flags & ( DEAD | KILL )){/* IT'S DEAD!.. WILL!!! kill it */ pthread_mutex_unlock(&p->lock); destroy_player(p); } }
void destroy_game(t_game *game) { destroy_player(game->player); LibererMap(game->maps); free(game); game = NULL; }
void clear_roster(Team *team) { int roster_size = sizeof(team->roster) / sizeof(Player); for (int i = 0 ; i < roster_size ; i++){ destroy_player(&team->roster[0]); } }
void destroy_guest(dbref guest) { if(!Wizard(mudconf.guest_nuker) || !Good_obj(mudconf.guest_nuker)) mudconf.guest_nuker = 1; if(!Guest(guest)) return; toast_player(guest); atr_add_raw(guest, A_DESTROYER, tprintf("%d", mudconf.guest_nuker)); destroy_player(guest); destroy_obj(mudconf.guest_nuker, guest); }
void cleanup_outbreak(Outbreak * outbreak) { int i; for (i = 0; i < outbreak->num_blocks; i++) { destroy_block(outbreak->blocks[i]); outbreak->blocks[i] = NULL; } outbreak->num_blocks = 0; destroy_ball(outbreak->ball); outbreak->ball = NULL; destroy_player(outbreak->player); outbreak->player = NULL; }
void destroy_char(Character *ch) { free_str(ch->name); destroy_flags(ch->flags); destroy_flags(ch->affectedBy); free_str(ch->description); free_mem(ch->classes); if (ch->pc != 0) { destroy_player(ch->pc); } if (ch->npc != 0) { destroy_npc(ch->npc); } free_mem(ch); }
void process_players() { player *scan,*su; char *oldstack; for(scan=flatlist_start;scan;scan=scan->flat_next) if ((scan->fd<0) || (scan->flags&PANIC) || (scan->flags&CHUCKOUT)) { oldstack=stack; current_player=scan; if (scan->location && scan->name[0] && !(scan->flags&RECONNECTION)) { sprintf(stack,"%s jumps up into the air and disappears with a loud *POP* !\n",full_name(scan)); stack=end_string(stack); tell_room(scan->location,oldstack); stack=oldstack; save_player(scan); } if (!(scan->flags&RECONNECTION)) { command_type=0; do_inform(scan,"[%s has disconnected] %s"); if (scan->saved && !(scan->flags&NO_SAVE_LAST_ON)) scan->saved->last_on=time(0); } if (sys_flags&VERBOSE) { if (scan->name[0]) sprintf(oldstack,"%s has disconnected.",scan->name); else strcpy(oldstack,"Disconnect from login."); stack=end_string(oldstack); log("connection",oldstack); } destroy_player(scan); sys_flags|=ENFORCE_NO_NEW; for (su=flatlist_start;su;su=su->flat_next) if (su->residency&OFF_DUTY) { sys_flags&=~ENFORCE_NO_NEW; break; } current_player=0; stack=oldstack; } else if (scan->flags&INPUT_READY) { if (!(scan->lagged) && !(scan->flags&PERM_LAG)) { current_player=scan; current_room=scan->location; input_for_one(scan); action="processing players"; current_player=0; current_room=0; #ifdef PC if (scan->flags&PROMPT && scan==input_player) #else if (scan->flags&PROMPT) #endif { if (scan->saved_flags&CONVERSE) do_prompt(scan,scan->converse_prompt); else do_prompt(scan,scan->prompt); } } memset(scan->ibuffer,0,IBUFFER_LENGTH); scan->flags &= ~INPUT_READY; } }
int dispatch(void * data, fdsession_t * fdsess, bool cache_flag) { btlsw_proto_t * pkg = reinterpret_cast<btlsw_proto_t *>(data); uint32_t len = pkg->len; uint16_t cmd = pkg->cmd; uint32_t seq = pkg->seq; int fd = fdsess->fd; uint32_t uid = pkg->id; TRACE_TLOG("dispatch[%u] sender=%u, fd=%u, seq=%u, len=%u, cache_flag=%d", cmd, uid, fd, seq, len, cache_flag); c_online *p_online = get_online_by_fd(fd); if (cmd == btlsw_online_register_cmd) { if (p_online) { /* 注册时, 发现通过fd连过来的online, 重复发来注册的包 */ ERROR_TLOG("dup reg online, fd=%d, olip=0x%X, u=%u", fdsess->fd, fdsess->remote_ip, uid); return -1; } uint32_t online_id = *((uint32_t *)pkg->body); p_online = new c_online(fdsess, online_id); add_online(p_online); DEBUG_TLOG("reg_online, fd=%d, olip=0x%X, olid=%u", fdsess->fd, fdsess->remote_ip, online_id); return 0; } if (!p_online) { /* 到此, 无论是新注册, 还是注册后协议, * 都应该有 online 了, 找不到就是有问题 */ ERROR_TLOG("nofound p_online by fd=%d, olip=0x%X, cmd=%u, u=%u", fd, fdsess->remote_ip, cmd, uid); return -1; } c_player *p_player = p_online->get_player(uid); if (cmd == btlsw_player_enter_hall_cmd) { if (p_player) { DEBUG_TLOG("player re_enter_hall, lastinfo: u=%u, role_tm=%u, olid=%u", p_player->m_role_tm, p_player->m_server_id, p_player->m_server_id); destroy_player(p_player); p_player = NULL; } uint32_t online_id = *((uint32_t *)pkg->body); uint32_t role_tm = *((uint32_t *)(pkg->body + 4)); p_player = alloc_player(p_online, uid, online_id, role_tm); } if (!p_player) { ERROR_TLOG("nofound c_player, cmd=%u, u=%u, olid=%u", cmd, uid, p_online->m_id); return -1; } if (cache_flag && p_player->m_waitcmd) { if (g_queue_get_length(p_player->m_pkg_queue) < MAX_CACHE_PKG) { DEBUG_TLOG("cache a pkg u=%u, cmd=%u, wcmd=%u", p_player->m_id, cmd, p_player->m_waitcmd); cache_a_pkg(p_player, pkg, len); return 0; } else { WARN_TLOG("too many cache pkg, u=%u, cmd=%u, wcmd=%u", p_player->m_id, cmd, p_player->m_waitcmd); return 0; } } p_player->m_waitcmd = cmd; p_player->m_seq = seq; p_player->m_ret = 0; p_player->m_last_pkg_time = get_now_tv()->tv_sec; bind_proto_cmd_t * p_cmd = NULL; if (0 != find_btlsw_cmd_bind(cmd, &p_cmd)) { ERROR_TLOG("btl sw cmdid not existed: %u", cmd); return 0; } uint32_t body_len = len - sizeof(btlsw_proto_t); bool read_ret = p_cmd->p_in->read_from_buf_ex((char *)data + sizeof(btlsw_proto_t), body_len); if (!read_ret) { ERROR_TLOG("read_from_buf_ex error cmd=%u, u=%u", cmd, p_player->m_id); return -1; } int cmd_ret = p_cmd->func(p_player, p_cmd->p_in, p_cmd->p_out, NULL); return cmd_ret; }
static void key_assigner_update(void* data, int status) { key_assigner_t* key = (key_assigner_t*)data; if(status == LOW) return; if(key->assigned == NOT_READY && key->pressed_key != 0) { key->timer += sync_elapsed; if(key->timer >= ACTIVATION_TIME) { int i; key->assigned = WAIT_KEYS; key->timer = (mFloat)0.0; key->timer_ready = (mFloat)0.0; key->timer_unassign = (mFloat)0.0; if(key->type == MOUSE) { key->assigned = CHOOSE; key->selection = KEY_NB; } if(input_set(&player_array[key->player_nb].input, key->sys, key->type)) { player_array[key->player_nb].input.flag = key->sys; if(key->type == MOUSE) { // For the mouse, we guarantee that each signal // will be in the range [1-4] for(i = 0 ; i < KEY_NB ; i++) player_array[key->player_nb].input.key[i] = i + 1; } else { for(i = 0 ; i < KEY_NB ; i++) player_array[key->player_nb].input.key[i] = 0; } } else key->assigned = NOT_READY; } } else if(key->assigned == CHOOSE || key->assigned == READY) { if(key->ready_pressed) { // Don't be able to accept if there's no model to load! if(model_nb_filenames > 0) key->timer_ready += sync_elapsed; } else if(key->unassign_pressed) key->timer_unassign += sync_elapsed; if(key->timer_unassign >= ACTIVATION_TIME) { int i; int players_not_ready = 0; key->assigned = NOT_READY; key->unassign_pressed = 0; key->timer_unassign = (mFloat)0.0; for(i = 0 ; i < KEY_NB ; i++) player_array[key->player_nb].input.key[i] = 0; for(i = 0 ; i < MAX_PLAYERS ; i++) player_array[i].box_force = 1; // This function sets 'ready' to 0 itself. destroy_player(&player_array[key->player_nb]); for(i = 0 ; i < MAX_PLAYERS ; i++) { if(!player_array[i].ready) players_not_ready++; } if(players_not_ready == MAX_PLAYERS) { dont_draw = 1; dont_compute = 1; } } else { if(key->assigned == CHOOSE) { if(key->timer_ready >= ACTIVATION_TIME) { int i; if(load_model(key)) { key->assigned = READY; player_array[key->player_nb].ready = 1; } key->timer_ready = (mFloat)0.0; key->ready_pressed = 0; // Just below, we compute something modulo // key->nb_frames, which raises an exception! key->nb_frames = 1; for(i = 0 ; i < MAX_PLAYERS ; i++) player_array[i].box_force = 1; } } // The state could have gone to 'Ready' by the // code above. if(key->assigned == READY) { key->animation_time += sync_elapsed; if(key->animation_time >= key->frame_time) { key->frame_nb = (key->frame_nb + 1) % key->nb_frames; key->animation_time = (mFloat)0.0; } if(dont_draw) { int players_ready = 0; int i; for(i = 0 ; i < MAX_PLAYERS ; i++) { if(((key_assigner_t*)container_assigner[i].data.data)->assigned == READY || ((key_assigner_t*)container_assigner[i].data.data)->assigned == NOT_READY) players_ready++; } if(players_ready == MAX_PLAYERS) client_join_local(NULL); } } } } }
void process_players() { player *scan; char *oldstack; for (scan = flatlist_start; scan; scan = scan->flat_next) { if (scan->flat_next) if (((player *)scan->flat_next)->flat_previous != scan) { raw_wall("\n\n -=> Non-terminated flatlist <=-\n\n"); raw_wall("\n\n -=> Dumping end off of list <=-\n\n"); scan->flat_next=NULL; } if ((scan->fd < 0) || (scan->flags & PANIC) || (scan->flags & CHUCKOUT)) { oldstack = stack; current_player = scan; if (scan->location && scan->name[0] && !(scan->flags & RECONNECTION)) { sprintf(stack, " %s suddenly dissolve%s into a squintillion dots" " that quickly disperse.\n", scan->name, single_s(scan)); stack = end_string(stack); tell_room(scan->location, oldstack); stack = oldstack; save_player(scan); } if (!(scan->flags & RECONNECTION)) { command_type = 0; if (scan->gender==PLURAL) do_inform(scan, "[%s have disconnected] %s"); else do_inform(scan, "[%s has disconnected] %s"); if (scan->saved && !(scan->flags & NO_SAVE_LAST_ON)) scan->saved->last_on = time(0); } if (sys_flags & VERBOSE || scan->residency == 0) { if (scan->name[0]) sprintf(oldstack, "%s has disconnected from %s", scan->name, scan->inet_addr); else sprintf(oldstack, "Disconnect from login. [%s]", scan->inet_addr); stack = end_string(oldstack); log("newconn", oldstack); } destroy_player(scan); current_player = 0; stack = oldstack; } else if (scan->flags & INPUT_READY) { /* there used to be this here... if (!(scan->lagged) && !(scan->flags & PERM_LAG)) for reference... */ if (!(scan->lagged)) { current_player = scan; current_room = scan->location; input_for_one(scan); action = "processing players"; current_player = 0; current_room = 0; #ifdef PC if (scan->flags & PROMPT && scan == input_player) #else if (scan->flags & PROMPT) #endif { if (scan->saved_flags & CONVERSE) do_prompt(scan, scan->converse_prompt); else do_prompt(scan, scan->prompt); } } memset(scan->ibuffer, 0, IBUFFER_LENGTH); scan->flags &= ~INPUT_READY; } } }