-
Notifications
You must be signed in to change notification settings - Fork 21
/
globalevent.cpp
376 lines (325 loc) · 9.7 KB
/
globalevent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// Global events
////////////////////////////////////////////////////////////////////////
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "globalevent.h"
#include "tools.h"
#include "player.h"
#include "scheduler.h"
#ifdef __DEBUG_LUASCRIPTS__
#include <sstream>
#endif
#include <cmath>
GlobalEvents::GlobalEvents() :
m_scriptInterface("GlobalEvent Interface")
{
m_scriptInterface.initState();
thinkEventId = 0;
timerEventId = 0;
}
GlobalEvents::~GlobalEvents()
{
clear();
}
void GlobalEvents::clearMap(GlobalEventMap& map)
{
for(GlobalEventMap::iterator it = map.begin(); it != map.end(); ++it)
delete it->second;
map.clear();
}
void GlobalEvents::clear()
{
g_scheduler.stopEvent(thinkEventId);
thinkEventId = 0;
g_scheduler.stopEvent(timerEventId);
timerEventId = 0;
clearMap(thinkMap);
clearMap(serverMap);
clearMap(timerMap);
m_scriptInterface.reInitState();
}
Event* GlobalEvents::getEvent(const std::string& nodeName)
{
if(asLowerCaseString(nodeName) == "globalevent")
return new GlobalEvent(&m_scriptInterface);
return NULL;
}
bool GlobalEvents::registerEvent(Event* event, xmlNodePtr)
{
GlobalEvent* globalEvent = dynamic_cast<GlobalEvent*>(event);
if(!globalEvent)
return false;
if(globalEvent->getEventType() == GLOBALEVENT_TIMER)
{
GlobalEventMap::iterator it = timerMap.find(globalEvent->getName());
if(it == timerMap.end())
{
timerMap.insert(std::make_pair(globalEvent->getName(), globalEvent));
if(timerEventId == 0)
{
timerEventId = g_scheduler.addEvent(createSchedulerTask(TIMER_INTERVAL,
boost::bind(&GlobalEvents::timer, this)));
}
return true;
}
}
else if(globalEvent->getEventType() != GLOBALEVENT_NONE)
{
GlobalEventMap::iterator it = serverMap.find(globalEvent->getName());
if(it == serverMap.end())
{
serverMap.insert(std::make_pair(globalEvent->getName(), globalEvent));
return true;
}
}
else // think event
{
GlobalEventMap::iterator it = thinkMap.find(globalEvent->getName());
if(it == thinkMap.end())
{
thinkMap.insert(std::make_pair(globalEvent->getName(), globalEvent));
if(thinkEventId == 0)
{
thinkEventId = g_scheduler.addEvent(createSchedulerTask(SCHEDULER_MINTICKS,
boost::bind(&GlobalEvents::think, this)));
}
return true;
}
}
std::cout << "[Warning - GlobalEvents::configureEvent] Duplicate registered globalevent with name: " << globalEvent->getName() << std::endl;
return false;
}
void GlobalEvents::startup()
{
execute(GLOBALEVENT_STARTUP);
}
void GlobalEvents::timer()
{
time_t now = time(NULL);
for(GlobalEventMap::iterator it = timerMap.begin(), end = timerMap.end(); it != end; ++it)
{
GlobalEvent* globalEvent = it->second;
if(globalEvent->getNextExecution() > now)
continue;
globalEvent->setNextExecution(globalEvent->getNextExecution() + 86400);
if(!globalEvent->executeEvent())
std::cout << "[Error - GlobalEvents::timer] Failed to execute event: " << globalEvent->getName() << std::endl;
}
g_scheduler.addEvent(createSchedulerTask(TIMER_INTERVAL,
boost::bind(&GlobalEvents::timer, this)));
}
void GlobalEvents::think()
{
int64_t now = OTSYS_TIME();
for(GlobalEventMap::iterator it = thinkMap.begin(); it != thinkMap.end(); ++it)
{
GlobalEvent* globalEvent = it->second;
if(globalEvent->getNextExecution() > now)
continue;
globalEvent->setNextExecution(globalEvent->getNextExecution() + now);
if(!globalEvent->executeEvent())
std::cout << "[Error - GlobalEvents::think] Failed to execute event: " << globalEvent->getName() << std::endl;
}
g_scheduler.addEvent(createSchedulerTask(SCHEDULER_MINTICKS,
boost::bind(&GlobalEvents::think, this)));
}
void GlobalEvents::execute(GlobalEvent_t type)
{
for(GlobalEventMap::iterator it = serverMap.begin(); it != serverMap.end(); ++it)
{
GlobalEvent* globalEvent = it->second;
if(globalEvent->getEventType() == type)
globalEvent->executeEvent();
}
}
GlobalEventMap GlobalEvents::getEventMap(GlobalEvent_t type)
{
switch(type)
{
case GLOBALEVENT_NONE:
return thinkMap;
case GLOBALEVENT_TIMER:
return timerMap;
case GLOBALEVENT_STARTUP:
case GLOBALEVENT_SHUTDOWN:
case GLOBALEVENT_RECORD:
{
GlobalEventMap retMap;
for(GlobalEventMap::iterator it = serverMap.begin(); it != serverMap.end(); ++it)
{
if(it->second->getEventType() == type)
retMap[it->first] = it->second;
}
return retMap;
}
default:
break;
}
return GlobalEventMap();
}
GlobalEvent::GlobalEvent(LuaScriptInterface* _interface):
Event(_interface)
{
m_nextExecution = 0;
m_interval = 0;
}
bool GlobalEvent::configureEvent(xmlNodePtr p)
{
std::string strValue;
if(!readXMLString(p, "name", strValue))
{
std::cout << "[Error - GlobalEvent::configureEvent] No name for a globalevent." << std::endl;
return false;
}
m_name = strValue;
m_eventType = GLOBALEVENT_NONE;
if(readXMLString(p, "type", strValue))
{
std::string tmpStrValue = asLowerCaseString(strValue);
if(tmpStrValue == "startup" || tmpStrValue == "start" || tmpStrValue == "load")
m_eventType = GLOBALEVENT_STARTUP;
else if(tmpStrValue == "shutdown" || tmpStrValue == "quit" || tmpStrValue == "exit")
m_eventType = GLOBALEVENT_SHUTDOWN;
else if(tmpStrValue == "record" || tmpStrValue == "playersrecord")
m_eventType = GLOBALEVENT_RECORD;
else
{
std::cout << "[Error - GlobalEvent::configureEvent] No valid type \"" << strValue << "\" for globalevent with name " << m_name << std::endl;
return false;
}
return true;
}
else if(readXMLString(p, "time", strValue) || readXMLString(p, "at", strValue))
{
std::vector<int32_t> params = vectorAtoi(explodeString(strValue, ":"));
if(params[0] < 0 || params[0] > 23)
{
std::cout << "[Error - GlobalEvent::configureEvent] No valid hour \"" << strValue << "\" for globalevent with name " << m_name << std::endl;
return false;
}
m_interval |= params[0] << 16;
int32_t hour = params[0];
int32_t min = 0;
int32_t sec = 0;
if(params.size() > 1)
{
if(params[1] < 0 || params[1] > 59)
{
std::cout << "[Error - GlobalEvent::configureEvent] No valid minute \"" << strValue << "\" for globalevent with name " << m_name << std::endl;
return false;
}
min = params[1];
if(params.size() > 2)
{
if(params[2] < 0 || params[2] > 59)
{
std::cout << "[Error - GlobalEvent::configureEvent] No valid second \"" << strValue << "\" for globalevent with name " << m_name << std::endl;
return false;
}
sec = params[2];
}
}
time_t current_time = time(NULL);
tm* timeinfo = localtime(¤t_time);
timeinfo->tm_hour = hour;
timeinfo->tm_min = min;
timeinfo->tm_sec = sec;
time_t difference = (time_t)difftime(mktime(timeinfo), current_time);
if(difference < 0)
difference += 86400;
m_nextExecution = current_time + difference;
m_eventType = GLOBALEVENT_TIMER;
return true;
}
int32_t intValue;
if(readXMLInteger(p, "interval", intValue))
{
m_interval = std::max<int32_t>(SCHEDULER_MINTICKS, intValue);
m_nextExecution = OTSYS_TIME() + m_interval;
return true;
}
std::cout << "[Error - GlobalEvent::configureEvent] No interval for globalevent with name " << m_name << std::endl;
return false;
}
std::string GlobalEvent::getScriptEventName()
{
switch(m_eventType)
{
case GLOBALEVENT_STARTUP:
return "onStartup";
case GLOBALEVENT_SHUTDOWN:
return "onShutdown";
case GLOBALEVENT_RECORD:
return "onRecord";
case GLOBALEVENT_TIMER:
return "onTime";
default:
break;
}
return "onThink";
}
uint32_t GlobalEvent::executeRecord(uint32_t current, uint32_t old)
{
//onRecord(current, old, cid)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
#ifdef __DEBUG_LUASCRIPTS__
std::ostringstream ss;
ss << getName() << " - " << old << " to " << current;
env->setEventDesc(ss.str());
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, current);
lua_pushnumber(L, old);
bool result = m_scriptInterface->callFunction(2);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - GlobalEvent::executeRecord] Call stack overflow." << std::endl;
return 0;
}
}
uint32_t GlobalEvent::executeEvent()
{
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
env->setScriptId(m_scriptId, m_scriptInterface);
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
int32_t params = 0;
if(m_eventType == GLOBALEVENT_NONE || m_eventType == GLOBALEVENT_TIMER)
{
lua_pushnumber(L, m_interval);
params = 1;
}
bool result = m_scriptInterface->callFunction(params);
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
std::cout << "[Error - GlobalEvent::executeEvent] Call stack overflow." << std::endl;
return 0;
}
}