Skip to content

Commit

Permalink
cancel faction_select when the game starts
Browse files Browse the repository at this point in the history
or when the hosts assigned  control of  that side to another player.
  • Loading branch information
gfgtdf committed Jun 29, 2018
1 parent e72a92a commit b8a119d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/gui/dialogs/multiplayer/faction_select.cpp
Expand Up @@ -53,11 +53,19 @@ faction_select::faction_select(ng::flg_manager& flg_manager, const std::string&
, last_faction_(flg_manager.current_faction_index())
, last_leader_(flg_manager.current_leader_index())
, last_gender_(flg_manager.current_gender_index())
, w_(nullptr)
{
}

void faction_select::cancel()
{
if(w_) {
w_->set_retval(retval::CANCEL);
}
}
void faction_select::pre_show(window& window)
{
w_ = &window;
find_widget<label>(&window, "starting_pos", false).set_label(std::to_string(side_));

//
Expand Down
4 changes: 4 additions & 0 deletions src/gui/dialogs/multiplayer/faction_select.hpp
Expand Up @@ -32,6 +32,9 @@ class faction_select : public modal_dialog

DEFINE_SIMPLE_EXECUTE_WRAPPER(faction_select)

void cancel();

int get_side_num() const { return side_; }
private:
ng::flg_manager& flg_manager_;

Expand All @@ -43,6 +46,7 @@ class faction_select : public modal_dialog

const int last_faction_, last_leader_, last_gender_;

gui2::window* w_;
/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const override;

Expand Down
23 changes: 22 additions & 1 deletion src/gui/dialogs/multiplayer/mp_join_game.cpp
Expand Up @@ -45,6 +45,7 @@
#include "mp_ui_alerts.hpp"
#include "statistics.hpp"
#include "units/types.hpp"
#include "utils/scope_exit.hpp"
#include "wesnothd_connection.hpp"


Expand Down Expand Up @@ -72,6 +73,7 @@ mp_join_game::mp_join_game(saved_game& state, mp::lobby_info& lobby_info, wesnot
, observe_game_(observe_game)
, stop_updates_(false)
, player_list_(nullptr)
, open_flg_dialog_(nullptr)
{
set_show_even_without_video(true);
}
Expand Down Expand Up @@ -350,9 +352,15 @@ void mp_join_game::show_flg_select(int side_num)

ng::flg_manager flg(era_factions, side_choice, lock_settings, use_map_settings, saved_game);


gui2::dialogs::faction_select dlg(flg, color, side_num);
dlg.show();


{
open_flg_dialog_ = &dlg;
utils::scope_exit se([this](){ open_flg_dialog_ = nullptr; });
dlg.show();
}
if(dlg.get_retval() != gui2::retval::OK) {
return;
}
Expand Down Expand Up @@ -518,11 +526,20 @@ void mp_join_game::network_handler(window& window)
}

if(data["failed"].to_bool()) {
if(open_flg_dialog_) {
open_flg_dialog_->cancel();
}
window.set_retval(retval::CANCEL);
} else if(data.child("start_game")) {
if(open_flg_dialog_) {
open_flg_dialog_->cancel();
}
level_["started"] = true;
window.set_retval(retval::OK);
} else if(data.child("leave_game")) {
if(open_flg_dialog_) {
open_flg_dialog_->cancel();
}
window.set_retval(retval::CANCEL);
}

Expand All @@ -537,6 +554,10 @@ void mp_join_game::network_handler(window& window)
if(config& side_to_change = get_scenario().find_child("side", "side", change["side"])) {
side_to_change.merge_with(change);
}
if(open_flg_dialog_ && open_flg_dialog_->get_side_num() == change["side"].to_int()) {
open_flg_dialog_->cancel();
}

} else if(data.has_child("scenario") || data.has_child("snapshot") || data.child("next_scenario")) {
level_ = first_scenario_ ? data : data.child("next_scenario");

Expand Down
3 changes: 3 additions & 0 deletions src/gui/dialogs/multiplayer/mp_join_game.hpp
Expand Up @@ -31,6 +31,7 @@ class tree_view_node;

namespace dialogs
{
class faction_select;

class mp_join_game : public modal_dialog, private plugin_executor
{
Expand Down Expand Up @@ -78,6 +79,8 @@ class mp_join_game : public modal_dialog, private plugin_executor
std::map<std::string, tree_view_node*> team_tree_map_;

std::unique_ptr<player_list_helper> player_list_;

gui2::dialogs::faction_select* open_flg_dialog_;
};

} // namespace dialogs
Expand Down
30 changes: 30 additions & 0 deletions src/utils/scope_exit.hpp
@@ -0,0 +1,30 @@
/*
Copyright (C) 2003 - 2018 the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#pragma once

#include "global.hpp"

#include <functional>

namespace utils {

class scope_exit {
//TODO: with c++17 we could make this a template class with 'F f_' instead of 'std::function<void ()> f_';
std::function<void ()> f_;
public:
template<typename F>
explicit scope_exit(F&& f) : f_(f) {}
~scope_exit() { if(f_) { f_(); }}
};
} // namespace utils

0 comments on commit b8a119d

Please sign in to comment.