| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- <?php
- /**
- * Rune entity file.
- *
- * Creates the entity and makes it available.
- *
- * @category Entity
- */
- /**
- * Require dependent entities if not present.
- */
- require_once(PATH::ENTITY . "Entity.php");
- require_once(PATH::ENTITY . "Rune_Set.php");
- require_once(PATH::ENTITY . "Rune_Stat.php");
- /**
- * Owned rune.
- *
- * Represents an object from the table 'rune'.
- *
- * @category Entity
- */
- class Rune extends Entity{
-
- /**
- * @var int Owner identifier.
- */
- private $player;
-
- /**
- * @var int Rune identifier.
- */
- private $id;
-
- /**
- * @var Rune_Set Rune set.
- */
- private $type;
-
- /**
- * @var int Position of the rune type (1-6).
- */
- private $slot;
-
- /**
- * @var int Rune stars.
- */
- private $stars;
-
- /**
- * @var bool Indicates if the rune is ancient.
- */
- private $ancient;
-
- /**
- * @var int Level of the rune.
- */
- private $level;
-
- /**
- * @var int Rune quality.
- *
- * @see QUALITY_ID
- */
- private $quality;
-
- /**
- * @var int Rune original quality.
- *
- * @see QUALITY_ID
- */
- private $original_quality;
-
- /**
- * @var string Rune quality name (uppercase).
- */
- private $quality_name;
-
- /**
- * @var string Rune original quality name (uppercase).
- */
- private $original_quality_name;
-
- /**
- * @var int Price of the rune.
- */
- private $value;
-
- /**
- * @var float Current efficiency of the rune.
- */
- private $efficiency;
-
- /**
- * @var float Maximum potential efficiency of the rune.
- */
- private $max_efficiency;
-
- /**
- * @var int Indicates if the rune is locked.
- */
- private $locked;
- private $flag_stats;
- /**
- * @var Rune_Stat Main stat of the rune.
- */
- private $main_stat;
-
- /**
- * @var Rune_Stat Extra stat of the rune.
- */
- private $innate_stat;
-
- /**
- * @var \Rune_Stat[] Substats of the rune.
- */
- private $substats = [];
-
- /**
- * @var \Rune_Stat[] All stats.
- *
- * Includes main, innate and substats
- */
- private $stats = [];
-
- /**
- * @var Unit Unit the rune is assigned to.
- */
- private $unit;
- /**
- * Constructor.
- *
- * Searches the database and retrieves the information about the
- * rune, populating it and it's items.
- *
- * @param int $id Rune id.
- */
- public function __construct($id){
-
- $statement = get_context()->get_db()->prepare("
- SELECT
- id,
- type,
- slot,
- stars,
- level,
- ancient,
- quality,
- original_quality,
- value,
- efficiency,
- max_efficiency,
- locked
- FROM rune
- WHERE id = :id;
- ");
- $statement->bindValue(':id', $id, SQLITE3_TEXT);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $this->id = $r["id"];
- $this->type = new Rune_Set($r["type"]);
- $this->slot = $r["slot"];
- $this->stars = $r["stars"];
- $this->level = $r["level"];
- $this->ancient = $r["ancient"];
- $this->quality = $r["quality"];
- $this->original_quality = $r["original_quality"];
- $this->value = $r["value"];
- $this->efficiency = $r["efficiency"];
- $this->max_efficiency = $r["max_efficiency"];
- $this->locked = filter_var($r["locked"], FILTER_VALIDATE_BOOLEAN);
-
- $this->quality_name = $this->map_quality_name($this->quality);
- $this->original_quality_name = $this->map_quality_name($this->original_quality);
-
- // Read efficiencies. If not set in DB, calculate and update.
- if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
- $this->efficiency = $this->calculate_efficiency();
- $this->max_efficiency = $this->calculate_maximum_efficiency();
- $statement = get_context()->get_db()->prepare("
- UPDATE rune
- SET
- efficiency = :efficiency,
- max_efficiency = :max_efficiency
- WHERE id = :rune
- ");
- $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
- $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
- $statement->bindValue(':rune', $this->id, SQLITE3_INTEGER);
- $statement->execute();
- }
- }
- }
-
- private function load_stats(){
- $statement = get_context()->get_db()->prepare("
- SELECT slot
- FROM rune_stat
- WHERE rune = :rune
- ORDER BY slot;
- ");
- $statement->bindValue(':rune', $this->id, SQLITE3_TEXT);
- $q = $statement->execute();
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- $stat = new Rune_Stat($this->id, $r["slot"]);
- array_push($this->stats, $stat);
- switch ($r["slot"]){
- case RUNE_STAT_SLOT_ID::MAIN:
- $this->main_stat = $stat;
- break;
- case RUNE_STAT_SLOT_ID::INNATE:
- $this->innate_stat = $stat;
- break;
- default:
- array_push($this->substats, $stat);
- break;
- }
- }
- $this->flag_stats = true;
- }
-
- private function load_unit(){
- $statement = get_context()->get_db()->prepare("
- SELECT unit
- FROM unit_rune
- WHERE
- rune = :rune AND
- rta = :mode;
- ");
- $statement->bindValue(':rune', $this->id, SQLITE3_TEXT);
- $statement->bindValue(':mode', get_context()->get_game_mode(), SQLITE3_TEXT);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $this->unit = new Unit($r["unit"]);
- }
- $this->flag_unit = true;
- }
-
- /**
- * Retrieves the owner's player identifier.
- *
- * @return number Player ID.
- */
- public function get_player(){
- return $this->player;
- }
-
- /**
- * Retrieves the rune identifier.
- *
- * @return number Rune ID.
- */
- public function get_id(){
- return $this->id;
- }
-
- /**
- * Retrieves the rune set.
- *
- * @return Rune_Set Set of the rune.
- */
- public function get_set(){
- return $this->type;
- }
-
- /**
- * Retrieves the rune slot.
- *
- * @return number Rune slot (1-6).
- */
- public function get_slot(){
- return $this->slot;
- }
-
- /**
- * Retrieves the rune stars (the grade).
- *
- * @return number Rune stars (1-6).
- */
- public function get_stars(){
- return $this->stars;
- }
-
- /**
- * Checks if it's an ancient rune.
- *
- * @return boolean True for ancient, false for normal.
- */
- public function is_ancient(){
- return $this->ancient;
- }
-
- /**
- * Retrieves the rune level
- *
- * @return number Rune level (0-15).
- */
- public function get_level(){
- return $this->level;
- }
-
- /**
- * Retrieves the rune quality identifier.
- *
- * @see QUALITY_ID
- *
- * @return number Quality identifier.
- */
- public function get_quality(){
- return $this->quality;
- }
-
- /**
- * Retrieves the rune original quality identifier.
- *
- * @see QUALITY_ID
- *
- * @return number Original quality identifier.
- */
- public function get_original_quality(){
- return $this->original_quality;
- }
-
- /**
- * Retrieves the rune quality name.
- *
- * @return string Quality name.
- */
- public function get_quality_name(){
- return $this->quality_name;
- }
-
- /**
- * Retrieves the rune original quality name.
- *
- * @return string Original quality name.
- */
- public function get_original_quality_name(){
- return $this->original_quality_name;
- }
-
- /**
- * Retrieves the rune value.
- *
- * @return number Rune value in mana stones.
- */
- public function get_value(){
- return $this->value;
- }
-
- /**
- * Retrieves the rune efficiency.
- *
- * @return number Rune efficiency (0-100)
- */
- public function get_efficiency(){
- return $this->efficiency;
- }
-
- /**
- * Retrieves the rune efficiency.
- *
- * @return number Rune efficiency (0-100)
- */
- public function get_max_efficiency(){
- return $this->max_efficiency;
- }
-
- /**
- * Checks if the rune is locked.
- *
- * @return bool True if locked, false otherwise.
- */
- public function is_locked(){
- return $this->locked;
- }
-
- /**
- * Retrieves the rune main_stat.
- *
- * @return Rune_Stat The main stat.
- */
- public function get_main_stat(){
- if (! $this->flag_stats){
- $this->load_stats();
- }
- return $this->main_stat;
- }
-
- /**
- * Retrieves the rune innate stat.
- *
- * @return Rune_Stat Innate stat, null if it hasn't.
- */
- public function get_innate_stat(){
- if (! $this->flag_stats){
- $this->load_stats();
- }
- return $this->innate_stat;
- }
-
- /**
- * Retrieves as many substats as the rune has, sorted.
- *
- * @return Rune_Stat[] Sorted array of the rune substats.
- */
- public function get_substats(){
- if (! $this->flag_stats){
- $this->load_stats();
- }
- return $this->substats;
- }
-
- /**
- * Retrieves all the rune stats, including main, innate and substats, in an unsorted array.
- *
- * @return Rune_Stat Every rune stat.
- */
- public function get_stats(){
- if (! $this->flag_stats){
- $this->load_stats();
- }
- return $this->stats;
- }
-
- /**
- * Retrieves the unit that has equipped the rune in the current game mode.
- *
- * @return Unit Unit that has the rune.
- */
- public function get_unit(){
- if (! $this->flag_unit){
- $this->load_unit();
- }
- return $this->unit;
- }
-
- /**
- * Gets the value of the main stat at an arbitrary level.
- *
- * @param int $level Desired level.
- * @return int Value of the main stat at the selected level.
- */
- public function get_main_stat_at_level($level){
- $level = intval($level);
- if ($level < 0 || $level > 15){
- return 0;
- }
- switch ($this->main_stat->get_stat()){
- case RUNE_STAT_ID::HP:
- return RUNE_STAT_VALUES::HP[$this->stars][$level];
- case RUNE_STAT_ID::HP_P:
- return RUNE_STAT_VALUES::HP_P[$this->stars][$level];
- case RUNE_STAT_ID::ATK:
- return RUNE_STAT_VALUES::ATK[$this->stars][$level];
- case RUNE_STAT_ID::ATK_P:
- return RUNE_STAT_VALUES::ATK_P[$this->stars][$level];
- case RUNE_STAT_ID::DEF:
- return RUNE_STAT_VALUES::DEF[$this->stars][$level];
- case RUNE_STAT_ID::DEF_P:
- return RUNE_STAT_VALUES::DEF_P[$this->stars][$level];
- case RUNE_STAT_ID::SPD:
- return RUNE_STAT_VALUES::SPD[$this->stars][$level];
- case RUNE_STAT_ID::CRR:
- return RUNE_STAT_VALUES::CRR[$this->stars][$level];
- case RUNE_STAT_ID::CRD:
- return RUNE_STAT_VALUES::CRD[$this->stars][$level];
- case RUNE_STAT_ID::RES:
- return RUNE_STAT_VALUES::RES[$this->stars][$level];
- case RUNE_STAT_ID::ACC:
- return RUNE_STAT_VALUES::ACC[$this->stars][$level];
- default:
- return 0;
- }
- }
-
- /**
- * Gets a stat name frm the mappings.
- *
- * @param int $q Quality id.
- * @return string Quality (Uppercase)
- */
- private function map_quality_name($q){
- switch ($q){
- case QUALITY_ID::NORMAL:
- return "NORMAL";
- case QUALITY_ID::MAGIC:
- return "MAGIC";
- case QUALITY_ID::RARE:
- return "RARE";
- case QUALITY_ID::HERO:
- return "HERO";
- case QUALITY_ID::LEGEND:
- return "LEGEND";
- default:
- return "";
- }
- }
-
- /**
- * Calcuates the efficiency of the rune.
- *
- * @return float Efficiency
- */
- private function calculate_efficiency(){
- $eff = [0.0, 0.0, 0.0, 0.0, 0.0];
-
- $query = "
- SELECT
- min,
- max
- FROM
- rune_roll_type,
- rune_stat_roll
- WHERE
- rune_roll_type.id = rune_stat_roll.type AND
- rune_roll_type.ancient = :ancient AND
- rune_roll_type.initial = :initial AND
- rune_roll_type.upgrade = :upgrade AND
- rune_stat_roll.stars = :stars AND
- rune_stat_roll.stat = :stat
- ";
-
- // Fixed stat
- if ($this->innate_stat != null && $this->innate_stat->get_value() > 0){
- $value = $this->innate_stat->get_value();
- $statement = get_context()->get_db()->prepare($query);
- $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
- $statement->bindValue(':initial', 1, SQLITE3_TEXT);
- $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
- $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
- $statement->bindValue(':stat', $this->innate_stat->stat, SQLITE3_TEXT);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $min_initial = 0;//$r["min"];
- $max_initial = $r["max"];
- $eff[0] = ($value - $min_initial) / (9 * ($max_initial - $min_initial));
- }
- }
- for ($i = 1; $i <= 4; $i ++){
- if (sizeof($this->stats) > $i && $this->stats[$i] != null && $this->stats[$i]->get_value() > 0){
- $value = $this->stats[$i]->get_value();
- $value = $this->stats[$i]->get_value();
- $statement = get_context()->get_db()->prepare($query);
- $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
- $statement->bindValue(':initial', 1, SQLITE3_TEXT);
- $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
- $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
- $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
- $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- $statement = get_context()->get_db()->prepare($query);
- $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
- $statement->bindValue(':initial', 0, SQLITE3_TEXT);
- $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
- $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
- $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
- $r_upgrade = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r_initial && $r_upgrade){
- $min_initial = 0;//$r_initial["min"];
- $max_initial = $r_initial["max"];
- $min_upgrade = 0;//$r_upgrade["min"];
- $max_upgrade = $r_upgrade["max"];
- $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 9 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
- }
- }
- }
- $efficiency = 0.0;
- for ($i = 0; $i <= 4; $i ++){
- $efficiency += $eff[$i];
- }
- $efficiency *= 100;
- return $efficiency;
- }
-
- /**
- * Calcuates the maximum efficiency of the rune.
- *
- * @return float Efficiency
- */
- private function calculate_maximum_efficiency(){
- $max_efficiency = $this->efficiency;
- if ($this->level < 12){
- $max_efficiency += (1/9);
- }
- if ($this->level < 9){
- $max_efficiency += (1/9);
- }
- if ($this->level < 6){
- $max_efficiency += (1/9);
- }
- if ($this->level < 3){
- $max_efficiency += (1/9);
- }
- return $max_efficiency;
- }
-
- }
- ?>
|