Enchantments_Page.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Enchantment list page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Custom_Filter.php");
  14. require_once(PATH::ENTITY . "Enchantment.php");
  15. /**
  16. * Enchantment list page model.
  17. *
  18. * @category Page
  19. */
  20. class Enchantments_Page extends Page{
  21. /**
  22. * @var \Enchantment[] Owned enchantment gems.
  23. */
  24. public $gems = [];
  25. /**
  26. * @var \Enchantment[] Owned enchantment grindstones.
  27. */
  28. public $grindstones = [];
  29. /**
  30. * @var \Filter[] Custom filters for this page.
  31. */
  32. public $custom_filters = [];
  33. /**
  34. * Constructor.
  35. *
  36. * Retrieves the data and initializes the variables.
  37. *
  38. * @global int Player ID.
  39. * @global resource Database connection.
  40. */
  41. public function __construct(){
  42. global $UID;
  43. global $db;
  44. $this->view = PATH::VIEW . "enchantments.php";
  45. $this->parse_filters();
  46. if (!isset($_GET["custom_filter"])){
  47. $s = $this->build_query();
  48. }
  49. else{
  50. $s = $this->build_custom_query($_GET["custom_filter"]);
  51. }
  52. $q = $db->query($s);
  53. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  54. $enchantment = new Enchantment($r["id"]);
  55. //if ($enchantment->gem == true){
  56. // array_push($this->gems, $enchantment);
  57. //}
  58. //else{
  59. array_push($this->grindstones, $enchantment);
  60. //}
  61. }
  62. $s = "
  63. SELECT id
  64. FROM filter
  65. WHERE
  66. uid = '$UID' AND
  67. page = 'ENCHANTMENTS';
  68. ";
  69. $q = $db->query($s);
  70. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  71. array_push($this->custom_filters, new Custom_Filter($r["id"]));
  72. }
  73. $this->title = "Enchantments - SWDB";
  74. $this->description = "Enchantment inventory";
  75. $this->canonical = URL::BASE . "enchantments/";
  76. }
  77. /**
  78. * Parses the request looking for the selected filters, validates them
  79. * and adds them to the $filters array.
  80. */
  81. private function parse_filters(){
  82. $this->filters = FILTER::ENCHANTMENT;
  83. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  84. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  85. }
  86. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  87. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  88. }
  89. if (isset($_GET["stat"])){
  90. $stats = $_GET["stat"];
  91. foreach ($stats as $stat){
  92. array_push($this->filters["STAT"], intval($stat));
  93. }
  94. }
  95. if (isset($_GET["rune"])){
  96. $runes = $_GET["rune"];
  97. foreach ($runes as $rune){
  98. array_push($this->filters["RUNE"], intval($rune));
  99. }
  100. }
  101. if (isset($_GET["type"])){
  102. $types = $_GET["type"];
  103. foreach ($types as $type){
  104. array_push($this->filters["TYPE"], $type);
  105. }
  106. }
  107. // TODO
  108. /*
  109. if (isset($_GET["archetype"])){
  110. $archetypes = $_GET["archetype"];
  111. foreach ($archetypes as $archetype){
  112. array_push($this->filters["ARCHETYPE"], intval($archetype));
  113. }
  114. }
  115. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  116. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  117. }
  118. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  119. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  120. }
  121. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  122. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  123. }
  124. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  125. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  126. }
  127. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  128. $this->filters["ASSIGNED"] = $_GET["assigned"];
  129. }*/
  130. }
  131. /**
  132. * Builds the query to the artifact table using the a custom filter.
  133. *
  134. * @param int $filter Custom filter ID.
  135. * @return string The query to be executed.
  136. * @global int Player ID.
  137. * @global resource Database connection.
  138. */
  139. private function build_custom_query($filter){
  140. global $UID;
  141. global $db;
  142. $s = "
  143. SELECT condition
  144. FROM filter
  145. WHERE
  146. uid = '$UID' AND
  147. id = $filter;
  148. ";
  149. $q = $db->query($s);
  150. $r = $q->fetchArray(SQLITE3_ASSOC);
  151. $cond = $r["condition"];
  152. $s = "
  153. SELECT
  154. id
  155. FROM enchantment
  156. WHERE uid = '$UID' AND (
  157. $cond
  158. )
  159. ORDER BY type, rune DESC, quality DESC;
  160. ";
  161. return $s;
  162. }
  163. /**
  164. * Builds the query to the artifact table using the selected or default
  165. * filters.
  166. *
  167. * @return string The query to be executed.
  168. * @global int Player ID.
  169. */
  170. private function build_query(){
  171. global $UID;
  172. $s = "
  173. SELECT id
  174. FROM enchantment
  175. WHERE
  176. uid = '$UID' AND
  177. quality >= " . $this->filters["MIN_QUALITY"] . " AND
  178. quality <= " . $this->filters["MAX_QUALITY"] . "
  179. ";
  180. if (count($this->filters["STAT"]) > 0){
  181. $s = $s . " AND stat IN ( ";
  182. foreach($this->filters["STAT"] as $t){
  183. $s = $s . "$t, ";
  184. }
  185. $s = $s . "-1) ";
  186. }
  187. if (count($this->filters["RUNE"]) > 0){
  188. $s = $s . " AND (rune IN ( ";
  189. foreach($this->filters["RUNE"] as $t){
  190. $s = $s . "'$t', ";
  191. }
  192. $s = $s . "'DUMMY0') ";
  193. if (in_array('0', $this->filters["RUNE"])){
  194. // Include inmemorials
  195. $s .= " OR type in (3, 4)) ";
  196. }
  197. else{
  198. $s .= ") ";
  199. }
  200. }
  201. if (count($this->filters["TYPE"]) == 1){
  202. // Only one selectd, it's either gems or grindstones
  203. if (in_array("GEM", $this->filters["TYPE"])){
  204. $s .= " AND type IN (1, 3, 5) "; // Gems.
  205. }
  206. else{
  207. $s .= " AND type IN (2, 4, 6) "; // Grindstones.
  208. }
  209. }
  210. $s = $s . "
  211. ORDER BY
  212. type = 3 DESC, -- Inmemorial Grindstone
  213. type = 4 DESC, -- Inmemorial Gem
  214. rune,
  215. type,
  216. quality DESC,
  217. stat
  218. ";
  219. /*if (count($this->filters["MAIN"]) > 0){
  220. $s = $s . " AND main_stat IN ( ";
  221. foreach($this->filters["MAIN"] as $t){
  222. $s = $s . "$t, ";
  223. }
  224. $s = $s . "-1) ";
  225. }
  226. if (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) == 0){
  227. // No element/archetype conditions
  228. }
  229. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) == 0){
  230. // Only selected elements
  231. $s = $s . " AND attribute IN ( ";
  232. foreach($this->filters["ELEMENT"] as $t){
  233. $s = $s . "$t, ";
  234. }
  235. $s = $s . "-1) ";
  236. }
  237. elseif (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) > 0){
  238. // Only selected types
  239. $s = $s . " AND archetype IN ( ";
  240. foreach($this->filters["ARCHETYPE"] as $t){
  241. $s = $s . "$t, ";
  242. }
  243. $s = $s . "-1) ";
  244. }
  245. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) > 0){
  246. // Selected elements and selected types
  247. $s = $s . " AND (archetype IN ( ";
  248. foreach($this->filters["ARCHETYPE"] as $t){
  249. $s = $s . "$t, ";
  250. }
  251. $s = $s . "-1) OR attribute IN ( ";
  252. foreach($this->filters["ELEMENT"] as $t){
  253. $s = $s . "$t, ";
  254. }
  255. $s = $s . "-1)) ";
  256. }
  257. switch ($this->filters["ASSIGNED"]){
  258. case 1:
  259. $s = $s . " AND assigned_to IS NOT NULL ";
  260. break;
  261. case 2:
  262. $s = $s . " AND assigned_to IS NULL ";
  263. break;
  264. case 3:
  265. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
  266. break;
  267. case 4:
  268. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
  269. break;
  270. }
  271. $s = $s . " ORDER BY type, attribute, archetype, level DESC, original_quality DESC;";
  272. */
  273. return $s;
  274. }
  275. }
  276. ?>