Chat.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. cs.unlock();
  84. }
  85. void Chat::removeObserver(int entityId)
  86. {
  87. cs.lock();
  88. for (auto it = this->observer.begin(); it; it++)
  89. {
  90. if (it->getEntityId() == entityId)
  91. {
  92. it->save();
  93. it.remove();
  94. break;
  95. }
  96. }
  97. cs.unlock();
  98. }
  99. void Chat::charApi(Framework::StreamReader* zRequest,
  100. Entity* zSource,
  101. NetworkMessage* zResponse)
  102. {
  103. char id;
  104. zRequest->lese(&id, 1);
  105. switch (id)
  106. {
  107. case 0: // send message
  108. {
  109. short len;
  110. zRequest->lese((char*)&len, 2);
  111. char* buffer = new char[len + 1];
  112. zRequest->lese(buffer, len);
  113. buffer[len] = 0;
  114. if (buffer[0] == '/')
  115. {
  116. commandExecutor->execute(buffer, zSource);
  117. }
  118. else
  119. {
  120. Player* p = dynamic_cast<Player*>(zSource);
  121. if (p)
  122. {
  123. broadcastMessage(
  124. buffer, getPlayerChannelName(p->getName()));
  125. }
  126. else
  127. {
  128. // TODO: implement entity channels
  129. }
  130. }
  131. delete[] buffer;
  132. break;
  133. }
  134. case 1: // add channel
  135. {
  136. char len;
  137. zRequest->lese(&len, 1);
  138. char* buffer = new char[len + 1];
  139. zRequest->lese(buffer, len);
  140. buffer[(int)len] = 0;
  141. for (ChatObserver* observer : this->observer)
  142. {
  143. if (observer->getEntityId() == zSource->getId())
  144. {
  145. observer->addChannel(buffer);
  146. break;
  147. }
  148. }
  149. delete[] buffer;
  150. }
  151. case 2: // remove channel
  152. {
  153. char len;
  154. zRequest->lese(&len, 1);
  155. char* buffer = new char[len + 1];
  156. zRequest->lese(buffer, len);
  157. buffer[(int)len] = 0;
  158. for (ChatObserver* observer : this->observer)
  159. {
  160. if (observer->getEntityId() == zSource->getId())
  161. {
  162. observer->removeChannel(buffer);
  163. }
  164. }
  165. delete[] buffer;
  166. }
  167. case 3: // add ignored player
  168. {
  169. char len;
  170. zRequest->lese(&len, 1);
  171. char* buffer = new char[len + 1];
  172. zRequest->lese(buffer, len);
  173. buffer[(int)len] = 0;
  174. for (ChatObserver* observer : this->observer)
  175. {
  176. if (observer->getEntityId() == zSource->getId())
  177. {
  178. observer->addIgnoredPlayer(buffer);
  179. }
  180. }
  181. delete[] buffer;
  182. }
  183. case 4: // remove ignored player
  184. {
  185. char len;
  186. zRequest->lese(&len, 1);
  187. char* buffer = new char[len + 1];
  188. zRequest->lese(buffer, len);
  189. buffer[(int)len] = 0;
  190. for (ChatObserver* observer : this->observer)
  191. {
  192. if (observer->getEntityId() == zSource->getId())
  193. {
  194. observer->removeIgnoredPlayer(buffer);
  195. }
  196. }
  197. delete[] buffer;
  198. }
  199. }
  200. }
  201. void Chat::broadcastMessage(Framework::Text message, Framework::Text channel)
  202. {
  203. addMessage(new ChatMessage(message, channel, ""));
  204. std::cout << "Chat [" << channel << "] " << message << "\n";
  205. }
  206. void Chat::sendMessageTo(
  207. Framework::Text message, Entity* zTarget, Framework::Text channel)
  208. {
  209. Player* p = dynamic_cast<Player*>(zTarget);
  210. if (p)
  211. {
  212. addMessage(new ChatMessage(message, channel, p->getName()));
  213. }
  214. }
  215. void Chat::save()
  216. {
  217. cs.lock();
  218. for (ChatObserver* obs : observer)
  219. {
  220. obs->save();
  221. }
  222. Framework::Datei messageData;
  223. messageData.setDatei(
  224. Game::INSTANCE->getWorldDirectory() + "/chat/history.chat");
  225. if (!messageData.existiert()) messageData.erstellen();
  226. messageData.open(Datei::Style::schreiben);
  227. for (ChatMessage* msg : history)
  228. {
  229. msg->writeTo(&messageData);
  230. }
  231. messageData.close();
  232. cs.unlock();
  233. }
  234. ChatCommandExecutor* Chat::zCommandExecutor() const
  235. {
  236. return commandExecutor;
  237. }
  238. Framework::Text Chat::getPlayerChannelName(Framework::Text playerName)
  239. {
  240. return Framework::Text("player:") + playerName;
  241. }