new_team.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. /**
  12. * Logs the user out and redirects to the login page.
  13. *
  14. * Reads the POST parameters looking for the following KEYS:
  15. * player: The owner's player ID.
  16. * area_type: The area type ID ({@see AREA_TYPE_ID}).
  17. * area: The area ID.
  18. * stage: Optional, the stage ID.
  19. * difficulty: Optional, the difficulty ID ({@see DIFFICULTY_ID})
  20. * ids: Unit IDs of the party.
  21. * name: Team name.
  22. * description: Optional, team description.
  23. * Validates the data and creates the team in the database.
  24. *
  25. * @return int 201 on success, HTTP error status codes on failure.
  26. * @category Action
  27. */
  28. function action(){
  29. $player = filter_input(INPUT_POST, 'player');
  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 400;
  47. }
  48. switch ($area_type){
  49. case AREA_TYPE_ID::SCENARIO:
  50. if ($stage < 0 || $stage > 7){
  51. return 400;
  52. }
  53. if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){
  54. return 400;
  55. }
  56. if ($id_count < 1 || $id_count > 4){
  57. return 400;
  58. }
  59. if (!APPLICATION::valid_id("SCENARIO_ID", $area)){
  60. return 400;
  61. }
  62. break;
  63. case AREA_TYPE_ID::CAIROS_DUNGEON:
  64. if ($stage < 0){
  65. return 400;
  66. }
  67. if ($stage > 12 && (DUNGEON_ID::GIANTS_KEEP == $area || DUNGEON_ID::DRAGONS_LAIR == $area || DUNGEON_ID::NECROPOLIS == $area)){
  68. return 400;
  69. }
  70. if ($stage > 10 && ($area != DUNGEON_ID::GIANTS_KEEP && $area != DUNGEON_ID::DRAGONS_LAIR && $area != DUNGEON_ID::NECROPOLIS)){
  71. return 400;
  72. }
  73. $difficulty = null;
  74. if ($id_count < 1 || $id_count > 5){
  75. return 400;
  76. }
  77. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  78. return 400;
  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 400;
  86. }
  87. if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){
  88. return 400;
  89. }
  90. break;
  91. case AREA_TYPE_ID::RIFT_RAID:
  92. if ($stage < 0 || $stage > 5){
  93. return 400;
  94. }
  95. $difficulty = null;
  96. if ($id_count < 1 || $id_count > 6){
  97. return 400;
  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 400;
  119. }
  120. if ($id_count < 1 || $id_count > 5){
  121. return 400;
  122. }
  123. if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){
  124. return 400;
  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 400;
  134. }
  135. $area = null;
  136. break;
  137. case AREA_TYPE_ID::DIMENSIONAL_HOLE:
  138. if ($stage < 0 || $stage > 5){
  139. return 400;
  140. }
  141. $difficulty = null;
  142. if ($id_count < 1 || $id_count > 4){
  143. return 400;
  144. }
  145. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  146. return 400;
  147. }
  148. break;
  149. case AREA_TYPE_ID::DIMENSIONAL_RIFT:
  150. if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  151. return 400;
  152. }
  153. $stage = null;
  154. if ($id_count < 1 || $id_count > 5){
  155. return 400;
  156. }
  157. $area = null;
  158. break;
  159. default:
  160. return 400;
  161. }
  162. $unique_id = array_unique($id);
  163. if ($id_count != sizeof($unique_id)){
  164. return 400;
  165. }
  166. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  167. $q = get_context()->get_db()->query($s);
  168. $r = $q->fetchArray(SQLITE3_ASSOC);
  169. $team_id = $r["id"];
  170. $statement = get_context()->get_db()->prepare("
  171. INSERT INTO team (
  172. player,
  173. id,
  174. name,
  175. description,
  176. area_type,
  177. area,
  178. stage,
  179. difficulty,
  180. score
  181. ) VALUES (
  182. :player,
  183. :id,
  184. :name,
  185. :description,
  186. :area_type,
  187. :area,
  188. :stage,
  189. :difficulty,
  190. :score
  191. );
  192. ");
  193. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  194. $statement->bindValue(':id', $team_id, SQLITE3_TEXT);
  195. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  196. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  197. $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT);
  198. $statement->bindValue(':area', $area, SQLITE3_TEXT);
  199. $statement->bindValue(':stage', $stage, SQLITE3_TEXT);
  200. $statement->bindValue(':difficulty', $difficulty, SQLITE3_TEXT);
  201. $statement->bindValue(':score', $score, SQLITE3_TEXT);
  202. $res = $statement->execute();
  203. if (!$res){
  204. return 500;
  205. }
  206. $j = 0;
  207. foreach (explode(',', $ids) as $i){
  208. $leader = 0;
  209. if ((string) $i == (string) $leader_id){
  210. $leader = 1;
  211. }
  212. $front = 0;
  213. if ($j < $total_front){
  214. $front = 1;
  215. }
  216. $statement = get_context()->get_db()->prepare("
  217. INSERT INTO team_unit (team, unit, leader, front)
  218. VALUES (:team, :unit, :leader, :front);
  219. ");
  220. $statement->bindValue(':team', $team_id, SQLITE3_TEXT);
  221. $statement->bindValue(':unit', $i, SQLITE3_TEXT);
  222. $statement->bindValue(':leader', $leader, SQLITE3_TEXT);
  223. $statement->bindValue(':front', $front, SQLITE3_TEXT);
  224. $statement->execute();
  225. $j ++;
  226. }
  227. return 201;
  228. }