<?php

require_once '../includes/php/connexionbdd.php';

$bdd = connexionBD();

$req = $bdd->query("SELECT * FROM rl_general");

while($donnee = $req->fetch())
{
    $steamid = $donnee['steam_id'];
    $pseudo = $donnee['pseudo'];

    $ch = curl_init("http://rltracker.pro/profiles/$steamid/steam");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $raw = curl_exec($ch);
    curl_close($ch);

    $html = new DOMDocument();
    @$html->loadHTML($raw);
    $xpath = new DOMXPath($html);
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_10']/div[@class='tier_name']");
    foreach ($domExemple as $exemple) {
        $tier = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_10']/div[@class='division']");
    foreach ($domExemple as $exemple) {
        $division = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_10']/div[@class='rating']");
    foreach ($domExemple as $exemple) {
        $rating = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_10']/div[@class='matches']");
    foreach ($domExemple as $exemple) {
        $matches = $exemple->nodeValue;
    }
    
    preg_match("/(?:\d*\.)?\d+/", $rating, $rating);
    preg_match("/(?:\d*\.)?\d+/", $matches, $matches);
    
    echo "$tier<br/>";
    echo "$division<br/>";
    echo "$rating[0]<br/>";
    echo "$matches[0]<br/>";
    echo "$pseudo<br/><br/>";
    
    $req2 = $bdd->query("SELECT id FROM rl_solo WHERE pseudo = '$pseudo'");
    
    if($req2->rowCount() != 0)
    {
        $req2 = $bdd->query("UPDATE rl_solo SET rank='$tier', division='$division', rating=$rating[0], matches=$matches[0] WHERE pseudo='$pseudo';") or die(print_r($bdd->errorInfo(), true));
        echo "Mise à jour<br/><br/>";
    }
    else
    {
        $req2 = $bdd->query("INSERT INTO rl_solo (pseudo, rank, division, rating, matches) VALUES ('$pseudo', '$tier', '$division', $rating[0], $matches[0]);") or die(print_r($bdd->errorInfo(), true));
    }
    
    
    
    
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_11']/div[@class='tier_name']");
    foreach ($domExemple as $exemple) {
        $tier = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_11']/div[@class='division']");
    foreach ($domExemple as $exemple) {
        $division = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_11']/div[@class='rating']");
    foreach ($domExemple as $exemple) {
        $rating = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_11']/div[@class='matches']");
    foreach ($domExemple as $exemple) {
        $matches = $exemple->nodeValue;
    }
    
    preg_match("/(?:\d*\.)?\d+/", $rating, $rating);
    preg_match("/(?:\d*\.)?\d+/", $matches, $matches);
    
    echo "$tier<br/>";
    echo "$division<br/>";
    echo "$rating[0]<br/>";
    echo "$matches[0]<br/>";
    echo "$pseudo<br/><br/>";
    
    $req2 = $bdd->query("SELECT id FROM rl_doubles WHERE pseudo = '$pseudo'");
    
    if($req2->rowCount() != 0)
    {
        $req2 = $bdd->query("UPDATE rl_doubles SET rank='$tier', division='$division', rating=$rating[0], matches=$matches[0] WHERE pseudo='$pseudo';") or die(print_r($bdd->errorInfo(), true));
        echo "Mise à jour<br/><br/>";
    }
    else
    {
        $req2 = $bdd->query("INSERT INTO rl_doubles (pseudo, rank, division, rating, matches) VALUES ('$pseudo', '$tier', '$division', $rating[0], $matches[0]);") or die(print_r($bdd->errorInfo(), true));
    }
    
    
    
    
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_12']/div[@class='tier_name']");
    foreach ($domExemple as $exemple) {
        $tier = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_12']/div[@class='division']");
    foreach ($domExemple as $exemple) {
        $division = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_12']/div[@class='rating']");
    foreach ($domExemple as $exemple) {
        $rating = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_12']/div[@class='matches']");
    foreach ($domExemple as $exemple) {
        $matches = $exemple->nodeValue;
    }
    
    preg_match("/(?:\d*\.)?\d+/", $rating, $rating);
    preg_match("/(?:\d*\.)?\d+/", $matches, $matches);
    
    echo "$tier<br/>";
    echo "$division<br/>";
    echo "$rating[0]<br/>";
    echo "$matches[0]<br/>";
    echo "$pseudo<br/><br/>";
    
    $req2 = $bdd->query("SELECT id FROM rl_solo_standard WHERE pseudo = '$pseudo'");
    
    if($req2->rowCount() != 0)
    {
        $req2 = $bdd->query("UPDATE rl_solo_standard SET rank='$tier', division='$division', rating=$rating[0], matches=$matches[0] WHERE pseudo='$pseudo';") or die(print_r($bdd->errorInfo(), true));
        echo "Mise à jour<br/><br/>";
    }
    else
    {
        $req2 = $bdd->query("INSERT INTO rl_solo_standard (pseudo, rank, division, rating, matches) VALUES ('$pseudo', '$tier', '$division', $rating[0], $matches[0]);") or die(print_r($bdd->errorInfo(), true));
    }
    
    
    
    
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_13']/div[@class='tier_name']");
    foreach ($domExemple as $exemple) {
        $tier = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_13']/div[@class='division']");
    foreach ($domExemple as $exemple) {
        $division = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_13']/div[@class='rating']");
    foreach ($domExemple as $exemple) {
        $rating = $exemple->nodeValue;
    }
    
    $domExemple = $xpath->query("//div[@class='col-xs-3 statistics_col playlist_13']/div[@class='matches']");
    foreach ($domExemple as $exemple) {
        $matches = $exemple->nodeValue;
    }
    
    preg_match("/(?:\d*\.)?\d+/", $rating, $rating);
    preg_match("/(?:\d*\.)?\d+/", $matches, $matches);
    
    echo "$tier<br/>";
    echo "$division<br/>";
    echo "$rating[0]<br/>";
    echo "$matches[0]<br/>";
    echo "$pseudo<br/><br/>";
    
    $req2 = $bdd->query("SELECT id FROM rl_standard WHERE pseudo = '$pseudo'");
    
    if($req2->fetch() != 0)
    {
        $req2 = $bdd->query("UPDATE rl_standard SET rank='$tier', division='$division', rating=$rating[0], matches=$matches[0] WHERE pseudo='$pseudo';") or die(print_r($bdd->errorInfo(), true));
        echo "Mise à jour<br/><br/>";
    }
    else
    {
        $req2 = $bdd->query("INSERT INTO rl_standard (pseudo, rank, division, rating, matches) VALUES ('$pseudo', '$tier', '$division', $rating[0], $matches[0]);") or die(print_r($bdd->errorInfo(), true));
    }
    
}