NewOnlineGame.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. $functionName = $_POST['f'];
  18. if ($functionName === "NewGame") {
  19. $playerIdsArray = explode(",",$playerIds);
  20. $playerCount = count($playerIdsArray);
  21. if ($playerCount == 1) {
  22. $status = "STARTED";
  23. } else {
  24. $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. "(SELECT id FROM users WHERE id IN ($playerIds) ORDER BY RAND() LIMIT 1), " .
  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, 'ACCEPTED'),";
  56. } else {
  57. $playerPartInsertSql .= "($gameId, $playerId, 1, 1, 'WAITING'),";
  58. }
  59. }
  60. $playerPartInsertSql = rtrim($playerPartInsertSql,",");
  61. $playerSql = "INSERT INTO gamePlayers (gameId, playerId, userLockedQuestions, questionsLost, status) VALUES $playerPartInsertSql";
  62. $result = mysqli_query($conn,$playerSql);
  63. $error = mysqli_error($conn);
  64. echo $gameId;
  65. if ($error !== "") {
  66. echo $error . " from sql " . $playerSql;
  67. }
  68. } else if ($functionName === "GetCategories") {
  69. $sql = "SELECT category.name as name, count(*) as num FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category ON questionToCategory.categoryId = category.id GROUP BY category.name"
  70. $result = $conn->query($sql);
  71. $json = array();
  72. while ($row = $result->fetch_assoc()) {
  73. $json = array('name' => $row['name'],
  74. 'value' => $row['value']);
  75. $jsonString = json_encode($json);
  76. echo $jsonString;
  77. }
  78. }
  79. ?>