roll.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. //Recup de la session
  3. if(!isset($_COOKIE['salon']) || !isset($_COOKIE['common-sess'])){
  4. exit(json_encode(['etat' => 'err']));
  5. }
  6. if($_COOKIE['common-sess'] != $_COOKIE['salon']){
  7. exit(json_encode(['etat' => 'err']));
  8. }
  9. //Lancement session
  10. session_name('common-sess');
  11. session_start();
  12. //Traitement formulaire
  13. $json = [];
  14. if(isset($_POST['roll'])){
  15. //Regarde si l'utilisateur est nouveau
  16. $name = '';
  17. if(isset($_POST['name']) && trim($_POST['name']) != ''){
  18. $name = $_POST['name'];
  19. if(!isset($_SESSION['user'][$name])){
  20. $_SESSION['user'][$name] = $name;
  21. }
  22. } else if(isset($_POST['save-name'])){
  23. $name = $_POST['save-name'];
  24. } else {
  25. $type = 'err';
  26. $value = 'Aucun lanceur';
  27. }
  28. //Si il y a un bien un utilisateur
  29. if($name != ''){
  30. //Tirage
  31. $roll = mt_rand(1, $_POST['roll']);
  32. //Affichage
  33. if(!isset($_POST['hide'])){
  34. $_SESSION['roll'][] = [
  35. 'name' => $name,
  36. 'roll' => $roll,
  37. 'dice' => $_POST['roll']
  38. ];
  39. }
  40. $json['roll'] = $roll;
  41. $json['dice'] = $_POST['roll'];
  42. $json['etat'] = 'ok';
  43. }
  44. } else {
  45. $json['etat'] = 'err';
  46. }
  47. echo json_encode($json);