Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Added to server.h:
- std::unordered_map<std::string, Connection> playerMap;
- //hash from player name to connection?
- Connection playerToConnection(std::string player);
- std::string kickPlayer(std::string player);
- std::string mutePlayer(std::string player);
- void endGame();
- void resetGame();
- std::string whisperToPlayer(std::string player);
- std::string handleCommand(std::string command);
- Added to server.cpp:
- networking::Connection Server::playerToConnection(std::string player) {
- //perhaps there is a hash like data structure to map connections to player names and vice verse?
- return playerMap.at(player);
- }
- std::string Server::kickPlayer(std::string player) {
- //map the player name and or id to a connection object?
- //Connection con = playerToConnection(player);
- return "So you don't like " + player + " very much, do you?\n";
- }
- std::string Server::mutePlayer(std::string player) {
- return player + " is annoying\n";
- }
- void Server::endGame() {
- }
- void Server::resetGame() {
- }
- std::string Server::whisperToPlayer(std::string player){
- return "Whispering to " + player;
- }
- std::string Server::handleCommand(std::string command) {
- //splice string
- std::istringstream temp(command);
- std::vector<std::string> cmd_args(std::istream_iterator<std::string>{temp},
- std::istream_iterator<std::string>());
- std::string result;
- if(cmd_args.size() == 2){
- if(cmd_args[0] == "kick"){ //kick player
- result = kickPlayer(cmd_args[1]);
- } else if (cmd_args[0] == "mute") { //mute player
- result = mutePlayer(cmd_args[1]);
- } else if (cmd_args[0] == "whisper") { //whisper to a player
- result = whisperToPlayer(cmd_args[1]);
- } else {
- result = "";
- }
- } else if(cmd_args.size() == 1) { //end the game
- if (command == "endgame") {
- endGame();
- result = "Want to end the game";
- } else if (command == "resetgame"){ //reset the game
- resetGame();
- result = "Want to reset the game";
- }
- }
- else {
- result = "";
- }
- return result;
- }
- Added to chatserver.cpp:
- enum MsgType { chat, command, game_spec };
- MessageResult
- processMessages(Server& server, const std::deque<Message>& incoming) {
- std::ostringstream result;
- bool quit = false;
- for (auto& message : incoming) {
- if (message.text == "quit") {
- server.disconnect(message.connection);
- } else if (message.text == "shutdown") {
- std::cout << "Shutting down.\n";
- quit = true;
- --------------------------------------------------------------------
- } else {
- } else if (message.text[0] == '/') { //it is a command
- result << server.handleCommand(message.text.substr(1));
- }else {
- result << message.connection.id << "> " << message.text << "\n";
- }
- ----------------------------------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement