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/TextOperations.h"
  21. #include "../lib/mapObjects/CArmedInstance.h"
  22. #include "../lib/CConfigHandler.h"
  23. #include "../lib/MetaString.h"
  24. static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
  25. {
  26. // FIXME: better/unified way to format date
  27. auto timeNowChrono = std::chrono::system_clock::now();
  28. timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
  29. return TextOperations::getFormattedTimeLocal(std::chrono::system_clock::to_time_t(timeNowChrono));
  30. }
  31. const std::vector<GameChatMessage> & GameChatHandler::getChatHistory()
  32. {
  33. return chatHistory;
  34. }
  35. void GameChatHandler::resetMatchState()
  36. {
  37. chatHistory.clear();
  38. }
  39. void GameChatHandler::sendMessageGameplay(const std::string & messageText)
  40. {
  41. LOCPLINT->cb->sendMessage(messageText, LOCPLINT->localState->getCurrentArmy());
  42. CSH->getGlobalLobby().sendMatchChatMessage(messageText);
  43. }
  44. void GameChatHandler::sendMessageLobby(const std::string & senderName, const std::string & messageText)
  45. {
  46. LobbyChatMessage lcm;
  47. lcm.message = messageText;
  48. lcm.playerName = senderName;
  49. CSH->sendLobbyPack(lcm);
  50. CSH->getGlobalLobby().sendMatchChatMessage(messageText);
  51. }
  52. void GameChatHandler::onNewLobbyMessageReceived(const std::string & senderName, const std::string & messageText)
  53. {
  54. if (!SEL)
  55. {
  56. logGlobal->debug("Received chat message for lobby but lobby not yet exists!");
  57. return;
  58. }
  59. auto * lobby = dynamic_cast<CLobbyScreen*>(SEL);
  60. // FIXME: when can this happen?
  61. assert(lobby);
  62. assert(lobby->card);
  63. if(lobby && lobby->card)
  64. {
  65. MetaString formatted = MetaString::createFromRawString("[%s] %s: %s");
  66. formatted.replaceRawString(getCurrentTimeFormatted());
  67. formatted.replaceRawString(senderName);
  68. formatted.replaceRawString(messageText);
  69. lobby->card->chat->addNewMessage(formatted.toString());
  70. if (!lobby->card->showChat)
  71. lobby->toggleChat();
  72. }
  73. chatHistory.push_back({senderName, messageText, getCurrentTimeFormatted()});
  74. }
  75. void GameChatHandler::onNewGameMessageReceived(PlayerColor sender, const std::string & messageText)
  76. {
  77. std::string timeFormatted = getCurrentTimeFormatted();
  78. std::string playerName = sender.isSpectator() ? "Spectator" : sender.toString(); //FIXME: should actually be player nickname, at least in MP
  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, getCurrentTimeFormatted()});
  85. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  86. LOCPLINT->cingconsole->addMessage(getCurrentTimeFormatted(), "System", messageText);
  87. }