NewOnlineGame.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. error_reporting( E_ALL );
  3. $hostname = 'localhost';
  4. $username = 'narKampen';
  5. $pass = 'IfRLzj2HJBXA9eei';
  6. $database = 'narKampen';
  7. $conn = new mysqli($hostname, $username, $pass, $database);
  8. if (!$conn) {
  9. die("Connection Failed. ". mysqli_connect_error());
  10. }
  11. mysqli_set_charset($conn,'utf8');
  12. $winNumber = $_POST['winNumber'];
  13. $limitPerQuestion = $_POST['limitPerQuestion'];
  14. $limitPerPlayer = $_POST['limitPerPlayer'];
  15. $playerIds = $_POST['playerIds'];
  16. $currentUser = $_POST['currentUser'];
  17. $playerIdsArray = explode(",",$playerIds);
  18. $playerCount = count($playerIdsArray);
  19. if ($playerCount == 1) {
  20. $status = "STARTED";
  21. } else {
  22. $status = "PENDING";
  23. }
  24. $sql = "INSERT INTO game(" .
  25. "gameMode, " .
  26. "status, " .
  27. "winNumber, " .
  28. "answerTimer, " .
  29. "roundTimeLimit, " .
  30. "numberOfPlayers, " .
  31. "currentPlayer, " .
  32. "round, " .
  33. "startedDate) " .
  34. "VALUES (" .
  35. "'Online', " .
  36. "'$status', " .
  37. "$winNumber, " .
  38. "$limitPerQuestion, " .
  39. "$limitPerPlayer, " .
  40. "$playerCount, " .
  41. "(SELECT id FROM users WHERE id IN ($playerIds) ORDER BY RAND() LIMIT 1), " .
  42. "1, " .
  43. "NOW())";
  44. $result = mysqli_query($conn, $sql);
  45. $error = mysqli_error($conn);
  46. if ($error !== "") {
  47. echo $error;
  48. }
  49. $gameId = mysqli_insert_id($conn);
  50. $playerPartInsertSql = "";
  51. foreach ($playerIdsArray AS $playerId) {
  52. if ($currentUser == $playerId) {
  53. $playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'ACCEPTED'),";
  54. } else {
  55. $playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'WAITING'),";
  56. }
  57. }
  58. $playerPartInsertSql = rtrim($playerPartInsertSql,",");
  59. $playerSql = "INSERT INTO gamePlayers (gameId, playerId, userLockedQuestions, questionsLost, status) VALUES $playerPartInsertSql";
  60. $result = mysqli_query($conn,$playerSql);
  61. $error = mysqli_error($conn);
  62. if ($error !== "") {
  63. echo $error . " from sql " . $playerSql;
  64. }
  65. ?>