GameChatHandler.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * GameChatHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "GameChatHandler.h"
  12. #include "GameInstance.h"
  13. #include "CServerHandler.h"
  14. #include "CPlayerInterface.h"
  15. #include "PlayerLocalState.h"
  16. #include "globalLobby/GlobalLobbyClient.h"
  17. #include "lobby/CLobbyScreen.h"
  18. #include "adventureMap/CInGameConsole.h"
  19. #include "../lib/callback/CCallback.h"
  20. #include "../lib/networkPacks/PacksForLobby.h"
  21. #include "../lib/mapObjects/CArmedInstance.h"
  22. #include "../lib/CConfigHandler.h"
  23. #include "../lib/GameLibrary.h"
  24. #include "../lib/texts/CGeneralTextHandler.h"
  25. #include "../lib/texts/TextOperations.h"
  26. const std::vector<GameChatMessage> & GameChatHandler::getChatHistory() const
  27. {
  28. return chatHistory;
  29. }
  30. void GameChatHandler::resetMatchState()
  31. {
  32. chatHistory.clear();
  33. }
  34. void GameChatHandler::sendMessageGameplay(const std::string & messageText)
  35. {
  36. GAME->interface()->cb->sendMessage(messageText, GAME->interface()->localState->getCurrentArmy());
  37. GAME->server().getGlobalLobby().sendMatchChatMessage(messageText);
  38. }
  39. void GameChatHandler::sendMessageLobby(const std::string & senderName, const std::string & messageText)
  40. {
  41. LobbyChatMessage lcm;
  42. MetaString txt;
  43. txt.appendRawString(messageText);
  44. lcm.message = txt;
  45. lcm.playerName = senderName;
  46. GAME->server().sendLobbyPack(lcm);
  47. GAME->server().getGlobalLobby().sendMatchChatMessage(messageText);
  48. }
  49. void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName, const std::string & messageText)
  50. {
  51. if (!SEL)
  52. {
  53. logGlobal->debug("Received chat message for lobby but lobby not yet exists!");
  54. return;
  55. }
  56. auto * lobby = dynamic_cast<CLobbyScreen*>(SEL);
  57. // FIXME: when can this happen?
  58. assert(lobby);
  59. assert(lobby->card);
  60. if(lobby && lobby->card)
  61. {
  62. MetaString formatted = MetaString::createFromRawString("[%s] %s: %s");
  63. formatted.replaceRawString(TextOperations::getCurrentFormattedTimeLocal());
  64. formatted.replaceRawString(senderName);
  65. formatted.replaceRawString(messageText);
  66. lobby->card->chat->addNewMessage(formatted.toString());
  67. if (!lobby->card->showChat)
  68. lobby->toggleChat();
  69. }
  70. chatHistory.push_back({senderName, messageText, TextOperations::getCurrentFormattedTimeLocal()});
  71. }
  72. void GameChatHandler::onNewGameMessageReceived(PlayerColor sender, const std::string & messageText)
  73. {
  74. std::string timeFormatted = TextOperations::getCurrentFormattedTimeLocal();
  75. std::string playerName = "<UNKNOWN>";
  76. if (sender.isValidPlayer())
  77. playerName = GAME->interface()->cb->getStartInfo()->playerInfos.at(sender).name;
  78. if (sender.isSpectator())
  79. playerName = LIBRARY->generaltexth->translate("vcmi.lobby.login.spectator");
  80. chatHistory.push_back({playerName, messageText, timeFormatted});
  81. GAME->interface()->cingconsole->addMessage(timeFormatted, playerName, messageText);
  82. }
  83. void GameChatHandler::onNewSystemMessageReceived(const std::string & messageText)
  84. {
  85. chatHistory.push_back({"System", messageText, TextOperations::getCurrentFormattedTimeLocal()});
  86. if(GAME->interface() && !settings["session"]["hideSystemMessages"].Bool())
  87. GAME->interface()->cingconsole->addMessage(TextOperations::getCurrentFormattedTimeLocal(), "System", messageText);
  88. }