improved handling of users trying to join a full game that disallows observers

This commit is contained in:
Gunter Labes 2008-11-11 22:41:10 +00:00
parent 36aedc719e
commit 193f375959
3 changed files with 25 additions and 18 deletions

View file

@ -898,22 +898,22 @@ bool game::end_turn() {
return true;
}
void game::add_player(const network::connection player, bool observer) {
bool game::add_player(const network::connection player, bool observer) {
if(is_member(player)) {
ERR_GAME << "ERROR: Player is already in this game. (socket: "
<< player << ")\n";
return;
return false;
}
// Hack to handle the pseudo games lobby_ and not_logged_in_.
if (owner_ == 0) {
observers_.push_back(player);
return;
return true;
}
const player_map::iterator user = player_info_->find(player);
if (user == player_info_->end()) {
ERR_GAME << "ERROR: Could not find user in player_info_. (socket: "
<< owner_ << ")\n";
return;
return false;
}
user->second.mark_available(id_, name_);
DBG_GAME << debug_player_info();
@ -924,7 +924,7 @@ void game::add_player(const network::connection player, bool observer) {
players_.push_back(player);
send_and_record_server_message((user->second.name() + " has joined the game.").c_str(), player);
} else if (!allow_observers()) {
return; //false;
return false;
} else {
if (!observer) became_observer = true;
DBG_GAME << "adding observer...\n";
@ -964,6 +964,7 @@ void game::add_player(const network::connection player, bool observer) {
// in case someone took the last slot right before this player
send_server_message("You are an observer.", player);
}
return true;
}
bool game::remove_player(const network::connection player, const bool disconnect) {

View file

@ -82,12 +82,17 @@ public:
*/
network::connection ban_user(const simple_wml::node& ban, const player_map::const_iterator banner);
void add_player(const network::connection player, bool observer = false);
/**
* Add a user to the game.
*
* @return True iff the user successfully joined the game.
*/
bool add_player(const network::connection player, bool observer = false);
/**
* Removes a user from the game.
*
* @return True iff the game ends that is if there are
* @return True iff the game ends. That is, if there are
* no more players or the host left on a not yet
* started game.
*/
@ -131,7 +136,7 @@ public:
/**
* Set the description to the number of available slots.
*
* @Returns True iff the number of slots has changed.
* @returns True iff the number of slots has changed.
*/
bool describe_slots();

View file

@ -1752,20 +1752,22 @@ void server::process_data_lobby(const network::connection sock,
lobby_.send_server_message("Incorrect password.", sock);
send_doc(games_and_users_list_, sock);
return;
} else if (observer && !(*g)->allow_observers()) {
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
<< "\tattempted to observe game:\t\"" << (*g)->name() << "\" ("
<< game_id << ") which doesn't allow observers.\n";
send_doc(leave_game_doc, sock);
lobby_.send_server_message("Attempt to observe a game that doesn't allow observers.", sock);
send_doc(games_and_users_list_, sock);
return;
} else if (!(*g)->level_init()) {
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
<< "\tattempted to join uninitialized game:\t\"" << (*g)->name()
<< "\" (" << game_id << ").\n";
send_doc(leave_game_doc, sock);
lobby_.send_server_message("Attempt to observe a game that doesn't allow observers.", sock);
lobby_.send_server_message("Attempt to join an uninitialized game.", sock);
send_doc(games_and_users_list_, sock);
return;
}
bool joined = (*g)->add_player(sock, observer);
if (!joined) {
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
<< "\tattempted to observe game:\t\"" << (*g)->name() << "\" ("
<< game_id << ") which doesn't allow observers.\n";
send_doc(leave_game_doc, sock);
lobby_.send_server_message("Attempt to observe a game that doesn't allow observers. (You probably joined the game shortly after it filled up.)", sock);
send_doc(games_and_users_list_, sock);
return;
}
@ -1773,7 +1775,6 @@ void server::process_data_lobby(const network::connection sock,
<< "\tjoined game:\t\"" << (*g)->name()
<< "\" (" << game_id << (observer ? ") as an observer.\n" : ").\n");
lobby_.remove_player(sock);
(*g)->add_player(sock, observer);
(*g)->describe_slots();
//send notification of changes to the game and user