<?php

session_start();

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

if(isset($_SESSION['pseudo']))
{
    $bdd = connexionBD();
    
    $score = $_POST['score'];
    $date = date('Y-m-d');
    $pseudo = $_SESSION['pseudo'];
    $ip = $_SERVER['REMOTE_ADDR'];
    
    $query = $bdd->query("SELECT score FROM tetris WHERE pseudo = '$pseudo';");
    $result = $query->rowCount();
    if($result == 0)
    {
        $query = $bdd->query("INSERT INTO tetris (date, pseudo, score, ip) VALUES ('$date', '$pseudo', '$score', '$ip');");
        
        $reponse = "v";
    }
    else
    {
        
        while($donnee = $query->fetch())
        {
            $current_score = $donnee['score'];
        }
        
        if($score >= $current_score)
        {
            $query = $bdd->query("UPDATE tetris SET date = '$date', score = '$score', ip = '$ip' WHERE pseudo = '$pseudo';");
            
            $reponse = "v";
        }
        else
        {
            $reponse = "m";
        }
    }
    
    
}
else
{
    $reponse = "n";
}

echo json_encode($reponse);


