ChatObserver.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "ChatObserver.h"
  2. #include <Datei.h>
  3. #include "Chat.h"
  4. #include "Game.h"
  5. #include "Player.h"
  6. ChatObserver::ChatObserver(int entityId)
  7. : ReferenceCounter(),
  8. entityId(entityId)
  9. {
  10. Framework::Datei config;
  11. config.setDatei(Game::INSTANCE->getWorldDirectory() + "/chat/observer/"
  12. + entityId + ".observer");
  13. if (config.existiert() && config.open(Framework::Datei::Style::lesen))
  14. {
  15. int count;
  16. config.lese((char*)&count, sizeof(int));
  17. for (int i = 0; i < count; i++)
  18. {
  19. char len;
  20. config.lese(&len, 1);
  21. char* buffer = new char[len + 1];
  22. config.lese(buffer, len);
  23. buffer[(int)len] = 0;
  24. channel.add(new Framework::Text(buffer));
  25. }
  26. config.lese((char*)&count, sizeof(int));
  27. for (int i = 0; i < count; i++)
  28. {
  29. char len;
  30. config.lese(&len, 1);
  31. char* buffer = new char[len + 1];
  32. config.lese(buffer, len);
  33. buffer[(int)len] = 0;
  34. ignoredPlayers.add(new Framework::Text(buffer));
  35. }
  36. config.close();
  37. }
  38. else
  39. {
  40. channel.add(new Framework::Text(Chat::CHANNEL_INFO));
  41. channel.add(new Framework::Text(Chat::CHANNEL_WARNING));
  42. channel.add(new Framework::Text(Chat::CHANNEL_ERROR));
  43. }
  44. }
  45. void ChatObserver::addChannel(Framework::Text channel)
  46. {
  47. this->channel.add(new Framework::Text(channel));
  48. }
  49. void ChatObserver::removeChannel(Framework::Text channel)
  50. {
  51. for (auto it = this->channel.begin(); it;)
  52. {
  53. if (it->istGleich(channel))
  54. {
  55. it.remove();
  56. continue;
  57. }
  58. it++;
  59. }
  60. }
  61. bool ChatObserver::isSubscribedTo(Framework::Text channel)
  62. {
  63. if (channel.hatAt(0, Chat::getPlayerChannelName("")))
  64. {
  65. for (Framework::Text* ignored : ignoredPlayers)
  66. {
  67. if (channel.istGleich(Chat::getPlayerChannelName(*ignored)))
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. else
  73. {
  74. for (Framework::Text* c : this->channel)
  75. {
  76. if (channel.istGleich(*c)) return 1;
  77. }
  78. return 0;
  79. }
  80. }
  81. void ChatObserver::addIgnoredPlayer(Framework::Text playerName)
  82. {
  83. ignoredPlayers.add(new Framework::Text(playerName));
  84. }
  85. void ChatObserver::removeIgnoredPlayer(Framework::Text playerName)
  86. {
  87. for (auto it = this->ignoredPlayers.begin(); it;)
  88. {
  89. if (it->istGleich(playerName))
  90. {
  91. it.remove();
  92. continue;
  93. }
  94. it++;
  95. }
  96. }
  97. int ChatObserver::getEntityId() const
  98. {
  99. return entityId;
  100. }
  101. void ChatObserver::save() const
  102. {
  103. Framework::Datei config;
  104. config.setDatei(Game::INSTANCE->getWorldDirectory() + "/chat/observer/"
  105. + entityId + ".observer");
  106. config.erstellen();
  107. if (config.open(Framework::Datei::Style::schreiben))
  108. {
  109. writeTo(&config);
  110. config.close();
  111. }
  112. }
  113. void ChatObserver::writeTo(Framework::StreamWriter* zWriter) const
  114. {
  115. int count = channel.getEintragAnzahl();
  116. zWriter->schreibe((char*)&count, sizeof(int));
  117. for (Framework::Text* c : channel)
  118. {
  119. char len = (char)c->getLength();
  120. zWriter->schreibe(&len, 1);
  121. zWriter->schreibe(c->getText(), len);
  122. }
  123. count = ignoredPlayers.getEintragAnzahl();
  124. zWriter->schreibe((char*)&count, sizeof(int));
  125. for (Framework::Text* ignored : ignoredPlayers)
  126. {
  127. char len = (char)ignored->getLength();
  128. zWriter->schreibe(&len, 1);
  129. zWriter->schreibe(ignored->getText(), len);
  130. }
  131. }
  132. bool ChatObserver::isValid() const
  133. {
  134. return Game::INSTANCE->zEntity(entityId) != 0;
  135. }
  136. Framework::Text ChatObserver::getPlayerName() const
  137. {
  138. Entity* zEntity = Game::INSTANCE->zEntity(entityId);
  139. if (zEntity)
  140. {
  141. Player* p = dynamic_cast<Player*>(zEntity);
  142. if (p)
  143. {
  144. return p->getName();
  145. }
  146. }
  147. return "";
  148. }