MP Lobby: moved turn/slot data formatting from server to client

This commit is contained in:
Charles Dang 2018-02-20 14:36:17 +11:00
parent f45cd3eeb8
commit 88e5c49f5d
3 changed files with 72 additions and 49 deletions

View file

@ -204,7 +204,7 @@ game_info::game_info(const config& game, const config& game_config, const std::v
, vision()
, status()
, time_limit()
, vacant_slots(lexical_cast_default<int>(game["slots"], 0)) // Can't use to_int() here.
, vacant_slots()
, current_turn(0)
, reloaded(game["savegame"].to_bool())
, started(false)
@ -411,19 +411,33 @@ game_info::game_info(const config& game, const config& game_config, const std::v
verified = false;
}
std::string turn = game["turn"];
// These should always be present in the data the server sends, but may or may not be empty.
// I'm just using child_or_empty here to preempt any cases where they might not be included.
const config& s = game.child_or_empty("slot_data");
const config& t = game.child_or_empty("turn_data");
if(!turn.empty()) {
if(!s.empty()) {
started = false;
vacant_slots = s["vacant"].to_unsigned();
if(vacant_slots > 0) {
status = formatter() << _n("Vacant Slot:", "Vacant Slots:", vacant_slots) << " " << vacant_slots << "/" << s["max"];
} else {
// TODO: status message for no vacant sides!
}
}
if(!t.empty()) {
started = true;
const std::string current_turn_string = turn.substr(0, turn.find_first_of('/'));
current_turn = lexical_cast<unsigned int>(current_turn_string);
current_turn = t["current"].to_unsigned();
const int max_turns = t["max"].to_int();
status = _("Turn") + " " + turn;
} else {
started = false;
if(vacant_slots > 0) {
status = _n("Vacant Slot:", "Vacant Slots:", vacant_slots) + " " + game["slots"];
if(max_turns > -1) {
status = formatter() << _("Turn") << " " << t["current"] << "/" << max_turns;
} else {
status = formatter() << _("Turn") << " " << t["current"];
}
}

View file

@ -886,23 +886,38 @@ void window::layout()
log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
point size = get_best_size();
const point mouse = get_mouse_position();
variables_.add("mouse_x", wfl::variant(mouse.x));
variables_.add("mouse_y", wfl::variant(mouse.y));
variables_.add("window_width", wfl::variant(0));
variables_.add("window_height", wfl::variant(0));
variables_.add("best_window_width", wfl::variant(size.x));
variables_.add("best_window_height", wfl::variant(size.y));
variables_.add("size_request_mode", wfl::variant("maximum"));
get_screen_size_variables(variables_);
const int maximum_width = automatic_placement_ ? maximum_width_
? std::min(maximum_width_, settings::screen_width)
: settings::screen_width
: w_(variables_, &functions_);
int maximum_width = 0;
int maximum_height = 0;
const int maximum_height = automatic_placement_ ? maximum_height_
? std::min(maximum_height_, settings::screen_height)
: settings::screen_height
: h_(variables_, &functions_);
if(automatic_placement_) {
if(maximum_width_ > 0) {
maximum_width = std::min(maximum_width_, settings::screen_width);
} else {
maximum_width = settings::screen_width;
}
if(maximum_height_ > 0) {
maximum_height = std::min(maximum_height_, settings::screen_height);
} else {
maximum_height = settings::screen_height;
}
} else {
maximum_width = w_(variables_, &functions_);
maximum_height = h_(variables_, &functions_);
}
/***** Handle click dismiss status. *****/
button* click_dismiss_button = nullptr;
@ -987,8 +1002,7 @@ void window::layout()
}
/***** Get the best location for the window *****/
point size = get_best_size();
size = get_best_size();
assert(size.x <= maximum_width && size.y <= maximum_height);
point origin(0, 0);

View file

@ -193,23 +193,6 @@ bool game::is_player(const socket_ptr& player) const
return std::find(players_.begin(), players_.end(), player) != players_.end();
}
namespace
{
std::string describe_turns(int turn, int num_turns)
{
std::ostringstream buf;
buf << turn;
// If the game has a turn limit.
if(num_turns > -1) {
buf << '/' << num_turns;
}
return buf.str();
}
} // anon namespace
std::string game::username(const socket_ptr& player) const
{
const auto iter = player_connections_.find(player);
@ -386,7 +369,6 @@ void game::start_game(const socket_ptr& starter)
void game::update_game()
{
started_ = false;
description_->set_attr("turn", "");
update_side_data();
describe_slots();
@ -706,16 +688,15 @@ bool game::describe_slots()
++i;
}
std::ostringstream buf;
buf << available_slots << '/' << num_sides;
std::string descr = buf.str();
if((*description_)["slots"] != descr) {
description_->set_attr_dup("slots", descr.c_str());
return true;
} else {
return false;
simple_wml::node* slots_cfg = description_->child("slot_data");
if(!slots_cfg) {
slots_cfg = &description_->add_child("slot_data");
}
slots_cfg->set_attr_int("vacant", available_slots);
slots_cfg->set_attr_int("max", num_sides);
return true;
}
bool game::player_is_banned(const socket_ptr& sock) const
@ -1320,7 +1301,14 @@ void game::process_change_turns_wml(simple_wml::document& data, const socket_ptr
num_turns_ = num_turns;
assert(static_cast<int>(this->current_turn()) == current_turn);
description_->set_attr_dup("turn", describe_turns(current_turn, num_turns_).c_str());
simple_wml::node* turns_cfg = description_->child("turn_data");
if(!turns_cfg) {
turns_cfg = &description_->add_child("turn_data");
}
ctw_node.copy_into(*turns_cfg);
// Don't send or store this change, all players should have gotten it by wml.
}
@ -1351,7 +1339,14 @@ bool game::end_turn()
return false;
}
description_->set_attr_dup("turn", describe_turns(current_turn(), num_turns_).c_str());
simple_wml::node* turns_cfg = description_->child("turn_data");
if(!turns_cfg) {
turns_cfg = &description_->add_child("turn_data");
}
turns_cfg->set_attr_int("current", current_turn());
turns_cfg->set_attr_int("max", num_turns_);
return true;
}