Add_Team_Action.php 11 KB

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