123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "ChatObserver.h"
- #include <Datei.h>
- #include "Game.h"
- ChatObserver::ChatObserver(int entityId)
- : ReferenceCounter(),
- entityId(entityId)
- {
- Framework::Datei config;
- config.setDatei(Game::INSTANCE->getWorldDirectory() + "/chat/observer/"
- + entityId + ".observer");
- if (config.existiert() && config.open(Datei::Style::lesen))
- {
- int cCount;
- config.lese((char*)&cCount, sizeof(int));
- for (int i = 0; i < cCount; i++)
- {
- char len;
- config.lese(&len, 1);
- char* buffer = new char[len + 1];
- config.lese(buffer, len);
- buffer[(int)len] = 0;
- channel.add(new Framework::Text(buffer));
- }
- while (!config.istEnde())
- {
- char len;
- config.lese(&len, 1);
- char* buffer = new char[len + 1];
- config.lese(buffer, len);
- buffer[(int)len] = 0;
- ignoredPlayers.add(new Framework::Text(buffer));
- }
- config.close();
- }
- else
- {
- channel.add(new Text(Chat::CHANNEL_INFO));
- channel.add(new Text(Chat::CHANNEL_WARNING));
- channel.add(new Text(Chat::CHANNEL_ERROR));
- }
- }
- void ChatObserver::addChannel(Framework::Text channel)
- {
- this->channel.add(new Framework::Text(channel));
- }
- void ChatObserver::removeChannel(Framework::Text channel)
- {
- for (auto it = this->channel.begin(); it;)
- {
- if (it->istGleich(channel))
- {
- it.remove();
- continue;
- }
- it++;
- }
- }
- bool ChatObserver::isSubscribedTo(Framework::Text channel)
- {
- if (channel.hatAt(0, Chat::getPlayerChannelName("")))
- {
- for (Framework::Text* ignored : ignoredPlayers)
- {
- if (channel.istGleich(Chat::getPlayerChannelName(*ignored)))
- return 0;
- }
- return 1;
- }
- else
- {
- for (Framework::Text* c : this->channel)
- {
- if (channel.istGleich(*c)) return 1;
- }
- return 0;
- }
- }
- void ChatObserver::addIgnoredPlayer(Framework::Text playerName)
- {
- ignoredPlayers.add(new Framework::Text(playerName));
- }
- void ChatObserver::removeIgnoredPlayer(Framework::Text playerName)
- {
- for (auto it = this->channel.begin(); it;)
- {
- if (it->istGleich(playerName))
- {
- it.remove();
- continue;
- }
- it++;
- }
- }
- int ChatObserver::getEntityId() const
- {
- return entityId;
- }
- void ChatObserver::save() const
- {
- Framework::Datei config;
- config.setDatei(Game::INSTANCE->getWorldDirectory() + "/chat/observer/"
- + entityId + ".observer");
- if (config.open(Datei::Style::schreiben))
- {
- int cCount = channel.getEintragAnzahl();
- config.schreibe((char*)&cCount, sizeof(int));
- for (Framework::Text* c : channel)
- {
- char len = (char)c->getLength();
- config.schreibe(&len, 1);
- config.schreibe(c->getText(), len);
- }
- for (Framework::Text* ignored : ignoredPlayers)
- {
- char len = (char)ignored->getLength();
- config.schreibe(&len, 1);
- config.schreibe(ignored->getText(), len);
- }
- config.close();
- }
- }
- bool ChatObserver::isValid() const
- {
- return Game::INSTANCE->zEntity(entityId) != 0;
- }
- Framework::Text ChatObserver::getPlayerName() const
- {
- Entity* zEntity = Game::INSTANCE->zEntity(entityId);
- if (zEntity)
- {
- Player* p = dynamic_cast<Player*>(zEntity);
- if (p)
- {
- return p->getName();
- }
- }
- return "";
- }
|