new_team.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * File for the team creation action.
  4. *
  5. * Implements an action function to be called from the {@see Controller}.
  6. *
  7. * @category Action
  8. */
  9. /**
  10. * Logs the user out and redirects to the login page.
  11. *
  12. * Reads the POST parameters looking for the following KEYS:
  13. * uid
  14. * area_type
  15. * area
  16. * stage
  17. * difficulty
  18. * ids
  19. * name
  20. * description
  21. * Validates the data and creates the team in the database.
  22. *
  23. * @return int 0 on success, negative values on error.
  24. * @category Action
  25. * @global resource Database connection.
  26. */
  27. function action(){
  28. global $db;
  29. $player = filter_input(INPUT_POST, 'uid');
  30. $area_type = filter_input(INPUT_POST, 'area_type');
  31. $area = filter_input(INPUT_POST, 'area');
  32. $stage = filter_input(INPUT_POST, 'stage');
  33. $difficulty = filter_input(INPUT_POST, 'difficulty');
  34. if ($difficulty == ''){
  35. $difficulty = null;
  36. }
  37. $ids = filter_input(INPUT_POST, 'ids');
  38. $name = filter_input(INPUT_POST, 'name');
  39. $description = filter_input(INPUT_POST, 'description');
  40. $id = preg_split("/[\s,]+/", $ids);
  41. $id_count = sizeof($id);
  42. if ($name == null || strlen($name) == 0){
  43. return -1;
  44. }
  45. switch ($area_type){
  46. case AREA_TYPE_ID::SCENARIO:
  47. if ($stage < 0 || $stage > 7){
  48. return -31;
  49. }
  50. if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){
  51. return -32;
  52. }
  53. if ($id_count < 1 || $id_count > 4){
  54. return -33;
  55. }
  56. if (!APPLICATION::valid_id("SCENARIO_ID", $area)){
  57. return -34;
  58. }
  59. break;
  60. case AREA_TYPE_ID::CAIROS_DUNGEON:
  61. if ($stage < 0 || $stage > 10){
  62. return -35;
  63. }
  64. $difficulty = null;
  65. if ($id_count < 1 || $id_count > 5){
  66. return -36;
  67. }
  68. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  69. return -37;
  70. }
  71. break;
  72. case AREA_TYPE_ID::RIFT_DUNGEON:
  73. $stage = null;
  74. $difficulty = null;
  75. if ($id_count < 1 || $id_count > 6){
  76. return -39;
  77. }
  78. if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){
  79. return -40;
  80. }
  81. break;
  82. case AREA_TYPE_ID::RIFT_RAID:
  83. if ($stage < 0 || $stage > 5){
  84. return -41;
  85. }
  86. $difficulty = null;
  87. if ($id_count < 1 || $id_count > 6){
  88. return -42;
  89. }
  90. $area = null;
  91. break;
  92. case AREA_TYPE_ID::ARENA:
  93. $area = null;
  94. $stage = null;
  95. $difficulty = null;
  96. break;
  97. case AREA_TYPE_ID::GUILD_WAR:
  98. $area = null;
  99. $stage = null;
  100. $difficulty = null;
  101. break;
  102. case AREA_TYPE_ID::GUILD_SIEGE:
  103. $area = null;
  104. $stage = null;
  105. $difficulty = null;
  106. break;
  107. case AREA_TYPE_ID::TARTARUS_LABYRINTH:
  108. if ($difficulty != null && !APPLICATION::valid_id("DIFFICULTY_ID", $difficulty)){
  109. return -43;
  110. }
  111. if ($id_count < 1 || $id_count > 5){
  112. return -44;
  113. }
  114. if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){
  115. return -45;
  116. }
  117. break;
  118. case AREA_TYPE_ID::TRIAL_OF_ASCENSION:
  119. if ($difficulty != null && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  120. $difficulty = null;
  121. }
  122. $stage = null;
  123. if ($id_count < 1 || $id_count > 5){
  124. return -47;
  125. }
  126. $area = null;
  127. break;
  128. case AREA_TYPE_ID::DIMENSIONAL_HOLE:
  129. if ($stage < 0 || $stage > 5){
  130. return -48;
  131. }
  132. $difficulty = null;
  133. if ($id_count < 1 || $id_count > 4){
  134. return -49;
  135. }
  136. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  137. return -50;
  138. }
  139. break;
  140. case AREA_TYPE_ID::DIMENSIONAL_RIFT:
  141. if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  142. return -51;
  143. }
  144. $stage = null;
  145. if ($id_count < 1 || $id_count > 5){
  146. return -52;
  147. }
  148. $area = null;
  149. break;
  150. default:
  151. return -3;
  152. }
  153. $unique_id = array_unique($id);
  154. if ($id_count != sizeof($unique_id)){
  155. return -4;
  156. }
  157. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  158. $q = $db->query($s);
  159. $r = $q->fetchArray(SQLITE3_ASSOC);
  160. $team_id = $r["id"];
  161. $statement = $db->prepare('INSERT INTO team (uid, id, name, description, area_type, area, stage, difficulty) VALUES (:uid, :id, :name, :description, :area_type, :area, :stage, :difficulty);');
  162. $statement->bindValue(':uid', $player);
  163. $statement->bindValue(':id', $team_id);
  164. $statement->bindValue(':name', $name);
  165. $statement->bindValue(':description', $description);
  166. $statement->bindValue(':area_type', $area_type);
  167. $statement->bindValue(':area', $area);
  168. $statement->bindValue(':stage', $stage);
  169. $statement->bindValue(':difficulty', $difficulty);
  170. error_log($statement);
  171. $res = $statement->execute();
  172. if (!$res){
  173. return -5;
  174. }
  175. foreach ($id as $i){
  176. // TODO: Leader and front
  177. $s = "INSERT INTO team_unit VALUES ($team_id, $i, 0, 0);";
  178. $db->query($s);
  179. }
  180. return 0;
  181. }
  182. ?>