ChatObserver.cpp 3.6 KB

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