move message processing to its own function...

...and also process pre-game messages properly

allow [stop_updates] through
This commit is contained in:
Gunter Labes 2008-02-08 16:38:30 +00:00
parent dbcae1d87e
commit aa14678eb0
3 changed files with 60 additions and 46 deletions

View file

@ -32,6 +32,19 @@
#define LOG_GAME LOG_STREAM(info, mp_server)
#define DBG_GAME LOG_STREAM(debug, mp_server)
namespace chat_message {
static void truncate_message(t_string& str) {
const size_t max_message_length = 256;
// The string send can contain utf-8 so truncate as wide_string otherwise
// an corrupted utf-8 string can be returned.
std::string tmp = str.str();
utils::truncate_as_wstring(tmp, max_message_length);
str = tmp;
}
} // end chat_message namespace
int game::id_num = 1;
game::game(player_map& players, const network::connection host, const std::string name)
@ -651,6 +664,35 @@ network::connection game::ban_user(const config& ban,
return 0;
}
void game::process_message(config data, const player_map::iterator user) {
// Hack to handle the pseudo game lobby_.
if (owner_ != 0) {
} else if (user->second.silenced()) {
return;
} else if (user->second.is_message_flooding()) {
network::send_data(construct_server_message(
"Warning: you are sending too many messages too fast. "
"Your message has not been relayed."), user->first, true);
return;
}
config* const message = data.child("message");
(*message)["sender"] = user->second.name();
chat_message::truncate_message((*message)["message"]);
std::string msg = (*message)["message"].base_str();
// Only log in the lobby_.
if (owner_ != 0) {
} else if (msg.substr(0,3) == "/me") {
LOG_GAME << network::ip_address(user->first) << "\t<"
<< user->second.name() << msg.substr(3) << ">\n";
} else {
LOG_GAME << network::ip_address(user->first) << "\t<"
<< user->second.name() << "> " << msg << "\n";
}
send_data(data, user->first);
}
//! Process [turn].
bool game::process_turn(config data, const player_map::const_iterator user) {
if (!started_) return false;

View file

@ -23,19 +23,6 @@
class player;
namespace chat_message {
static void truncate_message(t_string& str) {
const size_t max_message_length = 256;
// The string send can contain utf-8 so truncate as wide_string otherwise
// an corrupted utf-8 string can be returned.
std::string tmp = str.str();
utils::truncate_as_wstring(tmp, max_message_length);
str = tmp;
}
} // end chat_message namespace
typedef std::map<network::connection,player> player_map;
typedef std::vector<network::connection> user_vector;
typedef std::vector<network::connection> side_vector;
@ -89,6 +76,7 @@ public:
//! Let's a player owning a side give it to another player or observer.
void transfer_side_control(const network::connection sock, const config& cfg);
void process_message(config data, const player_map::iterator user);
//! Process [turn].
bool process_turn(config data, const player_map::const_iterator user);
//! Set the description to the number of available slots.

View file

@ -923,33 +923,9 @@ void server::process_data_lobby(const network::connection sock, const config& da
}
// See if it's a message, in which case we add the name of the sender,
// and forward it to all players in the lobby
// and forward it to all players in the lobby.
if (data.child("message")) {
// Make a modifiable copy.
config mdata = data;
config* const message = mdata.child("message");
if (pl->second.silenced()) {
return;
} else if (pl->second.is_message_flooding()) {
network::send_data(lobby_.construct_server_message(
"Warning: you are sending too many messages too fast. "
"Your message has not been relayed."), pl->first, true);
return;
}
(*message)["sender"] = pl->second.name();
chat_message::truncate_message((*message)["message"]);
std::string msg = (*message)["message"].base_str();
if (msg.substr(0,3) == "/me") {
LOG_SERVER << network::ip_address(sock) << "\t<"
<< pl->second.name() << msg.substr(3) << ">\n";
} else {
LOG_SERVER << network::ip_address(sock) << "\t<"
<< pl->second.name() << "> " << msg << "\n";
}
lobby_.send_data(mdata, sock);
lobby_.process_message(data, pl);
}
// Player requests update of lobby content,
@ -965,7 +941,7 @@ void server::process_data_lobby(const network::connection sock, const config& da
void server::process_data_game(const network::connection sock, const config& data) {
DBG_SERVER << "in process_data_game...\n";
const player_map::const_iterator pl = players_.find(sock);
const player_map::iterator pl = players_.find(sock);
if (pl == players_.end()) {
ERR_SERVER << "ERROR: Could not find player in players_. (socket: "
<< sock << ")\n";
@ -1118,12 +1094,7 @@ void server::process_data_game(const network::connection sock, const config& dat
} else if (data.child("load_next_scenario")) {
g->load_next_scenario(pl);
return;
} else if (data.values.find("side") != data.values.end()) return;
else if (data.child("side_secured")) return;
else if (data.values.find("failed") != data.values.end()) return;
else if (data.values.find("side_drop") != data.values.end()) return;
else if (data.child("error")) return;
else if (data.child("start_game")) {
} else if (data.child("start_game")) {
if (!g->is_owner(sock)) return;
// Send notification of the game starting immediately.
// g->start_game() will send data that assumes
@ -1231,6 +1202,19 @@ void server::process_data_game(const network::connection sock, const config& dat
lobby_.send_data(games_and_users_list_diff());
}
return;
} else if (data.child("message")) {
g->process_message(data, pl);
// Data to store and broadcast.
} else if (data.child("stop_updates")) {
// if (g->started()) g->record_data(data);
g->send_data(data, sock);
// Data to ignore.
} else if (data.child("error")
|| data.child("side_secured")
|| data.values.find("failed") != data.values.end()
|| data.values.find("side_drop") != data.values.end()
|| data.values.find("side") != data.values.end()) {
return;
}
WRN_SERVER << "Received unknown data from: " << pl->second.name()