From c79d12f3f8ff9dd162beba366774ec9b29642a3c Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 16 Apr 2024 18:56:24 +0200 Subject: [PATCH] =?UTF-8?q?clean=20code=20pour=20cr=C3=A9er=20la=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controleurs/creerPartie_controleur.php | 71 +++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/controleurs/creerPartie_controleur.php b/controleurs/creerPartie_controleur.php index 0567ccd..930f2dc 100644 --- a/controleurs/creerPartie_controleur.php +++ b/controleurs/creerPartie_controleur.php @@ -1,8 +1,77 @@ '; +} -function creerPartie($nbCartesVertes, $nbCartesOranges, $nbCartesNoires) { +function rangDansPlateau(int $idplateau, int $rang) +{ + return !empty(get_infos_requete("SELECT idplateau AS nb FROM est_compose WHERE idplateau = " . + $idplateau . " AND rang = " . $rang)['instances']); +} +function getCarteID(String $couleur, int $index) { + return get_infos_requete("SELECT idcarte FROM carte WHERE niveau = '" . $couleur . "'")['instances'][$index - 1]['idcarte']; +} + +function carteDansPlateau(int $idplateau, int $idcarte) { + return !empty(get_infos_requete("SELECT idcarte AS nb FROM est_compose WHERE idplateau = " . + $idplateau . " AND idcarte = " . $idcarte)['instances']); +} + +function ajouterCarte(int $idcarte, int $idplateau, int $rang) +{ + executer_une_requete("INSERT INTO est_compose (idcarte, idplateau, rang) VALUES (" . $idcarte . ", " . $idplateau . ", " . $rang . ")"); +} + +function ajouterCartesAlea(string $couleur, int $nbCartesCouleurPartie, int $nbCartes, int $idplateau) +{ + $nbCartesJeu = get_infos_requete("SELECT nombre FROM cartes_jeu WHERE couleur = '" . $couleur . "'")['instances'][0]['nombre']; + + for ($i = 0; $i < $nbCartesCouleurPartie; $i++) { + $rang = 1; + while (rangDansPlateau($idplateau, $rang)) { + $rang = rand(1, $nbCartes); + } + $idcarte = getCarteID($couleur, rand(1, $nbCartesJeu)); + while (carteDansPlateau($idplateau, $idcarte)) { + $idcarte = getCarteID($couleur, rand(1, $nbCartesJeu)); + } + ajouterCarte($idcarte, $idplateau, $rang); + } +} + +function creerPartie(int $nbCartesVertes, int $nbCartesOranges, int $nbCartesNoires) +{ + $total = $nbCartesVertes + $nbCartesOranges + $nbCartesNoires; + $res = get_infos_requete("INSERT INTO plateau (`taille`) VALUES ('" . $total . + "') RETURNING idplateau"); + + if ($res === false) { + echo 'La requête a échoué'; + return false; + } + + $idplateau = $res['instances'][0]['idplateau']; + + println("ID plateau : " . $idplateau); + + $res = get_infos_requete("INSERT INTO partie (date_partie, horaire, etat, idplateau) VALUES (NOW(), NOW(), 'a venir', " + . $idplateau . ") RETURNING idpartie"); + + if ($res === false) { + echo 'La requête a échoué'; + return false; + } + + $idpartie = $res['instances'][0]['idpartie']; + + println("ID partie : " . $idpartie); + + ajouterCartesAlea("noire", $nbCartesNoires, $total, $idplateau); + ajouterCartesAlea("orange", $nbCartesOranges, $total, $idplateau); + ajouterCartesAlea("verte", $nbCartesVertes, $total, $idplateau); }