Skip to content

Commit

Permalink
rewrite team::change_controller_by_wml to use nonthrowing functions
Browse files Browse the repository at this point in the history
we use enum.parse() instead of lexical_cast so that we don't have to
catch bad_enum_cast anymore.
  • Loading branch information
gfgtdf committed Feb 28, 2016
1 parent 0d2fe9b commit 4a1b330
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/team.cpp
Expand Up @@ -510,26 +510,24 @@ namespace

void team::change_controller_by_wml(const std::string& new_controller_string)
{
try
{
CONTROLLER new_controller = lexical_cast<CONTROLLER> (new_controller_string);
if(new_controller == CONTROLLER::EMPTY && resources::controller->current_side() == this->side()) {
//We don't allow changing the currently active side to "null" controlled.
throw bad_enum_cast(new_controller_string, "CONTROLLER"); //catched below
}
config choice = synced_context::ask_server_choice(controller_server_choice(new_controller, *this));
if(!new_controller.parse(choice["controller"])) {
ERR_NG << "recieved an invalid controller string from the server" << choice["controller"] << std::endl;
}
if(!resources::controller->is_replay()) {
set_local(choice["is_local"].to_bool());
}
change_controller(new_controller);
}
catch(const bad_enum_cast&)
{
CONTROLLER new_controller;
if(!new_controller.parse(new_controller_string)) {
ERR_NG << "ignored attempt to change controller to " << new_controller_string << std::endl;
return;
}
if(new_controller == CONTROLLER::EMPTY && resources::controller->current_side() == this->side()) {
ERR_NG << "ignored attempt to change the currently playing side's controller to 'null'" << std::endl;
return;
}
config choice = synced_context::ask_server_choice(controller_server_choice(new_controller, *this));
if(!new_controller.parse(choice["controller"])) {
//TODO: this should be more than a ERR_NG message.
ERR_NG << "recieved an invalid controller string from the server" << choice["controller"] << std::endl;
}
if(!resources::controller->is_replay()) {
set_local(choice["is_local"].to_bool());
}
change_controller(new_controller);
}

void team::change_team(const std::string &name, const t_string &user_name)
Expand Down

0 comments on commit 4a1b330

Please sign in to comment.