ChatObserver.cpp 3.8 KB

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