new_team.php 6.6 KB

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