forked from stfc2/Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily.php
342 lines (259 loc) · 12.5 KB
/
daily.php
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
<?php
/*
This file is part of STFC.it
Copyright 2008-2013 by Andrea Carolfi ([email protected]) and Cristiano Delogu
STFC.it is based on STFC
Copyright 2006-2007 by Michael Krauss ([email protected]) and Tobias Gafner
STFC is based on STGC,
Copyright 2003-2007 by Florian Brede ([email protected]) and Philipp Schmidt
STFC 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.
STFC 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/>.
*/
// #######################################################################################
// #######################################################################################
// Startup Config
// include game definitions, path url and so on
include('config.script.php');
// include commons classes and functions
include('commons.php');
include($game_path . 'include/global.php');
include($game_path . 'include/functions.php');
include($game_path . 'include/libs/maps.php');
define('TICK_LOG_FILE', $game_path . 'logs/daily.log');
define('IN_SCHEDULER', true); // we are in the scheduler...
error_reporting(E_ERROR);
if(!empty($_SERVER['SERVER_SOFTWARE'])) {
echo 'The scheduler can only be called by CLI!'; exit;
}
// #######################################################################################
// #######################################################################################
// Init
$starttime = ( microtime() + time() );
// create logging facility
$sdl = new scheduler();
// create sql-object for db-connection
$db = new sql($config['server'].":".$config['port'],
$config['game_database'],
$config['user'],
$config['password']);
$game = new game();
$sdl->log('<br><br><br><b>-------------------------------------------------------------</b><br>'.
'<b>Starting Daily script at '.date('d.m.y H:i:s', time()).'</b>');
// #######################################################################################
// #######################################################################################
// Reset New Registration Count
$sdl->start_job('Reset New Registration Count');
if(!$db->query('UPDATE config SET new_register = 0'))
$sdl->log('<b>Error:</b> cannot reset new registration count!');
$sdl->finish_job('Reset New Registration Count');
// #######################################################################################
// #######################################################################################
// Check sitting abuse and lock'em
$sdl->start_job('Sitting abuse check');
$sql = 'SELECT user_id,user_name,num_hits,num_sitting FROM user
WHERE (num_sitting/(num_hits+1))>0.35 AND
(num_sitting>50 OR (num_hits<10 AND num_sitting>30))';
if(($result = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> cannot select user data!');
}
else {
if(!$db->query('UPDATE user SET num_hits=0, num_sitting=0'))
$sdl->log('<b>Error:</b> cannot reset sitting information!');
while ($user=$db->fetchrow($result))
{
/* 25/08/14 - AC: According to the used query, num_sitting cannot be zero instead num_hits could be */
$val=$user['num_sitting']/($user['num_hits']+1);
/* 24/04/08 - AC: Add language translation based on user language */
switch($user['language'])
{
case 'GER':
$text='fast ausschließlich gesittet';
if ($val<0.8) $text='stark übermäßig gesittet';
if ($val<0.6) $text='Übermäßig gesittet';
if ($val<0.45) $text='etwas zuviel gesittet';
$message='Hallo '.$user['user_name'].',<br>dein Account kann <b>ab jetzt</b> für <b>einen Tag</b> nicht gesittet werden, weil er innerhalb der letzten 24 Stunden <b>'.$text.'</b> wurde.<br>Diese Nachricht wurde automatisch generiert, Beschwerden beim STFC2-Team bringen nichts.<br>~ Sitting-Abuse-Automatik';
$title='Sittingsperre';
break;
case 'ITA':
$text='quasi esclusivamente abusato';
if ($val<0.8) $text='fortemente abusato';
if ($val<0.6) $text='eccessivamente abusato';
if ($val<0.45) $text='leggermente abusato';
$message='Ciao '.$user['user_name'].',<br>il tuo account <b>da adesso</b> per <b>un giorno</b> non può essere dato in sitting, perché nelle ultime 24 ore è stato <b>'.$text.'</b>.<br>Questo messaggio è stato generato automaticamente, lamentele al team di STFC2 sono inutili.<br>~ Abuso Sitting Automatico';
$title='Sitting bloccato';
break;
default:
$text='almost exclusively sitted';
if ($val<0.8) $text='very excessively sitted';
if ($val<0.6) $text='excessively sitted';
if ($val<0.45) $text='a little too sitted';
$message='Hello '.$user['user_name'].',<br>your account <b>now</b> for <b>one day</b> can not be sitted, because within the last 24 hours it was <b>'.$text.'</ b>.<br>This message was automatically generated, complaints to the STFC2 team bring nothing.<br>~Automatic Abuse Sitting';
$title='Sitting locked';
break;
}
SystemMessage($user['user_id'],$title,$message);
$sql = 'UPDATE user SET num_sitting=-1 WHERE user_id='.$user['user_id'];
if(!$db->query($sql))
$sdl->log('<b>Error:</b> cannot lock sitting for user ID '.$user['user_id'].'!');
$sdl->log('User sitting locked: ID '.$user['user_id'].' Name: '.$user['user_name'].' Abuse: '.$val);
}
}
$sdl->finish_job('Sitting abuse check');
// #######################################################################################
// #######################################################################################
// Create or update galaxy map image
$sdl->start_job('Create galaxy map');
$maps = new maps();
$maps->create_galaxy_detail_map();
if (($destimg = ImageCreateTrueColor(150,150)) === false) {
$sdl->log('<b>Error:</b> problem creating image');
}
else {
if(($srcimg = ImageCreateFromPNG($game_path . 'maps/images/galaxy_detail.png')) === false) {
$sdl->log('<b>Error:</b> problem opening source image');
}
else {
if(!ImageCopyResampled($destimg,$srcimg,0,0,0,0,150,159,
ImageSX($srcimg),ImageSY($srcimg))) {
$sdl->log('<b>Error:</b> problem resizing image');
}
else {
imagepng ($destimg,$game_path . 'maps/images/galaxy_detail_small.png');
}
imagedestroy($srcimg);
}
imagedestroy($destimg);
}
$sdl->finish_job('Create galaxy map');
// #######################################################################################
// #######################################################################################
// Keep light the DB: delete old fleets movements!
$sdl->start_job('Slimming ship movements');
if(($cfg_data = $db->queryrow('SELECT * FROM config')) === false) {
$sdl->log('<b>Error:</b> cannot query tick data!');
}
else {
$ACTUAL_TICK = $cfg_data['tick_id'];
if(!$cfg_data['tick_stopped']) {
$oneWeek = (7 * 24 * 60 * 60);
$time = $ACTUAL_TICK - ($oneWeek / (TICK_DURATION * 60));
// Delete one week old deactivated moves
$sql = 'DELETE FROM scheduler_shipmovement
WHERE move_finish <= '.$time.' AND
move_status <> 0 AND
move_exec_started <> 0';
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> cannot delete scheduler_shipmovement data!');
}
}
else
$sdl->log('Tick has been stopped (Unlock in table "config")');
}
$sdl->finish_job('Slimming ship movements');
// #######################################################################################
// #######################################################################################
// Keep optimized tables with frequent deletions
$sdl->start_job('Optimize tables');
$sql = 'OPTIMIZE TABLE `FHB_cache_trupp_trade`,
`FHB_warteschlange`,
`logbook`,
`message`,
`planets`,
`scheduler_instbuild`,
`scheduler_research`,
`scheduler_resourcetrade`,
`scheduler_shipbuild`,
`scheduler_shipmovement`,
`ships`,
`ship_fleets`,
`starsystems_slots`,
`user`,
`user_diplomacy`';
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> cannot optimize frequently shrunk tables!');
}
$sdl->finish_job('Optimize tables');
// #######################################################################################
// #######################################################################################
// Compress yesterday's log files
$sdl->start_job('Compress log files');
// Yesterday date formatted
$yesterday = date('d-m-Y', strtotime("yesterday"));
// Names of the files we are compressing
$files = array(
$game_path."logs/tick_".$yesterday.".log",
$game_path."logs/moves_tick_".$yesterday.".log",
$game_path."logs/NPC_BOT_tick_".$yesterday.".log",
$game_path."logs/fixall/tick_".$yesterday.".log",
$game_path."logs/sixhours/tick_".$yesterday.".log"
);
foreach ($files as $key => $file) {
//$sdl->log("Compressing file ".$file."...");
// Compress file if it exists
if (file_exists($file)) {
// Name of the gz file we are creating
$gzfile = $file.".gz";
// Open the gz file (w9 is the highest compression)
if(($fp = gzopen ($gzfile, 'w9')) === false)
$sdl->log('<b>Error:</b> cannot create compressed file '.$gzfile);
else {
// Compress the file
gzwrite ($fp, file_get_contents($file));
// Close the gz file and we are done
gzclose($fp);
// Delete uncompressed file
if(!unlink($file))
$sdl->log('<b>Error:</b> cannot remove uncompressed file '.$file);
}
}
}
$sdl->finish_job('Compress log files');
// #######################################################################################
// #######################################################################################
// Clean temporary security images
$sdl->start_job('Clean temporary security images');
array_walk(glob($game_path."tmpsec/*.jpg"),'unlink');
$sdl->finish_job('Clean temporary security images');
// #######################################################################################
// #######################################################################################
// Once a month rotate admin end error HTML log files
$today = getdate();
if ($today['mday'] == 1) {
$sdl->start_job('Rotate log files');
// Prepare files postfix
$date = new DateTime('NOW');
$date->modify('-1 day');
$postfix = $date->format('_m_y');
$oldnames = array(
$game_path.'logs/admin_log.htm',
$game_path.'logs/error_log.htm'
);
$newnames = array(
$game_path.'logs/admin_log'.$postfix.'.htm',
$game_path.'logs/error_log'.$postfix.'.htm'
);
foreach ($oldnames as $i => $oldname) {
$newname = $newnames[$i];
if (!rename ($oldname, $newname)) {
$sdl->log('<b>Error:</b> cannot rename file '.$oldname.' into '.$newname);
}
if (!touch($oldname)) {
$sdl->log('<b>Error:</b> cannot create new empty file '.$oldname);
}
}
$sdl->finish_job('Rotate log files');
}
// #######################################################################################
// #######################################################################################
// Quit and close log
$db->close();
$sdl->log('<b>Finished Daily script in <font color=#009900>'.round((microtime()+time())-$starttime, 4).' secs</font><br>Executed Queries: <font color=#ff0000>'.$db->i_query.'</font></b>');
?>