GameChatHandler.cpp 3.2 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 "CServerHandler.h"
  13. #include "CPlayerInterface.h"
  14. #include "PlayerLocalState.h"
  15. #include "globalLobby/GlobalLobbyClient.h"
  16. #include "lobby/CLobbyScreen.h"
  17. #include "adventureMap/CInGameConsole.h"
  18. #include "../CCallback.h"
  19. #include "../lib/networkPacks/PacksForLobby.h"
  20. #include "../lib/mapObjects/CArmedInstance.h"
  21. #include "../lib/CConfigHandler.h"
  22. #include "../lib/VCMI_Lib.h"
  23. #include "../lib/texts/CGeneralTextHandler.h"
  24. #include "../lib/texts/TextOperations.h"
  25. const std::vector<GameChatMessage> & GameChatHandler::getChatHistory() const
  26. {
  27. return chatHistory;
  28. }
  29. void GameChatHandler::resetMatchState()
  30. {
  31. chatHistory.clear();
  32. }
  33. void GameChatHandler::sendMessageGameplay(const std::string & messageText)
  34. {
  35. LOCPLINT->cb->sendMessage(messageText, LOCPLINT->localState->getCurrentArmy());
  36. CSH->getGlobalLobby().sendMatchChatMessage(messageText);
  37. }
  38. void GameChatHandler::sendMessageLobby(const std::string & senderName, const std::string & messageText)
  39. {
  40. LobbyChatMessage lcm;
  41. MetaString txt;
  42. txt.appendRawString(messageText);
  43. lcm.message = txt;
  44. lcm.playerName = senderName;
  45. CSH->sendLobbyPack(lcm);
  46. CSH->getGlobalLobby().sendMatchChatMessage(messageText);
  47. }
  48. void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName, const std::string & messageText)
  49. {
  50. if (!SEL)
  51. {
  52. logGlobal->debug("Received chat message for lobby but lobby not yet exists!");
  53. return;
  54. }
  55. auto * lobby = dynamic_cast<CLobbyScreen*>(SEL);
  56. // FIXME: when can this happen?
  57. assert(lobby);
  58. assert(lobby->card);
  59. if(lobby && lobby->card)
  60. {
  61. MetaString formatted = MetaString::createFromRawString("[%s] %s: %s");
  62. formatted.replaceRawString(TextOperations::getCurrentFormattedTimeLocal());
  63. formatted.replaceRawString(senderName);
  64. formatted.replaceRawString(messageText);
  65. lobby->card->chat->addNewMessage(formatted.toString());
  66. if (!lobby->card->showChat)
  67. lobby->toggleChat();
  68. }
  69. chatHistory.push_back({senderName, messageText, TextOperations::getCurrentFormattedTimeLocal()});
  70. }
  71. void GameChatHandler::onNewGameMessageReceived(PlayerColor sender, const std::string & messageText)
  72. {
  73. std::string timeFormatted = TextOperations::getCurrentFormattedTimeLocal();
  74. std::string playerName = "<UNKNOWN>";
  75. if (sender.isValidPlayer())
  76. playerName = LOCPLINT->cb->getStartInfo()->playerInfos.at(sender).name;
  77. if (sender.isSpectator())
  78. playerName = "Spectator"; // FIXME: translate? Provide nickname somewhere?
  79. chatHistory.push_back({playerName, messageText, timeFormatted});
  80. LOCPLINT->cingconsole->addMessage(timeFormatted, playerName, messageText);
  81. }
  82. void GameChatHandler::onNewSystemMessageReceived(const std::string & messageText)
  83. {
  84. chatHistory.push_back({"System", messageText, TextOperations::getCurrentFormattedTimeLocal()});
  85. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  86. LOCPLINT->cingconsole->addMessage(TextOperations::getCurrentFormattedTimeLocal(), "System", messageText);
  87. }