new_team.php 7.2 KB

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