save_run_team.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * @category Action
  8. */
  9. /**
  10. * Saves a run party as a team.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * uid
  14. * run
  15. * name
  16. * description
  17. * Validates the data and creates the team in the database.
  18. *
  19. * @return int 0 on success, negative values on error.
  20. * @category Action
  21. * @global resource Database connection.
  22. */
  23. function action(){
  24. global $db;
  25. $player = filter_input(INPUT_POST, 'uid');
  26. $run = filter_input(INPUT_POST, 'run');
  27. $name = filter_input(INPUT_POST, 'name');
  28. $description = filter_input(INPUT_POST, 'description');
  29. if ($name == null || strlen($name) == 0){
  30. return -1;
  31. }
  32. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  33. $q = $db->query($s);
  34. $r = $q->fetchArray(SQLITE3_ASSOC);
  35. $team_id = $r["id"];
  36. $statement = $db->prepare("
  37. SELECT
  38. run.area AS area,
  39. run.stage AS stage,
  40. run.difficulty AS difficulty,
  41. k_area.type AS area_type
  42. FROM
  43. run,
  44. k_area
  45. WHERE
  46. run.uid = :uid AND
  47. run.id = :run AND
  48. k_area.id = run.area AND
  49. run.helper = 0;
  50. ");
  51. $statement->bindValue(':uid', $player, SQLITE3_INTEGER);
  52. $statement->bindValue(':run', $run, SQLITE3_INTEGER);
  53. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  54. if (!$r){
  55. return -2;
  56. }
  57. $statement = $db->prepare('INSERT INTO team (uid, id, name, description, area_type, area, stage, difficulty, score) VALUES (:uid, :id, :name, :description, :area_type, :area, :stage, :difficulty, :score);');
  58. $statement->bindValue(':uid', $player, SQLITE3_INTEGER);
  59. $statement->bindValue(':id', $team_id, SQLITE3_INTEGER);
  60. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  61. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  62. $statement->bindValue(':area_type', $r["area_type"], SQLITE3_INTEGER);
  63. $statement->bindValue(':area', $r["area"], SQLITE3_INTEGER);
  64. $statement->bindValue(':stage', $r["stage"], SQLITE3_INTEGER);
  65. $statement->bindValue(':difficulty', $r["difficulty"], SQLITE3_INTEGER);
  66. $statement->bindValue(':score', 0, SQLITE3_INTEGER);
  67. $res = $statement->execute();
  68. if (!$res){
  69. return -3;
  70. }
  71. $s = "SELECT unit FROM run_party WHERE run = $run AND unit IS NOT NULL;";
  72. $q = $db->query($s);
  73. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  74. $statement = $db->prepare("
  75. INSERT INTO team_unit (team, unit)
  76. VALUES (:team, :unit)
  77. ");
  78. $statement->bindValue(':team', $team_id, SQLITE3_INTEGER);
  79. $statement->bindValue(':unit', $r["unit"], SQLITE3_INTEGER);
  80. $statement->execute();
  81. }
  82. return 0;
  83. }
  84. ?>