Chat.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "Chat.h"
  2. #include <Datei.h>
  3. #include "Game.h"
  4. const Framework::Text Chat::CHANNEL_INFO = "system:INFO";
  5. const Framework::Text Chat::CHANNEL_WARNING = "system:WARNING";
  6. const Framework::Text Chat::CHANNEL_ERROR = "system:ERROR";
  7. Chat::Chat()
  8. : ReferenceCounter(),
  9. commandExecutor(new ChatCommandExecutor())
  10. {
  11. Framework::Datei messageData;
  12. messageData.setDatei(
  13. Game::INSTANCE->getWorldDirectory() + "/chat/history.chat");
  14. if (messageData.existiert() && messageData.open(Datei::Style::lesen))
  15. {
  16. std::cout << "loading chat history from "
  17. << messageData.zPfad()->getText() << std::endl
  18. << "Delete that file to reset the chat history or use the "
  19. "'/resetChat [channel] [timestamp]' command."
  20. << std::endl; // TODO: implement /resetChat command
  21. while (!messageData.istEnde())
  22. {
  23. ChatMessage* message = new ChatMessage(&messageData);
  24. history.add(message);
  25. }
  26. messageData.close();
  27. }
  28. }
  29. Chat::~Chat()
  30. {
  31. commandExecutor->release();
  32. }
  33. void Chat::addMessage(ChatMessage* message)
  34. {
  35. cs.lock();
  36. history.add(message);
  37. NetworkMessage* nMsg = new NetworkMessage();
  38. nMsg->sendChatMessage(message);
  39. nMsg->setUseBackground();
  40. for (auto it = observer.begin(); it;)
  41. {
  42. if (!it->isValid())
  43. {
  44. it.remove();
  45. continue;
  46. }
  47. if (it->isSubscribedTo(message->getChannel())
  48. && (!message->getTargetPlayerName().getLength()
  49. || it->getPlayerName().istGleich(
  50. message->getTargetPlayerName())))
  51. {
  52. Entity* zEtity = Game::INSTANCE->zEntity(it->getEntityId());
  53. if (zEtity)
  54. {
  55. Game::INSTANCE->sendMessage(
  56. dynamic_cast<NetworkMessage*>(nMsg->getThis()), zEtity);
  57. }
  58. }
  59. it++;
  60. }
  61. nMsg->release();
  62. cs.unlock();
  63. }
  64. void Chat::addObserver(int entityId)
  65. {
  66. cs.lock();
  67. ChatObserver* obs = new ChatObserver(entityId);
  68. this->observer.add(obs);
  69. Framework::Text name = obs->getPlayerName();
  70. for (ChatMessage* msg : history)
  71. {
  72. if (obs->isSubscribedTo(msg->getChannel())
  73. && (!msg->getTargetPlayerName().getLength()
  74. || name.istGleich(msg->getTargetPlayerName())))
  75. {
  76. NetworkMessage* nMsg = new NetworkMessage();
  77. nMsg->sendChatMessage(msg);
  78. nMsg->setUseBackground();
  79. Game::INSTANCE->sendMessage(
  80. nMsg, Game::INSTANCE->zEntity(entityId));
  81. }
  82. }
  83. Entity* zPlayer = Game::INSTANCE->zEntity(entityId);
  84. if (zPlayer)
  85. {
  86. NetworkMessage* options = new NetworkMessage();
  87. options->sendChatOptions(obs);
  88. Game::INSTANCE->sendMessage(options, zPlayer);
  89. }
  90. cs.unlock();
  91. }
  92. void Chat::removeObserver(int entityId)
  93. {
  94. cs.lock();
  95. for (auto it = this->observer.begin(); it; it++)
  96. {
  97. if (it->getEntityId() == entityId)
  98. {
  99. it->save();
  100. it.remove();
  101. break;
  102. }
  103. }
  104. cs.unlock();
  105. }
  106. void Chat::chatApi(Framework::StreamReader* zRequest,
  107. Entity* zSource,
  108. NetworkMessage* zResponse)
  109. {
  110. char id;
  111. zRequest->lese(&id, 1);
  112. switch (id)
  113. {
  114. case 0: // send message
  115. {
  116. short len;
  117. zRequest->lese((char*)&len, 2);
  118. char* buffer = new char[len + 1];
  119. zRequest->lese(buffer, len);
  120. buffer[len] = 0;
  121. if (buffer[0] == '/')
  122. {
  123. if (!commandExecutor->execute(buffer, zSource))
  124. {
  125. sendMessageTo("Unknown kommand. Use /help to get "
  126. "information about all known commands.",
  127. zSource,
  128. CHANNEL_ERROR);
  129. }
  130. }
  131. else
  132. {
  133. Player* p = dynamic_cast<Player*>(zSource);
  134. if (p)
  135. {
  136. broadcastMessage(
  137. buffer, getPlayerChannelName(p->getName()));
  138. }
  139. else
  140. {
  141. // TODO: implement entity channels
  142. }
  143. }
  144. delete[] buffer;
  145. break;
  146. }
  147. case 1: // add channel
  148. {
  149. char len;
  150. zRequest->lese(&len, 1);
  151. char* buffer = new char[len + 1];
  152. zRequest->lese(buffer, len);
  153. buffer[(int)len] = 0;
  154. for (ChatObserver* observer : this->observer)
  155. {
  156. if (observer->getEntityId() == zSource->getId())
  157. {
  158. observer->addChannel(buffer);
  159. break;
  160. }
  161. }
  162. delete[] buffer;
  163. break;
  164. }
  165. case 2: // remove channel
  166. {
  167. char len;
  168. zRequest->lese(&len, 1);
  169. char* buffer = new char[len + 1];
  170. zRequest->lese(buffer, len);
  171. buffer[(int)len] = 0;
  172. for (ChatObserver* observer : this->observer)
  173. {
  174. if (observer->getEntityId() == zSource->getId())
  175. {
  176. observer->removeChannel(buffer);
  177. }
  178. }
  179. delete[] buffer;
  180. break;
  181. }
  182. case 3: // add ignored player
  183. {
  184. char len;
  185. zRequest->lese(&len, 1);
  186. char* buffer = new char[len + 1];
  187. zRequest->lese(buffer, len);
  188. buffer[(int)len] = 0;
  189. for (ChatObserver* observer : this->observer)
  190. {
  191. if (observer->getEntityId() == zSource->getId())
  192. {
  193. observer->addIgnoredPlayer(buffer);
  194. }
  195. }
  196. delete[] buffer;
  197. break;
  198. }
  199. case 4: // remove ignored player
  200. {
  201. char len;
  202. zRequest->lese(&len, 1);
  203. char* buffer = new char[len + 1];
  204. zRequest->lese(buffer, len);
  205. buffer[(int)len] = 0;
  206. for (ChatObserver* observer : this->observer)
  207. {
  208. if (observer->getEntityId() == zSource->getId())
  209. {
  210. observer->removeIgnoredPlayer(buffer);
  211. }
  212. }
  213. delete[] buffer;
  214. break;
  215. }
  216. }
  217. }
  218. void Chat::broadcastMessage(Framework::Text message, Framework::Text channel)
  219. {
  220. addMessage(new ChatMessage(message, channel, ""));
  221. std::cout << "Chat [" << channel << "] " << message << "\n";
  222. }
  223. void Chat::sendMessageTo(
  224. Framework::Text message, Entity* zTarget, Framework::Text channel)
  225. {
  226. if (!zTarget)
  227. {
  228. std::cout << message << "\n";
  229. return;
  230. }
  231. Player* p = dynamic_cast<Player*>(zTarget);
  232. if (p)
  233. {
  234. addMessage(new ChatMessage(message, channel, p->getName()));
  235. }
  236. }
  237. void Chat::save()
  238. {
  239. cs.lock();
  240. for (ChatObserver* obs : observer)
  241. {
  242. obs->save();
  243. }
  244. Framework::Datei messageData;
  245. messageData.setDatei(
  246. Game::INSTANCE->getWorldDirectory() + "/chat/history.chat");
  247. if (!messageData.existiert()) messageData.erstellen();
  248. messageData.open(Datei::Style::schreiben);
  249. for (ChatMessage* msg : history)
  250. {
  251. msg->writeTo(&messageData);
  252. }
  253. messageData.close();
  254. cs.unlock();
  255. }
  256. ChatCommandExecutor* Chat::zCommandExecutor() const
  257. {
  258. return commandExecutor;
  259. }
  260. Framework::Text Chat::getPlayerChannelName(Framework::Text playerName)
  261. {
  262. return Framework::Text("player:") + playerName;
  263. }