1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <common/OptionsScorched.h>
22 #include <common/Logger.h>
23 #include <common/Defines.h>
24 #include <server/ServerTimedMessage.h>
25 #include <server/ServerChannelManager.h>
26 #include <server/ServerCommon.h>
27 #include <server/ScorchedServer.h>
28 #include <engine/GameState.h>
29 #include <XML/XMLFile.h>
30 #include <time.h>
31
ServerTimedMessage()32 ServerTimedMessage::ServerTimedMessage() :
33 lastReadTime_(0), lastCheckTime_(0)
34 {
35 }
36
~ServerTimedMessage()37 ServerTimedMessage::~ServerTimedMessage()
38 {
39 }
40
simulate()41 void ServerTimedMessage::simulate()
42 {
43 #ifndef S3D_SERVER
44 return;
45 #endif
46
47 //if (ScorchedServer::instance()->getGameState().getState() ==
48 // ServerState::ServerStateTooFewPlayers) return;
49
50 time_t currentTime = time(0);
51 if (currentTime > lastCheckTime_ + 5)
52 {
53 lastCheckTime_ = currentTime;
54
55 load();
56 checkEntries(currentTime);
57 }
58 }
59
checkEntries(time_t currentTime)60 void ServerTimedMessage::checkEntries(time_t currentTime)
61 {
62 std::list<TimedMessageEntry>::iterator itor;
63 for (itor = entries_.begin();
64 itor != entries_.end();
65 ++itor)
66 {
67 TimedMessageEntry &entry = (*itor);
68 if (entry.lastTime + (time_t) entry.timeInterval < currentTime)
69 {
70 entry.lastTime = currentTime;
71
72 LangString message = entry.messages.front();
73 ChannelText textMessage("announce", message);
74 ScorchedServer::instance()->getServerChannelManager().sendText(textMessage, false);
75 entry.messages.pop_front();
76 entry.messages.push_back(message);
77 }
78 }
79
80
81 }
82
load()83 bool ServerTimedMessage::load()
84 {
85 std::string filename =
86 S3D::getSettingsFile(S3D::formatStringBuffer("messages-%i.xml",
87 ScorchedServer::instance()->getOptionsGame().getPortNo()));
88 if (!S3D::fileExists(filename)) return true;
89
90 time_t fileTime = S3D::fileModTime(filename);
91 if (fileTime == lastReadTime_) return true;
92
93 XMLFile file;
94 if (!file.readFile(filename))
95 {
96 Logger::log(S3D::formatStringBuffer("Failed to parse user file \"%s\"\n%s",
97 filename.c_str(), file.getParserError()));
98 return false;
99 }
100
101 Logger::log(S3D::formatStringBuffer("Refreshing message list %s", filename.c_str()));
102 lastReadTime_ = fileTime;
103 entries_.clear();
104 if (!file.getRootNode()) return true; // Empty File
105
106 std::list<XMLNode *>::iterator childrenItor;
107 std::list<XMLNode *> &children = file.getRootNode()->getChildren();
108 for (childrenItor = children.begin();
109 childrenItor != children.end();
110 ++childrenItor)
111 {
112 XMLNode *currentNode = (*childrenItor);
113
114 std::string text;
115 TimedMessageEntry entry;
116 if (!currentNode->getNamedChild("repeattime", entry.timeInterval)) return false;
117 while (currentNode->getNamedChild("text", text, false))
118 {
119 entry.messages.push_back(LANG_STRING(text));
120 }
121 entries_.push_back(entry);
122 }
123 return true;
124 }
125