new_team.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. $leader_id = filter_input(INPUT_POST, 'leader');
  40. $total_front = filter_input(INPUT_POST, 'front');
  41. $description = filter_input(INPUT_POST, 'description');
  42. $id = preg_split("/[\s,]+/", $ids);
  43. $id_count = sizeof($id);
  44. if ($name == null || strlen($name) == 0){
  45. return -1;
  46. }
  47. switch ($area_type){
  48. case AREA_TYPE_ID::SCENARIO:
  49. if ($stage < 0 || $stage > 7){
  50. return -31;
  51. }
  52. if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){
  53. return -32;
  54. }
  55. if ($id_count < 1 || $id_count > 4){
  56. return -33;
  57. }
  58. if (!APPLICATION::valid_id("SCENARIO_ID", $area)){
  59. return -34;
  60. }
  61. break;
  62. case AREA_TYPE_ID::CAIROS_DUNGEON:
  63. if ($stage < 0){
  64. return -35;
  65. }
  66. if ($stage > 12 && ($stage == DUNGEON_ID::GIANTS_KEEP || $stage == DUNGEON_ID::DRAGONS_LAIR || $stage == DUNGEON_ID::NECROPOLIS)){
  67. return -35;
  68. }
  69. if ($stage > 10 && $stage =! DUNGEON_ID::GIANTS_KEEP && $stage =! DUNGEON_ID::DRAGONS_LAIR && $stage != DUNGEON_ID::NECROPOLIS){
  70. return -35;
  71. }
  72. $difficulty = null;
  73. if ($id_count < 1 || $id_count > 5){
  74. return -36;
  75. }
  76. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  77. return -37;
  78. }
  79. break;
  80. case AREA_TYPE_ID::RIFT_DUNGEON:
  81. $stage = null;
  82. $difficulty = null;
  83. if ($id_count < 1 || $id_count > 6){
  84. return -39;
  85. }
  86. if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){
  87. return -40;
  88. }
  89. break;
  90. case AREA_TYPE_ID::RIFT_RAID:
  91. if ($stage < 0 || $stage > 5){
  92. return -41;
  93. }
  94. $difficulty = null;
  95. if ($id_count < 1 || $id_count > 6){
  96. return -42;
  97. }
  98. $area = null;
  99. break;
  100. case AREA_TYPE_ID::ARENA:
  101. $area = null;
  102. $stage = null;
  103. $difficulty = null;
  104. break;
  105. case AREA_TYPE_ID::GUILD_WAR:
  106. $area = null;
  107. $stage = null;
  108. $difficulty = null;
  109. break;
  110. case AREA_TYPE_ID::GUILD_SIEGE:
  111. $area = null;
  112. $stage = null;
  113. $difficulty = null;
  114. break;
  115. case AREA_TYPE_ID::TARTARUS_LABYRINTH:
  116. if ($difficulty != null && !APPLICATION::valid_id("DIFFICULTY_ID", $difficulty)){
  117. return -43;
  118. }
  119. if ($id_count < 1 || $id_count > 5){
  120. return -44;
  121. }
  122. if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){
  123. return -45;
  124. }
  125. break;
  126. case AREA_TYPE_ID::TRIAL_OF_ASCENSION:
  127. if ($difficulty != null && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  128. $difficulty = null;
  129. }
  130. $stage = null;
  131. if ($id_count < 1 || $id_count > 5){
  132. return -47;
  133. }
  134. $area = null;
  135. break;
  136. case AREA_TYPE_ID::DIMENSIONAL_HOLE:
  137. if ($stage < 0 || $stage > 5){
  138. return -48;
  139. }
  140. $difficulty = null;
  141. if ($id_count < 1 || $id_count > 4){
  142. return -49;
  143. }
  144. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  145. return -50;
  146. }
  147. break;
  148. case AREA_TYPE_ID::DIMENSIONAL_RIFT:
  149. if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  150. return -51;
  151. }
  152. $stage = null;
  153. if ($id_count < 1 || $id_count > 5){
  154. return -52;
  155. }
  156. $area = null;
  157. break;
  158. default:
  159. return -3;
  160. }
  161. $unique_id = array_unique($id);
  162. if ($id_count != sizeof($unique_id)){
  163. return -4;
  164. }
  165. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  166. $q = $db->query($s);
  167. $r = $q->fetchArray(SQLITE3_ASSOC);
  168. $team_id = $r["id"];
  169. $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);');
  170. $statement->bindValue(':uid', $player);
  171. $statement->bindValue(':id', $team_id);
  172. $statement->bindValue(':name', $name);
  173. $statement->bindValue(':description', $description);
  174. $statement->bindValue(':area_type', $area_type);
  175. $statement->bindValue(':area', $area);
  176. $statement->bindValue(':stage', $stage);
  177. $statement->bindValue(':difficulty', $difficulty);
  178. $res = $statement->execute();
  179. if (!$res){
  180. return -5;
  181. }
  182. $j = 0;
  183. foreach ($id as $i){
  184. $leader = 0;
  185. if ((string) $i == (string) $leader_id){
  186. $leader = 1;
  187. }
  188. $front = 0;
  189. if ($j < $total_front){
  190. $front = 1;
  191. }
  192. $s = "INSERT INTO team_unit VALUES ($team_id, $i, $leader, $front);";
  193. $db->query($s);
  194. $j ++;
  195. }
  196. return 0;
  197. }
  198. ?>