Chat.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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::chatApi(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. if (!commandExecutor->execute(buffer, zSource))
  117. {
  118. sendMessageTo("Unknown kommand. Use /help to get "
  119. "information about all known commands.",
  120. zSource,
  121. CHANNEL_ERROR);
  122. }
  123. }
  124. else
  125. {
  126. Player* p = dynamic_cast<Player*>(zSource);
  127. if (p)
  128. {
  129. broadcastMessage(
  130. buffer, getPlayerChannelName(p->getName()));
  131. }
  132. else
  133. {
  134. // TODO: implement entity channels
  135. }
  136. }
  137. delete[] buffer;
  138. break;
  139. }
  140. case 1: // add channel
  141. {
  142. char len;
  143. zRequest->lese(&len, 1);
  144. char* buffer = new char[len + 1];
  145. zRequest->lese(buffer, len);
  146. buffer[(int)len] = 0;
  147. for (ChatObserver* observer : this->observer)
  148. {
  149. if (observer->getEntityId() == zSource->getId())
  150. {
  151. observer->addChannel(buffer);
  152. break;
  153. }
  154. }
  155. delete[] buffer;
  156. }
  157. case 2: // remove channel
  158. {
  159. char len;
  160. zRequest->lese(&len, 1);
  161. char* buffer = new char[len + 1];
  162. zRequest->lese(buffer, len);
  163. buffer[(int)len] = 0;
  164. for (ChatObserver* observer : this->observer)
  165. {
  166. if (observer->getEntityId() == zSource->getId())
  167. {
  168. observer->removeChannel(buffer);
  169. }
  170. }
  171. delete[] buffer;
  172. }
  173. case 3: // add ignored player
  174. {
  175. char len;
  176. zRequest->lese(&len, 1);
  177. char* buffer = new char[len + 1];
  178. zRequest->lese(buffer, len);
  179. buffer[(int)len] = 0;
  180. for (ChatObserver* observer : this->observer)
  181. {
  182. if (observer->getEntityId() == zSource->getId())
  183. {
  184. observer->addIgnoredPlayer(buffer);
  185. }
  186. }
  187. delete[] buffer;
  188. }
  189. case 4: // remove ignored player
  190. {
  191. char len;
  192. zRequest->lese(&len, 1);
  193. char* buffer = new char[len + 1];
  194. zRequest->lese(buffer, len);
  195. buffer[(int)len] = 0;
  196. for (ChatObserver* observer : this->observer)
  197. {
  198. if (observer->getEntityId() == zSource->getId())
  199. {
  200. observer->removeIgnoredPlayer(buffer);
  201. }
  202. }
  203. delete[] buffer;
  204. }
  205. }
  206. }
  207. void Chat::broadcastMessage(Framework::Text message, Framework::Text channel)
  208. {
  209. addMessage(new ChatMessage(message, channel, ""));
  210. std::cout << "Chat [" << channel << "] " << message << "\n";
  211. }
  212. void Chat::sendMessageTo(
  213. Framework::Text message, Entity* zTarget, Framework::Text channel)
  214. {
  215. if (!zTarget)
  216. {
  217. std::cout << message << "\n";
  218. return;
  219. }
  220. Player* p = dynamic_cast<Player*>(zTarget);
  221. if (p)
  222. {
  223. addMessage(new ChatMessage(message, channel, p->getName()));
  224. }
  225. }
  226. void Chat::save()
  227. {
  228. cs.lock();
  229. for (ChatObserver* obs : observer)
  230. {
  231. obs->save();
  232. }
  233. Framework::Datei messageData;
  234. messageData.setDatei(
  235. Game::INSTANCE->getWorldDirectory() + "/chat/history.chat");
  236. if (!messageData.existiert()) messageData.erstellen();
  237. messageData.open(Datei::Style::schreiben);
  238. for (ChatMessage* msg : history)
  239. {
  240. msg->writeTo(&messageData);
  241. }
  242. messageData.close();
  243. cs.unlock();
  244. }
  245. ChatCommandExecutor* Chat::zCommandExecutor() const
  246. {
  247. return commandExecutor;
  248. }
  249. Framework::Text Chat::getPlayerChannelName(Framework::Text playerName)
  250. {
  251. return Framework::Text("player:") + playerName;
  252. }