-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_replay.php
More file actions
109 lines (94 loc) · 3.13 KB
/
run_replay.php
File metadata and controls
109 lines (94 loc) · 3.13 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
<?php
include "db.php";
include "logging.php";
include "incidents.php";
include "get_config.php";
$sport = $_GET['sport'];
$leagues = $_GET['leagues'];
$start = $_GET['start'];
$end = $_GET['end'];
$replay = new stdClass();
$error = 0;
$total = 0;
$output;
$message = new stdClass();
// get witnesses
$witnesses = $config->subscriptions->witnesses;
// parse list of leagues and add quotes. Uses pipe seperator as comma screws with the URL
$str_arr = explode ("|", $leagues);
// do one league at a time
foreach ($str_arr as $league) {
// get the data
$data = array();
$game = '';
$count=0;
$q = $con->query("SELECT * FROM couch_potato.vwgameevents WHERE `sportname` = '$sport' AND `league` = '$league'
AND `status` = 0 AND `date` BETWEEN '$start' AND '$end'");
while ($row=mysqli_fetch_object($q)){
$replay->call = "create";
$replay->sport = $row->sportname;
$replay->league = $row->league;
$replay->home = $row->hometeam;
$replay->away = $row->awayteam;
$replay->start_time = str_replace(' ', 'T',$row->datetime) . ":00.000Z";
$replay->match_id = $row->gameid;
$incident = json_encode(make_incident($replay, $config),JSON_UNESCAPED_SLASHES);
if(send_message($incident) > 0 ){
$error++;
}
else{
$count++;
$total++;
}
}
output_results($league,$count);
}
if($error > 0 || $total == 0){
$message->status = "400";
$message->title = "Failed to run replay for [" . $sport . "]";
$message->subcode = "445";
$message->message = "Replay failed to send to one or more endpoints";
}
else{
$message->status = "200";
$message->title = "Replay completed";
$message->message = $sport .': {' . rtrim($output,', ') . '}';
}
echo json_encode($message);
function send_message($incident){
global $witnesses;
$error = 0;
// send incident message to BOS witnesses
foreach($witnesses as $witness) {
$curl = curl_init(trim($witness->url)); // one witness for now
$headers = [
'Content-Type: application/json'
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $incident);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
$error++;
}
else{
curl_close($curl);
$decoded = json_decode($curl_response);
if($decoded->id == null){
$error++;
}
else{
log_incident(json_decode($incident), $witness->url);
log_success($decoded, $witness->url);
}
}
}
return $error;
}
function output_results($league, $count){
global $output;
$output .= $league . ': ' . $count . ', ';
}
?>