Files
ritzenbergen-backend/bulitipp/check.php

136 lines
4.7 KiB
PHP
Executable File

<?php
include("../../mysqlverbinden.php");
include("../rowforeach.php");
include("inc.php");
$bundesliga = file_get_contents("liga.txt");
$year = intval(file_get_contents("saison.txt"));
$reload=false;
$lockFile = fopen(__DIR__ . '/script.lock', 'c');
if (!$lockFile) {
die('Lockdatei konnte nicht geöffnet werden');
}
if (flock($lockFile, LOCK_EX | LOCK_NB)) {
// Skript läuft noch nicht
// Paarungen holen
// HTTP-Request ausführen
$result=mysqli_execute_query($db_id,"SELECT COUNT(*) FROM `buli-paarungen`;",[]);
$row=mysqli_fetch_row($result);
if($row[0]==0){
echo "Keine Paarungen in der Datenbank, starte mit Spieltag 1";
$spieltag=1;
}else{
$spieltag=getmaxtippspieltag()+1;
}
if($spieltag<=34){
$response = file_get_contents("https://api.openligadb.de/getmatchdata/bl".$bundesliga."/".$year."/".$spieltag, false);
if ($response === false) {
echo "Fehler beim HTTP Request!";
}
$responseData = json_decode($response, true); // Response auswerten
if ($responseData!=null) {
// Response-Data ist ok
// Wochentag und Uhrzeit überprüfen, also auch überprüfen, ob die Daten schon aktuell sind
$aktuell = false;
foreach ($responseData as $key => $match) {
// Datum in UTC einlesen
$date = new DateTime($match["matchDateTimeUTC"], new DateTimeZone('UTC'));
// In die Zeitzone Berlin konvertieren
$date->setTimezone(new DateTimeZone('Europe/Berlin'));
$diff = time()-$date->getTimestamp();
if ($diff >= -60 * 60 * 24 * 21) {
$aktuell = true;
}
}
if ($aktuell) {
// Daten sind aktuell genug, Paarungen in die Datenbank einfügen
$spieltag=getmaxtippspieltag()+1;
foreach ($responseData as $key => $match) {
$deadline = new DateTime($match["matchDateTimeUTC"], new DateTimeZone('UTC'));
// In die Zeitzone Berlin konvertieren
$deadline->setTimezone(new DateTimeZone('Europe/Berlin'));
$heim=$match["team1"]["shortName"];
$gast=$match["team2"]["shortName"];
mysqli_execute_query($db_id,"INSERT INTO `buli-paarungen` (`heim`,`gast`,`spieltag`,`deadline`) VALUES (?,?,?,?);",[$heim,$gast,$spieltag,$deadline->format('Y-m-d H:i:s')]);
$reload=true;
}
}
} else {
// Response ist kein JSON
// echo "<script>window.alert(`JSON-Response ungültig (Z.84)! Bitte einen Admin kontaktieren! BuLi-Tipp Daten konnten nicht aktualisiert werden.`);</script>";
}
}
$spieltag=getmaxspieltag()+1;
$response = file_get_contents("https://api.openligadb.de/getmatchdata/bl".$bundesliga."/".$year."/".$spieltag, false);
if ($response === false) {
// echo "<script>window.alert(`Fehler beim HTTP-Request (Z.15)! Bitte einen Admin kontaktieren! BuLi-Tipp Daten konnten nicht aktualisiert werden.`);</script>";
}
$responseData = json_decode($response, true); // Response auswerten
if ($responseData) {
// Response-Data ist ok
foreach ($responseData as $key => $match) {
if ($match["matchIsFinished"]) {
// Spiel beendet, Ergebnisse in die Datenbank eintragen
$heim=$match["team1"]["shortName"];
$gast=$match["team2"]["shortName"];
$paarungsquery=srowforeach("SELECT `id` from `buli-paarungen` where spieltag=? AND heim=? AND gast=?;",[$spieltag,$heim,$gast]);
if(count($paarungsquery)==0) continue;
$paarungsid=$paarungsquery[0][0];
$score1=$match["matchResults"][1]["pointsTeam1"];
$score2=$match["matchResults"][1]["pointsTeam2"];
mysqli_execute_query($db_id,"INSERT INTO `buli-results` (paarung, score1,score2, spieltag) VALUES (?,?,?,?);",[$paarungsid,$score1,$score2,$spieltag]);
$reload=true;
}
}
} else {
// Response ist kein JSON
// echo "<script>window.alert(`JSON-Response ungültig (Z.29)! Bitte einen Admin kontaktieren! BuLi-Tipp Daten konnten nicht aktualisiert werden.`);</script>";
}
if(srowforeach("SELECT COUNT(`id`) from `buli-icons`;",[])[0][0]<9){
mysqli_execute_query($db_id,"TRUNCATE `buli-icons`;",[]);
$responseDataTeams=json_decode(file_get_contents("https://api.openligadb.de/getavailableteams/bl".$bundesliga."/".$year, false),true);
foreach($responseDataTeams as $key => $value){
$iconurl=$value["teamIconUrl"];
$name=$value["shortName"];
$image=file_get_contents($iconurl,false);
mysqli_execute_query($db_id,"INSERT INTO `buli-icons` (`team`,`img`) VALUES (?,?);",[$name,$image]);
}
}
flock($lockFile, LOCK_UN);
fclose($lockFile);
}