save_run_team.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * File for the run team saving action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. /**
  12. * Saves a run party as a team.
  13. *
  14. * Reads the POST parameters looking for the following KEYS:
  15. * player: Player ID.
  16. * run: Run ID.
  17. * name: New team name.
  18. * description: New team description.
  19. * score: New team score {@todo implement}.
  20. * Validates the data and creates the team in the database.
  21. *
  22. * @return int 201 on success, HTTP erro status codes on error.
  23. * @category Action
  24. */
  25. function action(){
  26. $player = filter_input(INPUT_POST, 'player');
  27. $run = filter_input(INPUT_POST, 'run');
  28. $name = filter_input(INPUT_POST, 'name');
  29. $description = filter_input(INPUT_POST, 'description');
  30. $score = intval(filter_input(INPUT_POST, 'score'));
  31. if ($name == null || strlen($name) == 0){
  32. return 400;
  33. }
  34. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  35. $q = get_context()->get_db()->query($s);
  36. $r = $q->fetchArray(SQLITE3_ASSOC);
  37. $team_id = $r["id"];
  38. $statement = get_context()->get_db()->prepare("
  39. SELECT
  40. run.area AS area,
  41. run.stage AS stage,
  42. run.difficulty AS difficulty,
  43. area.type AS area_type
  44. FROM
  45. run,
  46. area
  47. WHERE
  48. run.player = :player AND
  49. run.id = :run AND
  50. area.id = run.area AND
  51. run.helper = 0;
  52. ");
  53. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  54. $statement->bindValue(':run', $run, SQLITE3_TEXT);
  55. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  56. if (!$r){
  57. return 404;
  58. }
  59. $statement = get_context()->get_db()->prepare("
  60. INSERT INTO team (
  61. player,
  62. id,
  63. name,
  64. description,
  65. area_type,
  66. area,
  67. stage,
  68. difficulty,
  69. score
  70. ) VALUES (
  71. :player,
  72. :id,
  73. :name,
  74. :description,
  75. :area_type,
  76. :area,
  77. :stage,
  78. :difficulty,
  79. :score);
  80. ");
  81. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  82. $statement->bindValue(':id', $team_id, SQLITE3_TEXT);
  83. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  84. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  85. $statement->bindValue(':area_type', $r["area_type"], SQLITE3_TEXT);
  86. $statement->bindValue(':area', $r["area"], SQLITE3_TEXT);
  87. $statement->bindValue(':stage', $r["stage"], SQLITE3_TEXT);
  88. $statement->bindValue(':difficulty', $r["difficulty"], SQLITE3_TEXT);
  89. $statement->bindValue(':score', $score, SQLITE3_INTEGER);
  90. $res = $statement->execute();
  91. if (!$res){
  92. return 500;
  93. }
  94. $s = "SELECT unit FROM data.run_party WHERE run = $run AND unit IS NOT NULL;";
  95. $q = get_context()->get_db()->query($s);
  96. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  97. $statement = get_context()->get_db()->prepare("
  98. INSERT INTO data.team_unit (team, unit)
  99. VALUES (:team, :unit)
  100. ");
  101. $statement->bindValue(':team', $team_id, SQLITE3_TEXT);
  102. $statement->bindValue(':unit', $r["unit"], SQLITE3_TEXT);
  103. $statement->execute();
  104. }
  105. return 201;
  106. }
  107. ?>