* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ require_once(PATH::PAGE . "Page.php"); require_once(PATH::ENTITY . "Rune.php"); require_once(PATH::ENTITY . "Unit.php"); /** * Rune list page model. * * @category Page */ class Runes_Page extends Page{ /** * @var Rune[] Runes to show. * * In this case, the monster member is not the id, but a monster * instance. */ private $runes = []; /** * @var mixed[] Default filter values. */ private $filters = [ "TYPE" => [], "MAIN" => [], "SUB" => [], "SLOT" => [], "MIN_QUALITY" => QUALITY_ID::NORMAL, "MAX_QUALITY" => QUALITY_ID::LEGEND, "MIN_ORIGINAL_QUALITY" => QUALITY_ID::NORMAL, "MAX_ORIGINAL_QUALITY" => QUALITY_ID::LEGEND, "MIN_STARS" => 1, "MAX_STARS" => 6, "MIN_LEVEL" => 0, "MAX_LEVEL" => 15, "MIN_EFFICIENCY" => 0, "MAX_EFFICIENCY" => 100, "MIN_MAX_EFFICIENCY" => 0, "MAX_MAX_EFFICIENCY" => 100, "ASSIGNED" => 1, "GROUP" => "SLOT" ]; /** * Constructor. * * Retrieves the data and initializes the variables. */ public function __construct(){ parent::__construct(); $this->set_public(true); $this->set_view("runes.php"); $this->set_title("Runes"); $this->set_description(get_context()->get_player()->get_name() . "'s runes"); $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/runes/"); $this->add_css("runes.css"); $this->parse_filters(); // TODO: Group hardcoded $this->filters["GROUP"] = 1; $result_set = $this->prepare_statement()->execute(); while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){ array_push($this->runes, new Rune($result["id"])); } $this->set_code(200); $this->set_message("OK"); } /** * Parses the request looking for the selected filters, validates them * and adds them to the $filters array. */ private function parse_filters(){ if (isset($_GET["type"])){ $types = $_GET["type"]; foreach ($types as $type){ array_push($this->filters["TYPE"], intval($type)); } } if (isset($_GET["main_stat"])){ $mains = $_GET["main_stat"]; foreach ($mains as $main){ array_push($this->filters["MAIN"], intval($main)); } } if (isset($_GET["sub_stat"])){ $subs = $_GET["sub_stat"]; foreach ($subs as $sub){ array_push($this->filters["SUB"], intval($sub)); } } if (isset($_GET["slot"])){ $slots = $_GET["slot"]; foreach ($slots as $slot){ if (intval($slot) >= 1 && intval($slot) < 7){ array_push($this->filters["SLOT"], intval($slot)); } } } if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){ $this->filters["MIN_STARS"] = $_GET["min_stars"]; } if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){ $this->filters["MAX_STARS"] = $_GET["max_stars"]; } 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["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){ $this->filters["MIN_EFFICIENCY"] = $_GET["min_efficiency"]; } if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){ $this->filters["MAX_EFFICIENCY"] = $_GET["max_efficiency"]; } if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){ $this->filters["MIN_MAX_EFFICIENCY"] = $_GET["min_max_efficiency"]; } if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){ $this->filters["MAX_MAX_EFFICIENCY"] = $_GET["max_max_efficiency"]; } if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){ $this->filters["ASSIGNED"] = $_GET["assigned"]; } if (isset($_GET["group"]) && ($_GET["group"] == "1" || $_GET["group"] == "0")){ $this->filters["GROUP"] = $_GET["group"]; } } /** * Prepares the statement to execute against the database using the filter values. * * @return SQLite3Stmt prepared statement. */ private function prepare_statement(){ // Common items $query = " SELECT id FROM rune WHERE player = :player AND stars BETWEEN :min_stars AND :max_stars AND level BETWEEN :min_level AND :max_level AND quality BETWEEN :min_quality AND :max_quality AND original_quality BETWEEN :min_original_quality AND :max_original_quality AND efficiency BETWEEN :min_efficiency AND :max_efficiency AND max_efficiency BETWEEN :min_max_efficiency AND :max_max_efficiency "; // Some main stat selected. if (count($this->filters["MAIN"]) > 0){ $query .= " AND id IN (SELECT rune FROM rune_stat WHERE slot = :slot_main AND stat IN ("; for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){ $query .= ":main_$i, "; } $query = rtrim($query, ", ") . ")) "; } // Some substat selected. if (count($this->filters["SUB"]) > 0){ $query .= " AND id IN (SELECT rune FROM rune_stat WHERE slot <> :slot_main AND stat IN ("; for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){ $query .= ":sub_$i, "; } $query = rtrim($query, ", ") . ")) "; } // Some sets selected. if (count($this->filters["TYPE"]) > 0){ $query .= " AND type IN ("; for ($i = 0; $i < sizeof($this->filters["TYPE"]); $i ++){ $query .= ":type_$i, "; } $query = rtrim($query, ", ") . ") "; } // Some slots selected. if (count($this->filters["SLOT"]) > 0){ $query .= " AND slot IN ("; for ($i = 0; $i < sizeof($this->filters["SLOT"]); $i ++){ $query .= ":slot_$i, "; } $query = rtrim($query, ", ") . ") "; } switch ($this->filters["ASSIGNED"]){ case 0: // All break; case 1: // Assigned $query .= " AND id IN ( SELECT DISTINCT rune FROM unit_rune WHERE rta = :rta) "; break; case 2: // Unassigned $query .= " AND id NOT IN ( SELECT DISTINCT rune FROM unit_rune WHERE rta = :rta) "; break; case 3: // Unassigned or assigned to non-storage units $query .= " AND ( id NOT IN ( SELECT DISTINCT rune FROM unit_rune WHERE rta = :rta ) OR ( id IN ( SELECT DISTINCT unit_rune.rune FROM unit_rune, unit WHERE unit_rune.rta = :rta AND unit_rune.unit = unit.id AND unit.building = :storage_building ) ) )"; break; case 4: // Assigned to non-storage units $query .= " AND ( id IN ( SELECT DISTINCT unit_rune.rune FROM unit_rune, unit WHERE unit_rune.rta = :rta AND unit_rune.unit = unit.id AND unit.building = :storage_building ) )"; break; } $query .= " ORDER BY "; if ($this->filters["GROUP"] == 1){ // 1: Type, 0: Slot $query .= " type, slot, stars DESC, original_quality DESC, quality DESC, level;"; } else{ $query .= " slot, type, stars DESC, original_quality DESC, quality DESC, level;"; } $statement = get_context()->get_db()->prepare($query); $statement->bindValue(":rta", get_context()->get_game_mode(), SQLITE3_INTEGER); $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT); $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER); $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER); $statement->bindValue(":min_level", $this->filters["MIN_LEVEL"], SQLITE3_INTEGER); $statement->bindValue(":max_level", $this->filters["MAX_LEVEL"], SQLITE3_INTEGER); $statement->bindValue(":min_quality", $this->filters["MIN_QUALITY"], SQLITE3_INTEGER); $statement->bindValue(":max_quality", $this->filters["MAX_QUALITY"], SQLITE3_INTEGER); $statement->bindValue(":min_original_quality", $this->filters["MIN_ORIGINAL_QUALITY"], SQLITE3_INTEGER); $statement->bindValue(":max_original_quality", $this->filters["MAX_ORIGINAL_QUALITY"], SQLITE3_INTEGER); $statement->bindValue(":min_efficiency", $this->filters["MIN_EFFICIENCY"], SQLITE3_FLOAT); $statement->bindValue(":max_efficiency", $this->filters["MAX_EFFICIENCY"], SQLITE3_FLOAT); $statement->bindValue(":min_max_efficiency", $this->filters["MIN_MAX_EFFICIENCY"], SQLITE3_FLOAT); $statement->bindValue(":max_max_efficiency", $this->filters["MAX_MAX_EFFICIENCY"], SQLITE3_FLOAT); $statement->bindValue(":slot_main", RUNE_STAT_SLOT_ID::MAIN, SQLITE3_FLOAT); for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){ $statement->bindValue(":main_$i", $this->filters["MAIN"][$i], SQLITE3_INTEGER); } for ($i = 0; $i < sizeof($this->filters["SUB"]); $i ++){ $statement->bindValue(":sub_$i", $this->filters["SUB"][$i], SQLITE3_INTEGER); } for ($i = 0; $i < sizeof($this->filters["TYPE"]); $i ++){ $statement->bindValue(":type_$i", $this->filters["TYPE"][$i], SQLITE3_INTEGER); } for ($i = 0; $i < sizeof($this->filters["SLOT"]); $i ++){ $statement->bindValue(":slot_$i", $this->filters["SLOT"][$i], SQLITE3_INTEGER); } $statement->bindValue(":storage_building", BUILDING_ID::MONSTER_STORAGE, SQLITE3_INTEGER); return $statement; } /** * Retrieves the page filters. * * @return mixed[] Page filtes */ public function get_filters(){ return $this->filters; } /** * Retrieves one filter. * * @param string $key * @return mixed Filter value, null if if doesn't exist. */ public function get_filter($key){ if (array_key_exists($key, $this->filters)){ return $this->filters[$key]; } else{ return null; } } /** * Retrieves the selection of runes. * * @return Rune[] Runes to show on the page. */ public function get_runes(){ return $this->runes; } }