record and send player playing/observing status in wesnothd

This commit is contained in:
Tomasz Śniatowski 2009-07-20 18:38:11 +01:00
parent 52366e43f9
commit a2d950c4b1
3 changed files with 47 additions and 4 deletions

View file

@ -74,6 +74,7 @@ game::game(player_map& players, const network::connection host,
}
// Mark the host as unavailable in the lobby.
pl->second.mark_available(id_, name_);
p1->second.set_status(player::PLAYING);
}
game::~game()
@ -309,8 +310,10 @@ void game::update_side_data() {
}
if (side_found) {
players_.push_back(*user);
info->second.set_status(player::PLAYING);
} else {
observers_.push_back(*user);
info->second.set_status(player::OBSERVING);
}
}
DBG_GAME << debug_player_info();
@ -400,6 +403,7 @@ void game::transfer_side_control(const network::connection sock, const simple_wm
if (std::find(sides_.begin(), sides_.end(), old_player) == sides_.end()
&& is_player(old_player)) {
observers_.push_back(old_player);
oldplayer->second.set_status(player::OBSERVING);
players_.erase(std::remove(players_.begin(), players_.end(), old_player), players_.end());
// Tell others that the player becomes an observer.
send_and_record_server_message((old_player_name + " becomes an observer.").c_str());
@ -413,6 +417,7 @@ void game::transfer_side_control(const network::connection sock, const simple_wm
// If we gave the new side to an observer add him to players_.
if (is_observer(newplayer->first)) {
players_.push_back(newplayer->first);
newplayer->second.set_status(player::PLAYING);
observers_.erase(std::remove(observers_.begin(), observers_.end(), newplayer->first), observers_.end());
// Send everyone but the new player the observer_quit message.
send_observerquit(newplayer);
@ -925,6 +930,7 @@ bool game::add_player(const network::connection player, bool observer, bool admi
if (!started_ && !observer && take_side(user)) {
DBG_GAME << "adding player...\n";
players_.push_back(player);
user->second.set_status(player::PLAYING);
send_and_record_server_message((user->second.name() + " has joined the game.").c_str(), player);
} else if (!allow_observers() && !admin) {
return false;
@ -945,6 +951,7 @@ bool game::add_player(const network::connection player, bool observer, bool admi
<< (observer || became_observer ? " as an observer" : "")
<< ". (socket: " << player << ")\n";
user->second.mark_available(id_, name_);
user->second.set_status((observer || became_observer) ? player::OBSERVING : player::PLAYING);
DBG_GAME << debug_player_info();
// Send the user the game data.
//std::cerr << "SENDING LEVEL {{{" << level_.output() << "}}}\n";
@ -1014,7 +1021,9 @@ bool game::remove_player(const network::connection player, const bool disconnect
// Don't mark_available() since the player got already removed from the
// games_and_users_list_.
if (!disconnect) user->second.mark_available();
if (!disconnect) {
user->second.mark_available();
}
if (observer) {
send_observerquit(user);
} else {
@ -1041,6 +1050,7 @@ bool game::remove_player(const network::connection player, const bool disconnect
// Check whether the host is actually a player and make him one if not.
if (!is_player(owner_)) {
DBG_GAME << "making the owner a player...\n";
o->second.set_status(player::PLAYING);
observers_.erase(std::remove(observers_.begin(), observers_.end(), owner_), observers_.end());
players_.push_back(owner_);
send_observerquit(o);

View file

@ -27,17 +27,42 @@ wesnothd::player::player(const std::string& n, simple_wml::node& cfg,
, messages_since_flood_start_(0)
, MaxMessages(max_messages)
, TimePeriod(time_period)
, status_(LOBBY)
{
cfg_.set_attr_dup("name", n.c_str());
mark_available();
mark_registered(registered);
}
void wesnothd::player::set_status(wesnothd::player::STATUS status)
{
status_ = status;
switch (status)
{
case wesnothd::player::LOBBY:
cfg_.set_attr("status", "lobby");
break;
case wesnothd::player::PLAYING:
cfg_.set_attr("status", "playing");
break;
case wesnothd::player::OBSERVING:
cfg_.set_attr("status", "observing");
break;
default:
cfg_.set_attr("status", "unknown");
}
}
// keep 'available' and game name ('location') for backward compatibility
void wesnothd::player::mark_available(const int game_id,
const std::string location)
{
cfg_.set_attr("available", (game_id == 0) ? "yes" : "no");
if (game_id == 0) {
cfg_.set_attr("available", "yes");
set_status(LOBBY);
} else {
cfg_.set_attr("available", "no");
}
cfg_.set_attr_dup("game_id", lexical_cast<std::string>(game_id).c_str());
cfg_.set_attr_dup("location", location.c_str());
}

View file

@ -30,7 +30,15 @@ class game;
class player
{
public:
player(const std::string& n, simple_wml::node& cfg, bool registered, const size_t max_messages=4, const size_t time_period=10, const bool sp=false);
enum STATUS {
LOBBY,
PLAYING,
OBSERVING
};
player(const std::string& n, simple_wml::node& cfg, bool registered, const size_t max_messages=4, const size_t time_period=10, const bool sp=false);
void set_status(STATUS status);
// mark a player as member of the game 'game_id' or as located in the lobby
void mark_available(const int game_id=0, const std::string location="");
@ -72,7 +80,7 @@ private:
unsigned int messages_since_flood_start_;
const size_t MaxMessages;
const time_t TimePeriod;
game* game_;
STATUS status_;
};
} //namespace wesnothd