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); }