GameChatHandler.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "lobby/CSelectionBase.h"
  18. #include "adventureMap/CInGameConsole.h"
  19. #include "../CCallback.h"
  20. #include "../lib/networkPacks/PacksForLobby.h"
  21. #include "../lib/TextOperations.h"
  22. #include "../lib/mapObjects/CArmedInstance.h"
  23. #include "../lib/CConfigHandler.h"
  24. #include "../lib/MetaString.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. lcm.message = messageText;
  42. lcm.playerName = senderName;
  43. CSH->sendLobbyPack(lcm);
  44. CSH->getGlobalLobby().sendMatchChatMessage(messageText);
  45. }
  46. void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName, const std::string & messageText)
  47. {
  48. if (!SEL)
  49. {
  50. logGlobal->debug("Received chat message for lobby but lobby not yet exists!");
  51. return;
  52. }
  53. auto * lobby = dynamic_cast<CLobbyScreen*>(SEL);
  54. // FIXME: when can this happen?
  55. assert(lobby);
  56. assert(lobby->card);
  57. if(lobby && lobby->card)
  58. {
  59. MetaString formatted = MetaString::createFromRawString("[%s] %s: %s");
  60. formatted.replaceRawString(TextOperations::getCurrentFormattedTimeLocal());
  61. formatted.replaceRawString(senderName);
  62. formatted.replaceRawString(messageText);
  63. lobby->card->chat->addNewMessage(formatted.toString());
  64. if (lobby->card->chatMode != InfoCard::ChatMode::Enabled)
  65. lobby->card->setChat(InfoCard::ChatMode::Enabled);
  66. }
  67. chatHistory.push_back({senderName, messageText, TextOperations::getCurrentFormattedTimeLocal()});
  68. }
  69. void GameChatHandler::onNewGameMessageReceived(PlayerColor sender, const std::string & messageText)
  70. {
  71. std::string timeFormatted = TextOperations::getCurrentFormattedTimeLocal();
  72. std::string playerName = "<UNKNOWN>";
  73. if (sender.isValidPlayer())
  74. playerName = LOCPLINT->cb->getStartInfo()->playerInfos.at(sender).name;
  75. if (sender.isSpectator())
  76. playerName = "Spectator"; // FIXME: translate? Provide nickname somewhere?
  77. chatHistory.push_back({playerName, messageText, timeFormatted});
  78. LOCPLINT->cingconsole->addMessage(timeFormatted, playerName, messageText);
  79. }
  80. void GameChatHandler::onNewSystemMessageReceived(const std::string & messageText)
  81. {
  82. chatHistory.push_back({"System", messageText, TextOperations::getCurrentFormattedTimeLocal()});
  83. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  84. LOCPLINT->cingconsole->addMessage(TextOperations::getCurrentFormattedTimeLocal(), "System", messageText);
  85. }