-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Match.php
3261 lines (2833 loc) · 156 KB
/
Match.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
/**
* eBot - A bot for match management for CS:GO
* @license http://creativecommons.org/licenses/by/3.0/ Creative Commons 3.0
* @author Julien Pardons <julien.pardons@esport-tools.net>
* @version 3.0
* @date 21/10/2012
*/
namespace eBot\Match;
use eTools\Utils\Logger;
use eBot\Exception\MatchException;
use eBot\Match\Map;
use eTools\Task\TaskManager;
use eTools\Task\Task;
use eTools\Task\Taskable;
use eTools\Rcon\CSGO as Rcon;
class Match implements Taskable {
const STATUS_NOT_STARTED = 0;
const STATUS_STARTING = 1;
const STATUS_WU_KNIFE = 2;
const STATUS_KNIFE = 3;
const STATUS_END_KNIFE = 4;
const STATUS_WU_1_SIDE = 5;
const STATUS_FIRST_SIDE = 6;
const STATUS_WU_2_SIDE = 7;
const STATUS_SECOND_SIDE = 8;
const STATUS_WU_OT_1_SIDE = 9;
const STATUS_OT_FIRST_SIDE = 10;
const STATUS_WU_OT_2_SIDE = 11;
const STATUS_OT_SECOND_SIDE = 12;
const STATUS_END_MATCH = 13;
const STATUS_ARCHIVE = 14;
const TASK_ENGAGE_MAP = "engageMap";
const CHANGE_HOSTNAME = "changeHostname";
const TEST_RCON = "testRcon";
const REINIT_RCON = "rconReinit";
const SET_LIVE = "setLive";
const STOP_RECORD = "stopRecord";
const TASK_DELAY_READY = "delayReady";
const TASK_SEND_TEAM_NAMES = "sendTeamNames";
// Variable calculable (pas en BDD)
private $players = array();
private $players_old = array();
private $rcon = null;
private $lastMessage = 0;
private $match = null;
private $nbRS = 0;
private $message = 0;
private $pluginCsay = false;
private $pluginESL = false;
private $pluginSwitch = false;
private $pluginPrintPlayers = false;
private $waitForRestart = false;
private $flood = array();
private $gameBombPlanter = null;
private $gameBombDefuser = null;
private $enable = true;
private $userToEnter;
private $nbLast = array("nb_max_ct" => 0, "nb_max_t" => 0, "nb_ct" => 0, "nb_ct" => 0);
private $winKnife = "";
private $needDel = false;
private $firstFrag = false;
private $rsKnife = false;
private $password;
private $passwordChanged = false;
private $updatedHeatmap = false;
// Variable en BDD obligatoire
private $match_id;
private $server_ip;
private $season_id;
private $score = array("team_a" => 0, "team_b" => 0);
private $nbRound = 0;
private $nbOT = 0;
private $scoreSide = array();
private $scoreJoueurSide = array();
private $config_full_score = false;
private $config_ot = false;
private $ot_startmoney;
private $ot_maxround;
private $config_switch_auto = false;
private $config_kniferound = false;
private $config_streamer = false;
private $streamerReady = false;
private $rules;
private $maxRound = 15;
private $oldMaxround = 15;
private $maps = array();
private $matchData = array();
private $currentMap = null;
private $messageManager;
private $teamAName;
private $teamBName;
private $teamAFlag;
private $teamBFlag;
private $rconPassword;
private $isPaused;
private $backupFile;
private $timeRound = null;
private $roundEndEvent;
private $websocket = null;
private $pause = array("ct" => false, "t" => false);
private $unpause = array("ct" => false, "t" => false);
private $continue = array("ct" => false, "t" => false);
private $side = array("team_a" => "ct", "team_b" => "t");
private $ready = array("ct" => false, "t" => false);
private $stop = array("ct" => false, "t" => false);
private $playMap = array("ct" => "", "t" => "");
private $timeEngageMap = 0;
private $mapIsEngaged = false;
private $waitRoundStartRecord = false;
private $forceRoundStartRecord = false;
private $delay_ready_inprogress = false;
private $delay_ready_countdown = 10;
private $delay_ready_abort = false;
private $roundRestartEvent = false;
public function __construct($match_id, $server_ip, $rcon) {
Logger::debug("Registring MessageManager");
$this->messageManager = \eBot\Manager\MessageManager::getInstance("CSGO");
Logger::debug("Creating match #" . $match_id . " on $server_ip");
$this->match_id = $match_id;
$this->server_ip = $server_ip;
$query = \mysql_query("SELECT * FROM `matchs` WHERE id = '" . $match_id . "'");
if (!$query) {
throw new MatchException();
}
$this->matchData = \mysql_fetch_assoc($query);
// SETTING TEAMNAME AND FLAG
$teama_details = $this->getTeamDetails($this->matchData["team_a"], "a");
$this->teamAName = $teama_details['name'];
$this->teamAFlag = $teama_details['flag'];
$teamb_details = $this->getTeamDetails($this->matchData["team_b"], "b");
$this->teamBName = $teamb_details['name'];
$this->teamBFlag = $teamb_details['flag'];
$this->season_id = $this->matchData["season_id"];
$this->addMatchLog("----------- Creating log file -----------", false, false);
$this->addMatchLog("- Match Parameter", false, false);
$this->addMatchLog("- Match ID: " . $this->match_id, false, false);
$this->addMatchLog("- Teams: " . $this->teamAName . " - " . $this->teamBName, false, false);
$this->addMatchLog("- MaxRound: " . $this->matchData["max_round"], false, false);
$this->addMatchLog("- Overtime: " . ($this->matchData["config_ot"]) ? "yes (money: " . $this->matchData['overtime_startmoney'] . ", round: " . $this->matchData['overtime_max_round'] . ")" : "no", false, false);
// Get Websocket
$this->websocket['match'] = \eBot\Application\Application::getInstance()->getWebSocket('match');
$this->websocket['livemap'] = \eBot\Application\Application::getInstance()->getWebSocket('livemap');
$this->websocket['logger'] = \eBot\Application\Application::getInstance()->getWebSocket('logger');
$ip = explode(":", $this->server_ip);
try {
$this->rcon = new Rcon($ip[0], $ip[1], $rcon);
$this->rconPassword = $rcon;
Logger::log("RCON init ok");
$this->rcon->send("log on; logaddress_del " . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port() . ";logaddress_add " . \eBot\Config\Config::getInstance()->getBot_ip() . ":" . \eBot\Config\Config::getInstance()->getBot_port());
$this->addMatchLog("- RCON connection OK", true, false);
} catch (\Exception $ex) {
$this->needDel = true;
if (!is_numeric(\eBot\Manager\MatchManager::getInstance()->getRetry($this->match_id))) {
if ($this->status == 1) {
\mysql_query("UPDATE `matchs` SET `enable` = 0, `auto_start` = 0, `status` = 0 WHERE `id` = '" . $this->match_id . "'");
} else {
\mysql_query("UPDATE `matchs` SET `enable` = 0, `auto_start` = 0 WHERE `id` = '" . $this->match_id . "'");
}
} else {
if (\eBot\Manager\MatchManager::getInstance()->getRetry($this->match_id) > 3) {
if ($this->status == 1) {
\mysql_query("UPDATE `matchs` SET `enable` = 0, `auto_start` = 0, `status` = 0 WHERE `id` = '" . $this->match_id . "'");
} else {
\mysql_query("UPDATE `matchs` SET `enable` = 0, `auto_start` = 0 WHERE `id` = '" . $this->match_id . "'");
}
} else {
$this->addLog("Next retry (" . \eBot\Manager\MatchManager::getInstance()->getRetry($this->match_id) . "/3)");
\eBot\Manager\MatchManager::getInstance()->setRetry($this->match_id, \eBot\Manager\MatchManager::getInstance()->getRetry($this->match_id) + 1);
\eBot\Manager\MatchManager::getInstance()->delayServer($this->server_ip, 5);
}
}
$this->websocket['match']->sendData(json_encode(array('message' => 'button', 'content' => 'stop', 'id' => $this->match_id)));
Logger::error("Rcon failed - " . $ex->getMessage());
Logger::error("Match destructed.");
$this->addMatchLog("RCON Failed - " . $ex->getMessage(), false, false);
throw new MatchException();
}
TaskManager::getInstance()->addTask(new Task($this, self::TEST_RCON, microtime(true) + 10));
// CSay Detection
try {
$text = $this->rcon->send("csay_version");
if (preg_match('!"csay_version" = "(.*)"!', $text, $match)) {
$this->addLog("CSay version " . $match[1]);
$this->pluginCsay = true;
$this->pluginPrintPlayers = true;
$this->pluginSwitch = true;
$this->addMatchLog("- CSay version " . $match[1], false, false);
}
} catch (\Exception $ex) {
Logger::error("Error while getting plugins information");
}
// ESL Plugin Detection
try {
$text = $this->rcon->send("esl_version");
if (preg_match('!"esl_version" = "(.*)"!', $text, $match)) {
$this->addLog("ESL Plugin version " . $match[1]);
$this->pluginESL = true;
$this->addMatchLog("- ESL Plugin version " . $match[1], false, false);
}
} catch (\Exception $ex) {
Logger::error("Error while getting plugins information");
}
$this->config_full_score = $this->matchData["config_full_score"];
$this->config_kniferound = $this->matchData["config_knife_round"];
$this->config_switch_auto = $this->matchData["config_switch_auto"];
$this->config_ot = $this->matchData["config_ot"];
if ($this->config_ot) {
$this->ot_startmoney = $this->matchData["overtime_startmoney"];
$this->ot_maxround = $this->matchData["overtime_max_round"];
}
$this->config_streamer = $this->matchData["config_streamer"];
$this->maxRound = $this->matchData["max_round"];
$this->oldMaxround = $this->maxRound;
$this->rules = $this->matchData["rules"];
$this->status = $this->matchData["status"];
$this->isPaused = $this->matchData['is_paused'] == 1;
Logger::debug("Match config loaded - Printing configuration");
$this->addLog("Match configuration" .
" :: Full Score: " . (($this->config_full_score) ? "yes" : "no") .
" :: Switch Auto: " . (($this->config_switch_auto) ? "yes" : "no") .
" :: Over Time: " . (($this->config_ot) ? "yes" : "no") .
" :: KnifeRound: " . (($this->config_kniferound) ? "yes" : "no"));
$this->addMatchLog("- Match configuration" .
" :: Full Score: " . (($this->config_full_score) ? "yes" : "no") .
" :: Switch Auto: " . (($this->config_switch_auto) ? "yes" : "no") .
" :: Over Time: " . (($this->config_ot) ? "yes" : "no") .
" :: KnifeRound: " . (($this->config_kniferound) ? "yes" : "no"), false, true);
$this->addLog("MaxRound: " . $this->maxRound . " :: Rules: " . $this->rules);
// Map Start
Logger::debug("Loading maps");
$query = \mysql_query("SELECT * FROM `maps` WHERE match_id = '" . $match_id . "'");
if (!$query) {
throw new MatchException();
}
while ($data = \mysql_fetch_assoc($query)) {
$this->maps[$data["id"]] = new Map($data);
$this->maps[$data["id"]]->setNbMaxRound($this->maxRound);
}
// Fixing for maxround in OT
if ($this->getStatus() >= self::STATUS_WU_OT_1_SIDE) {
$this->maxRound = $this->ot_maxround;
}
if ($this->matchData["current_map"] == null) {
Logger::debug("No map is currently playing, picking one (based on default)");
foreach ($this->maps as &$map) {
if (($map->getStatus() == Map::STATUS_NOT_STARTED) && ($map->getMapsFor() == "default")) {
$this->currentMap = $map;
$this->matchData["current_map"] = $map->getMapId();
Logger::debug("Map #" . $map->getMapId() . " selected");
mysql_query("UPDATE `matchs` SET current_map='" . $map->getMapId() . "' WHERE id='" . $this->match_id . "'");
break;
}
}
} else {
if ($this->maps[$this->matchData["current_map"]]) {
$this->currentMap = $this->maps[$this->matchData["current_map"]];
} else {
$this->addLog("Can't find the map #" . $this->matchData["current_map"], Logger::ERROR);
throw new MatchException();
}
}
if ($this->currentMap == null) {
$this->addLog("No map found, exiting matchs", Logger::ERROR);
mysql_query("UPDATE `matchs` SET enable='0', status='" . self::STATUS_END_MATCH . "' WHERE id='" . $this->match_id . "'");
throw new MatchException();
}
$this->addLog("Maps selected: #" . $this->currentMap->getMapId() . " - " . $this->currentMap->getMapName() . " - " . $this->currentMap->getStatusText());
if ($this->currentMap->getMapName() != "tba") {
$this->mapIsEngaged = true;
$this->addLog("Setting map engaged");
} else {
$this->mapIsEngaged = false;
$this->addLog("Setting veto mode");
}
if ($this->getStatus() == self::STATUS_STARTING) {
if (($this->currentMap->getStatus() == Map::STATUS_NOT_STARTED) || ($this->currentMap->getStatus() == Map::STATUS_STARTING)) {
if ($this->config_kniferound) {
Logger::debug("Setting need knife round on map");
$this->currentMap->setNeedKnifeRound(true);
}
}
if ($this->currentMap->getMapName() != "tba") {
Logger::debug("Schedule task for first map");
TaskManager::getInstance()->addTask(new Task($this, self::TASK_ENGAGE_MAP, microtime(true) + 1));
} else {
if ($this->config_kniferound) {
$this->setStatus(self::STATUS_WU_KNIFE, true);
$this->currentMap->setStatus(Map::STATUS_WU_KNIFE, true);
} else {
$this->setStatus(self::STATUS_WU_1_SIDE, true);
$this->currentMap->setStatus(Map::STATUS_WU_1_SIDE, true);
}
}
} else {
if (($this->currentMap->getStatus() == Map::STATUS_NOT_STARTED) || ($this->currentMap->getStatus() == Map::STATUS_STARTING)) {
if ($this->currentMap->getMapName() != "tba") {
Logger::debug("Current map is not started/starting, engaging map");
TaskManager::getInstance()->addTask(new Task($this, self::TASK_ENGAGE_MAP, microtime(true) + 1));
} else {
if ($this->config_kniferound) {
$this->setStatus(self::STATUS_WU_KNIFE, true);
$this->currentMap->setStatus(Map::STATUS_WU_KNIFE, true);
} else {
$this->setStatus(self::STATUS_WU_1_SIDE, true);
$this->currentMap->setStatus(Map::STATUS_WU_1_SIDE, true);
}
}
} else {
Logger::debug("Restore score");
}
}
TaskManager::getInstance()->addTask(new Task($this, self::CHANGE_HOSTNAME, microtime(true) + 5));
// Setting side for maps
if ($this->currentMap->getCurrentSide() == "ct") {
$this->side['team_a'] = "ct";
$this->side['team_b'] = "t";
} else {
$this->side['team_a'] = "t";
$this->side['team_b'] = "ct";
}
$this->websocket['match']->sendData(json_encode(array('message' => 'teams', 'teamA' => $this->side['team_a'], 'teamB' => $this->side['team_b'], 'id' => $this->match_id)));
// Calculating scores
$this->currentMap->calculScores();
$this->score["team_a"] = $this->currentMap->getScore1();
$this->score["team_b"] = $this->currentMap->getScore2();
@mysql_query("UPDATE `matchs` SET score_a = '" . $this->score["team_a"] . "', score_b ='" . $this->score["team_b"] . "' WHERE id='" . $this->match_id . "'");
// Setting nb OverTime
$this->nbOT = $this->currentMap->getNbOt();
// This case happens only when the bot shutdown and restart at this status
if ($this->currentMap->getStatus() == Map::STATUS_END_KNIFE) {
$this->addLog("Setting round to knife round, because was waiting end knife");
$this->currentMap->setStatus(Map::STATUS_WU_KNIFE, true);
$this->setStatus(Map::STATUS_WU_KNIFE, true);
}
// Getting all players
$this->recupStatus();
// Setting server password
if ($this->matchData["config_password"] != "") {
$this->rcon->send("sv_password \"" . $this->matchData["config_password"] . "\"");
}
$this->addMatchLog("----------- End match loading -----------", false, false);
if ($this->getStatus() <= self::STATUS_WU_1_SIDE) {
$this->sendTeamNames();
}
if ($this->matchData["ingame_enable"] != null && !$this->matchData["ingame_enable"]) {
$this->addLog("Setting match not enabled");
$this->enable = false;
}
if ($this->getStatus() > self::STATUS_WU_1_SIDE) {
$this->streamerReady = true;
}
// Setting nbMaxRound for sided score
if ($this->getStatus() > self::STATUS_WU_OT_1_SIDE && $this->config_ot) {
$this->currentMap->setNbMaxRound($this->ot_maxround);
}
// Sending roundbackup format file
$this->rcon->send("mp_backup_round_file \"ebot_" . $this->match_id . "\"");
TaskManager::getInstance()->addTask(new Task($this, self::CHANGE_HOSTNAME, microtime(true) + 60));
}
private function recupStatus($eraseAll = false) {
if ($eraseAll) {
unset($this->players);
$this->players = array();
$this->addLog("Deleting all player in BDD");
mysql_query("DELETE FROM players WHERE map_id='" . $this->currentMap->getMapId() . "'");
}
if ($this->pluginPrintPlayers) {
$this->addLog("Getting status");
$text = $this->rcon->send("steamu_printPlayer");
$texts = explode("\n", trim($text));
foreach ($texts as $v) {
if (preg_match('!#(\d+) "(.*)" (.*) (\d+) (\d+).(\d+).(\d+).(\d+)!', $v, $arr)) {
$ip = $arr[5] . "." . $arr[6] . "." . $arr[7] . "." . $arr[8];
switch ($arr[4]) {
case 0:
$team = "";
break;
case 1:
$team = "SPECTATOR";
break;
case 2:
$team = "TERRORIST";
break;
case 3:
$team = "CT";
break;
}
$this->userToEnter[$arr[1]] = $ip;
$this->processPlayer($arr[1], $arr[2], $team, $arr[3]);
}
}
}
}
public function getNbRound() {
return $this->score["team_a"] + $this->score["team_b"] + 1;
}
public function getStatusText($normal = true) {
if ($normal)
$round = "Round #";
else
$round = "#";
switch ($this->getStatus()) {
case self::STATUS_NOT_STARTED:
return "Not started";
case self::STATUS_STARTING:
return "Starting";
case self::STATUS_WU_KNIFE:
return "Warmup Knife";
case self::STATUS_KNIFE:
return "Knife Round";
case self::STATUS_END_KNIFE:
return "Waiting choose team - Knife Round";
case self::STATUS_WU_1_SIDE:
return "Warmup first side";
case self::STATUS_FIRST_SIDE:
return "First side - " . $round . $this->getNbRound();
case self::STATUS_WU_2_SIDE:
return "Warmup second side";
case self::STATUS_SECOND_SIDE:
return "Second side - " . $round . $this->getNbRound();
case self::STATUS_WU_OT_1_SIDE:
return "Warmup first side OverTime";
case self::STATUS_OT_FIRST_SIDE:
return "First side OverTime - " . $round . $this->getNbRound();
case self::STATUS_WU_OT_2_SIDE:
return "Warmup second side OverTime";
case self::STATUS_OT_SECOND_SIDE:
return "Second side OverTime - " . $round . $this->getNbRound();
case self::STATUS_END_MATCH:
return "Finished";
}
}
public function getStatus() {
return $this->status;
}
public function setStatus($newStatus, $save = false) {
$this->status = $newStatus;
$this->websocket['match']->sendData(json_encode(array('message' => 'status', 'content' => $this->getStatusText(false), 'id' => $this->match_id)));
$this->websocket['match']->sendData(json_encode(array('message' => 'button', 'content' => $this->getStatus(), 'id' => $this->match_id)));
if ($save) {
$this->message = 0;
Logger::debug("Updating status to " . $this->getStatusText() . " in database");
if ($newStatus == self::STATUS_END_MATCH)
$setDisable = ", enable = '0'";
mysql_query("UPDATE `matchs` SET status='" . $newStatus . "' " . $setDisable . " WHERE id='" . $this->match_id . "'");
}
}
private function getHostname() {
return "eBot :: " . $this->teamAName . " vs " . $this->teamBName;
}
private function getStreamerReady() {
if ($this->config_streamer == "1")
return $this->streamerReady;
else
return false;
}
private function getTeamDetails($id, $t) {
if (is_numeric($id) && $id > 0) {
$ds = mysql_fetch_array(mysql_query("SELECT * FROM `teams` WHERE `id` = '$id'"));
return $ds;
} else {
if ($t == "a") {
return array("name" => $this->matchData['team_a_name'], "flag" => $this->matchData['team_a_flag']);
} elseif ($t == "b") {
return array("name" => $this->matchData['team_b_name'], "flag" => $this->matchData['team_b_flag']);
}
}
}
public function taskExecute($name) {
if ($name == self::SET_LIVE) {
$this->addLog("Setting live");
$this->enable = true;
} elseif ($name == self::TASK_ENGAGE_MAP) {
$tvTimeRemaining = $this->rcon->send("tv_time_remaining");
if (preg_match('/(?<time>\d+\.\d+) seconds/', $tvTimeRemaining, $preg)) {
TaskManager::getInstance()->addTask(new Task($this, self::TASK_ENGAGE_MAP, microtime(true) + floatval($preg['time']) + 1));
$this->addLog("Waiting till GOTV broadcast is finished! Mapchange in " . (intval($preg['time']) + 1) . " seconds");
} else {
$this->engageMap();
}
} elseif ($name == self::CHANGE_HOSTNAME) {
if ($this->rcon->getState()) {
$this->rcon->send('hostname "' . $this->getHostname() . '"');
} else {
TaskManager::getInstance()->addTask(new Task($this, self::CHANGE_HOSTNAME, microtime(true) + 5));
}
} elseif ($name == self::TEST_RCON) {
if ($this->rcon->getState()) {
if (!$this->rcon->send('echo eBot')) {
$ip = explode(":", $this->server_ip);
try {
$this->rcon = new Rcon($ip[0], $ip[1], $this->rconPassword);
$this->rcon->send("echo eBot");
} catch (\Exception $ex) {
Logger::error("Reinit rcon failed - " . $ex->getMessage());
Logger::error("Trying to rengage in 10 seconds");
$this->addMatchLog("RCON Connection failed, trying to engage the match in 10 seconds - " . $ex->getMessage(), true);
\eBot\Manager\MatchManager::getInstance()->setRetry($this->match_id, \eBot\Manager\MatchManager::getInstance()->getRetry($this->match_id) + 1);
\eBot\Manager\MatchManager::getInstance()->delayServer($this->server_ip, 10);
$this->needDel = true;
}
}
}
TaskManager::getInstance()->addTask(new Task($this, self::TEST_RCON, microtime(true) + 90));
} elseif ($name == self::REINIT_RCON) {
$ip = explode(":", $this->server_ip);
try {
$this->rcon = new Rcon($ip[0], $ip[1], $this->rconPassword);
$this->rcon->send("echo eBot");
} catch (\Exception $ex) {
Logger::error("Reinit rcon failed - " . $ex->getMessage());
Logger::error("Trying to rengage in 10 seconds");
$this->addMatchLog("RCON Connection failed, trying to engage the match in 10 seconds - " . $ex->getMessage(), true);
\eBot\Manager\MatchManager::getInstance()->delayServer($this->server_ip, 10);
$this->needDel = true;
}
} elseif ($name == self::STOP_RECORD) {
$this->needDelTask = false;
$this->addLog("Stopping record & push");
$this->rcon->send("tv_stoprecord");
if (\eBot\Config\Config::getInstance()->getDemoDownload())
$this->rcon->send('csay_tv_demo_push "' . $this->currentRecordName . '.dem" "http://' . \eBot\Config\Config::getInstance()->getBot_ip() . ':' . \eBot\Config\Config::getInstance()->getBot_port() . '/upload"');
$this->currentRecordName = "";
$this->rcon->send("exec server.cfg;");
} elseif ($name == self::TASK_DELAY_READY) {
if (\eBot\Config\Config::getInstance()->getDelayReady()) {
if ($this->delay_ready_countdown > 0 && !$this->delay_ready_abort && $this->ready['ct'] && $this->ready['t']) {
$this->say("Match starts in " . $this->delay_ready_countdown . " seconds. !abort to stop countdown.");
$this->delay_ready_countdown--;
TaskManager::getInstance()->addTask(new Task($this, self::TASK_DELAY_READY, microtime(true) + 1));
} elseif ($this->delay_ready_countdown == 0 && !$this->delay_ready_abort && $this->ready['ct'] && $this->ready['t']) {
$this->delay_ready_abort = false;
$this->delay_ready_countdown = 10;
$this->delay_ready_inprogress = false;
$this->startMatch(true);
}
}
} elseif ($name == self::TASK_SEND_TEAM_NAMES) {
$this->sendTeamNames();
}
}
/**
* Engaging the first map
*/
private function engageMap() {
$this->timeEngageMap = 0;
if ($this->currentMap == null) {
$this->addLog("Can't engage the map, map is null");
return;
}
if ((($this->currentMap->getStatus() == Map::STATUS_STARTING) || ($this->currentMap->getStatus() == Map::STATUS_NOT_STARTED)) OR !$this->mapIsEngaged) {
$this->addLog("Engaging the first map");
// Warmup
$this->rcon->send("mp_warmuptime 1");
$this->rcon->send("mp_warmup_pausetimer 1; mp_halftime_duration 5;");
if ($this->config_full_score) {
$this->rcon->send("mp_match_can_clinch 0;");
}
// Changing map
$this->addLog("Changing map to " . $this->currentMap->getMapName());
if (\eBot\Config\Config::getInstance()->getWorkshop() && \eBot\Config\Config::getInstance()->getWorkshopByMap($this->currentMap->getMapName()))
$this->rcon->send("changelevel workshop/" . \eBot\Config\Config::getInstance()->getWorkshopByMap($this->currentMap->getMapName()) . "/" . $this->currentMap->getMapName());
else
$this->rcon->send("changelevel " . $this->currentMap->getMapName());
if ($this->config_kniferound) {
$this->setStatus(self::STATUS_WU_KNIFE, true);
$this->currentMap->setStatus(Map::STATUS_WU_KNIFE, true);
} else {
$this->setStatus(self::STATUS_WU_1_SIDE, true);
$this->currentMap->setStatus(Map::STATUS_WU_1_SIDE, true);
}
$this->mapIsEngaged = true;
$this->websocket['match']->sendData(json_encode(array('message' => 'currentMap', 'mapname' => $this->currentMap->getMapName(), 'id' => $this->match_id)));
TaskManager::getInstance()->addTask(new Task($this, self::CHANGE_HOSTNAME, microtime(true) + 3));
} else {
$this->setStatus($this->currentMap->getStatus(), true);
Logger::error("Map already engaged");
}
}
private function isWarmupRound() {
return ($this->getStatus() == self::STATUS_WU_1_SIDE) || ($this->getStatus() == self::STATUS_WU_2_SIDE) || ($this->getStatus() == self::STATUS_WU_KNIFE) || ($this->getStatus() == self::STATUS_WU_OT_1_SIDE) || ($this->getStatus() == self::STATUS_WU_OT_2_SIDE);
}
private function isMatchRound() {
return ($this->getStatus() == self::STATUS_KNIFE) || ($this->getStatus() == self::STATUS_FIRST_SIDE) || ($this->getStatus() == self::STATUS_SECOND_SIDE) || ($this->getStatus() == self::STATUS_OT_FIRST_SIDE) || ($this->getStatus() == self::STATUS_OT_SECOND_SIDE);
}
public function sendRotateMessage() {
// This is to send to players the status
if ($this->isMatchRound() && !$this->enable) {
if (time() - $this->lastMessage >= 8) {
$this->say("\001Match is paused, write !continue to continue the match");
$teamA = strtoupper($this->side['team_a']);
$teamB = strtoupper($this->side['team_b']);
if ($this->continue[$this->side['team_a']])
$teamA = "\004$teamA\001";
if ($this->continue[$this->side['team_b']])
$teamB = "\004$teamB\001";
$this->lastMessage = time();
$message = "\003Waiting to continue the match - \005" . $this->teamAName . " \001($teamA\001) \001VS \001($teamB\001) \005" . $this->teamBName;
$this->say($message);
}
}
if ($this->isMatchRound() || $this->delay_ready_inprogress)
return;
if ($this->matchData["enable"] == 1) {
if (time() - $this->lastMessage >= 8) {
if ($this->getStatus() == self::STATUS_STARTING && $this->timeEngageMap > 0) {
$time = ceil(microtime(true) - $this->timeEngageMap);
$this->lastMessage = time();
$this->say("Waiting till GOTV Broadcast is finished.");
$this->say("The next map will start in #lightgreen" . $time . "#default seconds!");
} else {
// Récupération du SIDE de l'équipe
$teamA = strtoupper($this->side['team_a']);
$teamB = strtoupper($this->side['team_b']);
if ($this->ready[$this->side['team_a']])
$teamA = "\004$teamA\001";
if ($this->ready[$this->side['team_b']])
$teamB = "\004$teamB\001";
$message = "";
// Récupération du texte
switch ($this->getStatus()) {
case self::STATUS_WU_KNIFE: $message = "Warmup Knife Round";
break;
case self::STATUS_END_KNIFE: $message = "Waiting for team select from knife winner (!stay/!switch))";
break;
case self::STATUS_WU_1_SIDE: $message = "Warmup First Side";
break;
case self::STATUS_WU_2_SIDE: $message = "Warmup Second Side";
break;
case self::STATUS_WU_OT_1_SIDE: $message = "Warmup First Side OverTime";
break;
case self::STATUS_WU_OT_2_SIDE: $message = "Warmup Second Side OverTime";
break;
}
if ($this->mapIsEngaged && ($this->streamerReady || !$this->config_streamer)) {
$messages [] = "\003Please write \006!ready \003when your team is ready !";
$messages [] = "\003Available commands: !help, !rules, !ready, !notready";
} elseif ($this->mapIsEngaged && (!$this->streamerReady || $this->config_streamer)) {
$messages [] = "\003Streamers are not ready yet !";
} else {
$messages [] = "\003Please write \006!map mapname \003to select the map!";
$maps = \eBot\Config\Config::getInstance()->getMaps();
foreach ($maps as $map) {
$mapmessage .= "$map, ";
}
$messages [] = "\003" . substr($mapmessage, 0, -2);
}
if ($message)
$messages [] = "\003$message - \005" . $this->teamAName . " \001($teamA\001) \001VS \001($teamB\001) \005" . $this->teamBName;
$adverts = \eBot\Config\Config::getInstance()->getAdvertising($this->season_id);
for ($i = 0; $i < count($adverts['season_id']); $i++) {
$messages [] = "\003" . $adverts['message'][$i];
if ($message)
$messages [] = "\003$message - \005" . $this->teamAName . " \001($teamA\001) \001VS \001($teamB\001) \005" . $this->teamBName;
}
$message = $messages[$this->message++ % count($messages)];
$this->lastMessage = time();
$this->say($message);
}
}
}
}
public function say($message) {
/**
* \001 white
* \002 red
* \003 white
* \004 green
* \005 lightgreen
* \006 lightgreen2
* \007 lightred
*/
$message = str_replace("#default", "\001", $message);
$message = str_replace("#green", "\004", $message);
$message = str_replace("#lightgreen2", "\006", $message);
$message = str_replace("#lightgreen", "\005", $message);
$message = str_replace("#red", "\002", $message);
$message = str_replace("#lightred", "\007", $message);
// temporary fix because of ugly chatcolor after last csgo update
$message = str_replace("\003", "\001", $message);
try {
if (!$this->pluginCsay) {
$message = str_replace(array("\001", "\002", "\003", "\004", "\005", "\006", "\007"), array("", "", "", "", "", "", ""), $message);
$message = str_replace(";", ",", $message);
$this->rcon->send('say eBot: ' . addslashes($message) . '');
} else {
$message = str_replace('"', '\\"', $message);
$this->rcon->send('csay_all "' . "e\004Bot\001: " . $message . '"');
}
} catch (\Exception $ex) {
Logger::error("Say failed - " . $ex->getMessage());
}
}
public function destruct() {
$this->websocket['logger']->sendData('removeMatch_' . $this->match_id);
TaskManager::getInstance()->removeAllTaskForObject($this);
unset($this->rcon);
$this->addLog("Destructing match " . $this->match_id);
}
public function getNeedDel() {
return $this->needDel;
}
public function getNeedDelTask() {
return $this->needDelTask;
}
/**
* Process a message from log stream
* @param String $message
*/
public function processMessage($message) {
if ($this->getStatus() >= self::STATUS_END_MATCH)
return;
$message = $this->messageManager->processMessage($message);
if ($message != null) {
switch (get_class($message)) {
case "eBot\Message\Type\BombDefusing":
return $this->processBombDefusing($message);
case "eBot\Message\Type\BombPlanting":
return $this->processBombPlanting($message);
case "eBot\Message\Type\ChangeMap":
return $this->processChangeMap($message);
case "eBot\Message\Type\ChangeName":
return $this->processChangeName($message);
case "eBot\Message\Type\Connected":
return $this->processConnected($message);
case "eBot\Message\Type\Disconnected":
return $this->processDisconnected($message);
case "eBot\Message\Type\EnteredTheGame":
return $this->processEnteredTheGame($message);
case "eBot\Message\Type\GotTheBomb":
return $this->processGotTheBomb($message);
case "eBot\Message\Type\JoinTeam":
return $this->processJoinTeam($message);
case "eBot\Message\Type\Kill":
return $this->processKill($message);
case "eBot\Message\Type\KillAssist":
return $this->processKillAssist($message);
case "eBot\Message\Type\RoundRestart":
return $this->processRoundRestart($message);
case "eBot\Message\Type\RoundScored":
return $this->processRoundScored($message);
case "eBot\Message\Type\RemindRoundScored":
return $this->processRemindRoundScored($message);
case "eBot\Message\Type\RoundStart":
return $this->processRoundStart($message);
case "eBot\Message\Type\RoundSpawn":
return $this->processRoundSpawn($message);
case "eBot\Message\Type\Say":
return $this->processSay($message);
case "eBot\Message\Type\ThrewStuff":
return $this->processThrewStuff($message);
case "eBot\Message\Type\Purchased":
return $this->processPurchased($message);
case "eBot\Message\Type\TeamScored":
return $this->processTeamScored($message);
case "eBot\Message\Type\RoundEnd":
return $this->processRoundEnd($message);
default:
$this->addLog("Message non traité: " . get_class($message));
break;
}
}
}
private $tempScoreA = null;
private $tempScoreB = null;
private function processTeamScored(\eBot\Message\Type\TeamScored $message) {
/* if (!$this->roundEndEvent) {
if ($message->team == $this->side['team_a']) {
$this->tempScoreA = $message->score;
$this->addLog("Score for " . $this->teamAName . ": " . $this->tempScoreA);
} else {
$this->tempScoreB = $message->score;
$this->addLog("Score for " . $this->teamBName . ": " . $this->tempScoreB);
}
} */
}
private function processRoundEnd(\eBot\Message\Type\RoundEnd $message) {
if (!$this->roundEndEvent) {
$this->addLog("RoundEnd catched, but no RoundScored");
$lastRoundEnds = $this->rcon->send("ebot_get_last_roundend");
$lastRoundEnds = explode("\n", $lastRoundEnds);
$message = new \eBot\Message\CSGO\RoundScored();
$data = trim(str_replace("#", "", $lastRoundEnds[0]));
$data = str_replace("scored", "triggered", $data);
$this->addLog($data);
if ($message->match($data)) {
$this->processRoundScored($message->process());
}
}
}
private function processChangeMap(\eBot\Message\Type\ChangeMap $message) {
Logger::debug("Processing Change Map");
if (preg_match("!CRC!", $message->maps)) {
$this->addLog("Wrong map name ".$message->maps);
return;
}
if ($this->currentMap->getMapName() == "tba" || $this->getStatus() > 2 || strpos($message->maps,$this->currentMap->getMapName()) !== false ) {
$this->addLog("Loading maps " . $message->maps);
$this->addMatchLog("Loading maps " . $message->maps);
$ip = explode(":", $this->server_ip);
try {
$this->rcon = new Rcon($ip[0], $ip[1], $this->rconPassword);
$this->rcon->send("echo eBot;");
if ($this->matchData["config_password"] != "") {
$this->rcon->send("sv_password \"" . $this->matchData["config_password"] . "\"");
}
$this->rcon->send("mp_warmup_pausetimer 1");
if ($this->config_ot) {
$this->rcon->send("mp_overtime_enable 1");
$this->rcon->send("mp_overtime_maxrounds " . ($this->ot_maxround * 2));
$this->rcon->send("mp_overtime_startmoney " . $this->ot_startmoney);
$this->rcon->send("mp_overtime_halftime_pausetimer 1");
}
$this->sendTeamNames();
} catch (\Exception $ex) {
Logger::error("Reinit rcon failed - " . $ex->getMessage());
TaskManager::getInstance()->addTask(new Task($this, self::REINIT_RCON, microtime(true) + 1));
}
} else {
$this->addLog("Wrong map loaded " . $message->maps. " need ".$this->currentMap->getMapName());
$this->addMatchLog("Wrong map loaded " . $message->maps. " need ".$this->currentMap->getMapName());
$ip = explode(":", $this->server_ip);
try {
$this->rcon = new Rcon($ip[0], $ip[1], $this->rconPassword);
$this->rcon->send("echo eBot;");
$this->rcon->send("changelevel ".$this->currentMap->getMapName());
} catch (\Exception $ex) {
Logger::error("Reinit rcon failed - " . $ex->getMessage());
TaskManager::getInstance()->addTask(new Task($this, self::REINIT_RCON, microtime(true) + 1));
}
}
}
/**
* Processing message for planting bomb.
* Setting the gameBombPlanter to the user
* @param \eBot\Message\Type\BombPlanting $message
*/
private function processBombPlanting(\eBot\Message\Type\BombPlanting $message) {
Logger::debug("Processing Bomb Planting");
// Getting the player who planted the bomb
$user = $this->processPlayer($message->getUserId(), $message->getUserName(), $message->getUserTeam(), $message->getUserSteamid());
$this->gameBombPlanter = $user;
$this->addLog($message->getUserName() . " planted the bomb");
$this->addMatchLog($this->getColoredUserNameHTML($message->getUserName(), $message->getUserTeam()) . " planted the bomb");
// Dispatching events
$event = new \eBot\Events\Event\BombPlanting();
$event->setMatch($this);
$event->setUserId($message->getUserId());
$event->setUserName($message->getUserName());
$event->setUserTeam($message->getUserTeam());
$event->setUserSteamid($message->getUserSteamid());
\eBot\Events\EventDispatcher::getInstance()->dispatchEvent($event);
// Round TimeLine
$text = addslashes(serialize(array("id" => $user->getId(), "name" => $message->getUserName())));
\mysql_query("
INSERT INTO `round`
(`match_id`,`map_id`,`event_name`,`event_text`,`event_time`,`round_id`,`created_at`,`updated_at`)
VALUES
('" . $this->match_id . "', '" . $this->currentMap->getMapId() . "', 'bomb_planting', '" . $text . "', '" . $this->getRoundTime() . "', '" . $this->getNbRound() . "', NOW(), NOW())
");
}
/**
* Processing message for defusing bomb.
* Setting the gameBombDefuser to the user
* @param \eBot\Message\Type\BombDefusing $message
*/
private function processBombDefusing(\eBot\Message\Type\BombDefusing $message) {
Logger::debug("Processing Bomb Defusing");
// Getting the player who is defusing the bomb
$user = $this->processPlayer($message->getUserId(), $message->getUserName(), $message->getUserTeam(), $message->getUserSteamid());
$this->gameBombDefuser = $user;
$this->addLog($message->getUserName() . " is defusing bomb");
$this->addMatchLog($this->getColoredUserNameHTML($message->getUserName(), $message->getUserTeam()) . " is defusing bomb");
// Dispatching events
$event = new \eBot\Events\Event\BombDefusing();
$event->setMatch($this);
$event->setUserId($message->getUserId());
$event->setUserName($message->getUserName());
$event->setUserTeam($message->getUserTeam());
$event->setUserSteamid($message->getUserSteamid());
\eBot\Events\EventDispatcher::getInstance()->dispatchEvent($event);
// Round TimeLine
$text = addslashes(serialize(array("id" => $user->getId(), "name" => $message->getUserName())));
\mysql_query("
INSERT INTO `round`
(`match_id`,`map_id`,`event_name`,`event_text`,`event_time`,`round_id`,`created_at`,`updated_at`)