int main() { GameState *level = new_game_state(); add_player (level, new_player("mizu")); add_player (level, new_player("zeroeth")); add_player (level, new_player("haiiro")); add_player (level, new_player("takeshi")); player_report (level); }
void delete_player(dbref who) { int result; result = free_hash(NAME(who), player_list, PLAYER_HASH_SIZE); if (result) { int i; wall_wizards("## WARNING: Playername hashtable is inconsistent. Rebuilding it."); clear_players(); for (i = db_top; i-->0; ) { if (Typeof(i) == TYPE_PLAYER) { add_player(i); } } result = free_hash(NAME(who), player_list, PLAYER_HASH_SIZE); if (result) { wall_wizards("## WARNING: Playername hashtable still inconsistent after rebuild."); } } return; }
dbref create_player(const char *name, const char *password) { dbref player; if (!ok_player_name(name) || !ok_password(password)) return NOTHING; /* else he doesn't already exist, create him */ player = new_object(); /* initialize everything */ NAME(player) = alloc_string(name); DBFETCH(player)->location = tp_player_start; /* home */ FLAGS(player) = TYPE_PLAYER | PCREATE_FLAGS; OWNER(player) = player; DBFETCH(player)->sp.player.home = tp_player_start; DBFETCH(player)->exits = NOTHING; DBFETCH(player)->sp.player.pennies = tp_start_pennies; DBFETCH(player)->sp.player.password = NULL; // handle this last DBFETCH(player)->sp.player.curr_prog = NOTHING; DBFETCH(player)->sp.player.insert_mode = 0; /* link him to tp_player_start */ PUSH(player, DBFETCH(tp_player_start)->contents); add_player(player); DBDIRTY(player); DBDIRTY(tp_player_start); set_password(player, password); return player; }
static dbref make_player(const char *name, const char *password, const char *host, const char *ip) { dbref player; char temp[SBUF_LEN]; char *flaglist, *flagname; char flagbuff[BUFFER_LEN]; player = new_object(); /* initialize everything */ set_name(player, name); Location(player) = PLAYER_START; Home(player) = PLAYER_START; Owner(player) = player; Parent(player) = NOTHING; Type(player) = TYPE_PLAYER; Flags(player) = new_flag_bitmask("FLAG"); strcpy(flagbuff, options.player_flags); flaglist = trim_space_sep(flagbuff, ' '); if (*flaglist != '\0') { while (flaglist) { flagname = split_token(&flaglist, ' '); twiddle_flag_internal("FLAG", player, flagname, 0); } } if (Suspect_Site(host, player) || Suspect_Site(ip, player)) set_flag_internal(player, "SUSPECT"); set_initial_warnings(player); /* Modtime tracks login failures */ ModTime(player) = (time_t) 0; (void) atr_add(player, pword_attr, password_hash(password, NULL), GOD, 0); giveto(player, START_BONUS); /* starting bonus */ (void) atr_add(player, "LAST", show_time(mudtime, 0), GOD, 0); (void) atr_add(player, "LASTSITE", host, GOD, 0); (void) atr_add(player, "LASTIP", ip, GOD, 0); (void) atr_add(player, "LASTFAILED", " ", GOD, 0); snprintf(temp, sizeof temp, "%d", START_QUOTA); (void) atr_add(player, "RQUOTA", temp, GOD, 0); (void) atr_add(player, "MAILCURF", "0", GOD, AF_LOCKED | AF_NOPROG | AF_WIZARD); add_folder_name(player, 0, "inbox"); /* link him to PLAYER_START */ PUSH(player, Contents(PLAYER_START)); add_player(player); add_lock(GOD, player, Basic_Lock, parse_boolexp(player, "=me", Basic_Lock), LF_DEFAULT); add_lock(GOD, player, Enter_Lock, parse_boolexp(player, "=me", Basic_Lock), LF_DEFAULT); add_lock(GOD, player, Use_Lock, parse_boolexp(player, "=me", Basic_Lock), LF_DEFAULT); current_state.players++; local_data_create(player); return player; }
/** Add a player's alias list to the player list htab. * \param player dbref of player to add. * \param alias list of names ot use as hash table keys for player, * semicolon-separated. */ void add_player_alias(dbref player, const char *alias) { char tbuf1[BUFFER_LEN], *s, *sp; if (!hft_initialized) init_hft(); if (!alias) { add_player(player); return; } mush_strncpy(tbuf1, alias, BUFFER_LEN); s = trim_space_sep(tbuf1, ALIAS_DELIMITER); while (s) { sp = split_token(&s, ALIAS_DELIMITER); while (sp && *sp && *sp == ' ') sp++; if (sp && *sp) { dbref *p; p = slab_malloc(player_dbref_slab, NULL); if (!p) mush_panic("Unable to allocate memory in plyrlist!"); *p = player; hashadd(strupper(sp), p, &htab_player_list); } } }
void delete_player(dbref who) { int result; result = free_hash(NAME(who), player_list, PLAYER_HASH_SIZE); /* Nuke the alias managed by this dbref. This will not remove any aliases * that were manually set on #0, but they should auto-clean as the lookups * are attempted. */ rotate_alias(who, 1); if (result) { int i; wall_wizards(MARK "WARNING: Playername hashtable is inconsistent. Rebuilding it."); clear_players(); for (i = db_top; i-- > 0;) { if (Typeof(i) == TYPE_PLAYER) { add_player(i); } } result = free_hash(NAME(who), player_list, PLAYER_HASH_SIZE); if (result) { wall_wizards(MARK "WARNING: Playername hashtable still inconsistent after rebuild."); } } return; }
void egg_hatching(t_server *serv, void *data) { t_egg *hatch; char buff[512]; int len; hatch = (t_egg *)data; pop_egg(serv, hatch); if (hatch->chick != NULL) { gettimeofday(&hatch->chick->death, NULL); add_time(&hatch->chick->death, 1260.0 / serv->time_unit); hatch->chick->pos = hatch->pos; add_player(&hatch->team->players, hatch->chick); add_player_to_case(serv, hatch->chick, hatch->pos); len = snprintf(buff, sizeof(buff), "%d\n%d %d\n", hatch->team->team_size - nb_player_in_team(hatch->team), serv->width, serv->height); buff_append(hatch->chick->wrbuff, buff, len); graphic_instr_all(serv, (int *)hatch->chick, PNW); graphic_instr_all(serv, (int *)hatch->chick, PIN); graphic_instr_all(serv, &hatch->num_egg, EHT); } else graphic_instr_all(serv, &hatch->num_egg, EDI); free(hatch); }
void accept_new_client(t_server *serv) { struct sockaddr_in s_in; socklen_t addrlen; t_player *new_client; addrlen = sizeof(struct sockaddr_in); if ((new_client = malloc(sizeof(t_player))) == NULL) exit(EXIT_FAILURE); bzero(new_client, sizeof(t_player)); new_client->side = rand() % 4; new_client->level = 1; gettimeofday(&new_client->availability, NULL); if ((new_client->wrbuff = malloc(sizeof(t_buff))) == NULL || (new_client->rdbuff = malloc(sizeof(t_buff))) == NULL) return ; bzero(new_client->wrbuff, sizeof(t_buff)); bzero(new_client->rdbuff, sizeof(t_buff)); new_client->fd = accept(serv->fd, (struct sockaddr *)&s_in, &addrlen); if (new_client->fd == -1) exit(EXIT_FAILURE); add_player(&serv->tmp_player, new_client); buff_append(new_client->wrbuff, "BIENVENUE\n", strlen("BIENVENUE\n")); printf("New client connected\n"); }
void track_init(uv_loop_t* loop, luv_client_t** clients, int num_players) { int i; if (DRAW) init_screen(); for (i = 0; i < num_players; i++) add_player(loop, clients[i]->data); }
int do_client(int socket_client, t_player_lst **tmp, t_init *data) { char buff[MAX_MSG + 1]; int ret; if ((*tmp)->player.buf.c > 9) return (1); if ((ret = received(&((*tmp)->player), buff)) == -1) return (merror("recv")); else if (!ret) { *tmp = del_player(socket_client, data); return (0); } else if ((*tmp)->player.ghost == 1) { (*tmp)->player.ghost = 0; (*tmp)->player.timer = 0; ret = add_player(data, *tmp, (*tmp)->player.buf.read_buffer(&((*tmp)->player.buf))); ((ret == 1) ? (*tmp = del_player(socket_client, data)) : 0); if (ret == 1) return (0); } else if (istimeout(&(*tmp)->player) == -1) setup_timer(&(*tmp)->player, data); return (1); }
/** * 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 create_player(t_server *server, t_client *client) { t_player *player; player = xmalloc(sizeof(t_player)); init_player(player, client->socket, client->team); add_player(server, player, server->width, server->height); player->client = client; client->player = player; }
int init_players(int players, int human) { th_point pos; num_of_players = players; if(players_alloc(num_of_players)) { return 0; } last_player = 0; //TODO: This information should be readen from a map pos.x = 0; pos.y = 0; add_player("Human", CIV_DEFAULT, 50, 200, 200, 200, 200, pos); add_player("Computer", CIV_DEFAULT, 50, 200, 200, 200, 200, pos); human_player = human; return 1; }
void refresh_players(void) { int i; clear_players(); for (i = db_top; i-->0; ) { if (Typeof(i) == TYPE_PLAYER) { add_player(i); } } }
// 不指定位置,自动安排 int c_pvp_room::add_player(c_player * p_player, uint8_t * p_seat) { if (is_full()) { ERROR_TLOG("c_pvp_room::add_player: full, u=%u", p_player->m_id); return -1; } if (ROOM_MODE_PVP_COMPETE == get_mode()) { return c_room::add_player(p_player, p_seat); } // 自由模式要平衡两边队伍人数 uint8_t seat = 0; uint8_t seat_1 = -1; uint8_t seat_2 = -1; uint32_t team = 0; // 在队伍1的位置中找一个空位 find_first_blank(&seat_1, team_start_seat(TEAM_ONE), team_start_seat(TEAM_TWO)); // 在队伍2的位置中找一个空位 find_first_blank(&seat_2, team_start_seat(TEAM_TWO)); if (-1 == seat_1 && -1 != seat_2) { // 队伍1满了,只能安排到队伍2 team = TEAM_TWO; seat = seat_2; } else if (-1 != seat_1 && -1 == seat_2) { // 队伍2满了,只能安排到队伍1 team = TEAM_TWO; seat = seat_1; } else if (-1 != seat_1 && -1 != seat_2) { // 队伍1和2都有空位 uint8_t count_one = get_team_player_count(TEAM_ONE); uint8_t count_two = get_team_player_count(TEAM_TWO); if (count_one > count_two) { team = TEAM_TWO; seat = seat_2; } else { team = TEAM_ONE; seat = seat_1; } } else { // 不可能 return -1; } *p_seat = seat; return add_player(seat, p_player, team); }
int main(int argc, char **argv) { FILE *file; char name[MAX_NAME_LENGTH], clan[MAX_NAME_LENGTH]; struct player_array array = PLAYER_ARRAY_ZERO; unsigned i; load_config(); if (argc != 2) { fprintf(stderr, "usage: %s <clan_name>\n", argv[0]); return EXIT_FAILURE; } /* Load players */ sprintf(path, "%s/clans/%s", config.root, argv[1]); if (!(file = fopen(path, "r"))) return perror(path), EXIT_FAILURE; while (fscanf(file, " %s", name) == 1) { struct player player; if (!read_player(&player, name)) strcpy(player.clan, argv[1]); add_player(&array, &player); } fclose(file); /* Sort players */ qsort(array.players, array.length, sizeof(*array.players), cmp_player); /* Eventually, print them */ hex_to_string(argv[1], clan); print_header(&CTF_TAB, clan, NULL); printf("<h2>%s</h2>\n", clan); printf("<p>%u member(s)</p>\n", array.length); printf("<table><thead><tr><th></th><th>Name</th><th>Clan</th><th>Score</th></tr></thead>\n<tbody>\n"); for (i = 0; i < array.length; i++) html_print_player(&array.players[i], 0); printf("</tbody></table>\n"); print_footer(); return EXIT_SUCCESS; }
void init() { ::init(); add_action("filter", "", 1); if(!this_player()->query_ghost()) { write("The doctor says: Take two asprin and call me on Monday.\n"); this_player()->move(this_player()->query_default_start_location()); return; } add_player(this_player()); set_heart_beat(1); }
void NormalGame::init(Log *_log) { STACKTRACE; Game::init(_log); team_table_size = 0; view->window->locate(0,0,0,0,0,0.9,0,1); tw_delete_file(home_ini_full_path("tmp.ini")); tw_delete_file(home_ini_full_path("fleets.tmp")); tw_set_config_file(home_ini_full_path("tmp.ini")); set_config_string (NULL, "Ignorethis", ""); if (!log->playback) init_players(); log_file(home_ini_full_path("tmp.ini")); if (log->playback) { for (int i = 0; true; i += 1) { char buffy[64]; sprintf(buffy, "Player%d", i + 1); log_file(home_ini_full_path("tmp.ini")); const char *type = get_config_string(buffy, "Type", NULL); if (!type) break; const char *name = get_config_string(buffy, "Name", buffy); int channel = get_config_int(buffy, "Channel", -2); int ti = get_config_int(buffy, "Team", 0); add_player(create_control(channel, type), ti, name, buffy); player_fleet[i]->load(NULL, buffy); player_fleet[i]->save("fleets.tmp", buffy); } } prepare(); init_objects(); next_choose_new_ships_time = game_time + 200; // team and health indicators. indteamtoggle = 0; indhealthtoggle = 0; return; }
dbref create_player(const char *name, const char *password) { char buf[80]; dbref player; if (!ok_player_name(name) || !ok_password(password)) return NOTHING; if (!tp_building || tp_db_readonly) return NOTHING; /* else he doesn't already exist, create him */ player = new_object(); /* initialize everything */ NAME(player) = alloc_string(name); FLAGS(player) = TYPE_PLAYER | PCREATE_FLAGS; FLAG2(player) = PCREATE_FLAG2; DBFETCH(player)->location = RootRoom; /* home */ OWNER(player) = player; DBFETCH(player)->sp.player.home = RootRoom; DBFETCH(player)->exits = NOTHING; DBFETCH(player)->sp.player.pennies = tp_start_pennies; DBFETCH(player)->sp.player.password = alloc_string(password); DBFETCH(player)->sp.player.curr_prog = NOTHING; DBFETCH(player)->sp.player.insert_mode = 0; /* link him to tp_player_start */ PUSH(player, DBFETCH(RootRoom)->contents); add_player(player); DBDIRTY(player); DBDIRTY(RootRoom); sprintf(buf, CNOTE "%s is born!", PNAME(player)); anotify_except(DBFETCH(RootRoom)->contents, NOTHING, buf, player); return player; }
void game_state::process_join_request(udp_sock& my_server, byte_fetch& fetch, sockaddr_storage& who) { int32_t found_end = fetch.get<int32_t>(); if(found_end != canary_end) return; printf("Player joined %s:%s\n", get_addr_ip(who).c_str(), get_addr_port(who).c_str()); add_player(my_server, who); ///really need to pipe back player id ///should really dynamically organise teams ///so actually that's what I'll do byte_vector vec; vec.push_back(canary_start); vec.push_back(message::CLIENTJOINACK); vec.push_back<int32_t>(player_list.back().id); vec.push_back(canary_end); udp_send_to(my_server, vec.ptr, (const sockaddr*)&who); printf("sending ack\n"); }
/* Prints out everybody's info and gives the opportunity for players to enter and leave the game */ int FiveCardDraw::after_round(){ vector<shared_ptr<Player>> tempVP = vector<shared_ptr<Player>>(playersStillInRound); sort(tempVP.begin(), tempVP.end(), poker_rankTemp); unsigned int lastPlIndex = tempVP.size() - index::one; tempVP[index::zero]->won += winLoss::increment; cout << tempVP[index::zero]->name << winround << endl << endl; tempVP[index::zero]->numChips += pot; cout << tempVP[index::zero]->name << winpot << pot << "!" << endl; pot = 0; for (int i = 0; i < players.size(); i++){ if (players[i]->name != tempVP[0]->name){ players[i]->lost += winLoss::increment; } } for (shared_ptr<Player> p : players) { //check the folded flag here. If yes, do not print out hand, rather only that they folded. cout << playerlabel << p->name << endl; cout << winlabel << p->won << endl; cout << losslabel << p->lost << endl; cout << chipslabel << p->numChips << endl; cout << handlabel << p->hand << endl; } string response; do{ cout << leavegamebro << endl; get_input(response); bool playerExists = true; if (response == yesResponse){ cout << whosleaving << endl; string name; cin.clear(); cout << ">> "; getline(cin, name); cin.clear(); shared_ptr<Player> tempPlayer = find_player(name); if (tempPlayer == nullptr){ cout << whoutalkingbout << name << period << endl; playerExists = false; } if (playerExists){ ofstream ofs; ofs.open(tempPlayer->name); if (ofs.is_open()){ ofs << tempPlayer->name << outerspace << tempPlayer->won << outerspace << tempPlayer->lost << outerspace << tempPlayer->numChips; } ofs.close(); int index = index::negativeOne; for (unsigned int i = index::zero; i < players.size(); i++){ if (tempPlayer->name == players[i]->name){ index = i; } } players.erase(players.begin() + index); cout << name << " has left the game." << endl; } } } while (response != noResponse); do{ // Add a new player into the game cout << whoscoming << endl; cout << inputlabel; cin.clear(); getline(cin, response); cin.clear(); if (response == yesResponse){ cout << whatshisnamebro << endl; string playerName; cin.clear(); cout << ">> "; getline(cin, playerName); cin.clear(); try{ add_player(playerName); cout << playerName << " has been added." << endl; } catch (char const* message) { cout << message << endl; } catch (...){ continue; } } } while (response != noResponse); checkForNoChips(); dealerPos++; if (dealerPos >= players.size()){ dealerPos = index::zero; } for (unsigned int i = index::zero; i < players.size(); i++){ (*players[i]).hand = {}; } discardDeck = {}; deck = {}; // Create a standard deck of 52 cards for (int cardSuits = suit::club; cardSuits <= suit::spade; ++cardSuits) { for (int cardRanks = value::two; cardRanks <= value::ace; ++cardRanks) { value cv = static_cast<value>(cardRanks); suit cs = static_cast<suit>(cardSuits); Card tempCard; tempCard.s = cs; tempCard.v = cv; deck.add_card(tempCard); } } return errorMsg::success; }
// See the following for protocol details: // http://dev.kquery.com/index.php?article=42 query_status_t deal_with_gs2_packet( struct qserver *server, char *rawpkt, int pktlen ) { char *ptr = rawpkt; char *end = rawpkt + pktlen; unsigned char type = 0; unsigned char no_players = 0; unsigned char total_players = 0; unsigned char no_teams = 0; unsigned char total_teams = 0; unsigned char no_headers = 0; char **headers = NULL; debug( 2, "processing packet..." ); if ( pktlen < 15 ) { // invalid packet? return PKT_ERROR; } server->n_servers++; if ( server->server_name == NULL) { server->ping_total += time_delta( &packet_recv_time, &server->packet_time1 ); } else { gettimeofday( &server->packet_time1, NULL); } // Could check the header here should // match the 4 byte id sent ptr += 5; while ( 0 == type && ptr < end ) { // server info: // name value pairs null seperated // empty name && value signifies the end of section char *var, *val; int var_len, val_len; var = ptr; var_len = strlen( var ); if ( ptr + var_len + 2 > end ) { if ( 0 != var_len ) { malformed_packet( server, "no rule value" ); } else if ( get_player_info ) { malformed_packet( server, "no player headers" ); } return PKT_ERROR; } ptr += var_len + 1; val = ptr; val_len = strlen( val ); ptr += val_len + 1; debug( 2, "var:%s (%d)=%s (%d)\n", var, var_len, val, val_len ); // Lets see what we've got if ( 0 == strcmp( var, "hostname" ) ) { server->server_name = strdup( val ); } else if( 0 == strcmp( var, "game_id" ) ) { server->game = strdup( val ); } else if( 0 == strcmp( var, "gamever" ) ) { // format: // v1.0 server->protocol_version = atoi( val+1 ); add_rule( server, var, val, NO_FLAGS ); } else if( 0 == strcmp( var, "mapname" ) ) { server->map_name = strdup( val ); } else if( 0 == strcmp( var, "map" ) ) { // For BF2MC compatibility server->map_name = strdup( val ); } else if( 0 == strcmp( var, "maxplayers" ) ) { server->max_players = atoi( val ); } else if( 0 == strcmp( var, "numplayers" ) ) { server->num_players = no_players = atoi( val ); } else if( 0 == strcmp( var, "hostport" ) ) { change_server_port( server, atoi( val ), 0 ); } else if ( 0 == var_len ) { // check for end of section type = 1; } else { add_rule( server, var, val, NO_FLAGS ); } } if ( 1 != type ) { // no more info should be player headers here as we // requested it malformed_packet( server, "no player headers" ); return PKT_ERROR; } // player info header // format: // first byte = player count // followed by null seperated header no_players = (unsigned char)*ptr; debug( 2, "No Players:%d\n", no_players ); ptr++; if ( ptr >= end ) { malformed_packet( server, "no player headers" ); return PKT_ERROR; } while ( 1 == type && ptr < end ) { // first we have the headers null seperated char **tmpp; char *head = ptr; int head_len = strlen( head ); no_headers++; tmpp = (char**)realloc( headers, no_headers * sizeof( char* ) ); if ( NULL == tmpp ) { debug( 0, "Failed to realloc memory for headers\n" ); if ( NULL != headers ) { free( headers ); } return MEM_ERROR; } headers = tmpp; headers[no_headers-1] = head; ptr += head_len + 1; // end of headers check if ( 0x00 == *ptr ) { type = 2; ptr++; } debug( 2, "player header[%d] = '%s'", no_headers-1, head ); } if ( 2 != type ) { // no more info should be player info here as we // requested it malformed_packet( server, "no players" ); return PKT_ERROR; } while( 2 == type && ptr < end ) { // now each player details // add the player if ( 0x00 == *ptr ) { // no players if ( 0 != no_players ) { malformed_packet( server, "no players" ); return PKT_ERROR; } } else { struct player *player = add_player( server, total_players ); int i; for ( i = 0; i < no_headers; i++ ) { char *header = headers[i]; char *val; int val_len; if ( ptr >= end ) { malformed_packet( server, "short player detail" ); return PKT_ERROR; } val = ptr; val_len = strlen( val ); ptr += val_len + 1; // lets see what we got if ( 0 == strcmp( header, "player_" ) ) { player->name = strdup( val ); } else if ( 0 == strcmp( header, "score_" ) ) { player->score = atoi( val ); } else if ( 0 == strcmp( header, "deaths_" ) ) { player->deaths = atoi( val ); } else if ( 0 == strcmp( header, "ping_" ) ) { player->ping = atoi( val ); } else if ( 0 == strcmp( header, "kills_" ) ) { player->frags = atoi( val ); } else if ( 0 == strcmp( header, "team_" ) ) { player->team = atoi( val ); } else { int len = strlen( header ); if ( '_' == header[len-1] ) { header[len-1] = '\0'; } player_add_info( player, header, val, NO_FLAGS ); } debug( 2, "Player[%d][%s]=%s\n", total_players, headers[i], val ); } total_players++; } if ( total_players > no_players ) { malformed_packet( server, "to many players %d > %d", total_players, no_players ); return PKT_ERROR; } // check for end of player info if ( 0x00 == *ptr ) { if ( total_players != no_players ) { malformed_packet( server, "bad number of players %d != %d", total_players, no_players ); return PKT_ERROR; } type = 3; ptr++; } } if ( 3 != type ) { // no more info should be team info here as we // requested it malformed_packet( server, "no teams" ); return PKT_ERROR; } no_teams = (unsigned char)*ptr; ptr++; debug( 2, "No teams:%d\n", no_teams ); no_headers = 0; while ( 3 == type && ptr < end ) { // first we have the headers null seperated char **tmpp; char *head = ptr; int head_len = strlen( head ); no_headers++; tmpp = (char**)realloc( headers, no_headers * sizeof( char* ) ); if ( NULL == tmpp ) { debug( 0, "Failed to realloc memory for headers\n" ); if ( NULL != headers ) { free( headers ); } return MEM_ERROR; } headers = tmpp; headers[no_headers-1] = head; ptr += head_len + 1; // end of headers check if ( 0x00 == *ptr ) { type = 4; ptr++; } } if ( 4 != type ) { // no more info should be team info here as we // requested it malformed_packet( server, "no teams" ); return PKT_ERROR; } while( 4 == type && ptr < end ) { // now each teams details int i; for ( i = 0; i < no_headers; i++ ) { char *val; int val_len; if ( ptr >= end ) { malformed_packet( server, "short team detail" ); return PKT_ERROR; } val = ptr; val_len = strlen( val ); ptr += val_len + 1; // lets see what we got if ( 0 == strcmp( headers[i], "team_t" ) ) { // BF being stupid again teams 1 based instead of 0 players_set_teamname( server, total_teams + 1, val ); } debug( 2, "Team[%d][%s]=%s\n", total_teams, headers[i], val ); } total_teams++; if ( total_teams > no_teams ) { malformed_packet( server, "to many teams" ); return PKT_ERROR; } } return DONE_FORCE; }
int main(int argc, char **argv) { world_t world; game_t game; double last_time = 0; double current_time = 0; float deltatime = 0; float sleep_time; if (config_from_file("anko.cfg", 1) == 2) return EXIT_FAILURE; if (config_from_args(argc, argv)) return EXIT_FAILURE; if (!init()) goto err_init; if (!new_game(&game, config.board_width, config.board_height, &config.gen_params, config.sim_speed * 1000)) goto err_game; if (!create_world(&world, &game)) goto err_world; if ((current_ui = init_ui_game(&world)) == NULL) goto err_ui; world_set_active_player(&world, add_player(&game, TEAM_BURNER)); events_link_frame(¤t_ui); // link window event to game ui frame glClearColor(0, 0, 0, 1); last_time = 0; while(!glfwWindowShouldClose(window)) { current_time = glfwGetTime(); deltatime = current_time - last_time; update_speed(deltatime); glfwPollEvents(); // Update update_ui(current_ui, deltatime); update_world(&world); if (update_game(&game, deltatime * 1000)) refresh_world(&world); if(should_quit) glfwSetWindowShouldClose(window, GL_TRUE); // Rendering glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_ui(current_ui); font_swap_buffers(); glfwSwapBuffers(window); // Update speed and sleep if necessary last_time = current_time; sleep_time = (current_time + MAX_MSPF - glfwGetTime()) * 1000 * 1000; if(sleep_time > 0) usleep(sleep_time); } destroy_ui(current_ui); game_over(&game); end_of_the_world(&world); // Tin tin tin terminate(); return EXIT_SUCCESS; err_ui: end_of_the_world(&world); err_world: game_over(&game); err_game: terminate(); err_init: return EXIT_FAILURE; }
query_status_t deal_with_ts2_packet( struct qserver *server, char *rawpkt, int pktlen ) { char *s, *end; int ping, connect_time, mode = 0; char name[256]; debug( 2, "processing..." ); server->n_servers++; server->n_requests++; server->ping_total += time_delta( &packet_recv_time, &server->packet_time1 ); if ( 0 == pktlen ) { // Invalid password return REQ_ERROR; } rawpkt[pktlen]= '\0'; end = &rawpkt[pktlen]; s = rawpkt; s = strtok( rawpkt, "\015\012" ); while ( NULL != s ) { if ( 0 == mode ) { // Rules char *key = s; char *value = strchr( key, '=' ); if ( NULL != value ) { // Server Rule *value = '\0'; value++; if ( 0 == strcmp( "server_name", key ) ) { server->server_name = strdup( value ); } else if ( 0 == strcmp( "server_udpport", key ) ) { change_server_port( server, atoi( value ), 0 ); add_rule( server, key, value, NO_FLAGS ); } else if ( 0 == strcmp( "server_maxusers", key ) ) { server->max_players = atoi( value ); } else if ( 0 == strcmp( "server_currentusers", key ) ) { server->num_players = atoi( value); } else { add_rule( server, key, value, NO_FLAGS); } } else if ( 0 == strcmp( "OK", s ) ) { // end of rules request server->saved_data.pkt_index--; mode++; } else if ( 0 == strcmp( "[TS]", s ) ) { // nothing to do } else if ( 0 == strcmp( "ERROR, invalid id", s ) ) { // bad server server->server_name = DOWN; server->saved_data.pkt_index = 0; } } else if ( 1 == mode ) { // Player info if ( 3 == sscanf( s, "%*d %*d %*d %*d %*d %*d %*d %d %d %*d %*d %*d %*d \"0.0.0.0\" \"%255[^\"]", &ping, &connect_time, name ) ) { // Player info struct player *player = add_player( server, server->n_player_info ); if ( NULL != player ) { player->name = strdup( name ); player->ping = ping; player->connect_time = connect_time; } } else if ( 0 == strcmp( "OK", s ) ) { // end of rules request server->saved_data.pkt_index--; mode++; } else if ( 0 == strcmp( "[TS]", s ) ) { // nothing to do } else if ( 0 == strcmp( "ERROR, invalid id", s ) ) { // bad server server->server_name = DOWN; server->saved_data.pkt_index = 0; } } s = strtok( NULL, "\015\012" ); } gettimeofday( &server->packet_time1, NULL ); if ( 0 == server->saved_data.pkt_index ) { server->map_name = strdup( "N/A" ); return DONE_FORCE; } return INPROGRESS; }
query_status_t deal_with_ts3_packet(struct qserver *server, char *rawpkt, int pktlen) { char *s, *player_name = "unknown"; int valid_response = 0, mode = 0, all_servers = 0; char last_char; unsigned short port = 0, down = 0, auth_seen = 0; debug(2, "processing..."); if (0 == pktlen) { // Invalid password return (REQ_ERROR); } last_char = rawpkt[pktlen - 1]; rawpkt[pktlen - 1] = '\0'; s = rawpkt; all_servers = all_ts3_servers(server); debug(3, "packet: combined = %d, challenge = %ld, n_servers = %d", server->combined, server->challenge, server->n_servers); if (!server->combined) { server->retry1 = n_retries; if (0 == server->n_requests) { server->ping_total = time_delta(&packet_recv_time, &server->packet_time1); server->n_requests++; } if (server->n_servers >= server->challenge) { // response fragment recieved int pkt_id; int pkt_max; // We're expecting more to come debug(5, "fragment recieved..."); pkt_id = packet_count(server); pkt_max = pkt_id + 1; rawpkt[pktlen - 1] = last_char; // restore the last character if (!add_packet(server, 0, pkt_id, pkt_max, pktlen, rawpkt, 1)) { // fatal error e.g. out of memory return (MEM_ERROR); } // combine_packets will call us recursively return (combine_packets(server)); } } else { valid_response = valid_ts3_response(server, rawpkt + server->master_pkt_len, pktlen - server->master_pkt_len); debug(2, "combined packet: valid_response: %d, challenge: %ld, n_servers: %d, offset: %d", valid_response, server->challenge, server->n_servers, server->master_pkt_len); if (0 > valid_response) { // Error occured return (valid_response); } server->challenge += valid_response; if (valid_response) { // Got a valid response, send the next request int ret = send_ts3_request_packet(server); if (0 != ret) { // error sending packet debug(4, "Request failed: %d", ret); return (ret); } } if (server->n_servers > server->challenge) { // recursive call which is still incomplete return (INPROGRESS); } } // Correct ping // Not quite right but gives a good estimate server->ping_total = (server->ping_total * server->n_requests) / 2; debug(3, "processing response..."); s = strtok(rawpkt, "\012\015 |"); // NOTE: id=XXX and msg=XXX will be processed by the mod following the one they where the response of while (NULL != s) { debug(4, "LINE: %d, %s\n", mode, s); switch (mode) { case 0: // prompt, use or serverlist response if (0 == strcmp("TS3", s)) { // nothing to do unless in all servers mode if (1 == all_servers) { mode++; } } else if (0 == strncmp("error", s, 5)) { // end of use response mode++; } break; case 1: // serverinfo or serverlist response including condition authentication if ((0 == auth_seen) && (0 != strlen(get_param_value(server, "password", ""))) && (0 == strncmp("error", s, 5))) { // end of auth response auth_seen = 1; } else if (0 == strncmp("error", s, 5)) { // end of serverinfo response mode++; } else { // Server Rule char *key = s; char *value = strchr(key, '='); if (NULL != value) { *value = '\0'; value++; debug(6, "Rule: %s = %s\n", key, value); if (0 == strcmp("virtualserver_name", key)) { if (1 == all_servers) { struct qserver *new_server = add_qserver_byaddr(ntohl(server->ipaddr), port, server->type, NULL); if (NULL != new_server) { if (down) { // Status indicates this server is actually offline new_server->server_name = DOWN; } else { new_server->max_players = server->max_players; new_server->num_players = server->num_players; new_server->server_name = strdup(decode_ts3_val(value)); new_server->map_name = strdup("N/A"); new_server->ping_total = server->ping_total; new_server->n_requests = server->n_requests; } cleanup_qserver(new_server, FORCE); } down = 0; } else { server->server_name = strdup(decode_ts3_val(value)); } } else if (0 == strcmp("virtualserver_port", key)) { port = atoi(value); change_server_port(server, port, 0); add_rule(server, key, value, NO_FLAGS); } else if (0 == strcmp("virtualserver_maxclients", key)) { server->max_players = atoi(value); } else if (0 == strcmp("virtualserver_clientsonline", key)) { server->num_players = atoi(value); } else if (0 == strcmp("virtualserver_queryclientsonline", key)) { // clientsonline includes queryclientsonline so remove these from our count server->num_players -= atoi(value); } else if ((0 == strcmp("virtualserver_status", key)) && (0 != strcmp("online", value))) { // Server is actually offline to client so display as down down = 1; if (1 != all_servers) { server->server_name = DOWN; //server->saved_data.pkt_index = 0; return (DONE_FORCE); } } else if ((0 == strcmp("id", key)) || (0 == strcmp("msg", key))) { // Ignore details from the response code } else if (1 != all_servers) { add_rule(server, key, value, NO_FLAGS); } } } break; case 2: // clientlist response if (0 == strncmp("error", s, 5)) { // end of serverinfo response mode++; } else { // Client char *key = s; char *value = strchr(key, '='); if (NULL != value) { *value = '\0'; value++; debug(6, "Player: %s = %s\n", key, value); if (0 == strcmp("client_nickname", key)) { player_name = value; } else if (0 == strcmp("clid", key)) { } else if ((0 == strcmp("client_type", key)) && (0 == strcmp("0", value))) { struct player *player = add_player(server, server->n_player_info); if (NULL != player) { player->name = strdup(decode_ts3_val(player_name)); } } else if ((0 == strcmp("id", key)) || (0 == strcmp("msg", key))) { // Ignore details from the response code } } } break; } s = strtok(NULL, "\012\015 |"); } gettimeofday(&server->packet_time1, NULL); server->map_name = strdup("N/A"); return (DONE_FORCE); }
/* true return value indicates that connection must be closed */ int process_msg(int fd, char* msg) { int client_proto_major; int client_proto_minor; char * args; char * ptr, * ptr2; char * msg_orig; /* check for leading protocol tag */ if (!str_begins_static_str(msg, "FB/") || strlen(msg) < 8) { // 8 stands for "FB/M.m f"(oo) send_line_log(fd, fl_line_unrecognized, msg); return 1; } /* check if client protocol is compatible; for simplicity, we don't support client protocol more recent * than server protocol, we suppose that our servers are upgraded when a new release appears (but of * course client protocol older is supported within the major protocol) */ client_proto_major = charstar_to_int(msg + 3); client_proto_minor = charstar_to_int(msg + 5); if (client_proto_major != proto_major || client_proto_minor > proto_minor) { send_line_log(fd, fl_proto_mismatch, msg); return 1; } if (remote_proto_minor[fd] == -1) remote_proto_minor[fd] = client_proto_minor; msg_orig = strdup(msg); /* after protocol, first word is command, then possible args */ current_command = msg + 7; // 7 stands for "FB/M.m " if ((ptr = strchr(current_command, ' '))) { *ptr = '\0'; args = current_command + strlen(current_command) + 1; } else args = NULL; if (streq(current_command, "PING")) { send_line_log(fd, ok_pong, msg_orig); } else if (streq(current_command, "NICK")) { if (!args) { send_line_log(fd, wn_missing_arguments, msg_orig); } else { if ((ptr = strchr(args, ' '))) *ptr = '\0'; if (strlen(args) > 10) args[10] = '\0'; if (!is_nick_ok(args)) { send_line_log(fd, wn_nick_invalid, msg_orig); } else { if (nick[fd] != NULL) { free(nick[fd]); } nick[fd] = strdup(args); calculate_list_games(); send_ok(fd, msg_orig); } } } else if (streq(current_command, "GEOLOC")) { if (!args) { send_line_log(fd, wn_missing_arguments, msg_orig); } else { if ((ptr = strchr(args, ' '))) *ptr = '\0'; if (strlen(args) > 13) // sign, 4 digits, dot, colon, sign, 4 digits, dot args[13] = '\0'; if (geoloc[fd] != NULL) { free(geoloc[fd]); } geoloc[fd] = strdup(args); calculate_list_games(); send_ok(fd, msg_orig); } } else if (streq(current_command, "CREATE")) { if (!args) { send_line_log(fd, wn_missing_arguments, msg_orig); } else { if ((ptr = strchr(args, ' '))) *ptr = '\0'; if (strlen(args) > 10) args[10] = '\0'; if (!is_nick_ok(args)) { send_line_log(fd, wn_nick_invalid, msg_orig); } else if (!nick_available(args)) { send_line_log(fd, wn_nick_in_use, msg_orig); } else if (already_in_game(fd)) { send_line_log(fd, wn_already_in_game, msg_orig); } else if (games_open == 16) { // FB client can display 16 max send_line_log(fd, wn_max_open_games, msg_orig); } else { create_game(fd, strdup(args)); send_ok(fd, msg_orig); } } } else if (streq(current_command, "JOIN")) { if (!args || !(ptr = strchr(args, ' '))) { send_line_log(fd, wn_missing_arguments, msg_orig); } else { struct game * g; char* nick = ptr + 1; *ptr = '\0'; if ((ptr2 = strchr(ptr, ' '))) *ptr2 = '\0'; if (strlen(nick) > 10) nick[10] = '\0'; if (!is_nick_ok(nick)) { send_line_log(fd, wn_nick_invalid, msg_orig); } else if (!nick_available(nick)) { send_line_log(fd, wn_nick_in_use, msg_orig); } else if (already_in_game(fd)) { send_line_log(fd, wn_already_in_game, msg_orig); } else if (!(g = find_game_by_nick(args))) { send_line_log(fd, wn_no_such_game, msg_orig); } else { if (add_player(g, fd, strdup(nick))) send_ok(fd, msg_orig); else send_line_log(fd, wn_game_full, msg_orig); } } } else if (streq(current_command, "KICK")) { if (!args) { send_line_log(fd, wn_missing_arguments, msg_orig); } else { if ((ptr = strchr(args, ' '))) *ptr = '\0'; if (strlen(args) > 10) args[10] = '\0'; if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { struct game * g = find_game_by_fd(fd); if (g->players_conn[0] != fd) { send_line_log(fd, wn_not_creator, msg_orig); } else { kick_player(fd, g, args); } } } } else if (streq(current_command, "PART")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { player_part_game(fd); send_ok(fd, msg_orig); } } else if (streq(current_command, "LIST")) { send_line_log(fd, list_games_str, msg_orig); } else if (streq(current_command, "STATUS")) { // 1.0 command if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { status(fd, msg_orig); } } else if (streq(current_command, "STATUSGEO")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { status_geo(fd, msg_orig); } } else if (streq(current_command, "PROTOCOL_LEVEL")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { protocol_level(fd, msg_orig); } } else if (streq(current_command, "TALK")) { if (!args) { send_line_log(fd, wn_missing_arguments, msg_orig); } else { talk(fd, args); } } else if (streq(current_command, "START")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { start_game(fd); } } else if (streq(current_command, "CLOSE")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { close_game(fd); } } else if (streq(current_command, "SETOPTIONS")) { if (!args) { send_line_log(fd, wn_missing_arguments, msg_orig); } else if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { setoptions(fd, args); } } else if (streq(current_command, "LEADER_CHECK_GAME_START")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { leader_check_game_start(fd); } } else if (streq(current_command, "OK_GAME_START")) { if (!already_in_game(fd)) { send_line_log(fd, wn_not_in_game, msg_orig); } else { ok_start_game(fd); } } else if (streq(current_command, "ADMIN_REREAD")) { if (!admin_authorized[fd]) { send_line_log(fd, wn_denied, msg_orig); } else { reread(); send_ok(fd, "ADMIN_REREAD"); } } else { send_line_log(fd, wn_unknown_command, msg); } free(msg_orig); current_command = NULL; return 0; }
query_status_t deal_with_tm_packet( struct qserver *server, char *rawpkt, int pktlen ) { char *s; char *pkt = rawpkt; char *key = NULL, *value = NULL, *tmpp = NULL; char fullname[256]; struct player *player = NULL; int pkt_max = server->saved_data.pkt_max; unsigned total_len, expected_len; int method_response = 1; debug( 2, "processing..." ); s = rawpkt; // We may get the setup handle and the protocol version in one packet we may not // So we continue to parse if we see the handle if ( 4 <= pktlen && 0 == memcmp( pkt, "\x0b\x00\x00\x00", 4 ) ) { // setup handle identifier // greater 2^31 = XML-RPC, less = callback server->challenge = 0x80000001; if ( 4 == pktlen ) { return 0; } pktlen -= 4; pkt += 4; } if ( 11 <= pktlen && 1 == sscanf( pkt, "GBXRemote %d", &server->protocol_version ) ) { // Got protocol version send request send_tm_request_packet( server ); return 0; } if ( 8 <= pktlen && 0 == memcmp( pkt+4, &server->challenge, 4 ) ) { // first 4 bytes = the length // Note: We use pkt_id to store the length of the expected packet // this could cause loss but very unlikely unsigned long len; memcpy( &len, rawpkt, 4 ); // second 4 bytes = handle identifier we sent in the request if ( 8 == pktlen ) { // split packet // we have at least one more packet coming if ( ! add_packet( server, len, 0, 2, pktlen, rawpkt, 1 ) ) { // fatal error e.g. out of memory return -1; } return 0; } else { // ensure the length is stored server->saved_data.pkt_id = (int)len; s += 8; } } total_len = combined_length( server, server->saved_data.pkt_id ); expected_len = server->saved_data.pkt_id; debug( 2, "total: %d, expected: %d\n", total_len, expected_len ); if ( total_len < expected_len + 8 ) { // we dont have a complete response add the packet int last, new_max; if ( total_len + pktlen >= expected_len + 8 ) { last = 1; new_max = pkt_max; } else { last = 0; new_max = pkt_max + 1; } if ( ! add_packet( server, server->saved_data.pkt_id, pkt_max - 1, new_max, pktlen, rawpkt, 1 ) ) { // fatal error e.g. out of memory return -1; } if ( last ) { // we are the last packet run combine to call us back return combine_packets( server ); } return 0; } server->n_servers++; if ( server->server_name == NULL) { server->ping_total += time_delta( &packet_recv_time, &server->packet_time1 ); } else { gettimeofday( &server->packet_time1, NULL); } // Terminate the packet data pkt = (char*)malloc( pktlen + 1 ); if ( NULL == pkt ) { debug( 0, "Failed to malloc memory for packet terminator\n" ); return MEM_ERROR; } memcpy( pkt, rawpkt, pktlen ); pkt[pktlen] = '\0'; //fprintf( stderr, "S=%s\n", s ); s = strtok( pkt + 8, "\015\012" ); while ( NULL != s ) { //fprintf( stderr, "S=%s\n", s ); if ( 0 == strncmp( s, "<member><name>", 14 ) ) { key = s + 14; tmpp = strstr( key, "</name>" ); if ( NULL != tmpp ) { *tmpp = '\0'; } s = strtok( NULL, "\015\012" ); value = NULL; continue; } else if ( NULL != key && 0 == strncmp( s, "<value>", 7 ) ) { // value s += 7; if ( 0 == strncmp( s, "<string>", 8 ) ) { // String value = s+8; tmpp = strstr( s, "</string>" ); } else if ( 0 == strncmp( s, "<i4>", 4 ) ) { // Int value = s+4; tmpp = strstr( s, "</i4>" ); } else if ( 0 == strncmp( s, "<boolean>", 9 ) ) { // Boolean value = s+9; tmpp = strstr( s, "</boolean>" ); } else if ( 0 == strncmp( s, "<double>", 8 ) ) { // Double value = s+8; tmpp = strstr( s, "</double>" ); } // also have struct and array but not interested in those if ( NULL != tmpp ) { *tmpp = '\0'; } if ( NULL != value ) { debug( 4, "%s = %s\n", key, value ); } } else if ( 0 == strncmp( s, "</struct>", 9 ) && 3 > method_response ) { // end of method response method_response++; } if ( NULL != value && NULL != key ) { switch( method_response ) { case 1: // GetServerOptions response if ( 0 == strcmp( "Name", key ) ) { server->server_name = strdup( value ); } else if ( 0 == strcmp( "CurrentMaxPlayers", key ) ) { server->max_players = atoi( value ); } else { sprintf( fullname, "server.%s", key ); add_rule( server, fullname, value, NO_FLAGS); } break; case 2: // GetCurrentChallengeInfo response if ( 0 == strcmp( "Name", key ) ) { server->map_name = strdup( value ); } else { sprintf( fullname, "challenge.%s", key ); add_rule( server, fullname, value, NO_FLAGS); } break; case 3: // GetPlayerList response // Player info if ( 0 == strcmp( "Login", key ) ) { player = add_player( server, server->n_player_info ); server->num_players++; } else if ( NULL != player ) { if ( 0 == strcmp( "NickName", key ) ) { player->name = strdup( value ); } else if ( 0 == strcmp( "PlayerId", key ) ) { //player->number = atoi( value ); } else if ( 0 == strcmp( "TeamId", key ) ) { player->team = atoi( value ); } else if ( 0 == strcmp( "IsSpectator", key ) ) { player->flags = player->flags & 1; } else if ( 0 == strcmp( "IsInOfficialMode", key ) ) { player->flags = player->flags & 2; } else if ( 0 == strcmp( "LadderRanking", key ) ) { player->score = atoi( value ); } } break; } value = NULL; } s = strtok( NULL, "\015\012" ); } free( pkt ); if ( 0 == strncmp( rawpkt + pktlen - 19, "</methodResponse>", 17 ) ) { // last packet seen return DONE_FORCE; } return INPROGRESS; }
query_status_t deal_with_ottd_packet(struct qserver *server, char *rawpkt, int pktlen) { unsigned char *ptr = (unsigned char*)rawpkt; unsigned char *end = (unsigned char*)(rawpkt + pktlen); unsigned char type; char* str; char buf[32]; unsigned ver; server->n_servers++; if ( server->server_name == NULL) { server->ping_total += time_delta( &packet_recv_time, &server->packet_time1); server->n_requests++; } else { gettimeofday( &server->packet_time1, NULL); } FAIL_IF(pktlen < 4 || swap_short_from_little(rawpkt) > pktlen, "invalid packet"); type = ptr[2]; ver = ptr[3]; ptr += 4; debug(3, "len %hu type %hhu ver %hhu", swap_short_from_little(rawpkt), type, ver); FAIL_IF(ver != 4 && ver != 5, "only version 4 and 5 servers are supported"); if(type == 1) // info packet { unsigned numgrf = *ptr; FAIL_IF(ptr + numgrf * 20 + 1 > end, "invalid newgrf number"); ptr += numgrf * 20 + 1; snprintf(buf, sizeof(buf), "%u", swap_long_from_little(ptr)); add_rule(server, "date_days", buf, NO_FLAGS); ptr += 4; snprintf(buf, sizeof(buf), "%u", swap_long_from_little(ptr)); add_rule(server, "startdate_days", buf, NO_FLAGS); ptr += 4; FAIL_IF(ptr + 3 > end, "invalid packet"); snprintf(buf, sizeof(buf), "%hhu", ptr[0]); add_rule(server, "maxcompanies", buf, NO_FLAGS); snprintf(buf, sizeof(buf), "%hhu", ptr[1]); add_rule(server, "numcompanies", buf, NO_FLAGS); server->max_spectators = ptr[2]; ptr += 3; GET_STRING; server->server_name = strdup(str); GET_STRING; add_rule(server, "version", str, NO_FLAGS); FAIL_IF(ptr + 7 > end, "invalid packet"); { static const char* langs[] = { "any", "English", "German", "French" }; unsigned i = *ptr++; if(i > 3) i = 0; add_rule(server, "language", (char*)langs[i], NO_FLAGS); } add_rule(server, "password", *ptr++ ? "1" : "0", NO_FLAGS); server->max_players = *ptr++; server->num_players = *ptr++; server->num_spectators = *ptr++; GET_STRING; server->map_name = strdup(str); snprintf(buf, sizeof(buf), "%hu", swap_short_from_little(ptr)); add_rule(server, "map_width", buf, NO_FLAGS); snprintf(buf, sizeof(buf), "%hu", swap_short_from_little(ptr)); add_rule(server, "map_height", buf, NO_FLAGS); { static const char* sets[] = { "temperate", "arctic", "desert", "toyland" }; unsigned i = *ptr++; if(i > 3) i = 0; add_rule(server, "map_set", (char*)sets[i], NO_FLAGS); } add_rule(server, "dedicated", *ptr++ ? "1" : "0", NO_FLAGS); } else if(type == 3) // player packet { unsigned i, j; INVALID_IF(ptr + 2 > end); server->num_players = *ptr++; for(i = 0; i < server->num_players; ++i) { unsigned long long lli; struct player* player; unsigned char nr; nr = *ptr++; debug(3, "player number %d", nr); player = add_player(server, i); FAIL_IF(!player, "can't allocate player"); GET_STRING; player->name = strdup(str); debug(3, "name %s", str); player->frags = 0; INVALID_IF(ptr + 4 + 3*8 + 2 + 1 + 2*MAX_VEHICLE_TYPES + 2*MAX_STATION_TYPES > end); snprintf(buf, sizeof(buf), "%u", swap_long_from_little(ptr)); player_add_info(player, "startdate", buf, 0); ptr += 4; lli = swap_long_from_little(ptr+4); lli <<= 32; lli += swap_long_from_little(ptr); snprintf(buf, sizeof(buf), "%lld", lli); player_add_info(player, "value", buf, 0); ptr += 8; lli = swap_long_from_little(ptr+4); lli <<= 32; lli = swap_long_from_little(ptr); snprintf(buf, sizeof(buf), "%lld", lli); player_add_info(player, "money", buf, 0); ptr += 8; lli = swap_long_from_little(ptr+4); lli <<= 32; lli += swap_long_from_little(ptr); snprintf(buf, sizeof(buf), "%lld", lli); player_add_info(player, "income", buf, 0); ptr += 8; snprintf(buf, sizeof(buf), "%hu", swap_short_from_little(ptr)); player_add_info(player, "performance", buf, 0); ptr += 2; player_add_info(player, "password", *ptr?"1":"0", 0); ++ptr; for (j = 0; j < MAX_VEHICLE_TYPES; ++j) { snprintf(buf, sizeof(buf), "%hu", swap_short_from_little(ptr)); player_add_info(player, (char*)vehicle_types[j], buf, 0); ptr += 2; } for (j = 0; j < MAX_STATION_TYPES; ++j) { snprintf(buf, sizeof(buf), "%hu", swap_short_from_little(ptr)); player_add_info(player, (char*)station_types[j], buf, 0); ptr += 2; } if (ver != 5) { // connections while(ptr + 1 < end && *ptr) { ++ptr; GET_STRING; // client name debug(3, "%s played by %s", str, player->name); GET_STRING; // id INVALID_IF(ptr + 4 > end); ptr += 4; } ++ptr; // record terminated by zero byte } } // spectators while(ptr + 1 < end && *ptr) { ++ptr; GET_STRING; // client name debug(3, "spectator %s", str); GET_STRING; // id INVALID_IF(ptr + 4 > end); ptr += 4; } ++ptr; // record terminated by zero byte server->next_rule = NO_SERVER_RULES; // we're done server->next_player_info = server->num_players; // we're done } else { malformed_packet( server, "invalid type" ); return PKT_ERROR; } server->retry1 = n_retries; // we're done with this packet, reset retry counter return DONE_AUTO; }
void Replay::load_from_file(const Filename& fn) { clear(); level_filename = fn; // Standardwert: Annahme, Level in Replaydatei unsigned long vm = 0; // version_min erst spaeter setzen wegen add() std::vector <IO::Line> lines; if (!IO::fill_vector_from_file(lines, fn.get_rootful())) { file_not_found = true; holds_level = false; return; } for (IO::LineIt i = lines.begin(); i != lines.end(); ++i) switch(i->type) { case '$': if (i->text1 == gloB->replay_built_required) built_required = i->text2; else if (i->text1 == gloB->replay_permu ) permu = Permu (i->text2); else if (i->text1 == gloB->replay_level_filename) { // We changed the names of the directories on 2012-04-12. Probably // a year from this time on, there shouldn't be any important // replays with the old path anymore. Then, remove this erase() // to finally allow a directory (levels-dir)/levels/ in theory. std::string filestr = i->text2; if (filestr.substr(0, 7) == "levels/") filestr.erase(0, 7); level_filename = Filename( gloB->dir_levels.get_dir_rootless() + filestr); } break; case '#': if (i->text1 == gloB->replay_version_min ) vm = i->nr1; break; case '+': if (i->text1 == gloB->replay_player || i->text1 == gloB->replay_friend) { add_player(i->nr1, LixEn::string_to_style(i->text2), i->text3); if (i->text1 == gloB->replay_player) player_local = i->nr1; } break; case '!': { Data d; d.update = i->nr1; // d.player ist zwar ein char, aber wir lesen ja d.player = i->nr2; // nicht aktiv longs ein, sondern weisen nur zu. d.what = i->nr3; if (i->text1 == gloB->replay_spawnint ) d.action = SPAWNINT; else if (i->text1 == gloB->replay_skill ) d.action = SKILL; else if (i->text1 == gloB->replay_assign ) d.action = ASSIGN; else if (i->text1 == gloB->replay_assign_legacy) d.action = ASSIGN; else if (i->text1 == gloB->replay_aim ) d.action = AIM; else if (i->text1 == gloB->replay_nuke ) d.action = NUKE; add(d); break; } default: break; } // Variablen nach dem Laden zuweisen, damit add() nichts kaputtmacht version_min = vm; // check whether the pointed-to level exists, otherwise use itself // as a fallback level Level pointedto(level_filename); if (pointedto.get_status() == Level::BAD_FILE_NOT_FOUND || pointedto.get_status() == Level::BAD_EMPTY) { level_filename = fn; } // load the replay file itself as a level, to see whether there's a level // in the file itself. This is important e.g. for the extract button. Level itself(fn); if (itself.get_status() == Level::BAD_FILE_NOT_FOUND || itself.get_status() == Level::BAD_EMPTY) { holds_level = false; } else { holds_level = true; if (level_filename == fn) { built_required = Level::get_built(level_filename); } } }
dbref create_player(dbref creator, const char *name, const char *password) { char buf[BUFFER_LEN]; struct object *newp; dbref player; if (!ok_player_name(name) || !ok_password(password) || tp_db_readonly) return NOTHING; /* remove any existing alias with this name */ clear_alias(0, name); /* else he doesn't already exist, create him */ player = new_object(creator); newp = DBFETCH(player); /* initialize everything */ NAME(player) = alloc_string(name); FLAGS(player) = TYPE_PLAYER; if (OkObj(tp_player_prototype) && (Typeof(tp_player_prototype) == TYPE_PLAYER)) { FLAGS(player) = FLAGS(tp_player_prototype); FLAG2(player) = FLAG2(tp_player_prototype); if (tp_pcreate_copy_props) { newp->properties = copy_prop(tp_player_prototype); #ifdef DISKBASE newp->propsfpos = 0; newp->propsmode = PROPS_UNLOADED; newp->propstime = 0; newp->nextold = NOTHING; newp->prevold = NOTHING; dirtyprops(player); #endif } } if (OkObj(tp_player_start)) { DBFETCH(player)->location = tp_player_start; DBFETCH(player)->sp.player.home = tp_player_start; } else { DBFETCH(player)->location = GLOBAL_ENVIRONMENT; DBFETCH(player)->sp.player.home = GLOBAL_ENVIRONMENT; } OWNER(player) = player; newp->exits = NOTHING; newp->sp.player.pennies = tp_start_pennies; newp->sp.player.password = NULL; /* this has to stay here. -hinoserm */ newp->sp.player.curr_prog = NOTHING; newp->sp.player.insert_mode = 0; #ifdef IGNORE_SUPPORT newp->sp.player.ignoretime = 0; #endif /* IGNORE_SUPPORT */ /* set password */ set_password(player, password); /* link him to tp_player_start */ PUSH(player, DBFETCH(tp_player_start)->contents); add_player(player); DBDIRTY(player); DBDIRTY(tp_player_start); sprintf(buf, CNOTE "%s is born!", NAME(player)); anotify_except(DBFETCH(tp_player_start)->contents, NOTHING, buf, player); return player; }