GameChatHandler.cpp 2.8 KB

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