NewOnlineGame.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $categoryIds = $_POST['categoryIds'];
  18. $playerIdsArray = explode(",",$playerIds);
  19. shuffle($playerIdsArray);
  20. $playerCount = count($playerIdsArray);
  21. if ($playerCount == 1) {
  22. $status = "STARTED";
  23. } else {
  24. $status = Constants.GAME_STATUS_PENDING;
  25. }
  26. $sql = "INSERT INTO game(" .
  27. "gameMode, " .
  28. "status, " .
  29. "winNumber, " .
  30. "answerTimer, " .
  31. "roundTimeLimit, " .
  32. "numberOfPlayers, " .
  33. "currentPlayer, " .
  34. "round, " .
  35. "startedDate) " .
  36. "VALUES (" .
  37. "'Online', " .
  38. "'$status', " .
  39. "$winNumber, " .
  40. "$limitPerQuestion, " .
  41. "$limitPerPlayer, " .
  42. "$playerCount, " .
  43. $playerIdsArray[0] . ", " .
  44. "1, " .
  45. "NOW())";
  46. $result = mysqli_query($conn, $sql);
  47. $error = mysqli_error($conn);
  48. if ($error !== "") {
  49. echo $error;
  50. }
  51. $gameId = mysqli_insert_id($conn);
  52. $playerPartInsertSql = "";
  53. foreach ($playerIdsArray AS $playerId) {
  54. if ($currentUser == $playerId) {
  55. $playerPartInsertSql .= "($gameId, $playerId, 1, 1, 1, 'ACCEPTED'),";
  56. } else {
  57. $playerPartInsertSql .= "($gameId, $playerId, 1, 1, 1, 'WAITING'),";
  58. }
  59. }
  60. $playerPartInsertSql = rtrim($playerPartInsertSql,",");
  61. $playerSql = "INSERT INTO gamePlayers (gameId, playerId, userLockedQuestions, questionsLost, playerRound, status) VALUES $playerPartInsertSql";
  62. $result = mysqli_query($conn,$playerSql);
  63. $error = mysqli_error($conn);
  64. if ($error !== "") {
  65. echo $error . " from sql " . $playerSql;
  66. }
  67. $categoryValues = "";
  68. if (empty($categoryIds)) {
  69. $categoryValues = "(SELECT $gameId, id FROM category)";
  70. } else {
  71. $categoryIdsArray = explode(",", $categoryIds);
  72. foreach ($categoryIdsArray AS $catId) {
  73. $categoryValues .= "($gameId, $catId),";
  74. }
  75. }
  76. $categoryValues = rtrim($categoryValues, ",");
  77. $categorySql = "INSERT INTO gameCategories(gameId, categoryId) VALUES $categoryValues";
  78. $result = mysqli_query($conn, $categorySql);
  79. $error = mysqli_error($conn);
  80. if ($error !== "") {
  81. echo $error . " from sql " . $categorySql;
  82. }
  83. echo $gameId;
  84. ?>