[2280] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Damian 'Mozork' Frick |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[2911] | 29 | /** |
---|
[7403] | 30 | @file NotificationManager.cc |
---|
[2911] | 31 | @brief Implementation of the NotificationManager class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2280] | 34 | #include "NotificationManager.h" |
---|
| 35 | |
---|
[7403] | 36 | #include "core/command/ConsoleCommand.h" |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/GUIManager.h" |
---|
| 39 | #include "core/LuaState.h" |
---|
[7474] | 40 | #include "network/Host.h" |
---|
| 41 | #include "network/NetworkFunction.h" |
---|
[7403] | 42 | #include "util/ScopedSingletonManager.h" |
---|
[2911] | 43 | |
---|
[5619] | 44 | #include "interfaces/NotificationListener.h" |
---|
[2280] | 45 | |
---|
[7403] | 46 | #include "Notification.h" |
---|
| 47 | #include "NotificationQueue.h" |
---|
| 48 | |
---|
| 49 | #include "ToluaBindNotifications.h" |
---|
| 50 | |
---|
[2435] | 51 | namespace orxonox |
---|
| 52 | { |
---|
[2280] | 53 | |
---|
[6417] | 54 | const std::string NotificationManager::ALL("all"); |
---|
| 55 | const std::string NotificationManager::NONE("none"); |
---|
[2911] | 56 | |
---|
[7403] | 57 | // Register tolua_open function when loading the library. |
---|
| 58 | DeclareToluaInterface(Notifications); |
---|
[2911] | 59 | |
---|
[7403] | 60 | ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false); |
---|
| 61 | |
---|
[7484] | 62 | // Setting console command to enter the edit mode. |
---|
[7403] | 63 | SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode); |
---|
| 64 | |
---|
[7474] | 65 | registerStaticNetworkFunction(NotificationManager::sendNotification); |
---|
| 66 | |
---|
[2911] | 67 | /** |
---|
| 68 | @brief |
---|
| 69 | Constructor. Registers the Object. |
---|
| 70 | */ |
---|
| 71 | NotificationManager::NotificationManager() |
---|
[2280] | 72 | { |
---|
[2911] | 73 | RegisterRootObject(NotificationManager); |
---|
| 74 | |
---|
| 75 | this->highestIndex_ = 0; |
---|
[7403] | 76 | |
---|
| 77 | ModifyConsoleCommand("enterEditMode").setObject(this); |
---|
| 78 | |
---|
| 79 | COUT(3) << "NotificatioManager created." << std::endl; |
---|
[2280] | 80 | } |
---|
| 81 | |
---|
[2911] | 82 | /** |
---|
| 83 | @brief |
---|
| 84 | Destructor. |
---|
| 85 | */ |
---|
[2280] | 86 | NotificationManager::~NotificationManager() |
---|
| 87 | { |
---|
[7403] | 88 | ModifyConsoleCommand("enterEditMode").setObject(NULL); |
---|
[7163] | 89 | |
---|
[7403] | 90 | COUT(3) << "NotificationManager destroyed." << std::endl; |
---|
[2280] | 91 | } |
---|
[2501] | 92 | |
---|
[2911] | 93 | /** |
---|
| 94 | @brief |
---|
[7403] | 95 | Is called before the object is destroyed. |
---|
| 96 | */ |
---|
| 97 | void NotificationManager::preDestroy(void) |
---|
| 98 | { |
---|
| 99 | // Destroys all NotificationQueues that have been registered with the NotificationManager. |
---|
| 100 | for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); ) |
---|
| 101 | { |
---|
| 102 | NotificationQueue* queue = (*it).second; |
---|
| 103 | it++; |
---|
| 104 | queue->destroy(); |
---|
| 105 | } |
---|
| 106 | this->queues_.clear(); |
---|
| 107 | } |
---|
| 108 | |
---|
[7484] | 109 | /** |
---|
| 110 | @brief |
---|
| 111 | Sends a Notification with the specified message to the specified client from the specified sender. |
---|
| 112 | @param message |
---|
| 113 | The message that should be sent. |
---|
| 114 | @param clientId |
---|
| 115 | The id of the client the notification should be sent to. |
---|
| 116 | @param sender |
---|
| 117 | The sender that sent the notification. |
---|
| 118 | */ |
---|
[7474] | 119 | /*static*/ void NotificationManager::sendNotification(const std::string& message, unsigned int clientId, const std::string& sender) |
---|
| 120 | { |
---|
[7484] | 121 | // If we're in standalone mode or we're already no the right client we create and send the Notification. |
---|
[7474] | 122 | if(GameMode::isStandalone() || Host::getPlayerID() == clientId) |
---|
| 123 | { |
---|
| 124 | Notification* notification = new Notification(message); |
---|
| 125 | notification->send(sender); |
---|
| 126 | } |
---|
[7484] | 127 | // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network. |
---|
[7474] | 128 | else if(GameMode::isServer()) |
---|
| 129 | { |
---|
| 130 | callStaticNetworkFunction(NotificationManager::sendNotification, clientId, message, clientId, sender); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
[7403] | 134 | /** |
---|
| 135 | @brief |
---|
[5619] | 136 | Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender. |
---|
[2911] | 137 | @param notification |
---|
| 138 | The Notification to be registered. |
---|
| 139 | @return |
---|
| 140 | Returns true if successful. |
---|
| 141 | */ |
---|
| 142 | bool NotificationManager::registerNotification(Notification* notification) |
---|
| 143 | { |
---|
[7403] | 144 | assert(notification); |
---|
[5619] | 145 | |
---|
[5748] | 146 | std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time. |
---|
[5619] | 147 | |
---|
[7403] | 148 | // Add the Notification to the list that holds all Notifications. |
---|
| 149 | this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification)); |
---|
[5619] | 150 | |
---|
[7403] | 151 | if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications. |
---|
[2911] | 152 | return true; |
---|
[5619] | 153 | |
---|
[2911] | 154 | bool all = false; |
---|
[7403] | 155 | if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener. |
---|
[2911] | 156 | all = true; |
---|
[5619] | 157 | |
---|
[7403] | 158 | // Insert the Notification in all NotificationListeners that have its sender as target. |
---|
| 159 | for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners. |
---|
[2435] | 160 | { |
---|
[7417] | 161 | const std::set<std::string>& set = it->first->getTargetsSet(); |
---|
[7403] | 162 | bool bAll = set.find(NotificationManager::ALL) != set.end(); |
---|
| 163 | // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target. |
---|
| 164 | if(all || bAll || set.find(notification->getSender()) != set.end()) |
---|
[2280] | 165 | { |
---|
[7403] | 166 | if(!bAll) |
---|
| 167 | this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener. |
---|
| 168 | it->first->update(notification, time); // Update the NotificationListener. |
---|
[2435] | 169 | } |
---|
| 170 | } |
---|
[5619] | 171 | |
---|
[7403] | 172 | COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl; |
---|
[5619] | 173 | |
---|
[2280] | 174 | return true; |
---|
| 175 | } |
---|
[5619] | 176 | |
---|
[2911] | 177 | /** |
---|
| 178 | @brief |
---|
[7403] | 179 | Unregisters a Notification within the NotificationManager for a given NotificationListener. |
---|
[7163] | 180 | @param notification |
---|
| 181 | A pointer to the Notification to be unregistered. |
---|
| 182 | @param listener |
---|
| 183 | A pointer to the NotificationListener the Notification is unregistered for. |
---|
| 184 | */ |
---|
| 185 | void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener) |
---|
| 186 | { |
---|
| 187 | assert(notification); |
---|
| 188 | assert(listener); |
---|
| 189 | |
---|
[7403] | 190 | // Remove the Notification from the list of Notifications of the input NotificationListener. |
---|
| 191 | this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second)); |
---|
[7163] | 192 | |
---|
[7403] | 193 | COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl; |
---|
[7163] | 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | @brief |
---|
[7403] | 198 | Helper method that removes an input Notification form an input map. |
---|
[7163] | 199 | @param notification |
---|
[7403] | 200 | A pointer to the Notification to be removed. |
---|
[7163] | 201 | @param map |
---|
[7403] | 202 | The map the Notification should be removed from. |
---|
[7163] | 203 | @return |
---|
| 204 | Returns true if successful. |
---|
| 205 | */ |
---|
| 206 | bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map) |
---|
| 207 | { |
---|
| 208 | // Iterates through all items in the map until the Notification is found. |
---|
| 209 | //TODO: Do more efficiently? |
---|
| 210 | for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++) |
---|
| 211 | { |
---|
| 212 | if(it->second == notification) |
---|
| 213 | { |
---|
| 214 | map.erase(it); |
---|
| 215 | return true; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | @brief |
---|
[5619] | 223 | Registers a NotificationListener within the NotificationManager. |
---|
| 224 | @param listener |
---|
| 225 | The NotificationListener to be registered. |
---|
[2911] | 226 | @return |
---|
[7403] | 227 | Returns true if successful. Fales if the NotificationListener is already registered. |
---|
[2911] | 228 | */ |
---|
[5619] | 229 | bool NotificationManager::registerListener(NotificationListener* listener) |
---|
[2280] | 230 | { |
---|
[7403] | 231 | assert(listener); |
---|
| 232 | |
---|
| 233 | // If the NotificationListener is already registered. |
---|
| 234 | if(this->listenerList_.find(listener) != this->listenerList_.end()) |
---|
| 235 | return false; |
---|
| 236 | |
---|
[2911] | 237 | this->highestIndex_ += 1; |
---|
[7403] | 238 | unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely. |
---|
[5619] | 239 | |
---|
[7403] | 240 | this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners. |
---|
[5619] | 241 | |
---|
[7417] | 242 | const std::set<std::string>& set = listener->getTargetsSet(); |
---|
[5619] | 243 | |
---|
[7403] | 244 | // If all senders are the target of the NotificationListener, then the list of Notifications for that specific NotificationListener is the same as the list of all Notifications. |
---|
| 245 | bool bAll = set.find(NotificationManager::ALL) != set.end(); |
---|
| 246 | std::multimap<std::time_t, Notification*>* map; |
---|
| 247 | if(bAll) |
---|
| 248 | this->notificationLists_[index] = &this->allNotificationsList_; |
---|
| 249 | // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners. |
---|
| 250 | else |
---|
[2284] | 251 | { |
---|
[7403] | 252 | this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>; |
---|
| 253 | map = this->notificationLists_[index]; |
---|
[2284] | 254 | } |
---|
[5619] | 255 | |
---|
[7403] | 256 | // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener. |
---|
| 257 | for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) |
---|
[2911] | 258 | { |
---|
[7403] | 259 | if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target. |
---|
| 260 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); |
---|
[2911] | 261 | } |
---|
[2501] | 262 | |
---|
[7403] | 263 | listener->update(); // Update the listener. |
---|
[5619] | 264 | |
---|
[7163] | 265 | COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl; |
---|
[5619] | 266 | |
---|
[2911] | 267 | return true; |
---|
[2280] | 268 | } |
---|
[5619] | 269 | |
---|
[2911] | 270 | /** |
---|
| 271 | @brief |
---|
[7403] | 272 | Unregisters a NotificationListener within the NotificationManager. |
---|
| 273 | @param listener |
---|
| 274 | The NotificationListener to be unregistered. |
---|
[7163] | 275 | */ |
---|
| 276 | void NotificationManager::unregisterListener(NotificationListener* listener) |
---|
| 277 | { |
---|
| 278 | assert(listener); |
---|
| 279 | |
---|
[7403] | 280 | unsigned int identifier = this->listenerList_.find(listener)->second; |
---|
[7163] | 281 | std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second; |
---|
| 282 | |
---|
[7403] | 283 | // If the map is not the map of all Notifications, make sure all Notifications are unregistered. |
---|
[7163] | 284 | std::multimap<std::time_t, Notification*>::iterator it = map->begin(); |
---|
[7403] | 285 | if(map != &this->allNotificationsList_) |
---|
[7163] | 286 | { |
---|
[7403] | 287 | while(it != map->end()) |
---|
| 288 | { |
---|
| 289 | this->unregisterNotification(it->second, listener); |
---|
| 290 | it = map->begin(); |
---|
| 291 | } |
---|
| 292 | delete map; |
---|
[7163] | 293 | } |
---|
| 294 | |
---|
[7403] | 295 | // Remove the NotificationListener from the list of NotificationListeners. |
---|
[7163] | 296 | this->listenerList_.erase(listener); |
---|
[7403] | 297 | // Remove the Notifications list that was associated with the input NotificationListener. |
---|
[7163] | 298 | this->notificationLists_.erase(identifier); |
---|
| 299 | |
---|
| 300 | COUT(4) << "NotificationListener unregistered with the NotificationManager." << std::endl; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | @brief |
---|
[7403] | 305 | Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map. |
---|
[5619] | 306 | @param listener |
---|
| 307 | The NotificationListener the Notifications are fetched for. |
---|
[2911] | 308 | @param map |
---|
[7403] | 309 | A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. |
---|
[2911] | 310 | @param timeFrameStart |
---|
| 311 | The start time of the timeframe. |
---|
| 312 | @param timeFrameEnd |
---|
| 313 | The end time of the timeframe. |
---|
| 314 | @return |
---|
| 315 | Returns true if successful. |
---|
| 316 | */ |
---|
[7403] | 317 | void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) |
---|
[2346] | 318 | { |
---|
[7403] | 319 | assert(listener); |
---|
| 320 | assert(map); |
---|
[2501] | 321 | |
---|
[7403] | 322 | std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener. |
---|
[5619] | 323 | |
---|
[2911] | 324 | std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; |
---|
[7403] | 325 | // Iterators pointing to the bounds specified by the specified start and end times of the time frame. |
---|
[2911] | 326 | itLowest = notifications->lower_bound(timeFrameStart); |
---|
[7403] | 327 | itHighest = notifications->upper_bound(timeFrameEnd); |
---|
[5619] | 328 | |
---|
[7403] | 329 | for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it. |
---|
| 330 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map. |
---|
| 331 | } |
---|
[5619] | 332 | |
---|
[7403] | 333 | /** |
---|
| 334 | @brief |
---|
| 335 | Enters the edit mode of the NotificationLayer. |
---|
| 336 | */ |
---|
| 337 | void NotificationManager::enterEditMode(void) |
---|
| 338 | { |
---|
| 339 | GUIManager::getInstance().hideGUI("NotificationLayer"); |
---|
| 340 | GUIManager::getInstance().showGUI("NotificationLayer", false, false); |
---|
| 341 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()"); |
---|
[2346] | 342 | } |
---|
[2280] | 343 | |
---|
[7403] | 344 | /** |
---|
| 345 | @brief |
---|
| 346 | Registers a NotificationQueue. |
---|
| 347 | This makes sure that the NotificationQueue can be attained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager. |
---|
| 348 | @param queue |
---|
| 349 | A pointer to the NotificationQueue to be registered. |
---|
| 350 | @return |
---|
| 351 | Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false. |
---|
| 352 | */ |
---|
| 353 | bool NotificationManager::registerQueue(NotificationQueue* queue) |
---|
| 354 | { |
---|
| 355 | return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | @brief |
---|
| 360 | Unregisters a NotificationQueue. |
---|
| 361 | @param queue |
---|
| 362 | A pointer to the NotificationQueue to be unregistered. |
---|
| 363 | */ |
---|
| 364 | void NotificationManager::unregisterQueue(NotificationQueue* queue) |
---|
| 365 | { |
---|
| 366 | this->queues_.erase(queue->getName()); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | /** |
---|
| 370 | @brief |
---|
| 371 | Loads all the NotificationQueues that should exist. |
---|
| 372 | */ |
---|
| 373 | void NotificationManager::loadQueues(void) |
---|
| 374 | { |
---|
| 375 | new NotificationQueue("all"); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | /** |
---|
| 379 | @brief |
---|
| 380 | Creates a new NotificationQueue. |
---|
| 381 | This is used in lua. |
---|
| 382 | @param name |
---|
| 383 | The name of the new NotificationQueue. |
---|
| 384 | */ |
---|
| 385 | void NotificationManager::createQueue(const std::string& name) |
---|
| 386 | { |
---|
| 387 | new NotificationQueue(name); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | /** |
---|
| 391 | @brief |
---|
| 392 | Get the NotificationQueue with the input name. |
---|
| 393 | @param name |
---|
| 394 | The name of the NotificationQueue. |
---|
| 395 | @return |
---|
| 396 | Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists. |
---|
| 397 | */ |
---|
| 398 | NotificationQueue* NotificationManager::getQueue(const std::string & name) |
---|
| 399 | { |
---|
| 400 | std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name); |
---|
| 401 | // Returns NULL if no such NotificationQueue exists. |
---|
| 402 | if(it == this->queues_.end()) |
---|
| 403 | return NULL; |
---|
| 404 | |
---|
| 405 | return (*it).second; |
---|
| 406 | } |
---|
| 407 | |
---|
[2280] | 408 | } |
---|