split process_network_data() into smaller functions

This commit is contained in:
gfgtdf 2024-03-03 00:01:59 +01:00
parent 9d7fd59cc8
commit b79e305419
2 changed files with 194 additions and 182 deletions

View File

@ -510,56 +510,55 @@ turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_data_impl(cons
}
else if (auto change = cfg.optional_child("change_controller"))
{
if(change->empty()) {
ERR_NW << "Bad [change_controller] signal from server, [change_controller] tag was empty.";
return turn_info::PROCESS_CONTINUE;
return process_network_change_controller_impl(*change);
}
const int side = change["side"].to_int();
const bool is_local = change["is_local"].to_bool();
const std::string player = change["player"];
const std::string controller_type = change["controller"];
const std::size_t index = side - 1;
if(index >= gamestate().board_.teams().size()) {
ERR_NW << "Bad [change_controller] signal from server, side out of bounds: " << change->debug();
return turn_info::PROCESS_CONTINUE;
}
const team & tm = gamestate().board_.teams().at(index);
const bool was_local = tm.is_local();
gamestate().board_.side_change_controller(side, is_local, player, controller_type);
if (!was_local && tm.is_local()) {
on_not_observer();
}
// TODO: can we replace this with just a call to play_controller::update_viewing_player() ?
auto disp_set_team = [](int side_index) {
const bool side_changed = static_cast<int>(display::get_singleton()->viewing_team()) != side_index;
display::get_singleton()->set_team(side_index);
if(side_changed) {
display::get_singleton()->queue_rerender();
}
};
if (gamestate().board_.is_observer() || (gamestate().board_.teams())[display::get_singleton()->playing_team()].is_local_human()) {
disp_set_team(display::get_singleton()->playing_team());
} else if (tm.is_local_human()) {
disp_set_team(side - 1);
}
resources::whiteboard->on_change_controller(side,tm);
display::get_singleton()->labels().recalculate_labels();
const bool restart = game_display::get_singleton()->playing_side() == side && (was_local || tm.is_local());
return restart ? turn_info::PROCESS_RESTART_TURN : turn_info::PROCESS_CONTINUE;
}
else if (auto side_drop_c = cfg.optional_child("side_drop"))
{
return process_network_side_drop_impl(*side_drop_c);
}
// The host has ended linger mode in a campaign -> enable the "End scenario" button
// and tell we did get the notification.
else if (cfg.has_child("notify_next_scenario")) {
if(chat_only) {
return turn_info::PROCESS_CANNOT_HANDLE;
}
return turn_info::PROCESS_END_LINGER;
}
//If this client becomes the new host, notify the play_controller object about it
else if (cfg.has_child("host_transfer")) {
mp_info_->is_host = true;
if(is_linger_mode()) {
end_turn_enable(true);
}
}
else
{
ERR_NW << "found unknown command:\n" << cfg.debug();
}
return turn_info::PROCESS_CONTINUE;
}
turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_turn_impl(const config& t, bool chat_only)
{
//t can contain a [command] or a [upload_log]
assert(t.all_children_count() == 1);
if(!t.child_or_empty("command").has_child("speak") && chat_only) {
return turn_info::PROCESS_CANNOT_HANDLE;
}
/** @todo FIXME: Check what commands we execute when it's our turn! */
//note, that this function might call itself recursively: do_replay -> ... -> get_user_choice -> ... -> playmp_controller::pull_remote_choice -> sync_network -> handle_turn
resources::recorder->add_config(t, replay::MARK_AS_SENT);
turn_info::PROCESS_DATA_RESULT retv = replay_to_process_data_result(do_replay());
return retv;
}
turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_side_drop_impl(const config& side_drop_c)
{
// Only the host receives this message when a player leaves/disconnects.
const int side_drop = side_drop_c["side_num"].to_int(0);
std::size_t index = side_drop -1;
@ -579,7 +578,7 @@ turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_data_impl(cons
if (ctrl == side_controller::type::ai) {
gamestate().board_.side_drop_to(side_drop, *ctrl);
return restart ? turn_info::PROCESS_RESTART_TURN:turn_info::PROCESS_CONTINUE;
return restart ? turn_info::PROCESS_RESTART_TURN : turn_info::PROCESS_CONTINUE;
}
//null controlled side cannot be dropped because they aren't controlled by anyone.
else if (ctrl != side_controller::type::human) {
@ -695,49 +694,60 @@ turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_data_impl(cons
break;
}
}
}
// The host has ended linger mode in a campaign -> enable the "End scenario" button
// and tell we did get the notification.
else if (cfg.has_child("notify_next_scenario")) {
if(chat_only) {
return turn_info::PROCESS_CANNOT_HANDLE;
}
return turn_info::PROCESS_END_LINGER;
}
//If this client becomes the new host, notify the play_controller object about it
else if (cfg.has_child("host_transfer")) {
mp_info_->is_host = true;
if(is_linger_mode()) {
end_turn_enable(true);
}
}
else
{
ERR_NW << "found unknown command:\n" << cfg.debug();
}
return turn_info::PROCESS_CONTINUE;
}
turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_turn_impl(const config& t, bool chat_only)
turn_info::PROCESS_DATA_RESULT playmp_controller::process_network_change_controller_impl(const config& change)
{
//t can contain a [command] or a [upload_log]
assert(t.all_children_count() == 1);
if(!t.child_or_empty("command").has_child("speak") && chat_only) {
return turn_info::PROCESS_CANNOT_HANDLE;
if(change.empty()) {
ERR_NW << "Bad [change_controller] signal from server, [change_controller] tag was empty.";
return turn_info::PROCESS_CONTINUE;
}
/** @todo FIXME: Check what commands we execute when it's our turn! */
//note, that this function might call itself recursively: do_replay -> ... -> get_user_choice -> ... -> playmp_controller::pull_remote_choice -> sync_network -> handle_turn
resources::recorder->add_config(t, replay::MARK_AS_SENT);
turn_info::PROCESS_DATA_RESULT retv = replay_to_process_data_result(do_replay());
return retv;
const int side = change["side"].to_int();
const bool is_local = change["is_local"].to_bool();
const std::string player = change["player"];
const std::string controller_type = change["controller"];
const std::size_t index = side - 1;
if(index >= gamestate().board_.teams().size()) {
ERR_NW << "Bad [change_controller] signal from server, side out of bounds: " << change.debug();
return turn_info::PROCESS_CONTINUE;
}
const team & tm = gamestate().board_.teams().at(index);
const bool was_local = tm.is_local();
gamestate().board_.side_change_controller(side, is_local, player, controller_type);
if (!was_local && tm.is_local()) {
on_not_observer();
}
// TODO: can we replace this with just a call to play_controller::update_viewing_player() ?
auto disp_set_team = [](int side_index) {
const bool side_changed = static_cast<int>(display::get_singleton()->viewing_team()) != side_index;
display::get_singleton()->set_team(side_index);
if(side_changed) {
display::get_singleton()->queue_rerender();
}
};
if (gamestate().board_.is_observer() || (gamestate().board_.teams())[display::get_singleton()->playing_team()].is_local_human()) {
disp_set_team(display::get_singleton()->playing_team());
} else if (tm.is_local_human()) {
disp_set_team(side - 1);
}
resources::whiteboard->on_change_controller(side,tm);
display::get_singleton()->labels().recalculate_labels();
const bool restart = game_display::get_singleton()->playing_side() == side && (was_local || tm.is_local());
return restart ? turn_info::PROCESS_RESTART_TURN : turn_info::PROCESS_CONTINUE;
}
turn_info::PROCESS_DATA_RESULT playmp_controller::sync_network()
{
//there should be nothing left on the replay and we should get turn_info::PROCESS_CONTINUE back.

View File

@ -68,6 +68,8 @@ private:
void process_network_data(bool chat_only = false);
turn_info::PROCESS_DATA_RESULT process_network_data_impl(const config& cfg, bool chat_only = false);
turn_info::PROCESS_DATA_RESULT process_network_turn_impl(const config& t, bool chat_only = false);
turn_info::PROCESS_DATA_RESULT process_network_side_drop_impl(const config& t);
turn_info::PROCESS_DATA_RESULT process_network_change_controller_impl(const config& );
turn_info::PROCESS_DATA_RESULT sync_network();
turn_info::PROCESS_DATA_RESULT process_network_data_from_reader();