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
| <?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
function redirect($page){
if($page!=='')header('Location: ./'.$page);
exit();
}
require_once('./../BDD/connect.php');
$connexion = mysqli_connect(SERVEUR,NOM,PASSE,BASE);
if(!$connexion){ //si erreur
echo 'Désolé, connexion à '.SERVEUR.' temporairement impossible !';
echo 'Essayez plus tard !';
exit();
}
//CONNEXION OK
$requete='select COUNT(idAtt) from WaitMatch';
$resultat=mysqli_query($connexion,$requete);
$value=mysqli_fetch_row($resultat);/*Valeur unique ==> [0]*/
if($value==0){
/*Personne n'attend un match, on est inséré dans la table
On commence par récupérer notre id*/
$pseudo=isset($_SESSION['pseudo'])?$_SESSION['pseudo']:'';
if($pseudo=='')redirect('notFound.html');
$reqId='select idU from Utilisateur where pseudo="'.mysqli_real_escape_string($connexion,$pseudo).'"';
$resId=mysqli_query($connexion,$reqId);
//Un seul résultat possible
$emp=mysqli_fetch_assoc($resId));
$myId=$emp['idU'];
//Requete d'insertion le champ waiting est à false par defaut
$insert="insert into WaitMatch(idU1) values ('$myId')";
$resInsert=mysqli_query($connexion,$insert);
//On est en attente de match
}elseif($value==1){
/*Une personne attend, on lance le match après récupération de son id*/
$reqIdAdv="select idU1 from WaitMatch";
$resIdAdv=mysqli_query($connexion,$reqIdAdv);
$idAdv=mysqli_fetch_assoc($resIdAdv));
$id=$idAdv['idU1'];
//On alter pour signifier qu'il n'y a plus besoin d'attendre à l'adversaire
$alterWait="update WaitMatch set waiting = 'false' where waiting = 'true' and idU1='$id'";
$resAlterWait= mysqli_query($connexion,$alterWait);
//a partir de l'id le pseudo de l'adv
$reqPseudoAdv="select pseudo into Utilisateur where idU='$id'";
$resPseudoAdv=mysqli_query($connexion,$reqPseudoAdv);
$emp=mysqli_fetch_assoc($resPseudoAdv));
$pseudoAdv=$emp['pseudo'];
$_SESSION['pseudoAdv']=$pseudoAdv;
/*On a le pseudo de l'adv, on peut supprimer dans la table
$delWait="delete * from WaitMatch";
$reqDel=mysqli_query($connexion,$reqDel);*/
//On part en match
redirect('combat.php');
}else{
redirect('notFound.html');
}
?>
<!DOCTYPE>
<html>
<head>
<title>AttendMatch</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="./wait.js"></script>
</head>
<body>
<p onclick='alert(getValue())'>Toujours en attente ?</p>
</body>
</html> |
Partager