-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscheduler.php
More file actions
executable file
·271 lines (240 loc) · 10.2 KB
/
scheduler.php
File metadata and controls
executable file
·271 lines (240 loc) · 10.2 KB
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
<?php
// 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 2 of the License, or (at your
// option) any later version.
//
// File: scheduler.php
/******************************************************************
* Explanation of the scheduler *
* *
* Here are the scheduler DB fields, and what they are used for : *
* - sched_id : Unique ID. Before calling the file responsible *
* for the event, the variable $sched_var_id will be set to *
* this value, so the called file can modify the triggering *
* scheduler entry if it needs to. *
* *
* - is_loop : Set this to 'Y' if you want the event to be looped *
* endlessly. If this value is set to 'Y', the 'spawn' field is *
* not used. *
* *
* - loop_count : If you want your event to be run a certain number *
* of times only, set this to the number of times. For this to *
* work, loop must be set to 'N'. When the event has been run *
* spawn number of times, it is deleted from the scheduler. *
* *
* - ticks_left : Used internally by the scheduler. It represents *
* the number of mins elapsed since the last call. ALWAYS set *
* this to 0 when scheduling a new event. *
* *
* - ticks_full : This is the interval in minutes between *
* different runs of your event. Set this to the frenquency *
* you wish the event to happen. For example, if you want your *
* event to be run every three minutes, set this to 3. *
* *
* - file : This is the file that will be called when an event *
* has been trigerred. *
* *
* - extra_info : This is a text variable that can be used to *
* store any extra information concerning the event triggered. *
* It will be made available to the called file through the *
* variable $sched_var_extrainfo. *
* *
* If you are including files in your trigger file, it is important*
* to use include_once() instead of include(), as your file might *
* be called multiple times in a single execution. If you need to *
* define functions, you can put them in your own *
* include file, with an include statement. THEY CANNOT BE *
* DEFINED IN YOUR MAIN FILE BODY. This would cause PHP to issue a *
* multiple function declaration error. *
* *
* End of scheduler explanation *
******************************************************************/
// AADATA&admin_password=password&game_number=0
require_once ("config/config_sched.php");
scheduler_log("Scheduler Started","\n");
$template_object->enable_gzip = 0;
$langdir = $default_lang;
include ("languages/$langdir/lang_common.inc");
$title = "System Update";
if($adminexecuted == 1){
TextFlush("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"Pragma\" content=\"no-cache\">
<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">
<title>$title</title>
<style type=\"text/css\">
<!--
body { font-family: Verdana, Arial, sans-serif; font-size: x-small;}
td { font-size: 12px; color: #e0e0e0; font-family: verdana; }
-->
</style>
</head>
<body marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 background=\"templates/default_paged/images/bgoutspace1.png\" bgcolor=\"#000000\" text=\"#c0c0c0\" link=\"#52ACEA\" vlink=\"#52ACEA\" alink=\"#52ACEA\">
<table><tr><td>
");
TextFlush( "<H1>$title</H1>\n");
}
$BenchmarkTimer = new c_Timer;
$sf = (bool) ini_get('safe_mode');
if (!$sf)
{
set_time_limit(600);
}
mt_srand(hexdec(AAT_substr(md5(microtime()), -8)) & 0x7fffffff);
if ($admin_password == $adminpass)
{
$sched_res = $db->SelectLimit("SELECT last_run FROM {$db_prefix}scheduler where enable_schedule = 1 order by execution_order ASC", 1);
$startschedtime = $sched_res->fields['last_run'];
$sched_res->close();
$sched_res = $db->SelectLimit("SELECT last_run FROM {$db_prefix}scheduler where enable_schedule = 1 order by execution_order DESC", 1);
$endschedtime = $sched_res->fields['last_run'];
$sched_res->close();
$unixdate = strtotime($startschedtime);
$numberofticks = 3;
$daylightsavingstimelimit = ($numberofticks * $sched_ticks) * 60; // How many seconds outside normal before reset (3 * 5) * 60 = 900 (15 minutes)
$unixdatecheck = strtotime(date("Y-m-d H:i:s")) - $daylightsavingstimelimit; // Spring forward check
$unixdatecheck2 = strtotime(date("Y-m-d H:i:s")) + $daylightsavingstimelimit; // Fall back check
$startschedtime = $endschedtime;
if($startschedtime == $endschedtime || $unixdate < $unixdatecheck || $unixdate > $unixdatecheck2)
{
$starttime = time();
$findem = $db->Execute("SELECT sector_id FROM {$db_prefix}universe where sg_sector = 0 and sector_id > 3");
$sector_list_total=$findem->RecordCount();
$sector_list=$findem->GetArray();
$findem->close();
$cargo_query = $db->Execute("SELECT class, prate from {$db_prefix}class_modules_commodities where cargoport=1");
db_op_result($cargo_query,__LINE__,__FILE__);
while (!$cargo_query->EOF)
{
$cargotype = $cargo_query->fields['class'];
$prate[$cargotype] = $cargo_query->fields['prate'];
$cargo_query->Movenext();
}
$cargo_query->close();
$cargo_query = $db->Execute("SELECT class, prate from {$db_prefix}class_modules_planet where class = 'Planet_Torpedo' or class = 'Planet_Fighter'");
db_op_result($cargo_query,__LINE__,__FILE__);
while (!$cargo_query->EOF)
{
$cargotype = $cargo_query->fields['class'];
$prate[$cargotype] = $cargo_query->fields['prate'];
$cargo_query->Movenext();
}
$cargo_query->close();
$runstamp = date("Y-m-d H:i:s");
$sched_res = $db->Execute("SELECT * FROM {$db_prefix}scheduler where enable_schedule = 1 order by execution_order ASC");
if ($sched_res)
{
while (!$sched_res->EOF)
{
$event = $sched_res->fields;
$multiplier = ($sched_ticks / $event['ticks_full']) + ($event['ticks_left'] / $event['ticks_full']);
$multiplier = (int) $multiplier * $scheduler_passes;
$ticks_left = ($sched_ticks + $event['ticks_left']) % $event['ticks_full'];
$expoprod = mypw($colonist_reproduction_rate + 1, $multiplier) * $multiplier;
if ($event['is_loop'] == 'N')
{
if ($multiplier > $event['loop_count'])
{
$multiplier = $event['loop_count'];
}
if ($event['loop_count'] == 0)
{
$debug_query = $db->Execute("UPDATE {$db_prefix}scheduler SET enable_schedule='0' WHERE sched_id=$event[sched_id]");
db_op_result($debug_query,__LINE__,__FILE__);
}
else
{
$sched_var_id = $event['sched_id'];
$sched_var_extrainfo = $event['extra_info'];
$sched_i = 0;
while ($sched_i < $multiplier)
{
if($enable_scheduler == 1 or $event['sched_file'] == "sched_updateserverlist.inc"){
$BenchmarkTimer->start();
TextFlush();
$querycountstart = $db->query_count;
scheduler_log("Starting $event[sched_file]","");
include ("scheduler/$event[sched_file]");
$objectexecution = new scheduler();
scheduler_log("Ending $event[sched_file]","\n");
$StopTime = $BenchmarkTimer->stop();
$Elapsed = $BenchmarkTimer->elapsed();
$Elapsed = sprintf("%01.2f", $Elapsed);
$querycountend = $db->query_count - $querycountstart;
TextFlush("<br>\nElapsed Time: $Elapsed seconds, Queries Executed: $querycountend<br>");
TextFlush ("<hr>");
}
else
{
TextFlush();
TextFlush ("Scheduler Disabled $event[sched_file]<br><hr><br>");
}
$sched_i++;
}
$debug_query = $db->Execute("UPDATE {$db_prefix}scheduler SET last_run='$runstamp', ticks_left=$ticks_left, loop_count=loop_count-$multiplier WHERE sched_id=$event[sched_id]");
db_op_result($debug_query,__LINE__,__FILE__);
}
}
else
{
$sched_var_id = $event['sched_id'];
$sched_var_extrainfo = $event['extra_info'];
$sched_i = 0;
while ($sched_i < $multiplier)
{
if($enable_scheduler == 1 or $event['sched_file'] == "sched_updateserverlist.inc"){
$BenchmarkTimer->start();
TextFlush();
$querycountstart = $db->query_count;
scheduler_log("Starting $event[sched_file]","");
include ("scheduler/$event[sched_file]");
scheduler_log("Ending $event[sched_file]","\n");
$StopTime = $BenchmarkTimer->stop();
$Elapsed = $BenchmarkTimer->elapsed();
$Elapsed = sprintf("%01.2f", $Elapsed);
$querycountend = $db->query_count - $querycountstart;
TextFlush("<br>\nElapsed Time: $Elapsed seconds, Queries Executed: $querycountend<br>");
TextFlush ("<hr>");
}
else
{
TextFlush();
TextFlush ("Scheduler Disabled $event[sched_file]<br><hr><br>");
}
$sched_i++;
}
$debug_query = $db->Execute("UPDATE {$db_prefix}scheduler SET last_run='$runstamp', ticks_left=$ticks_left WHERE sched_id=$event[sched_id]");
db_op_result($debug_query,__LINE__,__FILE__);
}
$sched_res->MoveNext();
}
}
$sched_res->close();
}
else
{
TextFlush ("Previous schedule has not finished executing.<br><br>");
}
$runtime = time() - $starttime;
TextFlush ("<p>The scheduler took $runtime seconds to execute.<br>" . $db->query_count . " queries in " . sprintf("%01.4f", $db->query_time_total) . " seconds<p>");
scheduler_log("The scheduler took $runtime seconds to execute.\n" . $db->query_count . " queries in " . sprintf("%01.4f", $db->query_time_total) . " seconds<p>","\n");
scheduler_log("Scheduler Ended","\n\n");
if($adminexecuted == 1){
unset ($template_object);
TextFlush ("<br><br></td></tr></table>");
TextFlush( "</body></html>\n");
}
}
else
{
if($adminexecuted == 1){
unset ($template_object);
TextFlush ("Scheduler Failed<br><br></td></tr></table>");
TextFlush( "</body></html>\n");
}
scheduler_log("Scheduler Failed","\n");
}
$db->close();
?>