| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
-
- /**
- * Artifact list page file.
- *
- * Provides a class with all the properties and methods to display the page.
- *
- * @category Page
- */
- /**
- * Require dependent files if not present.
- */
- require_once(PATH::PAGE . "Page.php");
- require_once(PATH::ENTITY . "Custom_Filter.php");
- require_once(PATH::ENTITY . "Artifact.php");
- require_once(PATH::ENTITY . "Unit.php");
- /**
- * Artifact list page model.
- *
- * @category Page
- */
- class Artifacts_Page extends Page{
- /**
- * @var \Artifact[] Artiffacts to show.
- *
- * In this case, the monster member is not the id, but a monster
- * instance.
- */
- public $artifacts = [];
- /**
- * @var \Filter[] Custom filters for this page.
- */
- public $custom_filters = [];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- *
- * @global int Player ID.
- * @global resource Database connection.
- */
- public function __construct(){
- global $UID;
- global $db;
- $this->view = PATH::VIEW . "artifacts.php";
- $this->parse_filters();
- if (!isset($_GET["custom_filter"])){
- $s = $this->build_query();
- }
- else{
- $s = $this->build_custom_query($_GET["custom_filter"]);
- }
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- $artifact = new Artifact($r["id"]);
- if (strlen($artifact->assigned_to) > 0){
- $artifact->assigned_to = new Unit($artifact->assigned_to, false);
- }
- array_push($this->artifacts, $artifact);
- }
- $s = "
- SELECT id
- FROM filter
- WHERE
- uid = '$UID' AND
- page = 'ARTIFACTS';
- ";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- array_push($this->custom_filters, new Custom_Filter($r["id"]));
- }
- $this->title = "Artifacts - SWDB";
- $this->description = "Artifact inventory";
- $this->canonical = URL::BASE . "artifacts/";
- }
- /**
- * Parses the request looking for the selected filters, validates them
- * and adds them to the $filters array.
- */
- private function parse_filters(){
- $this->filters = FILTER::ARTIFACT;
- if (isset($_GET["element"])){
- $elements = $_GET["element"];
- foreach ($elements as $element){
- array_push($this->filters["ELEMENT"], intval($element));
- }
- }
- if (isset($_GET["archetype"])){
- $archetypes = $_GET["archetype"];
- foreach ($archetypes as $archetype){
- array_push($this->filters["ARCHETYPE"], intval($archetype));
- }
- }
- if (isset($_GET["main_stat"])){
- $mains = $_GET["main_stat"];
- foreach ($mains as $main){
- array_push($this->filters["MAIN"], intval($main));
- }
- }
- if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
- $this->filters["MIN_LEVEL"] = $_GET["min_level"];
- }
- if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
- $this->filters["MAX_LEVEL"] = $_GET["max_level"];
- }
- if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
- $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
- }
- if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
- $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
- }
- if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
- $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
- }
- if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
- $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
- }
- if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
- $this->filters["ASSIGNED"] = $_GET["assigned"];
- }
- }
- /**
- * Builds the query to the artifact table using the a custom filter.
- *
- * @param int $filter Custom filter ID.
- * @return string The query to be executed.
- * @global int Player ID.
- * @global resource Database connection.
- */
- private function build_custom_query($filter){
- global $UID;
- global $db;
- $s = "
- SELECT condition
- FROM filter
- WHERE
- uid = '$UID' AND
- id = $filter;
- ";
- $q = $db->query($s);
- $r = $q->fetchArray(SQLITE3_ASSOC);
- $cond = $r["condition"];
- $s = "
- SELECT
- id,
- assigned_to
- FROM artifact
- WHERE uid = '$UID' AND (
- $cond
- )
- ORDER BY slot, type, stars DESC, original_quality DESC, quality DESC, level;
- ";
- return $s;
- }
- /**
- * Builds the query to the artifact table using the selected or default
- * filters.
- *
- * @return string The query to be executed.
- * @global int Player ID.
- */
- private function build_query(){
- global $UID;
- $s =
- "SELECT " .
- " id, " .
- " assigned_to " .
- "FROM artifact " .
- "WHERE " .
- " uid = '$UID' AND " .
- " level >= " . $this->filters["MIN_LEVEL"] . " AND " .
- " level <= " . $this->filters["MAX_LEVEL"] . " AND " .
- " quality >= " . $this->filters["MIN_QUALITY"] . " AND " .
- " quality <= " . $this->filters["MAX_QUALITY"] . " AND " .
- " original_quality >= " . $this->filters["MIN_ORIGINAL_QUALITY"] . " AND " .
- " original_quality <= " . $this->filters["MAX_ORIGINAL_QUALITY"] . " AND " .
- " efficiency >= " . $this->filters["MIN_EFFICIENCY"] . " AND " .
- " efficiency <= " . $this->filters["MAX_EFFICIENCY"] . " AND " .
- " max_efficiency >= " . $this->filters["MIN_MAX_EFFICIENCY"] . " AND " .
- " max_efficiency <= " . $this->filters["MAX_MAX_EFFICIENCY"] . " ";
- if (count($this->filters["MAIN"]) > 0){
- $s = $s . " AND main_stat IN ( ";
- foreach($this->filters["MAIN"] as $t){
- $s = $s . "$t, ";
- }
- $s = $s . "-1) ";
- }
- if (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) == 0){
- // No element/archetype conditions
- }
- elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) == 0){
- // Only selected elements
- $s = $s . " AND attribute IN ( ";
- foreach($this->filters["ELEMENT"] as $t){
- $s = $s . "$t, ";
- }
- $s = $s . "-1) ";
- }
- elseif (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) > 0){
- // Only selected types
- $s = $s . " AND archetype IN ( ";
- foreach($this->filters["ARCHETYPE"] as $t){
- $s = $s . "$t, ";
- }
- $s = $s . "-1) ";
- }
- elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) > 0){
- // Selected elements and selected types
- $s = $s . " AND (archetype IN ( ";
- foreach($this->filters["ARCHETYPE"] as $t){
- $s = $s . "$t, ";
- }
- $s = $s . "-1) OR attribute IN ( ";
- foreach($this->filters["ELEMENT"] as $t){
- $s = $s . "$t, ";
- }
- $s = $s . "-1)) ";
- }
- switch ($this->filters["ASSIGNED"]){
- case 1:
- $s = $s . " AND assigned_to IS NOT NULL ";
- break;
- case 2:
- $s = $s . " AND assigned_to IS NULL ";
- break;
- case 3:
- $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
- break;
- case 4:
- $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
- break;
- }
- $s = $s . " ORDER BY type, attribute, archetype, level DESC, original_quality DESC;";
- error_log($s);
- return $s;
- }
- }
- ?>
|