Report_Rune_Enchantment_Page.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * Upgradeable runes report 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 . "Unit.php");
  14. require_once(PATH::ENTITY . "Rune.php");
  15. require_once(PATH::ENTITY . "Enchantment.php");
  16. /**
  17. * Upgradeable runes report page model.
  18. *
  19. * @category Page
  20. */
  21. class Report_Rune_Enchantment_Page extends Page{
  22. /**
  23. * @var mixed[] List of available upgrades.
  24. *
  25. * Can be described as:
  26. * $upgrades = [
  27. * [
  28. * unit (Unit),
  29. * upgrade = [
  30. * [
  31. * rune (Rune),
  32. * enchant = [(Enchantment)]
  33. * ]
  34. * ]
  35. * ]
  36. * ]
  37. */
  38. public $upgrades = [];
  39. /**
  40. * @var mixed[] Default filter values.
  41. */
  42. public $filters = [
  43. "MONSTER_NAME" => "",
  44. "MONSTER_MIN_STARS" => 5,
  45. "MONSTER_MAX_STARS" => 6,
  46. "MONSTER_IN_STORAGE" => false,
  47. "TEAM" => false,
  48. "RUNE_MIN_STARS" => 5,
  49. "RUNE_MAX_STARS" => 6,
  50. "RUNE_MIN_LEVEL" => 0,
  51. "RUNE_MAX_LEVEL" => 15,
  52. "RUNE_MIN_QUALITY" => QUALITY_ID::HERO,
  53. "RUNE_MAX_QUALITY" => QUALITY_ID::LEGEND,
  54. "RUNE_MIN_ORIGINAL_QUALITY" => QUALITY_ID::HERO,
  55. "RUNE_MAX_ORIGINAL_QUALITY" => QUALITY_ID::LEGEND,
  56. "RUNE_MIN_EFFICIENCY" => 0,
  57. "RUNE_MAX_EFFICIENCY" => 100,
  58. "RUNE_MIN_MAX_EFFICIENCY" => 0,
  59. "RUNE_MAX_MAX_EFFICIENCY" => 100,
  60. "RUNE_SLOT" => [],
  61. "GRIND" => 1,
  62. "GRIND_MIN_QUALITY" => QUALITY_ID::RARE,
  63. "GRIND_STAT" => [
  64. RUNE_STAT_ID::HP_P,
  65. RUNE_STAT_ID::ATK_P,
  66. RUNE_STAT_ID::DEF_P,
  67. RUNE_STAT_ID::SPD,
  68. RUNE_STAT_ID::CRD,
  69. RUNE_STAT_ID::CRR,
  70. RUNE_STAT_ID::ACC
  71. ],
  72. "GEM" => 1,
  73. "GEM_MIN_QUALITY" => QUALITY_ID::RARE,
  74. "GEM_STAT_FROM" => [
  75. RUNE_STAT_ID::HP,
  76. RUNE_STAT_ID::ATK,
  77. RUNE_STAT_ID::DEF,
  78. RUNE_STAT_ID::RES
  79. ],
  80. "GEM_STAT_TO" => [
  81. RUNE_STAT_ID::HP_P,
  82. RUNE_STAT_ID::ATK_P,
  83. RUNE_STAT_ID::DEF_P,
  84. RUNE_STAT_ID::SPD,
  85. RUNE_STAT_ID::CRD,
  86. RUNE_STAT_ID::CRR,
  87. RUNE_STAT_ID::ACC
  88. ]
  89. ];
  90. /**
  91. * Constructor.
  92. *
  93. * Retrieves the data and initializes the variables.
  94. */
  95. public function __construct(){
  96. $this->view = PATH::VIEW . "report_rune_enchantment.php";
  97. $this->parse_filters();
  98. $s = $this->build_query();
  99. $q = get_context()->get_db()->query($s);
  100. $prev_monster = "";
  101. $prev_rune = "";
  102. $upgrade = null;
  103. $rupgrade = null;
  104. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  105. if ($r["unit"] != $prev_monster){
  106. if ($prev_rune != ""){
  107. array_push($upgrade["upgrade"], $rupgrade);
  108. }
  109. if ($prev_monster != ""){
  110. array_push($this->upgrades, $upgrade);
  111. }
  112. $prev_monster = $r["unit"];
  113. $prev_rune = "";
  114. $upgrade = [
  115. "unit" => new Unit($r["unit"]),
  116. "upgrade" => [],
  117. ];
  118. $rupgrade = [
  119. "rune" => new Rune($r["rune"]),
  120. "enchantment" => [],
  121. ];
  122. }
  123. if ($r["rune"] != $prev_rune){
  124. if ($prev_rune != ""){
  125. array_push($upgrade["upgrade"], $rupgrade);
  126. }
  127. $prev_rune = $r["rune"];
  128. $rupgrade = [
  129. "rune" => new Rune($r["rune"]),
  130. "enchantment" => [],
  131. ];
  132. }
  133. $enchantment= new Enchantment($r["enchantment"]);
  134. array_push($rupgrade["enchantment"], $enchantment);
  135. }
  136. // Last entries
  137. array_push($upgrade["upgrade"], $rupgrade);
  138. array_push($this->upgrades, $upgrade);
  139. $this->title = "Rune enchantment - SWDB";
  140. $this->description = "Find runes that can be upgraded via enchantments.";
  141. $this->canonical = URL::BASE . "report/rune_enchantment/";
  142. }
  143. /**
  144. * Parses the filters passed in the request and sets $filters.
  145. * Filters must be in $_GET, nd the available ones are max_stars (1-6),
  146. * min_stars (1-6) and in_storage (on/off).
  147. */
  148. private function parse_filters(){
  149. // Monster
  150. if (isset($_GET["monster_min_stars"]) && intval($_GET["monster_min_stars"]) > 0 && intval($_GET["monster_min_stars"]) < 7){
  151. $this->filters["MONSTER_MIN_STARS"] = $_GET["monster_min_stars"];
  152. }
  153. if (isset($_GET["monster_max_stars"]) && intval($_GET["monster_max_stars"]) > 0 && intval($_GET["monster_max_stars"]) < 7){
  154. $this->filters["MONSTER_MAX_STARS"] = $_GET["monster_max_stars"];
  155. }
  156. if (isset($_GET["monster_in_storage"]) && $_GET["monster_in_storage"] == "on"){
  157. $this->filters["MONSTER_IN_STORAGE"] = true;
  158. }
  159. else{
  160. $this->filters["MONSTER_IN_STORAGE"] = false;
  161. }
  162. if (isset($_GET["monster_name"]) && strlen($_GET["monster_name"]) > 0){
  163. $this->filters["MONSTER_NAME"] = SQLite3::escapeString($_GET["monster_name"]);
  164. }
  165. // Runes
  166. if (isset($_GET["rune_min_stars"]) && intval($_GET["rune_min_stars"]) > 0 && intval($_GET["rune_min_stars"]) < 7){
  167. $this->filters["RUNE_MIN_STARS"] = $_GET["rune_min_stars"];
  168. }
  169. if (isset($_GET["rune_max_stars"]) && intval($_GET["rune_max_stars"]) > 0 && intval($_GET["rune_max_stars"]) < 7){
  170. $this->filters["RUNE_MAX_STARS"] = $_GET["rune_max_stars"];
  171. }
  172. if (isset($_GET["rune_min_level"]) && intval($_GET["rune_min_level"]) >= 0 && intval($_GET["rune_min_level"]) <= 15){
  173. $this->filters["RUNE_MIN_LEVEL"] = $_GET["rune_min_level"];
  174. }
  175. if (isset($_GET["rune_max_level"]) && intval($_GET["rune_max_level"]) >= 0 && intval($_GET["rune_max_level"]) <= 15){
  176. $this->filters["RUNE_MAX_LEVEL"] = $_GET["rune_max_level"];
  177. }
  178. if (isset($_GET["rune_min_quality"]) && intval($_GET["rune_min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_quality"]) <= QUALITY_ID::LEGEND){
  179. $this->filters["RUNE_MIN_QUALITY"] = $_GET["rune_min_quality"];
  180. }
  181. if (isset($_GET["rune_max_quality"]) && intval($_GET["rune_max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_quality"]) <= QUALITY_ID::LEGEND){
  182. $this->filters["RUNE_MAX_QUALITY"] = $_GET["rune_max_quality"];
  183. }
  184. 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){
  185. $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] = $_GET["rune_min_original_quality"];
  186. }
  187. 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){
  188. $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] = $_GET["rune_max_original_quality"];
  189. }
  190. if (isset($_GET["rune_min_efficiency"]) && intval($_GET["rune_min_efficiency"]) >= 0 && intval($_GET["rune_min_efficiency"]) <= 100){
  191. $this->filters["RUNE_MIN_EFFICIENCY"] = $_GET["rune_min_efficiency"];
  192. }
  193. if (isset($_GET["rune_max_efficiency"]) && intval($_GET["rune_max_efficiency"]) >= 0 && intval($_GET["rune_max_efficiency"]) <= 100){
  194. $this->filters["RUNE_MAX_EFFICIENCY"] = $_GET["rune_max_efficiency"];
  195. }
  196. if (isset($_GET["rune_min_max_efficiency"]) && intval($_GET["rune_min_max_efficiency"]) >= 0 && intval($_GET["rune_min_max_efficiency"]) <= 100){
  197. $this->filters["RUNE_MIN_MAX_EFFICIENCY"] = $_GET["rune_min_max_efficiency"];
  198. }
  199. if (isset($_GET["rune_max_max_efficiency"]) && intval($_GET["rune_max_max_efficiency"]) >= 0 && intval($_GET["rune_max_max_efficiency"]) <= 100){
  200. $this->filters["RUNE_MAX_MAX_EFFICIENCY"] = $_GET["rune_max_max_efficiency"];
  201. }
  202. if (isset($_GET["rune_slot"])){
  203. $slots = $_GET["rune_slot"];
  204. foreach ($slots as $slot){
  205. if (intval($slot) >= 1 && intval($slot) < 7){
  206. array_push($this->filters["RUNE_SLOT"], intval($slot));
  207. }
  208. }
  209. }
  210. // Grindstones
  211. if (isset($_GET["grind_min_quality"]) && intval($_GET["grind_min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["grind_min_quality"]) < QUALITY_ID::LEGEND){
  212. $this->filters["GRIND_MIN_QUALITY"] = $_GET["grind_min_quality"];
  213. }
  214. if (isset($_GET["grind_stat"])){
  215. $stats = $_GET["grind_stat"];
  216. $this->filters["GRIND_STAT"] = [];
  217. foreach ($stats as $stat){
  218. array_push($this->filters["GRIND_STAT"], intval($stat));
  219. }
  220. }
  221. if (isset($_GET["gem_stat_from"])){
  222. $stats = $_GET["gem_stat_from"];
  223. $this->filters["GEM_STAT_FROM"] = [];
  224. foreach ($stats as $stat){
  225. array_push($this->filters["GEM_STAT_FROM"], intval($stat));
  226. }
  227. }
  228. if (isset($_GET["gem_stat_to"])){
  229. $stats = $_GET["gem_stat_to"];
  230. $this->filters["GEM_STAT_TO"] = [];
  231. foreach ($stats as $stat){
  232. array_push($this->filters["GEM_STAT_TO"], intval($stat));
  233. }
  234. }
  235. return;
  236. }
  237. /**
  238. * Builds the query for the page, using the providded or default filters.
  239. *
  240. * @return string The query to be executed.
  241. */
  242. private function build_query(){
  243. $s = "
  244. SELECT DISTINCT
  245. unit.id AS unit,
  246. rune.id AS rune,
  247. enchantment.id AS enchantment
  248. FROM
  249. unit,
  250. monster,
  251. rune,
  252. unit_rune,
  253. enchantment,
  254. enchantment_type
  255. WHERE
  256. unit.player = '" . get_context()->get_player()->get_id() . "'
  257. AND rune.player = '" . get_context()->get_player()->get_id() . "'
  258. AND enchantment.player = '" . get_context()->get_player()->get_id() . "'
  259. AND unit.id = unit_rune.unit
  260. AND rune.id = unit_rune.rune
  261. AND unit_rune.rta = " . get_context()->get_game_mode() . "
  262. AND unit.monster = monster.id
  263. AND enchantment.type = enchantment_type.id
  264. -- RUNE FILTERS
  265. AND unit.stars >= " . $this->filters["RUNE_MIN_STARS"] . "
  266. AND unit.stars <= " . $this->filters["RUNE_MAX_STARS"] . "
  267. AND rune.quality >= " . $this->filters["RUNE_MIN_QUALITY"] . "
  268. AND rune.quality <= " . $this->filters["RUNE_MAX_QUALITY"] . "
  269. AND rune.level >= " . $this->filters["RUNE_MIN_LEVEL"] . "
  270. AND rune.level <= " . $this->filters["RUNE_MAX_LEVEL"] . "
  271. AND rune.stars >= " . $this->filters["RUNE_MIN_STARS"] . "
  272. AND rune.stars <= " . $this->filters["RUNE_MAX_STARS"] . "
  273. AND rune.original_quality >= " . $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] . "
  274. AND rune.original_quality <= " . $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] . "
  275. AND rune.efficiency >= " . $this->filters["RUNE_MIN_EFFICIENCY"] . "
  276. AND rune.efficiency <= " . $this->filters["RUNE_MAX_EFFICIENCY"] . "
  277. AND rune.max_efficiency >= " . $this->filters["RUNE_MIN_MAX_EFFICIENCY"] . "
  278. AND rune.max_efficiency <= " . $this->filters["RUNE_MAX_MAX_EFFICIENCY"] . "
  279. AND (
  280. rune.type = enchantment.rune
  281. OR enchantment_type.inmemorial = 1
  282. )
  283. AND enchantment_type.ancient = rune.ancient
  284. ";
  285. // Monster name
  286. $cond = "";
  287. if (strlen($this->filters["MONSTER_NAME"]) > 0){
  288. $cond = "AND upper(monster.name) LIKE upper('%" . $this->filters["MONSTER_NAME"] . "%') ";
  289. }
  290. $s .= $cond . " -- MONSTER NAME \n";
  291. // Monsters in storage
  292. $cond = "";
  293. if (!$this->filters["MONSTER_IN_STORAGE"]){
  294. $cond = "AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE . " ";
  295. }
  296. $s .= $cond . " -- STORAGE \n";
  297. // Rune slot
  298. $cond = "";
  299. if (count($this->filters["RUNE_SLOT"]) > 0){
  300. $in = "(";
  301. foreach($this->filters["RUNE_SLOT"] as $t){
  302. $in .= "$t, ";
  303. }
  304. $in = rtrim($in, ", ") . ")";
  305. $cond = "AND rune.slot IN $in";
  306. }
  307. $s .= $cond . " -- RUNE SLOT \n";
  308. // Nevermind the type, discard enchantments with the same stat as
  309. // the main or inate substat
  310. $s .= "
  311. AND enchantment.stat NOT IN (
  312. SELECT stat
  313. FROM rune_stat
  314. WHERE
  315. rune_stat.rune = rune.id
  316. AND (
  317. rune_stat.slot = " . RUNE_STAT_SLOT_ID::MAIN . "
  318. OR rune_stat.slot = " . RUNE_STAT_SLOT_ID::INNATE . "
  319. )
  320. )
  321. ";
  322. // Begin type switch block
  323. $s .= " AND ( -- TYPE SWITCH \n";
  324. // Begin grindstone block
  325. $s .= "
  326. (
  327. enchantment_type.gem = 0
  328. AND enchantment.stat IN (SELECT stat FROM rune_stat rune_stat WHERE rune_stat.rune = rune.id)
  329. AND enchantment.quality >= " . $this->filters["GRIND_MIN_QUALITY"] . " -- GRINDSTONE QUALITY
  330. ";
  331. // Grindstone stat
  332. $cond = "";
  333. if (count($this->filters["GRIND_STAT"]) > 0){
  334. $in = "(";
  335. foreach($this->filters["GRIND_STAT"] as $t){
  336. $in .= "$t, ";
  337. }
  338. $in = rtrim($in, ", ") . ")";
  339. $cond = "AND enchantment.stat IN $in";
  340. }
  341. $s .= $cond . " -- GRINDSTONE STAT \n";
  342. $s .= "
  343. )\n
  344. ";
  345. // Begin gem block
  346. $s .= " OR (
  347. enchantment_type.gem = 1
  348. AND enchantment.stat NOT IN (SELECT stat FROM rune_stat rune_stat WHERE rune_stat.rune = rune.id)
  349. AND enchantment.quality >= " . $this->filters["GEM_MIN_QUALITY"] . " -- GEM QUALITY
  350. ";
  351. // Gem stat to
  352. $cond = "";
  353. if (count($this->filters["GEM_STAT_TO"]) > 0){
  354. $in = "(";
  355. foreach($this->filters["GEM_STAT_TO"] as $t){
  356. $in .= "$t, ";
  357. }
  358. $in = rtrim($in, ", ") . ")";
  359. $cond = "AND enchantment.stat IN $in";
  360. }
  361. $s .= $cond . " -- GEM STAT TO \n";
  362. // Gem stat from
  363. $cond = "";
  364. if (count($this->filters["GEM_STAT_FROM"]) > 0){
  365. $in = "(";
  366. foreach($this->filters["GEM_STAT_FROM"] as $t){
  367. $in .= "$t, ";
  368. }
  369. $in = rtrim($in, ", ") . ")";
  370. $cond = "
  371. AND rune.id IN(
  372. SELECT DISTINCT rune
  373. FROM rune_stat rune_stat
  374. WHERE
  375. rune_stat.rune = rune.id
  376. AND stat IN $in
  377. )
  378. ";
  379. }
  380. $s .= $cond . " -- GEM STAT FROM \n";
  381. $s .= "
  382. )\n
  383. ";
  384. // End type switch block
  385. $s .= " ) -- END TYPE SWITCH \n";
  386. return $s;
  387. }
  388. }
  389. ?>