- Timestamp:
- May 2, 2011, 10:20:45 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/tutoriallevel2/src/modules/notifications/NotificationManager.cc
r8371 r8374 36 36 #include "core/command/ConsoleCommand.h" 37 37 #include "core/CoreIncludes.h" 38 #include "core/GameMode.h" 38 39 #include "core/GUIManager.h" 39 40 #include "core/LuaState.h" 40 #include "network/Host.h"41 #include "network/NetworkFunction.h"42 41 #include "util/Convert.h" 43 42 #include "util/ScopedSingletonManager.h" … … 53 52 { 54 53 55 const std::string NotificationManager::ALL("all");56 const std::string NotificationManager::NONE("none");57 58 54 // Register tolua_open function when loading the library. 59 55 DeclareToluaInterface(Notifications); … … 64 60 SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode); 65 61 66 registerStaticNetworkFunction(NotificationManager::sendNotification);67 68 62 /** 69 63 @brief … … 73 67 { 74 68 RegisterRootObject(NotificationManager); 75 76 this->highestIndex_ = 0;77 69 78 70 ModifyConsoleCommand("enterEditMode").setObject(this); … … 113 105 this->queues_.clear(); 114 106 } 115 116 /** 117 @brief 118 Sends a Notification with the specified message to the specified client from the specified sender. 119 @param message 120 The message that should be sent. 121 @param clientId 122 The id of the client the notification should be sent to. 123 @param sender 124 The sender that sent the notification. 125 @param isLocal 126 If this is set to true (false is default), then the Notification is sent to the client where this function is executed, meaning the Notification is sent locally. 127 */ 128 /*static*/ void NotificationManager::sendNotification(const std::string& message, unsigned int clientId, const std::string& sender, bool isLocal) 129 { 130 // If we're in standalone mode or we're already no the right client we create and send the Notification. 131 if(GameMode::isStandalone() || isLocal || Host::getPlayerID() == clientId) 132 { 133 Notification* notification = new Notification(message, sender); 134 if(NotificationManager::getInstance().registerNotification(notification)) 135 COUT(3) << "Notification \"" << notification->getMessage() << "\" sent." << std::endl; 136 } 137 // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network. 138 else if(GameMode::isServer()) 139 { 140 callStaticNetworkFunction(NotificationManager::sendNotification, clientId, message, clientId, sender); 141 } 142 } 143 144 /** 145 @brief 146 Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender. 107 108 bool NotificationManager::registerNotification(const std::string& message, const std::string& sender) 109 { 110 Notification* notification = new Notification(message, sender); 111 return this->registerNotification(notification); 112 } 113 114 /** 115 @brief 116 Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationQueues associated with its sender. 147 117 @param notification 148 118 The Notification to be registered. … … 159 129 this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification)); 160 130 161 if(notification->getSender() == Notification Manager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.131 if(notification->getSender() == NotificationListener::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications. 162 132 return true; 163 133 164 134 bool all = false; 165 if(notification->getSender() == Notification Manager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.135 if(notification->getSender() == NotificationListener::ALL) // If all are the sender, then the Notifications is added to every NotificationQueue. 166 136 all = true; 167 137 168 // Insert the Notification in all Notification Listeners that have its sender as target.169 for(std::map< NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.170 { 171 const std::set<std::string>& set = it-> first->getTargetsSet();172 bool bAll = set.find(Notification Manager::ALL) != set.end();173 // If either the Notification has as sender 'all', the Notification Listener displays all Notifications or the NotificationListenerhas the sender of the Notification as target.138 // Insert the Notification in all NotificationQueues that have its sender as target. 139 for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues. 140 { 141 const std::set<std::string>& set = it->second->getTargetsSet(); 142 bool bAll = set.find(NotificationListener::ALL) != set.end(); 143 // If either the Notification has as sender 'all', the NotificationQueue displays all Notifications or the NotificationQueue has the sender of the Notification as target. 174 144 if(all || bAll || set.find(notification->getSender()) != set.end()) 175 145 { 176 146 if(!bAll) 177 this->notificationLists_[it->second ]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.178 it-> first->update(notification, time); // Update the NotificationListener.147 this->notificationLists_[it->second->getName()]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationQueue. 148 it->second->update(notification, time); // Update the NotificationQueue. 179 149 } 180 150 } … … 187 157 /** 188 158 @brief 189 Unregisters a Notification within the NotificationManager for a given Notification Listener.159 Unregisters a Notification within the NotificationManager for a given NotificationQueue. 190 160 @param notification 191 161 A pointer to the Notification to be unregistered. 192 @param listener193 A pointer to the Notification Listenerthe Notification is unregistered for.194 */ 195 void NotificationManager::unregisterNotification(Notification* notification, Notification Listener* listener)162 @param queue 163 A pointer to the NotificationQueue the Notification is unregistered for. 164 */ 165 void NotificationManager::unregisterNotification(Notification* notification, NotificationQueue* queue) 196 166 { 197 167 assert(notification); 198 assert( listener);199 200 // Remove the Notification from the list of Notifications of the input Notification Listener.201 this->removeNotification(notification, *(this->notificationLists_.find( this->listenerList_.find(listener)->second)->second));202 203 COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;168 assert(queue); 169 170 // Remove the Notification from the list of Notifications of the input NotificationQueue. 171 this->removeNotification(notification, *(this->notificationLists_.find(queue->getName())->second)); 172 173 COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from NotificationQueue " << queue->getName() << "." << std::endl; 204 174 } 205 175 … … 231 201 /** 232 202 @brief 233 Registers a NotificationListener within the NotificationManager. 234 @param listener 235 The NotificationListener to be registered. 236 @return 237 Returns true if successful. Fales if the NotificationListener is already registered. 238 */ 239 bool NotificationManager::registerListener(NotificationListener* listener) 240 { 241 assert(listener); 242 243 // If the NotificationListener is already registered. 244 if(this->listenerList_.find(listener) != this->listenerList_.end()) 245 return false; 246 247 this->highestIndex_ += 1; 248 unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely. 249 250 this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners. 251 252 const std::set<std::string>& set = listener->getTargetsSet(); 253 254 // 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. 255 bool bAll = set.find(NotificationManager::ALL) != set.end(); 256 std::multimap<std::time_t, Notification*>* map = NULL; 257 if(bAll) 258 this->notificationLists_[index] = &this->allNotificationsList_; 259 // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners. 260 else 261 { 262 this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>; 263 map = this->notificationLists_[index]; 264 } 265 266 // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener. 267 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) 268 { 269 if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target. 270 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); 271 } 272 273 listener->update(); // Update the listener. 274 275 COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl; 276 277 return true; 278 } 279 280 /** 281 @brief 282 Unregisters a NotificationListener within the NotificationManager. 283 @param listener 284 The NotificationListener to be unregistered. 285 */ 286 void NotificationManager::unregisterListener(NotificationListener* listener) 287 { 288 assert(listener); 289 290 unsigned int identifier = this->listenerList_.find(listener)->second; 291 std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second; 292 293 // If the map is not the map of all Notifications, make sure all Notifications are unregistered. 294 std::multimap<std::time_t, Notification*>::iterator it = map->begin(); 295 if(map != &this->allNotificationsList_) 296 { 297 while(it != map->end()) 298 { 299 this->unregisterNotification(it->second, listener); 300 it = map->begin(); 301 } 302 delete map; 303 } 304 305 COUT(4) << "NotificationListener '" << identifier << "' unregistered with the NotificationManager." << std::endl; 306 307 // Remove the NotificationListener from the list of NotificationListeners. 308 this->listenerList_.erase(listener); 309 // Remove the Notifications list that was associated with the input NotificationListener. 310 this->notificationLists_.erase(identifier); 311 } 312 313 /** 314 @brief 315 Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map. 316 @param listener 317 The NotificationListener the Notifications are fetched for. 203 Fetches the Notifications for a specific NotificationQueue in a specified timeframe and stores them in the input map. 204 @param queue 205 The NotificationQueue the Notifications are fetched for. 318 206 @param map 319 207 A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. … … 325 213 Returns true if successful. 326 214 */ 327 void NotificationManager::getNotifications(Notification Listener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)328 { 329 assert( listener);215 void NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) 216 { 217 assert(queue); 330 218 assert(map); 331 219 332 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[ this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.220 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue. 333 221 334 222 std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; … … 343 231 /** 344 232 @brief 345 Fetches the newest Notifications for a specific Notification Listenerand stores them in the input map.346 @param listener347 The Notification Listenerthe Notifications are fetched for.233 Fetches the newest Notifications for a specific NotificationQueue and stores them in the input map. 234 @param queue 235 The NotificationQueue the Notifications are fetched for. 348 236 @param map 349 237 A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. … … 353 241 Returns true if successful. 354 242 */ 355 void NotificationManager::getNewestNotifications(Notification Listener* listener, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications)356 { 357 assert( listener);243 void NotificationManager::getNewestNotifications(NotificationQueue* queue, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications) 244 { 245 assert(queue); 358 246 assert(map); 359 247 360 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[ this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.248 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue. 361 249 362 250 if(!notifications->empty()) // If the list of Notifications is not empty. … … 390 278 @brief 391 279 Registers a NotificationQueue. 392 This makes sure that the NotificationQueue can be a ttained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.280 This makes sure that the NotificationQueue can be accessed through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager. 393 281 @param queue 394 282 A pointer to the NotificationQueue to be registered. … … 398 286 bool NotificationManager::registerQueue(NotificationQueue* queue) 399 287 { 288 assert(queue); 289 290 // If the NotificationQueue is already registered. 291 if(this->queues_.find(queue->getName()) != this->queues_.end()) 292 return false; 293 294 this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)); // Add the NotificationQueue to the list of NotificationQueues. 295 296 const std::set<std::string>& set = queue->getTargetsSet(); 297 298 // If all senders are the target of the NotificationQueue, then the list of Notifications for that specific NotificationQueue is the same as the list of all Notifications. 299 bool bAll = set.find(NotificationListener::ALL) != set.end(); 300 std::multimap<std::time_t, Notification*>* map = NULL; 301 if(bAll) 302 this->notificationLists_[queue->getName()] = &this->allNotificationsList_; 303 // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationQueues. 304 else 305 { 306 this->notificationLists_[queue->getName()] = new std::multimap<std::time_t, Notification*>; 307 map = this->notificationLists_[queue->getName()]; 308 } 309 310 // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue. 311 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) 312 { 313 if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target. 314 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); 315 } 316 317 queue->update(); // Update the queue. 318 400 319 COUT(4) << "NotificationQueue '" << queue->getName() << "' registered with the NotificationManager." << std::endl; 401 return t his->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;320 return true; 402 321 } 403 322 … … 410 329 void NotificationManager::unregisterQueue(NotificationQueue* queue) 411 330 { 331 assert(queue); 332 333 std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(queue->getName())->second; 334 335 // If the map is not the map of all Notifications, make sure all Notifications are unregistered. 336 std::multimap<std::time_t, Notification*>::iterator it = map->begin(); 337 if(map != &this->allNotificationsList_) 338 { 339 while(it != map->end()) 340 { 341 this->unregisterNotification(it->second, queue); 342 it = map->begin(); 343 } 344 delete map; 345 } 346 347 // Remove the NotificationQueue from the list of NotificationQueues. 348 this->queues_.erase(queue->getName()); 349 // Remove the Notifications list that was associated with the input NotificationQueue. 350 this->notificationLists_.erase(queue->getName()); 351 412 352 COUT(4) << "NotificationQueue '" << queue->getName() << "' unregistered with the NotificationManager." << std::endl; 413 this->queues_.erase(queue->getName());414 353 } 415 354 … … 424 363 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.moveQueue(\"all\", 0, 10, 0.3, 0)"); 425 364 426 NotificationQueue* infoQueue = new NotificationQueue("info", Notification Manager::ALL, 1, -1);365 NotificationQueue* infoQueue = new NotificationQueue("info", NotificationListener::ALL, 1, -1); 427 366 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changeQueueFont(\"info\", 24, \"FFFFFF00\")"); 428 367 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.resizeQueue(\"info\", 0.6, 0, " + multi_cast<std::string>(infoQueue->getMaxSize()) + ")");
Note: See TracChangeset
for help on using the changeset viewer.