new_team.php 7.9 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. * @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. * player
  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. */
  26. function action(){
  27. $player = filter_input(INPUT_POST, 'player');
  28. $area_type = filter_input(INPUT_POST, 'area_type');
  29. $area = filter_input(INPUT_POST, 'area');
  30. $stage = filter_input(INPUT_POST, 'stage');
  31. $difficulty = filter_input(INPUT_POST, 'difficulty');
  32. if ($difficulty == ''){
  33. $difficulty = null;
  34. }
  35. $ids = filter_input(INPUT_POST, 'ids');
  36. $name = filter_input(INPUT_POST, 'name');
  37. $leader_id = filter_input(INPUT_POST, 'leader');
  38. $total_front = filter_input(INPUT_POST, 'front');
  39. $description = filter_input(INPUT_POST, 'description');
  40. $score = 0;
  41. $id = preg_split("/[\s,]+/", $ids);
  42. $id_count = sizeof($id);
  43. if ($name == null || strlen($name) == 0){
  44. return -1;
  45. }
  46. switch ($area_type){
  47. case AREA_TYPE_ID::SCENARIO:
  48. if ($stage < 0 || $stage > 7){
  49. return -31;
  50. }
  51. if (!($difficulty == 0 || APPLICATION::valid_id("DIFFICULTY_ID", $difficulty) || $difficulty == DIFFICULTY_ID::EASY)){
  52. return -32;
  53. }
  54. if ($id_count < 1 || $id_count > 4){
  55. return -33;
  56. }
  57. if (!APPLICATION::valid_id("SCENARIO_ID", $area)){
  58. return -34;
  59. }
  60. break;
  61. case AREA_TYPE_ID::CAIROS_DUNGEON:
  62. if ($stage < 0){
  63. return -35;
  64. }
  65. if ($stage > 12 && (DUNGEON_ID::GIANTS_KEEP == $area || DUNGEON_ID::DRAGONS_LAIR == $area || DUNGEON_ID::NECROPOLIS == $area)){
  66. return -35;
  67. }
  68. if ($stage > 10 && ($area != DUNGEON_ID::GIANTS_KEEP && $area != DUNGEON_ID::DRAGONS_LAIR && $area != DUNGEON_ID::NECROPOLIS)){
  69. return -35;
  70. }
  71. $difficulty = null;
  72. if ($id_count < 1 || $id_count > 5){
  73. return -36;
  74. }
  75. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  76. return -37;
  77. }
  78. break;
  79. case AREA_TYPE_ID::RIFT_DUNGEON:
  80. $stage = null;
  81. $difficulty = null;
  82. if ($id_count < 1 || $id_count > 6){
  83. return -39;
  84. }
  85. if (!APPLICATION::valid_id("ELEMENTAL_RIFT_DUNGEON_ID", $area)){
  86. return -40;
  87. }
  88. break;
  89. case AREA_TYPE_ID::RIFT_RAID:
  90. if ($stage < 0 || $stage > 5){
  91. return -41;
  92. }
  93. $difficulty = null;
  94. if ($id_count < 1 || $id_count > 6){
  95. return -42;
  96. }
  97. $area = null;
  98. break;
  99. case AREA_TYPE_ID::ARENA:
  100. $area = null;
  101. $stage = null;
  102. $difficulty = null;
  103. break;
  104. case AREA_TYPE_ID::GUILD_WAR:
  105. $area = null;
  106. $stage = null;
  107. $difficulty = null;
  108. break;
  109. case AREA_TYPE_ID::GUILD_SIEGE:
  110. $area = null;
  111. $stage = null;
  112. $difficulty = null;
  113. break;
  114. case AREA_TYPE_ID::TARTARUS_LABYRINTH:
  115. if ($difficulty != null && !APPLICATION::valid_id("DIFFICULTY_ID", $difficulty)){
  116. return -43;
  117. }
  118. if ($id_count < 1 || $id_count > 5){
  119. return -44;
  120. }
  121. if (!APPLICATION::valid_id("LABYRINTH_STAGES_ID", $stage)){
  122. return -45;
  123. }
  124. break;
  125. case AREA_TYPE_ID::TRIAL_OF_ASCENSION:
  126. if ($difficulty != null && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  127. $difficulty = null;
  128. }
  129. $stage = null;
  130. if ($id_count < 1 || $id_count > 5){
  131. return -47;
  132. }
  133. $area = null;
  134. break;
  135. case AREA_TYPE_ID::DIMENSIONAL_HOLE:
  136. if ($stage < 0 || $stage > 5){
  137. return -48;
  138. }
  139. $difficulty = null;
  140. if ($id_count < 1 || $id_count > 4){
  141. return -49;
  142. }
  143. if (!APPLICATION::valid_id("DUNGEON_ID", $area)){
  144. return -50;
  145. }
  146. break;
  147. case AREA_TYPE_ID::DIMENSIONAL_RIFT:
  148. if ($difficulty != 0 && $difficulty != DIFFICULTY_ID::NORMAL && $difficulty != DIFFICULTY_ID::HARD){
  149. return -51;
  150. }
  151. $stage = null;
  152. if ($id_count < 1 || $id_count > 5){
  153. return -52;
  154. }
  155. $area = null;
  156. break;
  157. default:
  158. return -3;
  159. }
  160. $unique_id = array_unique($id);
  161. if ($id_count != sizeof($unique_id)){
  162. return -4;
  163. }
  164. $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
  165. $q = get_context()->get_db()->query($s);
  166. $r = $q->fetchArray(SQLITE3_ASSOC);
  167. $team_id = $r["id"];
  168. $statement = get_context()->get_db()->prepare("
  169. INSERT INTO team (
  170. player,
  171. id,
  172. name,
  173. description,
  174. area_type,
  175. area,
  176. stage,
  177. difficulty,
  178. score
  179. ) VALUES (
  180. :player,
  181. :id,
  182. :name,
  183. :description,
  184. :area_type,
  185. :area,
  186. :stage,
  187. :difficulty,
  188. :score
  189. );
  190. ");
  191. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  192. $statement->bindValue(':id', $team_id, SQLITE3_TEXT);
  193. $statement->bindValue(':name', $name, SQLITE3_TEXT);
  194. $statement->bindValue(':description', $description, SQLITE3_TEXT);
  195. $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT);
  196. $statement->bindValue(':area', $area, SQLITE3_TEXT);
  197. $statement->bindValue(':stage', $stage, SQLITE3_TEXT);
  198. $statement->bindValue(':difficulty', $difficulty, SQLITE3_TEXT);
  199. $statement->bindValue(':score', $score, SQLITE3_TEXT);
  200. $res = $statement->execute();
  201. if (!$res){
  202. return -5;
  203. }
  204. $j = 0;
  205. foreach (explode(',', $ids) as $i){
  206. $leader = 0;
  207. if ((string) $i == (string) $leader_id){
  208. $leader = 1;
  209. }
  210. $front = 0;
  211. if ($j < $total_front){
  212. $front = 1;
  213. }
  214. $statement = get_context()->get_db()->prepare("
  215. INSERT INTO team_unit (team, unit, leader, front)
  216. VALUES (:team, :unit, :leader, :front);
  217. ");
  218. $statement->bindValue(':team', $team_id, SQLITE3_TEXT);
  219. $statement->bindValue(':unit', $i, SQLITE3_TEXT);
  220. $statement->bindValue(':leader', $leader, SQLITE3_TEXT);
  221. $statement->bindValue(':front', $front, SQLITE3_TEXT);
  222. $statement->execute();
  223. $j ++;
  224. }
  225. return 0;
  226. }
  227. ?>