Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. Added to server.h:
  2.  
  3. std::unordered_map<std::string, Connection> playerMap;
  4. //hash from player name to connection?
  5. Connection playerToConnection(std::string player);
  6. std::string kickPlayer(std::string player);
  7. std::string mutePlayer(std::string player);
  8. void endGame();
  9. void resetGame();
  10. std::string whisperToPlayer(std::string player);
  11. std::string handleCommand(std::string command);
  12.  
  13. Added to server.cpp:
  14.  
  15. networking::Connection Server::playerToConnection(std::string player) {
  16. //perhaps there is a hash like data structure to map connections to player names and vice verse?
  17. return playerMap.at(player);
  18. }
  19.  
  20. std::string Server::kickPlayer(std::string player) {
  21. //map the player name and or id to a connection object?
  22. //Connection con = playerToConnection(player);
  23. return "So you don't like " + player + " very much, do you?\n";
  24. }
  25.  
  26. std::string Server::mutePlayer(std::string player) {
  27. return player + " is annoying\n";
  28. }
  29.  
  30. void Server::endGame() {
  31. }
  32.  
  33. void Server::resetGame() {
  34. }
  35.  
  36. std::string Server::whisperToPlayer(std::string player){
  37. return "Whispering to " + player;
  38. }
  39.  
  40. std::string Server::handleCommand(std::string command) {
  41. //splice string
  42. std::istringstream temp(command);
  43.  
  44. std::vector<std::string> cmd_args(std::istream_iterator<std::string>{temp},
  45. std::istream_iterator<std::string>());
  46. std::string result;
  47.  
  48. if(cmd_args.size() == 2){
  49. if(cmd_args[0] == "kick"){ //kick player
  50. result = kickPlayer(cmd_args[1]);
  51. } else if (cmd_args[0] == "mute") { //mute player
  52. result = mutePlayer(cmd_args[1]);
  53. } else if (cmd_args[0] == "whisper") { //whisper to a player
  54. result = whisperToPlayer(cmd_args[1]);
  55. } else {
  56. result = "";
  57. }
  58. } else if(cmd_args.size() == 1) { //end the game
  59. if (command == "endgame") {
  60. endGame();
  61. result = "Want to end the game";
  62.  
  63. } else if (command == "resetgame"){ //reset the game
  64. resetGame();
  65. result = "Want to reset the game";
  66. }
  67. }
  68. else {
  69. result = "";
  70. }
  71. return result;
  72. }
  73.  
  74. Added to chatserver.cpp:
  75.  
  76. enum MsgType { chat, command, game_spec };
  77.  
  78. MessageResult
  79. processMessages(Server& server, const std::deque<Message>& incoming) {
  80. std::ostringstream result;
  81. bool quit = false;
  82. for (auto& message : incoming) {
  83. if (message.text == "quit") {
  84. server.disconnect(message.connection);
  85. } else if (message.text == "shutdown") {
  86. std::cout << "Shutting down.\n";
  87. quit = true;
  88. --------------------------------------------------------------------
  89. } else {
  90. } else if (message.text[0] == '/') { //it is a command
  91. result << server.handleCommand(message.text.substr(1));
  92. }else {
  93. result << message.connection.id << "> " << message.text << "\n";
  94. }
  95. ----------------------------------------------------------------------
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement