inform the server about turn changes by wml

The server uses this to display the current turn in the lobby.
This commit is contained in:
gfgtdf 2015-06-09 23:29:38 +02:00
parent 6aa3b3bc9a
commit 9e1aae1f79
7 changed files with 88 additions and 14 deletions

View File

@ -707,9 +707,9 @@ WML_HANDLER_FUNCTION(modify_turns, /*event_info*/, cfg)
config::attribute_value current = cfg["current"];
tod_manager& tod_man = *resources::tod_manager;
if(!add.empty()) {
tod_man.modify_turns(add);
tod_man.modify_turns_by_wml(add);
} else if(!value.empty()) {
tod_man.set_number_of_turns(value.to_int(-1));
tod_man.set_number_of_turns_by_wml(value.to_int(-1));
}
// change current turn only after applying mods
if(!current.empty()) {
@ -719,7 +719,7 @@ WML_HANDLER_FUNCTION(modify_turns, /*event_info*/, cfg)
if(new_turn_number_u < 1 || (new_turn_number > tod_man.number_of_turns() && tod_man.number_of_turns() != -1)) {
ERR_NG << "attempted to change current turn number to one out of range (" << new_turn_number << ")" << std::endl;
} else if(new_turn_number_u != current_turn_number) {
tod_man.set_turn(new_turn_number_u, *resources::gamedata);
tod_man.set_turn_by_wml(new_turn_number_u, *resources::gamedata);
resources::screen->new_turn();
}
}

View File

@ -1485,7 +1485,7 @@ int game_lua_kernel::impl_game_config_set(lua_State *L)
modify_int_attrib("rest_heal_amount", game_config::rest_heal_amount = value);
modify_int_attrib("recall_cost", game_config::recall_cost = value);
modify_int_attrib("kill_experience", game_config::kill_experience = value);
modify_int_attrib("last_turn", tod_man().set_number_of_turns(value));
modify_int_attrib("last_turn", tod_man().set_number_of_turns_by_wml(value));
std::string err_msg = "unknown modifiable property of game_config: ";
err_msg += m;

View File

@ -95,6 +95,7 @@ game::game(player_map& players, const network::connection host,
history_(),
description_(NULL),
end_turn_(0),
num_turns_(0),
all_observers_muted_(false),
bans_(),
termination_(),
@ -164,16 +165,16 @@ bool game::is_player(const network::connection player) const {
}
namespace {
std::string describe_turns(int turn, const simple_wml::string_span& num_turns)
std::string describe_turns(int turn, int num_turns)
{
char buf[50];
snprintf(buf,sizeof(buf),"%d/",turn);
if(num_turns == "-1") {
return buf + std::string("-");
char buf[100];
if(num_turns == -1) {
snprintf(buf, sizeof(buf), "%d/-", turn);
} else {
return buf + std::string(num_turns.begin(), num_turns.end());
snprintf(buf, sizeof(buf), "%d/%d", turn, num_turns);
}
return buf;
}
}//anon namespace
@ -317,6 +318,8 @@ void game::start_game(const player_map::const_iterator starter) {
<< ". Current side is: " << side + 1 << ".\n";
}
end_turn_ = (turn - 1) * nsides_ + side - 1;
num_turns_ = lexical_cast_default<int>((*starting_pos( level_.root()))["turns"], -1);
end_turn();
clear_history();
// Send [observer] tags for all observers that are already in the game.
@ -1108,6 +1111,26 @@ void game::process_change_controller_wml(simple_wml::document& data, const playe
//Dont send or store this change, all players should have gotten it by wml.
}
void game::process_change_turns_wml(simple_wml::document& data, const player_map::const_iterator user)
{
if(!started_ || !is_player(user->first))
return;
simple_wml::node const& ctw_node = *data.child("change_turns_wml");
const int current_turn = ctw_node["current"].to_int();
const int num_turns = ctw_node["max"].to_int();
if(num_turns > 10000 || current_turn > 10000) {
//ignore this to prevent errors related to integer overflow.
return;
}
set_current_turn(current_turn);
num_turns_ = num_turns;
assert(this->current_turn() == current_turn);
description_->set_attr_dup("turn", describe_turns(current_turn, num_turns_).c_str());
//Dont send or store this change, all players should have gotten it by wml.
}
bool game::end_turn() {
// It's a new turn every time each side in the game ends their turn.
++end_turn_;
@ -1127,8 +1150,8 @@ bool game::end_turn() {
if (description_ == NULL) {
return false;
}
//FIXME: this "turn" attribute migth be outdated if teh number of turns was changed by wml.
description_->set_attr_dup("turn", describe_turns(current_turn(), level_["turns"]).c_str());
description_->set_attr_dup("turn", describe_turns(current_turn(), num_turns_).c_str());
return true;
}

View File

@ -82,6 +82,11 @@ public:
size_t nplayers() const { return players_.size(); }
size_t nobservers() const { return observers_.size(); }
size_t current_turn() const { return (nsides_ ? end_turn_ / nsides_ + 1 : 0); }
void set_current_turn(int turn)
{
int current_side = end_turn_ % nsides_;
end_turn_ = current_side + nsides_ * ( turn - 1);
}
void mute_all_observers();
@ -167,6 +172,8 @@ public:
void process_whiteboard(simple_wml::document& data, const player_map::const_iterator user);
/** Handles incoming [change_controller_wml] data. */
void process_change_controller_wml(simple_wml::document& data, const player_map::const_iterator user);
/** Handles incoming [change_turns_wml] data. */
void process_change_turns_wml(simple_wml::document& data, const player_map::const_iterator user);
/**
* Set the description to the number of available slots.
@ -390,7 +397,7 @@ private:
simple_wml::node* description_;
int end_turn_;
int num_turns_;
bool all_observers_muted_;
std::vector<std::string> bans_;

View File

@ -2700,6 +2700,10 @@ void server::process_data_game(const network::connection sock,
} else if (data.child("change_controller_wml")) {
g.process_change_controller_wml(data,pl);
return;
} else if (data.child("change_turns_wml")) {
g.process_change_turns_wml(data,pl);
update_game_in_lobby(g);
return;
} else if (data.child("require_random")) {
g.require_random(data,pl);
return;

View File

@ -25,6 +25,10 @@
#include "unit.hpp"
#include "unit_abilities.hpp"
#include "wml_exception.hpp"
#include "resources.hpp"
#include "team.hpp"
#include "network.hpp"
#include "config_assign.hpp"
#include <boost/foreach.hpp>
#include <boost/range/adaptors.hpp>
@ -378,6 +382,31 @@ void tod_manager::set_number_of_turns(int num)
num_turns_ = std::max<int>(num, -1);
}
void tod_manager::update_server_information() const
{
if(resources::controller->current_team().is_local()) {
//TODO: move netword stuff to playturn.cpp
//the currently active side informs the mp server about the turn change.
network::send_data(config_of
("change_turns_wml", config_of
("current", turn_)
("max", num_turns_)
),
0
);
}
}
void tod_manager::modify_turns_by_wml(const std::string& mod)
{
modify_turns(mod);
update_server_information();
}
void tod_manager::set_number_of_turns_by_wml(int num)
{
set_number_of_turns(num);
update_server_information();
}
void tod_manager::set_turn(const int num, boost::optional<game_data &> vars, const bool increase_limit_if_needed)
{
const int new_turn = std::max<int>(num, 1);
@ -393,6 +422,11 @@ void tod_manager::set_turn(const int num, boost::optional<game_data &> vars, con
vars->get_variable("turn_number") = new_turn;
}
void tod_manager::set_turn_by_wml(const int num, boost::optional<game_data &> vars, const bool increase_limit_if_needed)
{
set_turn(num, vars, increase_limit_if_needed);
update_server_information();
}
void tod_manager::set_new_current_times(const int new_current_turn_number)
{
currentTime_ = calculate_current_time(times_.size(), new_current_turn_number, currentTime_);

View File

@ -158,8 +158,14 @@ class tod_manager : public savegame::savegame_config
void modify_turns(const std::string& mod);
void set_number_of_turns(int num);
void update_server_information() const;
void modify_turns_by_wml(const std::string& mod);
void set_number_of_turns_by_wml(int num);
/** Dynamically change the current turn number. */
void set_turn(const int num, boost::optional<game_data&> vars = boost::none, const bool increase_limit_if_needed = true);
/** Dynamically change the current turn number. */
void set_turn_by_wml(const int num, boost::optional<game_data&> vars = boost::none, const bool increase_limit_if_needed = true);
/**
* Function to move to the next turn.