new_team.php 6.7 KB

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