ServerTimedMessage.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/OptionsScorched.h>
00022 #include <common/Logger.h>
00023 #include <common/Defines.h>
00024 #include <server/ServerTimedMessage.h>
00025 #include <server/ServerChannelManager.h>
00026 #include <server/ServerCommon.h>
00027 #include <server/ScorchedServer.h>
00028 #include <server/ServerState.h>
00029 #include <engine/GameState.h>
00030 #include <XML/XMLFile.h>
00031 #include <time.h>
00032 
00033 ServerTimedMessage::ServerTimedMessage() : 
00034         lastReadTime_(0), lastCheckTime_(0)
00035 {
00036 }
00037 
00038 ServerTimedMessage::~ServerTimedMessage()
00039 {
00040 }
00041 
00042 void ServerTimedMessage::simulate()
00043 {
00044 #ifndef S3D_SERVER
00045         return;
00046 #endif
00047 
00048         if (ScorchedServer::instance()->getGameState().getState() ==
00049                 ServerState::ServerStateTooFewPlayers) return;
00050 
00051         time_t currentTime = time(0);
00052         if (currentTime > lastCheckTime_ + 5)
00053         {
00054                 lastCheckTime_ = currentTime;
00055 
00056                 load();
00057                 checkEntries(currentTime);
00058         }
00059 }
00060 
00061 void ServerTimedMessage::checkEntries(time_t currentTime)
00062 {
00063         std::list<TimedMessageEntry>::iterator itor;
00064         for (itor = entries_.begin();
00065                 itor != entries_.end();
00066                 itor++)
00067         {
00068                 TimedMessageEntry &entry = (*itor);
00069                 if (entry.lastTime + (time_t) entry.timeInterval < currentTime)
00070                 {
00071                         entry.lastTime = currentTime;
00072                         
00073                         LangString message = entry.messages.front();
00074                         ChannelText textMessage("announce", message);
00075                         ServerChannelManager::instance()->sendText(textMessage, false);
00076                         entry.messages.pop_front();
00077                         entry.messages.push_back(message);
00078                 }
00079         }
00080 
00081 
00082 }
00083 
00084 bool ServerTimedMessage::load()
00085 {
00086         std::string filename = 
00087                 S3D::getSettingsFile(S3D::formatStringBuffer("messages-%i.xml", 
00088                         ScorchedServer::instance()->getOptionsGame().getPortNo()));
00089         if (!S3D::fileExists(filename)) return true;
00090 
00091         time_t fileTime = S3D::fileModTime(filename);
00092         if (fileTime == lastReadTime_) return true;
00093 
00094         XMLFile file;
00095         if (!file.readFile(filename))
00096         {
00097                 Logger::log(S3D::formatStringBuffer("Failed to parse user file \"%s\"\n%s", 
00098                         filename.c_str(), file.getParserError()));
00099                 return false;
00100         }
00101 
00102         Logger::log(S3D::formatStringBuffer("Refreshing message list %s", filename.c_str()));
00103         lastReadTime_ = fileTime;
00104         entries_.clear();
00105         if (!file.getRootNode()) return true; // Empty File
00106 
00107         std::list<XMLNode *>::iterator childrenItor;
00108         std::list<XMLNode *> &children = file.getRootNode()->getChildren();
00109         for (childrenItor = children.begin();
00110                  childrenItor != children.end();
00111                 childrenItor++)
00112         {
00113                 XMLNode *currentNode = (*childrenItor);
00114 
00115                 std::string text;
00116                 TimedMessageEntry entry;
00117                 if (!currentNode->getNamedChild("repeattime", entry.timeInterval)) return false;
00118                 while (currentNode->getNamedChild("text", text, false))
00119                 {
00120                         entry.messages.push_back(LANG_STRING(text));
00121                 }
00122                 entries_.push_back(entry);
00123         }
00124         return true;
00125 }

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3