Add_Team_Action.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. require_once(PATH::ACTION . "Action.php");
  12. /**
  13. * Logs the user out and redirects to the login page.
  14. *
  15. * Reads the POST parameters looking for the following KEYS:
  16. * player: The owner's player ID.
  17. * area_type: The area type ID ({@see AREA_TYPE_ID}).
  18. * area: The area ID.
  19. * stage: Optional, the stage ID.
  20. * difficulty: Optional, the difficulty ID ({@see DIFFICULTY_ID})
  21. * ids: Unit IDs of the party.
  22. * name: Team name.
  23. * description: Optional, team description.
  24. * Validates the data and creates the team in the database.
  25. *
  26. * @category Action
  27. * @todo: Document properly
  28. */
  29. class Add_Team_Action extends Action{
  30. /**
  31. * Executes the action.
  32. */
  33. function execute(){
  34. $player = filter_input(INPUT_POST, 'player');
  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. $leader_id = filter_input(INPUT_POST, 'leader');
  45. $total_front = filter_input(INPUT_POST, 'front');
  46. $description = filter_input(INPUT_POST, 'description');
  47. $score = 0;
  48. $id = preg_split("/[\s,]+/", $ids);
  49. $id_count = sizeof($id);
  50. if ($name == null || strlen($name) == 0){
  51. $this->code = 400;
  52. $this->message = "POST parameter 'name' not specified or invalid.";
  53. return;
  54. }
  55. switch ($area_type){
  56. case AREA_TYPE_ID::SCENARIO:
  57. if ($stage < 0 || $stage > 7){
  58. $this->code = 400;
  59. $this->message = "Invalid stage for the selected area.";
  60. return;
  61. }
  62. if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){
  63. $this->code = 400;
  64. $this->message = "Invalid difficulty for the selected area.";
  65. return;
  66. }
  67. if ($id_count < 1 || $id_count > 4){
  68. $this->code = 400;
  69. $this->message = "Invalid number of units for the selected area.";
  70. return;
  71. }
  72. if (!APPLICATION::valid_id("SCENARIO_ID", $area)){
  73. $this->code = 400;
  74. $this->message = "Invalid area.";
  75. return;
  76. }
  77. break;
  78. case AREA_TYPE_ID::CAIROS_DUNGEON:
  79. if ($stage < 0){
  80. $this->code = 400;
  81. $this->message = "Invalid stage for the selected area.";
  82. return;
  83. }
  84. if ($stage > 12 && (DUNGEON_ID::GIANTS_KEEP == $area || DUNGEON_ID::DRAGONS_LAIR == $area || DUNGEON_ID::NECROPOLIS == $area)){
  85. $this->code = 400;
  86. $this->message = "Invalid stage for the selected area.";
  87. return;
  88. }
  89. if ($stage > 10 && ($area != DUNGEON_ID::GIANTS_KEEP && $area != DUNGEON_ID::DRAGONS_LAIR && $area != DUNGEON_ID::NECROPOLIS)){
  90. $this->code = 400;
  91. $this->message = "Invalid stage for the selected area.";
  92. return;
  93. }
  94. $difficulty = null;
  95. if ($id_count < 1 || $id_count > 5){
  96. $this->code = 400;
  97. $this->message = "Invalid number of units for the selected area.";
  98. return;
  99. }
  100. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  101. $this->code = 400;
  102. $this->message = "Invalid area.";
  103. return;
  104. }
  105. break;
  106. case AREA_TYPE_ID::RIFT_DUNGEON:
  107. $stage = null;
  108. $difficulty = null;
  109. if ($id_count < 1 || $id_count > 6){
  110. $this->code = 400;
  111. $this->message = "Invalid number of units for the selected area.";
  112. return;
  113. }
  114. if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){
  115. $this->code = 400;
  116. $this->message = "Invalid area.";
  117. return;
  118. }
  119. break;
  120. case AREA_TYPE_ID::RIFT_RAID:
  121. if ($stage < 0 || $stage > 5){
  122. $this->code = 400;
  123. $this->message = "Invalid stage for the selected area.";
  124. return;
  125. }
  126. $difficulty = null;
  127. if ($id_count < 1 || $id_count > 6){
  128. $this->code = 400;
  129. $this->message = "Invalid number of units for the selected area.";
  130. return;
  131. }
  132. $area = null;
  133. break;
  134. case AREA_TYPE_ID::ARENA:
  135. $area = null;
  136. $stage = null;
  137. $difficulty = null;
  138. break;
  139. case AREA_TYPE_ID::GUILD_WAR:
  140. $area = null;
  141. $stage = null;
  142. $difficulty = null;
  143. break;
  144. case AREA_TYPE_ID::GUILD_SIEGE:
  145. $area = null;
  146. $stage = null;
  147. $difficulty = null;
  148. break;
  149. case AREA_TYPE_ID::TARTARUS_LABYRINTH:
  150. if ($difficulty != null && !APPLICATION::valid_id("DIFFICULTY_ID", $difficulty)){
  151. $this->code = 400;
  152. $this->message = "Invalid difficulty for the selected area.";
  153. return;
  154. }
  155. if ($id_count < 1 || $id_count > 5){
  156. $this->code = 400;
  157. $this->message = "Invalid number of units for the selected area.";
  158. return;
  159. }
  160. if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){
  161. $this->code = 400;
  162. $this->message = "Invalid area.";
  163. return;
  164. }
  165. break;
  166. case AREA_TYPE_ID::TRIAL_OF_ASCENSION:
  167. if ($difficulty != null && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  168. $difficulty = null;
  169. }
  170. $stage = null;
  171. if ($id_count < 1 || $id_count > 5){
  172. $this->code = 400;
  173. $this->message = "Invalid number of units for the selected area.";
  174. return;
  175. }
  176. $area = null;
  177. break;
  178. case AREA_TYPE_ID::DIMENSIONAL_HOLE:
  179. if ($stage < 0 || $stage > 5){
  180. $this->code = 400;
  181. $this->message = "Invalid stage for the selected area.";
  182. return;
  183. }
  184. $difficulty = null;
  185. if ($id_count < 1 || $id_count > 4){
  186. $this->code = 400;
  187. $this->message = "Invalid number of units for the selected area.";
  188. return;
  189. }
  190. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  191. $this->code = 400;
  192. $this->message = "Invalid area.";
  193. return;
  194. }
  195. break;
  196. case AREA_TYPE_ID::DIMENSIONAL_RIFT:
  197. if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  198. $this->code = 400;
  199. $this->message = "Invalid difficulty for the selected area.";
  200. return;
  201. }
  202. $stage = null;
  203. if ($id_count < 1 || $id_count > 5){
  204. $this->code = 400;
  205. $this->message = "Invalid number of units for the selected area.";
  206. return;
  207. }
  208. $area = null;
  209. break;
  210. default:
  211. return 400;
  212. }
  213. $unique_id = array_unique($id);
  214. if ($id_count != sizeof($unique_id)){
  215. $this->code = 400;
  216. $this->message = "Duplicated units in the team.";
  217. return;
  218. }
  219. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  220. $q = get_context()->get_db()->query($s);
  221. $r = $q->fetchArray(SQLITE3_ASSOC);
  222. $team_id = $r["id"];
  223. $statement = get_context()->get_db()->prepare("
  224. INSERT INTO team (
  225. player,
  226. id,
  227. name,
  228. description,
  229. area_type,
  230. area,
  231. stage,
  232. difficulty,
  233. score
  234. ) VALUES (
  235. :player,
  236. :id,
  237. :name,
  238. :description,
  239. :area_type,
  240. :area,
  241. :stage,
  242. :difficulty,
  243. :score
  244. );
  245. ");
  246. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  247. $statement->bindValue(':id', $team_id, SQLITE3_TEXT);
  248. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  249. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  250. $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT);
  251. $statement->bindValue(':area', $area, SQLITE3_TEXT);
  252. $statement->bindValue(':stage', $stage, SQLITE3_TEXT);
  253. $statement->bindValue(':difficulty', $difficulty, SQLITE3_TEXT);
  254. $statement->bindValue(':score', $score, SQLITE3_TEXT);
  255. $res = $statement->execute();
  256. if (! $res){
  257. $this->code = 500;
  258. $this->message = "Internal error.";
  259. return;
  260. }
  261. $j = 0;
  262. foreach (explode(',', $ids) as $i){
  263. $leader = 0;
  264. if ((string) $i == (string) $leader_id){
  265. $leader = 1;
  266. }
  267. $front = 0;
  268. if ($j < $total_front){
  269. $front = 1;
  270. }
  271. $statement = get_context()->get_db()->prepare("
  272. INSERT INTO team_unit (team, unit, leader, front)
  273. VALUES (:team, :unit, :leader, :front);
  274. ");
  275. $statement->bindValue(':team', $team_id, SQLITE3_TEXT);
  276. $statement->bindValue(':unit', $i, SQLITE3_TEXT);
  277. $statement->bindValue(':leader', $leader, SQLITE3_TEXT);
  278. $statement->bindValue(':front', $front, SQLITE3_TEXT);
  279. $statement->execute();
  280. $j ++;
  281. }
  282. $this->code = 204;
  283. $this->message = "No content.";
  284. return;
  285. }
  286. }