Report_Runeupgrade_Page.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * Upgradeable runes report page model.
  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 . "Unit.php");
  15. require_once(PATH::ENTITY . "Rune.php");
  16. /**
  17. * Upgradeable runes report page model.
  18. *
  19. * @category Page
  20. */
  21. class Report_Runeupgrade_Page extends Page{
  22. /**
  23. * @var \Filter[] Custom filters for this page.
  24. */
  25. public $custom_filters = [];
  26. /**
  27. * @var mixed[] List of available upgrades.
  28. *
  29. * Can be described as:
  30. * $upgrades = [
  31. * [
  32. * unit (Unit),
  33. * upgrade = [
  34. * [
  35. * rune (Rune),
  36. * alternative = [(Rune)]
  37. * ]
  38. * ]
  39. * ]
  40. * ]
  41. */
  42. public $upgrades = [];
  43. /**
  44. * Constructor.
  45. *
  46. * Retrieves the data and initializes the variables.
  47. *
  48. * @param resource $db Connection to the database.
  49. * @global int Player ID.
  50. */
  51. public function __construct($db){
  52. global $UID;
  53. parent::__construct($db);
  54. $this->view = PATH::VIEW . "report_runeupgrade.php";
  55. $this->parse_filters();
  56. if (!isset($_GET["custom_filter"])){
  57. $s = $this->build_query();
  58. }
  59. else{
  60. $s = $this->build_custom_query($_GET["custom_filter"]);
  61. }
  62. $q = $this->db->query($s);
  63. $prev_monster = "";
  64. $prev_rune = "";
  65. $upgrade = null;
  66. $rupgrade = null;
  67. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  68. if ($r["unit"] != $prev_monster){
  69. if ($prev_rune != ""){
  70. array_push($upgrade["upgrade"], $rupgrade);
  71. }
  72. if ($prev_monster != ""){
  73. array_push($this->upgrades, $upgrade);
  74. }
  75. $prev_monster = $r["unit"];
  76. $prev_rune = "";
  77. $upgrade = [
  78. "unit" => new Unit($this->db, $r["unit"]),
  79. "upgrade" => [],
  80. ];
  81. $rupgrade = [
  82. "rune" => new Rune($this->db, $r["id"]),
  83. "alternative" => [],
  84. ];
  85. }
  86. if ($r["id"] != $prev_rune){
  87. if ($prev_rune != ""){
  88. array_push($upgrade["upgrade"], $rupgrade);
  89. }
  90. $prev_rune = $r["id"];
  91. $rupgrade = [
  92. "rune" => new Rune($this->db, $r["id"]),
  93. "alternative" => [],
  94. ];
  95. }
  96. $rune_alt = new Rune($this->db, $r["alternative"]);
  97. // If the alternative rune isassigned
  98. if ($this->filters["ALTERNATIVE_SOURCE"] > 0 && strlen($rune_alt->assigned_to) > 0){
  99. $rune_alt->assigned_to = new Unit($this->db, $rune_alt->assigned_to);
  100. }
  101. array_push($rupgrade["alternative"], $rune_alt);
  102. }
  103. // Last entries
  104. array_push($upgrade["upgrade"], $rupgrade);
  105. array_push($this->upgrades, $upgrade);
  106. // Get custom filters for the form
  107. $s = "
  108. SELECT id
  109. FROM filter
  110. WHERE
  111. uid = '$UID' AND
  112. page = 'REPORT_RUNEUPGRADE';
  113. ";
  114. $q = $this->db->query($s);
  115. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  116. array_push($this->custom_filters, new Custom_Filter($this->db, $r["id"]));
  117. }
  118. $this->title = "Replaceable runes - SWDB";
  119. $this->description = "Find equiped runes that can be substituted for better ones.";
  120. $this->canonical = URL::BASE . "report/runeupgrade/";
  121. }
  122. /**
  123. * Parses the filters passed in the request and sets $filters.
  124. * Filters must be in $_GET, nd the available ones are max_stars (1-6),
  125. * min_stars (1-6) and in_storage (on/off).
  126. */
  127. private function parse_filters(){
  128. $this->filters = FILTER::RUNEUPGRADE;
  129. // Monster
  130. if (isset($_GET["monster_min_stars"]) && intval($_GET["monster_min_stars"]) > 0 && intval($_GET["monster_min_stars"]) < 7){
  131. $this->filters["MONSTER_MIN_STARS"] = $_GET["monster_min_stars"];
  132. }
  133. if (isset($_GET["monster_max_stars"]) && intval($_GET["monster_max_stars"]) > 0 && intval($_GET["monster_max_stars"]) < 7){
  134. $this->filters["MONSTER_MAX_STARS"] = $_GET["monster_max_stars"];
  135. }
  136. if (isset($_GET["monster_in_storage"]) && $_GET["monster_in_storage"] == "on"){
  137. $this->filters["MONSTER_IN_STORAGE"] = true;
  138. }
  139. else{
  140. $this->filters["MONSTER_IN_STORAGE"] = false;
  141. }
  142. if (isset($_GET["monster_name"]) && strlen($_GET["monster_name"]) > 0){
  143. $this->filters["MONSTER_NAME"] = SQLite3::escapeString($_GET["monster_name"]);
  144. }
  145. // Runes
  146. if (isset($_GET["rune_min_stars"]) && intval($_GET["rune_min_stars"]) > 0 && intval($_GET["rune_min_stars"]) < 7){
  147. $this->filters["RUNE_MIN_STARS"] = $_GET["rune_min_stars"];
  148. }
  149. if (isset($_GET["rune_max_stars"]) && intval($_GET["rune_max_stars"]) > 0 && intval($_GET["rune_max_stars"]) < 7){
  150. $this->filters["RUNE_MAX_STARS"] = $_GET["rune_max_stars"];
  151. }
  152. if (isset($_GET["rune_min_level"]) && intval($_GET["rune_min_level"]) >= 0 && intval($_GET["rune_min_level"]) <= 15){
  153. $this->filters["RUNE_MIN_LEVEL"] = $_GET["rune_min_level"];
  154. }
  155. if (isset($_GET["rune_max_level"]) && intval($_GET["rune_max_level"]) >= 0 && intval($_GET["rune_max_level"]) <= 15){
  156. $this->filters["RUNE_MAX_LEVEL"] = $_GET["rune_max_level"];
  157. }
  158. if (isset($_GET["rune_min_quality"]) && intval($_GET["rune_min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_quality"]) <= QUALITY_ID::LEGEND){
  159. $this->filters["RUNE_MIN_QUALITY"] = $_GET["rune_min_quality"];
  160. }
  161. if (isset($_GET["rune_max_quality"]) && intval($_GET["rune_max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_quality"]) <= QUALITY_ID::LEGEND){
  162. $this->filters["RUNE_MAX_QUALITY"] = $_GET["rune_max_quality"];
  163. }
  164. if (isset($_GET["rune_min_original_quality"]) && intval($_GET["rune_min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_original_quality"]) <= QUALITY_ID::LEGEND){
  165. $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] = $_GET["rune_min_original_quality"];
  166. }
  167. if (isset($_GET["rune_max_original_quality"]) && intval($_GET["rune_max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_original_quality"]) <= QUALITY_ID::LEGEND){
  168. $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] = $_GET["rune_max_original_quality"];
  169. }
  170. if (isset($_GET["rune_min_efficiency"]) && intval($_GET["rune_min_efficiency"]) >= 0 && intval($_GET["rune_min_efficiency"]) <= 100){
  171. $this->filters["RUNE_MIN_EFFICIENCY"] = $_GET["rune_min_efficiency"];
  172. }
  173. if (isset($_GET["rune_max_efficiency"]) && intval($_GET["rune_max_efficiency"]) >= 0 && intval($_GET["rune_max_efficiency"]) <= 100){
  174. $this->filters["RUNE_MAX_EFFICIENCY"] = $_GET["rune_max_efficiency"];
  175. }
  176. if (isset($_GET["rune_min_max_efficiency"]) && intval($_GET["rune_min_max_efficiency"]) >= 0 && intval($_GET["rune_min_max_efficiency"]) <= 100){
  177. $this->filters["RUNE_MIN_MAX_EFFICIENCY"] = $_GET["rune_min_max_efficiency"];
  178. }
  179. if (isset($_GET["rune_max_max_efficiency"]) && intval($_GET["rune_max_max_efficiency"]) >= 0 && intval($_GET["rune_max_max_efficiency"]) <= 100){
  180. $this->filters["RUNE_MAX_MAX_EFFICIENCY"] = $_GET["rune_max_max_efficiency"];
  181. }
  182. if (isset($_GET["rune_slot"])){
  183. $slots = $_GET["rune_slot"];
  184. foreach ($slots as $slot){
  185. if (intval($slot) >= 1 && intval($slot) < 7){
  186. array_push($this->filters["RUNE_SLOT"], intval($slot));
  187. }
  188. }
  189. }
  190. // Alternatives
  191. if (isset($_GET["alternative_min_stars"]) && intval($_GET["alternative_min_stars"]) > 0 && intval($_GET["alternative_min_stars"]) < 7){
  192. $this->filters["ALTERNATIVE_MIN_STARS"] = $_GET["alternative_min_stars"];
  193. }
  194. if (isset($_GET["alternative_min_level"]) && intval($_GET["alternative_min_level"]) >= 0 && intval($_GET["alternative_min_level"]) <= 15){
  195. $this->filters["ALTERNATIVE_MIN_LEVEL"] = $_GET["alternative_min_level"];
  196. }
  197. if (isset($_GET["alternative_min_original_quality"]) && intval($_GET["alternative_min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["alternative_min_original_quality"]) <= QUALITY_ID::LEGEND){
  198. $this->filters["ALTERNATIVE_MIN_ORIGINAL_QUALITY"] = $_GET["alternative_min_original_quality"];
  199. }
  200. if (isset($_GET["alternative_condition"]) && intval($_GET["alternative_condition"]) >= 0 && intval($_GET["alternative_condition"]) <= QUALITY_ID::LEGEND){
  201. $this->filters["ALTERNATIVE_CONDITION"] = $_GET["alternative_condition"];
  202. }
  203. if (isset($_GET["alternative_source"]) && intval($_GET["alternative_source"]) >= 0 && intval($_GET["alternative_source"]) <= 2){
  204. $this->filters["ALTERNATIVE_SOURCE"] = $_GET["alternative_source"];
  205. }
  206. return;
  207. }
  208. /**
  209. * Builds the query to the rune table using the a custom filter.
  210. *
  211. * @param int $filter Custom filter ID.
  212. * @return string The query to be executed.
  213. * @global int Player ID.
  214. */
  215. private function build_custom_query($filter){
  216. global $UID;
  217. $s = "
  218. SELECT condition
  219. FROM filter
  220. WHERE
  221. uid = '$UID' AND
  222. id = $filter;
  223. ";
  224. $q = $this->db->query($s);
  225. $r = $q->fetchArray(SQLITE3_ASSOC);
  226. $cond = $r["condition"];
  227. $s = "
  228. SELECT
  229. unit.id AS unit,
  230. rune.id AS id,
  231. rune_alt.id AS alternative
  232. FROM
  233. rune,
  234. rune rune_alt,
  235. unit,
  236. k_unit
  237. WHERE
  238. unit.uid = '$UID' AND
  239. rune.uid = '$UID' AND
  240. rune_alt.uid = '$UID' AND
  241. unit.id = rune.assigned_to AND
  242. unit.unit = k_unit.id AND
  243. rune.type = rune_alt.type AND
  244. rune.slot = rune_alt.slot AND
  245. rune.main_stat = rune_alt.main_stat AND
  246. rune.id <> rune_alt.id AND
  247. (
  248. $cond
  249. )
  250. ORDER BY
  251. unit.stars DESC,
  252. unit.level DESC,
  253. unit.id,
  254. rune.slot,
  255. id,
  256. rune_alt.stars DESC,
  257. rune_alt.original_quality DESC,
  258. rune_alt.main_stat_value DESC;
  259. ";
  260. return $s;
  261. }
  262. /**
  263. * Builds the query for the page, using the providded or default filters.
  264. *
  265. * @return string The query to be executed.
  266. * @global int Player ID.
  267. */
  268. private function build_query(){
  269. global $UID;
  270. $s = "
  271. SELECT
  272. unit.id AS unit,
  273. rune.id AS id,
  274. rune_alt.id AS alternative
  275. FROM
  276. rune,
  277. rune rune_alt,
  278. unit,
  279. k_unit
  280. WHERE
  281. unit.uid = '$UID' AND
  282. rune.uid = '$UID' AND
  283. rune_alt.uid = '$UID' AND
  284. unit.id = rune.assigned_to AND
  285. unit.unit = k_unit.id AND
  286. unit.stars >= " . $this->filters["MONSTER_MIN_STARS"] . " AND
  287. unit.stars <= " . $this->filters["MONSTER_MAX_STARS"] . " AND
  288. rune.stars >= " . $this->filters["RUNE_MIN_STARS"] . " AND
  289. rune.stars <= " . $this->filters["RUNE_MAX_STARS"] . " AND
  290. rune.level >= " . $this->filters["RUNE_MIN_LEVEL"] . " AND
  291. rune.level <= " . $this->filters["RUNE_MAX_LEVEL"] . " AND
  292. rune.quality >= " . $this->filters["RUNE_MIN_QUALITY"] . " AND
  293. rune.quality <= " . $this->filters["RUNE_MAX_QUALITY"] . " AND
  294. rune.original_quality >= " . $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] . " AND
  295. rune.original_quality <= " . $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] . " AND
  296. rune.efficiency >= " . $this->filters["RUNE_MIN_EFFICIENCY"] . " AND
  297. rune.efficiency <= " . $this->filters["RUNE_MAX_EFFICIENCY"] . " AND
  298. rune.max_efficiency >= " . $this->filters["RUNE_MIN_MAX_EFFICIENCY"] . " AND
  299. rune.max_efficiency <= " . $this->filters["RUNE_MAX_MAX_EFFICIENCY"] . " AND
  300. rune.type = rune_alt.type AND
  301. rune.slot = rune_alt.slot AND
  302. rune.main_stat = rune_alt.main_stat AND
  303. rune.id <> rune_alt.id AND
  304. rune_alt.stars >= " . $this->filters["ALTERNATIVE_MIN_STARS"] . " AND
  305. rune_alt.level >= " . $this->filters["ALTERNATIVE_MIN_LEVEL"] . " AND
  306. rune_alt.original_quality >= " . $this->filters["ALTERNATIVE_MIN_ORIGINAL_QUALITY"] . " AND
  307. ";
  308. switch ($this->filters["ALTERNATIVE_SOURCE"]){
  309. case 0: // Unassigned
  310. $s = $s . " rune_alt.assigned_to IS NULL AND ";
  311. break;
  312. case 1: // Unassigned or assigned to lower star monsters
  313. $s = $s . "
  314. (
  315. rune_alt.assigned_to IS NULL OR
  316. rune_alt.assigned_to IN (SELECT id FROM unit m WHERE m.stars < unit.stars)
  317. ) AND
  318. ";
  319. break;
  320. // Case 2: Assigned or unassigned.
  321. }
  322. switch ($this->filters["ALTERNATIVE_CONDITION"]){
  323. case 0: // Higher grade, any quality
  324. $s = $s .
  325. " rune_alt.stars >= rune.stars ";
  326. break;
  327. case 1: // Higher grade or higher quality
  328. $s = $s .
  329. " ( " .
  330. " rune_alt.stars > rune.stars OR " .
  331. " rune_alt.original_quality > rune.original_quality " .
  332. " ) ";
  333. break;
  334. case 2: // Equal or higher grade and equal or higher quality
  335. $s = $s .
  336. " rune_alt.stars >= rune.stars AND " .
  337. " rune_alt.original_quality >= rune.original_quality ";
  338. break;
  339. case 3: // Equal or higher grade and higher quality
  340. $s = $s .
  341. " rune_alt.stars >= rune.stars AND " .
  342. " rune_alt.original_quality > rune.original_quality ";
  343. break;
  344. case 4: // Equal grade and equal or higher quality
  345. $s = $s .
  346. " rune_alt.stars > rune.stars AND " .
  347. " rune_alt.original_quality >= rune.original_quality ";
  348. break;
  349. default: // Case 5: Higher grade and higher quality
  350. $s = $s .
  351. " rune_alt.stars > rune.stars AND " .
  352. " rune_alt.original_quality > rune.original_quality ";
  353. break;
  354. }
  355. if (strlen($this->filters["MONSTER_NAME"]) > 0){
  356. $s = $s . " AND upper(k_unit.name) LIKE = upper('%" . $this->filters["MONSTER_NAME"] . "%') ";
  357. }
  358. if (!$this->filters["MONSTER_IN_STORAGE"]){
  359. $s = $s . " AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE;
  360. }
  361. if (count($this->filters["RUNE_SLOT"]) > 0){
  362. $s = $s . " AND rune.slot IN ( ";
  363. foreach($this->filters["RUNE_SLOT"] as $t){
  364. $s = $s . "$t, ";
  365. }
  366. $s = $s . "-1) ";
  367. }
  368. $s = $s . "
  369. ORDER BY
  370. unit.stars DESC,
  371. unit.level DESC,
  372. unit.id,
  373. rune.slot,
  374. id,
  375. rune_alt.stars DESC,
  376. rune_alt.original_quality DESC,
  377. rune_alt.main_stat_value DESC;
  378. ";
  379. return $s;
  380. }
  381. }
  382. ?>