diff options
Diffstat (limited to 'src/core/gameserver.cc')
-rw-r--r-- | src/core/gameserver.cc | 196 |
1 files changed, 125 insertions, 71 deletions
diff --git a/src/core/gameserver.cc b/src/core/gameserver.cc index 14f97bb..f251b75 100644 --- a/src/core/gameserver.cc +++ b/src/core/gameserver.cc @@ -4,17 +4,17 @@ the terms of the GNU General Public License version 2 */ -#include <iomanip> #include <fstream> +#include <iomanip> #include "auxiliary/functions.h" -#include "sys/sys.h" #include "core/application.h" #include "core/cvar.h" #include "core/func.h" #include "core/gameserver.h" #include "core/netserver.h" #include "filesystem/filesystem.h" +#include "sys/sys.h" namespace core { @@ -174,8 +174,6 @@ GameServer::GameServer() : GameInterface() func = Func::add("who", func_who, Func::Shared); func->set_info("get a list of connected players"); - server_players.clear(); - if (!Cvar::sv_dedicated->value()) { player_connect(localplayer()); } @@ -218,8 +216,6 @@ GameServer::~GameServer() Func::remove("who"); server_instance = 0; - - server_players.clear(); } void GameServer::abort() @@ -227,21 +223,6 @@ void GameServer::abort() server_running = false; } -void GameServer::list_players() -{ - using namespace std; - stringstream msgstr; - int count = 0; - - for (Players::iterator it = server_players.begin(); it != server_players.end(); it++) { - msgstr.str(""); - con_print << setw(3) << (*it)->id() << aux::pad_left((*it)->name(), 24) << std::endl; - count++; - } - - con_print << count << " connected " << aux::plural("player", count) << std::endl; -} - void GameServer::showtime() { using namespace std; @@ -265,7 +246,7 @@ Player *GameServer::find_player(std::string const search) std::istringstream searchstr(search); int id = 0; if (searchstr >> id) { - for (std::list<Player *>:: iterator it = server_players.begin(); it != server_players.end(); it++) { + for (std::list<Player *>:: iterator it = game_players.begin(); it != game_players.end(); it++) { if ((*it)->id() == id) { return (*it); } @@ -275,7 +256,7 @@ Player *GameServer::find_player(std::string const search) if (search.size() <3) return 0; - for (std::list<Player *>:: iterator it = server_players.begin(); it != server_players.end(); it++) { + for (std::list<Player *>:: iterator it = game_players.begin(); it != game_players.end(); it++) { if (aux::text_strip_lowercase((*it)->name()).find(lowercase(search)) != std::string::npos) return (*it); } @@ -298,14 +279,35 @@ void GameServer::say(Player *player, std::string const &message) notification.append("^F:^B "); notification.append(message); - // send to application - application()->notify_message(notification); - application()->notify_sound("com/chat"); - - // broadcast to remote clients - if (server_network) { - server_network->broadcast_message("public", notification); + broadcast_message(Message::Public, notification); +} + +void GameServer::private_message(Player *player, std::string const &args) +{ + if (!args.size()) + return; + + if (player->mute()) { + send(player, "^BYou have been muted."); + return; + } + + std::string target; + std::stringstream argstr(args); + if (!(argstr >> target)) { + return; } + + core::Player *targetplayer = core::server()->find_player(target); + if (!targetplayer) { + send(player, "^BPlayer " + target + "^B not found."); + return; + } + + + std::string message(args.substr(target.size())); + send_message(Message::Private, player, "^FTo ^B" + targetplayer->name() + "^F:" + message); + send_message(Message::Private, targetplayer, "^FFrom ^B" + player->name() + "^F:" + message); } // FIXME kicked by @@ -327,44 +329,114 @@ void GameServer::kick(Player *player, std::string const &reason) } // broadcast an "info" message to all players -void GameServer::broadcast(std::string const & message, Player *ignore_player) +void GameServer::broadcast(std::string const message, Player *ignore_player) { if (!message.size()) return; - // send to application - if (ignore_player != game()->localplayer()) - application()->notify_message(message); - - // broadcast to remote clients - if (server_network) { - server_network->broadcast_message("info", message, ignore_player); - } + broadcast_message(Message::Info, message, ignore_player); } // send and "info" message to a single player -void GameServer::send(Player *player, std::string message) +void GameServer::send(Player *player, std::string const message) +{ + send_message(Message::Info, player, message); +} + +// send an rcon message to a single player +void GameServer::send_rcon(Player *player, std::string const message) +{ + send_message(Message::RCon, player, message); +} + +void GameServer::send_message(Message::Channel const channel, Player *player, std::string const message) { if (!message.size()) return; - // send to application if (player == localplayer()) { - application()->notify_message(message); + application()->notify_message(channel, message); return; + } else { + if (server_network) { + std::string msg_channel; + switch(channel) { + case core::Message::Info: // Info message + msg_channel.assign("info"); + break; + + case core::Message::Local: // Chat message in the local zone + msg_channel.assign("local"); + break; + + case core::Message::Public: // Public chat message + msg_channel.assign("public"); + break; + + case core::Message::Private: // Private chat message + msg_channel.assign("private"); + break; + + case core::Message::RCon: // RCon message + msg_channel.assign("rcon"); + break; + + default: + con_warn << "message on unknown channel " << channel << "!" << std::endl; + return; + break; + } + + NetClient *client = server_network->find_client(player); + if (client) { + server_network->send_message(client, msg_channel.c_str(), message); + } + } } +} + +// broadcast a message on a specified channel to all players +void GameServer::broadcast_message(Message::Channel const channel, std::string const message, Player *ignore_player) +{ + if (!message.size()) + return; - // send to remote clients + // send to application + if (ignore_player != game()->localplayer()) + application()->notify_message(channel, message); + + // broadcast to remote clients if (server_network) { - NetClient *client = server_network->find_client(player); - if (client) { - server_network->send_message(client, "info", message); + std::string msg_channel; + switch(channel) { + case core::Message::Info: // Info message + msg_channel.assign("info"); + break; + + case core::Message::Local: // Chat message in the local zone + msg_channel.assign("local"); + break; + + case core::Message::RCon: // RCon message + msg_channel.assign("rcon"); + break; + + case core::Message::Public: // Public chat message + msg_channel.assign("public"); + break; + + default: + con_warn << "message on unknown channel " << channel << "!" << std::endl; + return; + break; } + + server_network->broadcast_message(msg_channel.c_str(), message); } } // broadcast a sound event to all players -void GameServer::broadcast_sound(std::string const & sound, Player *ignore_player) +void GameServer::broadcast_sound(std::string const sound, Player *ignore_player) { if (!sound.size()) return; @@ -381,7 +453,7 @@ void GameServer::broadcast_sound(std::string const & sound, Player *ignore_playe } // send a sound event to a single player -void GameServer::send_sound(Player *player, std::string sound) +void GameServer::send_sound(Player *player, std::string const sound) { if (!sound.size()) return; @@ -401,24 +473,6 @@ void GameServer::send_sound(Player *player, std::string sound) } } -// send an rcon message to a single player -void GameServer::send_rcon(Player *player, std::string message) -{ - // send to application - if (player == localplayer()) { - con_print << message << std::endl; - return; - } - - // send to remote clients - if (server_network) { - NetClient *client = server_network->find_client(player); - if (client) { - server_network->send_message(client, "rcon", message); - } - } -} - // execute a command for a remote player void GameServer::exec(Player *player, std::string const & cmdline) { @@ -478,7 +532,7 @@ void GameServer::player_connect(Player *player) server_module->player_connect(player); // manage player list - server_players.push_back(player); + game_players.push_back(player); } void GameServer::player_disconnect(Player *player) @@ -495,12 +549,12 @@ void GameServer::player_disconnect(Player *player) server_module->player_disconnect(player); // manage player list - std::list<Player *>:: iterator it = server_players.begin(); - while (((*it)->id() != player->id()) && (it != server_players.end())) { + std::list<Player *>:: iterator it = game_players.begin(); + while (((*it)->id() != player->id()) && (it != game_players.end())) { it++; } - if (it != server_players.end()) { - server_players.erase(it); + if (it != game_players.end()) { + game_players.erase(it); } } |