added support for setting passwords on mp games

This commit is contained in:
David White 2007-12-23 05:47:22 +00:00
parent edafdc4435
commit bcffd0fb55
8 changed files with 62 additions and 1 deletions

View file

@ -915,6 +915,9 @@ connect::connect(game_display& disp, const config& game_config, const game_data&
config response;
config& create_game = response.add_child("create_game");
create_game["name"] = params.name;
if(params.password.empty() == false) {
create_game["password"] = params.password;
}
/*
// The number of human-controlled sides is important to know
// to let the server decide how many players can join this game

View file

@ -82,6 +82,7 @@ create::create(game_display& disp, const config &cfg, chat& c, config& gamelist)
launch_game_(disp.video(), _("OK")),
regenerate_map_(disp.video(), _("Regenerate")),
generator_settings_(disp.video(), _("Settings...")),
password_button_(disp.video(), _("Set Password...")),
era_combo_(disp, std::vector<std::string>()),
vision_combo_(disp, std::vector<std::string>()),
name_entry_(disp.video(), 32),
@ -316,6 +317,11 @@ void create::process_event()
}
}
if(password_button_.pressed()) {
gui::show_dialog(disp_, NULL, _("Set Password"),
_("Set the password that people wanting to join your game as players must enter."), gui::OK_ONLY, NULL, NULL, _("Password: "), &parameters_.password);
}
// Turns per game
const int cur_turns = turns_slider_.value();
@ -684,6 +690,8 @@ void create::layout_children(const SDL_Rect& rect)
ypos += era_label_.height() + border_size;
era_combo_.set_location(xpos, ypos);
ypos += era_combo_.height() + border_size;
password_button_.set_location(xpos, ypos);
ypos += password_button_.height() + border_size;
#ifdef MP_VISION_OPTIONAL
vision_combo_.set_location(xpos, ypos);

View file

@ -58,6 +58,7 @@ public:
void reset() {
name = "";
era = "";
password = "";
num_turns = 0;
village_gold = 0;
xp_modifier = 0;
@ -75,6 +76,7 @@ public:
std::string name;
std::string era;
std::string password;
int num_turns;
int village_gold;
@ -153,6 +155,7 @@ private:
gui::button launch_game_;
gui::button regenerate_map_;
gui::button generator_settings_;
gui::button password_button_;
gui::combo era_combo_;
gui::combo vision_combo_;

View file

@ -371,6 +371,7 @@ void gamebrowser::set_game_items(const config& cfg, const config& game_config)
for(game = games.begin(); game != games.end(); ++game) {
bool verified = true;
games_.push_back(game_item());
games_.back().password_required = (**game)["password"] == "yes";
if((**game)["mp_era"] != "") {
const config* const era_cfg = game_config.find_child("era", "id", (**game)["mp_era"]);
utils::string_map symbols;
@ -471,6 +472,9 @@ void gamebrowser::set_game_items(const config& cfg, const config& game_config)
if(slots != "")
games_.back().status = std::string(ngettext(_("Vacant Slot:"), _("Vacant Slots:"),
games_.back().vacant_slots)) + " " + slots;
if(games_.back().vacant_slots > 0 && games_.back().password_required) {
games_.back().status += std::string(" (") + std::string(_("Password Required")) + ")";
}
}
games_.back().use_map_settings = ((**game)["mp_use_map_settings"] == "yes");
@ -714,6 +718,16 @@ void lobby::process_event()
if(!games_menu_.empty() && selected >= 0) {
gamebrowser::game_item game = games_menu_.selected_game();
std::string password;
if(join && game.password_required) {
const int res = gui::show_dialog(disp_, NULL, _("Password Required"),
_("Joining this game requires a password."),
gui::OK_CANCEL, NULL, NULL, _("Password: "), &password);
if(res != 0) {
return;
}
}
config response;
config& join = response.add_child("join");
join["id"] = game.id;
@ -723,6 +737,10 @@ void lobby::process_event()
else{
join["observe"] = "no";
}
if(!password.empty()) {
join["password"] = password;
}
network::send_data(response, 0, true);
if(observe) {

View file

@ -52,6 +52,7 @@ public:
bool observers;
bool use_map_settings;
bool verified;
bool password_required;
};
gamebrowser(CVideo& video,const config* map_hashes);
void scroll(unsigned int pos);

View file

@ -958,6 +958,13 @@ void game::reset_history() {
end_turn_ = 0;
}
void game::set_description(config* desc) {
description_ = desc;
if(!password_.empty()) {
(*description_)["password"] = "yes";
}
}
void game::end_game(const config& games_and_users_list) {
const user_vector& users = all_game_users();
// Set the availability status for all quitting players.

View file

@ -111,9 +111,14 @@ public:
//! Functions to set/get the address of the game's summary description as
//! sent to players in the lobby.
void set_description(config* desc) { description_ = desc; }
void set_description(config* desc);
config* description() const { return description_; }
void set_password(const std::string& passwd) { password_ = passwd; }
bool password_matches(const std::string& passwd) const {
return password_.empty() || passwd == password_;
}
const std::string& termination_reason() const {
static const std::string aborted = "aborted";
return termination_.empty() ? aborted : termination_;
@ -168,6 +173,7 @@ private:
int id_;
//! The name of the game.
std::string name_;
std::string password_;
network::connection owner_;
user_vector players_;
user_vector observers_;

View file

@ -856,12 +856,17 @@ void server::process_data_lobby(const network::connection sock, const config& da
if (data.child("create_game")) {
const std::string game_name = (*data.child("create_game"))["name"];
const std::string game_password = (*data.child("create_game"))["password"];
DBG_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
<< "\tcreates a new game: " << game_name << ".\n";
// Create the new game, remove the player from the lobby
// and set the player as the host/owner.
games_.push_back(game(players_, sock, game_name));
game& g = games_.back();
if(game_password.empty() == false) {
g.set_password(game_password);
}
g.level() = (*data.child("create_game"));
lobby_.remove_player(sock);
lobby_.send_data(games_and_users_list_diff());
@ -872,6 +877,7 @@ void server::process_data_lobby(const network::connection sock, const config& da
if (data.child("join")) {
const std::string& id = (*data.child("join"))["id"];
const bool observer = ((*data.child("join"))["observe"] == "yes");
const std::string& password = (*data.child("join"))["password"];
int game_id;
try {
game_id = lexical_cast<int>(id);
@ -903,6 +909,15 @@ void server::process_data_lobby(const network::connection sock, const config& da
"You are banned from this game."), sock, send_gzipped_);
network::send_data(games_and_users_list_, sock, send_gzipped_);
return;
} else if(!observer && !g->password_matches(password)) {
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
<< "\tattempted to join game:\t\"" << g->name() << "\" ("
<< id << ") with bad password\n";
network::send_data(config("leave_game"), sock, send_gzipped_);
network::send_data(lobby_.construct_server_message(
"Incorrect password"), sock, send_gzipped_);
network::send_data(games_and_users_list_, sock, send_gzipped_);
return;
} else if (observer && !g->allow_observers()) {
WRN_SERVER << network::ip_address(sock) << "\t" << pl->second.name()
<< "\tattempted to observe game:\t\"" << g->name() << "\" ("