forked from stfc2/Scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
2434 lines (1909 loc) · 93.3 KB
/
main.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
This file is part of 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');
error_reporting(E_ERROR);
ini_set('memory_limit', '200M');
set_time_limit(240); // 4 minutes
if(!empty($_SERVER['SERVER_SOFTWARE'])) {
echo 'The scheduler can only be called by CLI!'; exit;
}
define('TICK_LOG_FILE', $game_path . 'logs/tick_'.date('d-m-Y', time()).'.log');
define('IN_SCHEDULER', true); // we are in the scheduler...
// include commons classes and functions
include('commons.php');
// ########################################################################################
// ########################################################################################
// Init
$starttime = ( microtime() + time() );
include($game_path . 'include/global.php');
include($game_path . 'include/functions.php');
include($game_path . 'include/text_races.php');
include($game_path . 'include/race_data.php');
include($game_path . 'include/ship_data.php');
include($game_path . 'include/libs/moves.php');
include($game_path . 'include/libs/world.php'); // Needed by NPC BOT
$sdl = new scheduler();
$db = new sql($config['server'].":".$config['port'], $config['game_database'], $config['user'], $config['password']); // create sql-object for db-connection
$game = new game();
$sdl->log('<br><br><br><b>-------------------------------------------------------------</b><br>'.
'<b>Starting Scheduler at '.date('d.m.y H:i:s', time()).'</b>');
if(($cfg_data = $db->queryrow('SELECT * FROM config')) === false) {
$sdl->log('- Fatal: Could not query tick data! ABORTED');
exit;
}
$ACTUAL_TICK = $cfg_data['tick_id'];
$NEXT_TICK = ($cfg_data['tick_time'] - time());
$LAST_TICK_TIME = ($cfg_data['tick_time']-TICK_DURATION*60);
$STARDATE = $cfg_data['stardate'];
$FUTURE_SHIP = $cfg_data['future_ship'];
if($cfg_data['tick_stopped']) {
$sdl->log('Finished Scheduler in '.round((microtime()+time())-$starttime, 4).' secs<br>Tick has been stopped (Unlock in table "config")');
exit;
}
if(empty($ACTUAL_TICK)) {
$sdl->log('Finished Scheduler in '.round((microtime()+time())-$starttime, 4).' secs<br>- Fatal: empty($ACTUAL_TICK) == true');
exit;
}
if(!$db->query('UPDATE config SET tick_time = '.(time() + 60 * TICK_DURATION))) {
$sdl->log('- Notice: Could not update tick_time! CONTINUED');
}
/*
Example Job:
$sdl->start_job('Mine Job');
do something ... during error / message:
$sdl->log('...');
best also - before, so it's apart from the other messages, also: $sdl->log('- this was not true');
$sdl->finish_job('Mine Job'); // terminates the timer
*/
// ########################################################################################
// ########################################################################################
$sdl->start_job('Extra-Optimal Range Upgrade Planet Step');
$threshold = new DateTime('now');
date_sub($threshold,date_interval_create_from_date_string("45 days"));
$uts_threshold = date_format($threshold,'U');
$sql= 'UPDATE planets SET planet_available_points = 677 WHERE planet_available_points = 320 AND planet_owner > 10 AND planet_owned_date < '.$uts_threshold;
$db->query($sql);
$res = $db->num_rows();
if ($res > 0) $sdl->log('Extra-Optimal Range Upgrade Planet this time: '.$res);
$sdl->finish_job('Extra-Optimal Range Upgrade Planet Step');
// ########################################################################################
// ########################################################################################
// Building Scheduler
$sdl->start_job('Building Scheduler');
$sql = 'SELECT planet_id,installation_type
FROM scheduler_instbuild
WHERE build_finish <= '.$ACTUAL_TICK;
if(($q_inst = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query scheduler instbuild data! - SKIPPED');
}
else if($db->num_rows() > 0)
{
while($build = $db->fetchrow($q_inst)) {
$recompute_static = (in_array($build['installation_type'], array(1, 2, 3, 11))) ? 1 : 0;
$sql = 'UPDATE planets
SET building_'.($build['installation_type'] + 1).' = building_'.($build['installation_type'] + 1).' + 1,
recompute_static = '.$recompute_static.'
WHERE planet_id = '.$build['planet_id'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Query sched_instbuild @ planets failed! - CONTINUED');
}
}
$sql = 'DELETE FROM scheduler_instbuild
WHERE build_finish <= '.$ACTUAL_TICK;
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not delete instbuild data - CONTINUED');
}
unset($build);
}
$sdl->finish_job('Building Scheduler');
// ########################################################################################
// ########################################################################################
// Academy Scheduler
$sdl->start_job('Academy Scheduler v4-redear');
$db->lock();
if(!$db->query('UPDATE planets SET
unittrain_error = 0
WHERE unittrain_error <> 0')) {
$sdl->log('<b>Error:</b> Cannot reset training errors - CONTINUED');
}
$tmp = 'SELECT planet_id,
research_4,
resource_1,
resource_2,
resource_3,
resource_4,
max_units,
unit_1,
unit_2,
unit_3,
unit_4,
unit_5,
unit_6,
unittrainid_1,
unittrainid_2,
unittrainid_3,
unittrainid_4,
unittrainid_5,
unittrainid_6,
unittrainid_7,
unittrainid_8,
unittrainid_9,
unittrainid_10,
unittrainnumber_1,
unittrainnumber_2,
unittrainnumber_3,
unittrainnumber_4,
unittrainnumber_5,
unittrainnumber_6,
unittrainnumber_7,
unittrainnumber_8,
unittrainnumber_9,
unittrainnumber_10,
unittrainnumberleft_1,
unittrainnumberleft_2,
unittrainnumberleft_3,
unittrainnumberleft_4,
unittrainnumberleft_5,
unittrainnumberleft_6,
unittrainnumberleft_7,
unittrainnumberleft_8,
unittrainnumberleft_9,
unittrainnumberleft_10,
unittrainendless_1,
unittrainendless_2,
unittrainendless_3,
unittrainendless_4,
unittrainendless_5,
unittrainendless_6,
unittrainendless_7,
unittrainendless_8,
unittrainendless_9,
unittrainendless_10,
unittrain_actual,
unittrainid_nexttime,
user_race
FROM planets
LEFT JOIN user ON user_id=planet_owner
WHERE (unittrainid_nexttime<="'.$ACTUAL_TICK.'") AND (unittrainid_nexttime>0)';
if(!($academyquery=$db->query($tmp))) {
$sdl->log('<b>Error:</b> Could not query unittrain data! - SKIPPED');
}
else
{
while (($planet=$db->fetchrow($academyquery))==true)
{
// Look whether the construction number is within normal parameters, but should never lie outside:
if ($planet['unittrain_actual'] < 1 || $planet['unittrain_actual'] > 10) {
if(!$db->query('UPDATE planets SET unittrain_actual="1" WHERE planet_id="'.$planet['planet_id'].'"'))
$sdl->log('<b>Error:</b> Cannot fix training queue pointer on planet #'.$planet['planet_id'].'- CONTINUED');
}
// If within normal parameters
else {
// Unit in training
$t=($planet['unittrainid_'.($planet['unittrain_actual'])])-1;
// Needed resources
$need_res_1 = UnitPrice($t,0,$planet['user_race']);
$need_res_2 = UnitPrice($t,1,$planet['user_race']);
$need_res_3 = UnitPrice($t,2,$planet['user_race']);
$need_res_4 = UnitPrice($t,3,$planet['user_race']);
// Check if we're handling a break and if we're training a
// unit, that the planet has the needed resources
if ($t>5 || $t<0 || ($need_res_1 <= $planet['resource_1'] &&
$need_res_2 <= $planet['resource_2'] &&
$need_res_3 <= $planet['resource_3'] &&
$need_res_4 <= $planet['resource_4']))
{
$sql=array();
// 2pre1: The SQL Query for 2. prepare, because the data under 1. can change:
$t++;
if ($t < 7 && $t > 0)
{
$sql[]='resource_1=resource_1-'.$need_res_1.',
resource_2=resource_2-'.$need_res_2.',
resource_3=resource_3-'.$need_res_3.',
resource_4=resource_4-'.$need_res_4.',
unit_'.$t.'=unit_'.$t.'+1';
}
// 1. For the next unit jump + new time set:
// if left<=0
$planet['unittrainnumberleft_'.($planet['unittrain_actual'])]--;
// We build further on the same slot:
if ($planet['unittrainnumberleft_'.($planet['unittrain_actual'])]>0)
{
// Only set the recent time:
$training_time = $ACTUAL_TICK;
// If Unit
if ($t < 7) {
$training_time += UnitTimeTicksScheduler($t-1,$planet['research_4'],$planet['user_race']);
}
// If Break
else {
switch($t)
{
// 3 minute break
case 10:
$training_time++;
break;
// 27 minutes break
case 11:
$training_time += 9;
break;
// 54 minutes break
case 12:
$training_time += 18;
break;
}
}
$sql[]='unittrainnumberleft_'.($planet['unittrain_actual']).'=unittrainnumberleft_'.($planet['unittrain_actual']).'-1,
unittrain_actual = "'.($planet['unittrain_actual']).'",
unittrainid_nexttime = "'.$training_time.'"';
}
else // We do not build further on the same slot:
{
// If endless built, put back again the number...:
if ($planet['unittrainendless_'.($planet['unittrain_actual'])]==1)
{
$planet['unittrainnumberleft_'.($planet['unittrain_actual'])]=$planet['unittrainnumber_'.($planet['unittrain_actual'])];
$sql[]='unittrainnumberleft_'.($planet['unittrain_actual']).'=unittrainnumber_'.($planet['unittrain_actual']);
}
else
$sql[]='unittrainnumberleft_'.($planet['unittrain_actual']).'=0';
// Now we take the construction of the next unit in the list:
$started=0;
$tries=0;
while ($started==0 && $tries<=10)
{
$planet['unittrain_actual']++;
if ($planet['unittrain_actual']>10) $planet['unittrain_actual']=1;
// Unit in training
$t=$planet['unittrainid_'.($planet['unittrain_actual'])];
if ($t <13 && $t >= 0 &&
$planet['unittrainnumberleft_'.($planet['unittrain_actual'])]>0)
{
$training_time = $ACTUAL_TICK;
// If Unit
if ($t < 7) {
$training_time += UnitTimeTicksScheduler($t-1,$planet['research_4'],$planet['user_race']);
}
// If Break
else {
switch($t)
{
// 3 minute break
case 10:
$training_time++;
break;
// 27 minutes break
case 11:
$training_time += 9;
break;
// 54 minutes break
case 12:
$training_time += 18;
break;
}
}
$sql[]='unittrain_actual = "'.($planet['unittrain_actual']).'",
unittrainid_nexttime = "'.$training_time.'"';
$started=1;
}
$tries++;
}
if (!$started)
{
$sql[]='unittrainid_nexttime="-1"';
}
}
// 2. Add the last planet unit (if planet at the limit, remains unit as finished in the loop):
// unittrain_error=2 if planet full
$damn_units = ($planet['unit_1']*2+
$planet['unit_2']*3+
$planet['unit_3']*4+
$planet['unit_4']*4+
$planet['unit_5']*4+
$planet['unit_6']*4);
if($planet['max_units'] <= $damn_units) {
if(!$db->query('UPDATE planets SET unittrain_error="2" WHERE planet_id="'.$planet['planet_id'].'"'))
$sdl->log('<b>Error:</b> Cannot set "unit space full" training error on planet #'.$planet['planet_id'].' CONTINUED');
}
else {
if (isset($sql) && count($sql)>0) {
if(!$db->query('UPDATE planets SET '.implode(",", $sql).' WHERE planet_id='.$planet['planet_id']))
$sdl->log('<b>Error:</b> Cannot update training queue on planet #'.$planet['planet_id'].' CONTINUED');
}
}
unset($sql);
}
// If we did not have enough resources
else {
if(!$db->query('UPDATE planets SET unittrain_error="1" WHERE planet_id="'.$planet['planet_id'].'"'))
$sdl->log('<b>Error:</b> Cannot set "not enough resources" training error on planet #'.$planet['planet_id'].' CONTINUED');
}
} // End of "within normal parameters"
} // End while
} // End of: Successfull Planet Query
$db->unlock();
$sdl->finish_job('Academy Scheduler v4-redear');
// ########################################################################################
// ########################################################################################
// Shiprepair Scheduler
$sdl->start_job('Shiprepair Scheduler');
$sql = 'SELECT s.ship_id, s.ship_untouchable,
t.ship_torso, t.value_5, t.max_torp
FROM (ships s) LEFT JOIN (ship_templates t) ON s.template_id=t.id
WHERE s.ship_repair>0 AND s.ship_repair<= '.$ACTUAL_TICK;
if(($q_ship = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query shiprepair data! - SKIPPED');
}
else
{
while($ship = $db->fetchrow($q_ship)) {
// DC ---- Ships in Refitting does not get repaired
if ($ship['ship_untouchable'] == SHIP_IN_REFIT)
$sql = 'UPDATE ships
SET ship_repair=0,
ship_untouchable=0'.($ship['ship_torso'] > 2 ? ', torp = '.$ship['max_torp'] : '' ).'
WHERE ship_id='.$ship['ship_id'];
else
$sql = 'UPDATE ships
SET hitpoints='.$ship['value_5'].',
ship_repair=0,
ship_untouchable=0
WHERE ship_id='.$ship['ship_id'];
// DC ----
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not update processed ships data: <b>'.$sql.'</b> - CONTINUED');
}
}
}
$sdl->finish_job('Shiprepair Scheduler');
// ########################################################################################
// ########################################################################################
// Shipscrap Scheduler
$sdl->start_job('Shipscrap Scheduler');
$sql = 'SELECT s.ship_id,
s.fleet_id,
s.hitpoints,
s.unit_1,
s.unit_2,
s.unit_3,
s.unit_4,
t.id,
t.value_5,
t.buildtime,
t.resource_1,
t.resource_2,
t.resource_3,
t.unit_5,
t.unit_6
FROM (ships s) LEFT JOIN (ship_templates t) ON s.template_id=t.id
WHERE s.ship_scrap>0 AND s.ship_scrap<= '.$ACTUAL_TICK;
if(($q_ship = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query shiprepair data! - SKIPPED');
}
else
{
while($ship = $db->fetchrow($q_ship)) {
$res[0]=round(0.7*($ship['resource_1']-$ship['resource_1']/$ship['value_5']*($ship['value_5']-$ship['hitpoints'])),0);
$res[1]=round(0.7*($ship['resource_2']-$ship['resource_2']/$ship['value_5']*($ship['value_5']-$ship['hitpoints'])),0);
$res[2]=round(0.7*($ship['resource_3']-$ship['resource_3']/$ship['value_5']*($ship['value_5']-$ship['hitpoints'])),0);
$unit[0]=$ship['unit_1'];
$unit[1]=$ship['unit_2'];
$unit[2]=$ship['unit_3'];
$unit[3]=$ship['unit_4'];
$unit[4]=$ship['unit_5'];
$unit[5]=$ship['unit_6'];
$sql = 'DELETE FROM ships WHERE ship_id='.$ship['ship_id'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not delete ship: <b>'.$sql.'</b> - CONTINUED');
}
else
{
$planet_id = ((-1)*$ship['fleet_id']);
$sdl->log('The ship <b>#'.$ship['ship_id'].'(template '.$ship['id'].')</b> on planet <b>#'.$planet_id.'</b> was dismantled successfully!');
$sql = 'UPDATE planets
SET resource_1=resource_1+'.$res[0].',
resource_2=resource_2+'.$res[1].',
resource_3=resource_3+'.$res[2].',
unit_1=unit_1+'.$unit[0].',
unit_2=unit_2+'.$unit[1].',
unit_3=unit_3+'.$unit[2].',
unit_4=unit_4+'.$unit[3].',
unit_5=unit_5+'.$unit[4].',
unit_6=unit_6+'.$unit[5].'
WHERE planet_id='.$planet_id;
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not update planets data: <b>'.$sql.'</b> - CONTINUED');
}
}
}
}
$sdl->finish_job('Shipscrap Scheduler');
// ########################################################################################
// ########################################################################################
// Shipyard Scheduler
$sdl->start_job('Shipyard Scheduler');
$sql = 'SELECT ssb.*,
st.id AS template_id, st.value_5 AS template_value_5, st.value_9 AS template_value_9, st.rof AS template_rof, st.max_torp AS template_max_torp,
p.planet_owner as user_id, p.building_7
FROM (scheduler_shipbuild ssb)
INNER JOIN (planets p) ON p.planet_id = ssb.planet_id
LEFT JOIN (ship_templates st) ON st.id = ssb.ship_type
WHERE ssb.finish_build <= '.$ACTUAL_TICK;
if(!$q_shipyard = $db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not query shipbuild data! - SKIPPED');
}
else {
$bShipyardFull = false;
while($shipbuild = $db->fetchrow($q_shipyard)) {
$sql = '
SELECT COUNT(*) AS no_ships
FROM ships
WHERE fleet_id = -'.$shipbuild['planet_id'];
if(!$q_spacedock = $db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not query spacedock number of ships! - CONTINUED AND JUMP NEXT');
continue;
}
$spacedock = $db->fetchrow($q_spacedock);
if ($spacedock['no_ships'] >= $MAX_SPACEDOCK_SHIPS[$shipbuild['building_7']]) {
$bShipyardFull = true;
}
else
$bShipyardFull = false;
if ($bShipyardFull) {
$sql = '
UPDATE scheduler_shipbuild
SET start_build = start_build + 1,
finish_build = finish_build + 1
WHERE planet_id = '.$shipbuild['planet_id'].'
AND finish_build > '.$ACTUAL_TICK;
if(!$db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not update start and finish scheduler! - CONTINUED!');
continue;
}
} else {
if(empty($shipbuild['template_id'])) {
$sdl->log(' - <b>Warning:</b> Could not find template '.$shipbuild['template_id'].'! - CONTINUED AND JUMP TO NEXT');
continue;
}
$sql = 'DELETE FROM scheduler_shipbuild
WHERE planet_id = '.$shipbuild['planet_id'].' AND
finish_build = '.$shipbuild['finish_build'].'
LIMIT 1';
if(!$db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not delete shipbuild data on planet '.$shipbuild['planet_id'].' ending in tick '.$shipbuild['finish_build'].'! - CONTINUED AND JUMP TO NEXT');
continue;
}
$sql = 'INSERT INTO ships (fleet_id, user_id, template_id, experience, hitpoints, construction_time, unit_1, unit_2, unit_3, unit_4, rof, torp)
VALUES (-'.$shipbuild['planet_id'].', '.$shipbuild['user_id'].', '.$shipbuild['ship_type'].', '.$shipbuild['template_value_9'].', '.$shipbuild['template_value_5'].', '.$game->TIME.', '.$shipbuild['unit_1'].', '.$shipbuild['unit_2'].', '.$shipbuild['unit_3'].', '.$shipbuild['unit_4'].', '.$shipbuild['template_rof'].', '.$shipbuild['template_max_torp'].')';
if(!$db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not insert new ship data! - CONTINUED AND JUMP TO NEXT');
continue;
}
$sdl->log('<b>Added Ship from Yard to Dock:</b> Planet #'.$shipbuild['planet_id'].' for User #'.$shipbuild['user_id'].' with Template #'.$shipbuild['ship_type'].' - <b>SUCCESS!</b>');
}
}
}
$sdl->finish_job('Shipyard Scheduler');
// ########################################################################################
// ########################################################################################
// Research Scheduler
$sdl->start_job('Research Scheduler');
$sql = 'SELECT sr.*,
p.research_1, p.research_2, p.research_3, p.research_4, p.research_5,
p.catresearch_1, p.catresearch_2, p.catresearch_3, p.catresearch_4, p.catresearch_5,
p.catresearch_6, p.catresearch_7, p.catresearch_8, p.catresearch_9, p.catresearch_10
FROM (scheduler_research sr)
LEFT JOIN (planets p) ON p.planet_id = sr.planet_id
WHERE sr.research_finish <= '.$ACTUAL_TICK;
if(($q_research = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query research data - SKIPPED');
}
else if($db->num_rows() > 0) {
$n_techs = 0;
while($research = $db->fetchrow($q_research)) {
if($research['research_id'] < 5) {
$sql = 'UPDATE planets
SET research_'.($research['research_id']+1).' = research_'.($research['research_id']+1).' +1,
recompute_static = 1
WHERE planet_id = '.$research['planet_id'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Query sched_research @ user failed! - CONTINUED');
}
}
else {
$sql = 'UPDATE planets
SET catresearch_'.($research['research_id']-4).' = catresearch_'.($research['research_id']-4).' + 1
WHERE planet_id = '.$research['planet_id'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Query sched_research @ user failed:<br> '.$sql.' <br> - CONTINUED');
}
}
$n_techs++;
}
$sql = 'DELETE FROM scheduler_research
WHERE research_finish <= '.$ACTUAL_TICK.'
LIMIT '.$n_techs;
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not delete processed research data');
}
}
$sdl->finish_job('Research Scheduler');
// ########################################################################################
// ########################################################################################
// Resourcetrade Scheduler
$sdl->start_job('Resourcetrade Scheduler');
$sql = 'SELECT *
FROM (scheduler_resourcetrade s)
WHERE s.arrival_time <= '.$ACTUAL_TICK;
if(($q_rtrade = $db->query($sql)) === false) {
$sdl->log(' - <b>Warning:</b> Could not query scheduler resourcetrade data! - SKIPPED');
}
else if($db->num_rows() > 0) {
$n_resourcetrades = 0;
while($trade = $db->fetchrow($q_rtrade)) {
$sql = 'UPDATE planets
SET resource_1=resource_1+'.$trade['resource_1'].',resource_2=resource_2+'.$trade['resource_2'].',resource_3=resource_3+'.$trade['resource_3'].',resource_4=resource_4+'.$trade['resource_4'].',
unit_1=unit_1+'.$trade['unit_1'].',unit_2=unit_2+'.$trade['unit_2'].',unit_3=unit_3+'.$trade['unit_3'].',unit_4=unit_4+'.$trade['unit_4'].',unit_5=unit_5+'.$trade['unit_5'].',unit_6=unit_6+'.$trade['unit_6'].'
WHERE planet_id = '.$trade['planet'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Query sched_resourcetrade @ planets failed! - CONTINUED');
}
else { $sdl->log('<b>Transport delivered</b> Transport ID: '.$trade['id'].' at Planet: '.$trade['planet'].' <b>WARES</b> - Metal: '.$trade['resource_1'].' Minerals: '.$trade['resource_2'].' Dilithium: '.$trade['resource_3'].' Workers: '.$trade['resource_4'].' lvl1: '.$trade['unit_1'].' lvl2: '.$trade['unit_2'].' lvl3: '.$trade['unit_3'].' lvl4: '.$trade['unit_4'].' lvl5: '.$trade['unit_5'].' lvl6: '.$trade['unit_6'].''); }
++$n_resourcetrades;
}
$sql = 'DELETE FROM scheduler_resourcetrade
WHERE arrival_time <= '.$ACTUAL_TICK.'
LIMIT '.$n_resourcetrades;
if(!$db->query($sql)) {
$sdl->log('<b>Error: (Critical)</b> Could not delete scheduler_resourcetrade data - CONTINUED');
}
unset($trade);
}
$sdl->finish_job('Resourcetrade Scheduler');
// ########################################################################################
// ########################################################################################
// Future Humans Rewards System
$sdl->start_job('Future Humans Rewards');
$sql = 'SELECT count(user_id) AS n_ships, user_id, target_planet_id
FROM future_human_reward
WHERE sent = 0
GROUP BY user_id';
if(($fh_stream = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query future human reward! - SKIPPED');
}
else if($db->num_rows() > 0) {
// Load Future human ship's template
$sql = 'SELECT id, value_9, value_5, min_unit_1, min_unit_2, min_unit_3, min_unit_4, rof, max_torp
FROM ship_templates
WHERE id = '.$FUTURE_SHIP;
$template = $db->queryrow($sql);
while($player_to_serve = $db->fetchrow($fh_stream))
{
$sql = 'INSERT INTO ship_fleets (fleet_name, user_id, planet_id, n_ships)
VALUES ("Reward",
'.$player_to_serve['user_id'].',
'.$player_to_serve['target_planet_id'].',
'.$player_to_serve['n_ships'].')';
if(!$db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not create Reward Fleet for user '.$player_to_serve['user_id'].' - CONTINUED');
continue;
}
$new_fleet_id = $db->insert_id();
for($i = 0; $i < $player_to_serve['n_ships']; $i++)
{
$sql = 'INSERT INTO ships (fleet_id, user_id, template_id, experience, hitpoints, construction_time, unit_1, unit_2, unit_3, unit_4, rof, torp, last_refit_time)
VALUES ('.$new_fleet_id.',
'.$player_to_serve['user_id'].',
'.$template['id'].',
'.$template['value_9'].',
'.$template['value_5'].',
'.$game->TIME.',
'.$template['min_unit_1'].',
'.$template['min_unit_2'].',
'.$template['min_unit_3'].',
'.$template['min_unit_4'].',
'.$template['rof'].',
'.$template['max_torp'].',
'.$game->TIME.')';
if(!$db->query($sql)) {
$sdl->log(' - <b>Warning:</b> Could not Insert '.$player_to_serve['n_ships'].' Reward ship for user '.$player_to_serve['user_id'].' - CONTINUED');
continue;
}
}
$sql = 'UPDATE future_human_reward SET sent = 1 WHERE user_id = '.$player_to_serve['user_id'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not update future human reward - CONTINUED');
}
}
}
$sdl->finish_job('Future Humans Rewards');
// ########################################################################################
// ########################################################################################
//BOT
ini_set('memory_limit', '500M');
define('FILE_PATH_hg',$game_path);
define('TICK_LOG_FILE_NPC', $game_path.'logs/NPC_BOT_tick_'.date('d-m-Y', time()).'.log');
include('NPC_BOT.php');
include('ferengi.php');
include('borg.php');
include('settlers.php');
$sdl->start_job('Ramona comes over - oh women are so wonderful');
$quark = new Ferengi($db,$sdl);
$quark->Execute(1,"Normal operation in the test round",0,"#DEEB24");
$sdl->finish_job('Ramona comes over - oh women are so wonderful');
$sdl->start_job('SevenOfNine is coming - oh borg are not so beautiful');
$borg = new Borg($db,$sdl);
$borg->Execute(1);
$sdl->finish_job('SevenOfNine is coming - oh borg are not so beautiful');
$sdl->start_job('Mayflower is coming - settlers are the real workforce');
$settlers = new Settlers($db,$sdl);
$settlers->Execute(1);
$sdl->finish_job('Mayflower is coming - settlers are the real workforce');
// ########################################################################################
// ########################################################################################
// Update Tick-ID
// (here, everything is completed, which is based on the ticking ID)
if(substr($STARDATE, -1, 1) == '9') {
$new_stardate = (string)( ((float)$STARDATE) + 0.1 ).'.0';
}
else {
$new_stardate = (string)( ((float)$STARDATE) + 0.1 );
}
if(!$db->query('UPDATE config SET tick_id = tick_id + 1, shipwreck_id=shipwreck_id+1, tick_securehash = "'.md5($ACTUAL_TICK).'", stardate = "'.$new_stardate.'"')) {
$sdl->log('<b>Error:</b> Could not update tick ID, Tick stopped, sent mail to [email protected]');
mail('[email protected]','STFC2: Tickstop','Tick '.$ACTUAL_TICK.' has been stopped.\nError message:\n'.$db->raise_error().'\n\nGreetings, STGC Scheduler');
$db->raise_error();
$sdl->log('Tick '.$ACTUAL_TICK.' has (presumably) halted.<br>Error message:<br>'.$db->error['message'].'<br><br>Error Source: " UPDATE config SET tick_id = tick_id + 1, tick_securehash = "'.md5($ACTUAL_TICK).'" "<br><br>Greetings, STGC Scheduler');
$db->query('UPDATE config SET tick_stopped = 1');
exit;
}
// ########################################################################################
// ########################################################################################
// Shipdestruction (Wrecking) // Formerly RUST
/*if ($cfg_data['shipwreck_id'] > SHIP_RUST_CHECK)
{
$sdl->start_job('Shipdestruction (Wrecking)');
$sql = 'UPDATE ships s LEFT JOIN ship_templates t ON t.id=s.template_id SET s.hitpoints=s.hitpoints-t.value_5/100 WHERE s.hitpoints>t.value_5/2 AND s.fleet_id>0 AND s.next_refit <='.$ACTUAL_TICK;
if(($db->query($sql)) === false)
$sdl->log('<b>Error:</b> Could not execute shipwrecking query! CONTINUED');
if(!$db->query('UPDATE config SET shipwreck_id='.rand(0,100))) {
$sdl->log('- Could not update shipwreck_id! CONTINUED');
}
$sdl->finish_job('Shipdestruction (Wrecking)');
}
else
$sdl->log('<font color=#0000ff>Shipdestruction (Wrecking) [Skipped '.$cfg_data['shipwreck_id'].'/'.(SHIP_RUST_CHECK).']</font><br>');
*/
// ########################################################################################
// ########################################################################################
// Destruction based on troop strength
$sdl->start_job('Destruction based on troop strength');
$sql = 'SELECT planet_id, planet_name, planet_owner,
unit_1, unit_2, unit_3, unit_4, min_security_troops,
building_1, building_2, building_3, building_4, building_5,
building_6, building_7, building_8, building_9, building_10,
building_11, building_12, building_13
FROM planets
WHERE min_security_troops > (100+unit_1*2+unit_2*3+unit_3*4+unit_4*4) AND planet_owner>10';
if(($q_planets = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query planets data to destroy buildings based on troop strength! - CONTINUED');
}
$n_destruction=0;
$n_caused_destruction=0;
$rand=rand(0,200);
while($planet = $db->fetchrow($q_planets)) {
if(empty($planet['planet_id'])) {
continue;
}
$security_troops = ($planet['unit_1']*2+$planet['unit_2']*3+$planet['unit_3']*4+$planet['unit_4']*4);
$chance=5 - 5/$planet['min_security_troops']*$security_troops;
//$chance=5;$rand=5;
if ($rand<=$chance && $planet['min_security_troops']>$security_troops)
{
$victim=array(4,6,7,8,10,11,9);
$chance*=20;
// Provide the list of the buildings which could be destroyed:
if ($chance>50) array_push($victim,5);
if ($chance>60) array_push($victim,1);
if ($chance>70) array_push($victim,2);
if ($chance>80) array_push($victim,3);
if ($chance>90) array_push($victim,0);
$rand_building=rand(0,count($victim)-1);
$sdl->log('Building:'.count($victim).' of 12, randomly chosen:'.$rand_building.'<br>');
if ($planet['building_'.$rand_building]>1)
{
$log_data=array(
'planet_name' => $planet['planet_name'],
'building_id' => $rand_building,
'prev_level' => $planet['building_'.$rand_building],
'troops_percent' => (100/$planet['min_security_troops']*$security_troops),
);
/* 16/05/08 - AC: Add logbook title translation */
$log_title = 'Insurrection on planet '.$planet['planet_name'];
$sql = 'SELECT language FROM user WHERE user_id = '.$planet['planet_owner'];
if(($user = $db->queryrow($sql)) == true) {
switch($user['language'])
{
case 'GER':
$log_title = 'Aufstände auf Planet '.$planet['planet_name'];
break;
case 'ITA':
$log_title = 'Insurrezione sul pianeta '.$planet['planet_name'];
break;
}
}
/* */
add_logbook_entry($planet['planet_owner'], LOGBOOK_GOVERNMENT, $log_title, $log_data);
//SystemMessage($planet['planet_owner'],'Destruction (temp. msg., rmv)','Building '.$rand_building.' wird von '.$planet['building_'.$rand_building].' of '.($planet['building_'.$rand_building]-1).' set!');
$sql = 'UPDATE planets
SET building_'.$rand_building.'=building_'.$rand_building.'-1 WHERE planet_id = '.$planet['planet_id'];
if(!$db->query($sql)) {
$sdl->log('<b>Error:</b> Could not destroy building '.$rand_building.' of planet <b>'.$planet['planet_id'].'</b>! - CONTINUED');
}
}
}
++$n_destruction;
}
$sdl->log('(Affected '.$n_destruction.' planets)');
$sdl->finish_job('Destruction based on troop strength');
// ########################################################################################
// ########################################################################################
// Planet revolution based on troop strength
$sdl->start_job('Planet insurrection set');
$sql = 'UPDATE planets SET planet_insurrection_time=0 WHERE min_security_troops <= (100+unit_1*2+unit_2*3+unit_3*4+unit_4*4)';
if(($db->query($sql)) === false) $sdl->log('<b>Error:</b> Could not update (unset) planets insurrection data! - CONTINUED');
$sql = 'UPDATE planets SET planet_insurrection_time=UNIX_TIMESTAMP() WHERE min_security_troops > (100+unit_1*2+unit_2*3+unit_3*4+unit_4*4) AND planet_insurrection_time=0';
if(($db->query($sql)) === false) $sdl->log('<b>Error:</b> Could not update (set) planets insurrection data! - CONTINUED');
$sdl->finish_job('Planet insurrection set');
// ########################################################################################
// ########################################################################################
// Planet revolution based on troop strength
$sdl->start_job('Planet revolution based on troop strength');
$sql = 'SELECT planet_id, planet_name, planet_owner, planet_points
unit_1, unit_2, unit_3, unit_4, min_security_troops,
user_alliance
FROM planets
LEFT JOIN user ON user_id = planet_owner
WHERE ((min_security_troops > (100+unit_1*2+unit_2*3+unit_3*4+unit_4*4) AND planet_points<30) OR
((unit_1*2+unit_2*3+unit_3*4+unit_4*4) / min_security_troops < 0.3)) AND
planet_owner>10 AND planet_owner_enum>3 AND planet_insurrection_time>0 AND
(UNIX_TIMESTAMP()-planet_insurrection_time)>3600*48';
if(($q_planets = $db->query($sql)) === false) {
$sdl->log('<b>Error:</b> Could not query planets data to start revolutions based on troop strength! - CONTINUED');
}
$n_revolution=0;
$n_revolution_done=0;
while($planet = $db->fetchrow($q_planets)) {
if(empty($planet['planet_id'])) {
continue;
}
$rand=rand(0,100);
if ($rand==2)
{
$sdl->log('Planet '.$planet['planet_name'].' ('.$planet['planet_id'].') taken over by NPC');
$sql = 'UPDATE planets