Report_Rune_Enchantment_Page.php 17 KB

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