| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732 |
- <?php
- /**
- * Unit 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 . "K_Unit.php");
- require_once(PATH::ENTITY . "Team.php");
- require_once(PATH::ENTITY . "Rune.php");
- require_once(PATH::ENTITY . "Artifact.php");
- require_once(PATH::ENTITY . "Building.php");
- /**
- * A Unit.
- *
- * Represents an object from the table 'unit'.
- *
- * @category Entity
- */
- class Unit extends Entity{
- /**
- * @var int User ID.
- */
- public $uid;
- /**
- * @var int Unit ID.
- */
- public $id;
- /**
- * @var Building Building the unit is on.
- */
- public $building;
- /**
- * @var K_Unit Base unit.
- */
- public $unit;
- /**
- * @var int Unit stars.
- */
- public $stars;
- /**
- * @var int Unit level.
- */
- public $level;
- /**
- * @var int Unit base HP stat.
- */
- public $hp;
- /**
- * @var int Unit base ATK stat.
- */
- public $attack;
- /**
- * @var int Unit base DEF stat.
- */
- public $defense;
- /**
- * @var int Unit base SPD stat.
- */
- public $speed;
- /**
- * @var int Unit base CRR stat.
- */
- public $crit_rate;
- /**
- * @var int Unit base CRD stat.
- */
- public $crit_damage;
- /**
- * @var int Unit base RES stat.
- */
- public $resistance;
- /**
- * @var int Unit base ACC stat.
- */
- public $accuracy;
- /**
- * @var int Total gained experience.
- */
- public $experience;
- /**
- * @var int Total gained experience in its current level.
- */
- public $exp_gained;
- /**
- * @var int Experience / hour in the current building.
- */
- public $exp_gain_rate;
- /**
- * @var int Equipped transmogification.
- */
- public $costume;
- /**
- * @var K_Source Source the unit was aquired from.
- */
- public $source;
- /**
- * @var Datetime Time the unit was obtained.
- */
- public $create_time;
- /**
- * @var string Indicates the Homunculus name
- */
- public $homunculus_name;
- /**
- * @var bool Indicates if the monster is locked.
- */
- public $lock;
- /**
- * @var int Unit rune HP stat.
- */
- public $rune_hp = 0;
- /**
- * @var int Unit rune ATK stat.
- */
- public $rune_attack = 0;
- /**
- * @var int Unit rune DEF stat.
- */
- public $rune_defense = 0;
- /**
- * @var int Unit rune SPD stat.
- */
- public $rune_speed = 0;
- /**
- * @var int Unit rune CRR stat.
- */
- public $rune_crit_rate = 0;
- /**
- * @var int Unit rune CRD stat.
- */
- public $rune_crit_damage = 0;
- /**
- * @var int Unit rune RES stat.
- */
- public $rune_resistance = 0;
- /**
- * @var int Unit rune ACC stat.
- */
- public $rune_accuracy = 0;
- /**
- * @var int Unit artifact HP stat.
- */
- public $artifact_hp = 0;
- /**
- * @var int Unit artifact ATK stat.
- */
- public $artifact_attack = 0;
- /**
- * @var int Unit artifact DEF stat.
- */
- public $artifact_defense = 0;
- /**
- * @var int Unit artifact SPD stat.
- */
- public $artifact_speed = 0;
- /**
- * @var int Unit artifact CRR stat.
- */
- public $artifact_crit_rate = 0;
- /**
- * @var int Unit artifact CRD stat.
- */
- public $artifact_crit_damage = 0;
- /**
- * @var int Unit artifact RES stat.
- */
- public $artifact_resistance = 0;
- /**
- * @var int Unit artifact ACC stat.
- */
- public $artifact_accuracy = 0;
- /**
- * @var int Unit building HP stat.
- */
- public $building_hp = 0;
- /**
- * @var int Unit building ATK stat.
- */
- public $building_attack = 0;
- /**
- * @var int Unit building DEF stat.
- */
- public $building_defense = 0;
- /**
- * @var int Unit building SPD stat.
- */
- public $building_speed = 0;
- /**
- * @var int Unit building CRR stat.
- */
- public $building_crit_rate = 0;
- /**
- * @var int Unit building CRD stat.
- */
- public $building_crit_damage = 0;
- /**
- * @var int Unit building RES stat.
- */
- public $building_resistance = 0;
- /**
- * @var int Unit building ACC stat.
- */
- public $building_accuracy = 0;
- /**
- * @var int Unit total HP stat.
- */
- public $total_hp = 0;
- /**
- * @var int Unit total ATK stat.
- */
- public $total_attack = 0;
- /**
- * @var int Unit total DEF stat.
- */
- public $total_defense = 0;
- /**
- * @var int Unit total SPD stat.
- */
- public $total_speed = 0;
- /**
- * @var int Unit total CRR stat.
- */
- public $total_crit_rate = 0;
- /**
- * @var int Unit total CRD stat.
- */
- public $total_crit_damage = 0;
- /**
- * @var int Unit total RES stat.
- */
- public $total_resistance = 0;
- /**
- * @var int Unit total ACC stat.
- */
- public $total_accuracy = 0;
- /**
- * @var int Unit effective HP
- */
- public $effective_hp = 0;
- /**
- * @var int Unit effective damage
- */
- public $effective_dmg = 0;
- /**
- * @var float Average rune efficiency.
- */
- public $avg_rune_efficiency = 0.0;
- /**
- * @var \Rune[] Equiped Runes.
- */
- public $rune = [];
- /**
- * @var Artifact Type artifact equipped.
- */
- public $artifact_type;
- /**
- * @var Artifact Elemental artifact equipped.
- */
- public $artifact_element;
- /**
- * @var \Team[] The Teamss the unit is on.
- */
- public $teams = [];
- /**
- * @var int[] Array with the levels of the skills, in orer.
- */
- public $skill_levels = [];
- /**
- * @var int Total experience required for the current level.
- */
- public $experience_level = 0;
- /**
- * @var int Experience required to level up.
- */
- public $experience_level_up = 0;
- /**
- * @var int Indcates if the unit is in storage.
- */
- public $in_storage = false;
- /**
- * @var string Unit title.
- * If Homunculus,it will be it's name.
- * If awakened, it will be the awakened from name.
- * If unawakened, it will be <element> <name>.
- */
- public $title;
- /**
- * Constructor.
- *
- * Searches the database and retrieves the information about the
- * monster, populating it and it's items.
- *
- * @param int $id Monster identifier.
- * @param bool $complete If false, query only monster and k_monster.
- * @global int Player ID.
- * @global resource Database connection.
- */
- public function __construct($id, $complete = true){
- global $UID;
- global $db;
- $s =
- "SELECT " .
- " uid, " .
- " id, " .
- " building, " .
- " unit, " .
- " stars, " .
- " level, " .
- " hp, " .
- " attack, " .
- " defense, " .
- " speed, " .
- " crit_rate, " .
- " crit_damage, " .
- " resistance, " .
- " accuracy, " .
- " experience, " .
- " exp_gained, " .
- " exp_gain_rate, " .
- " costume, " .
- " source, " .
- " create_time, " .
- " homunculus_name, " .
- " lock " .
- "FROM unit " .
- "WHERE " .
- " uid = '$UID' AND " .
- " id = '$id'; ";
- $q = $db->query($s);
- $r = $q->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $this->uid = $r["uid"];
- $this->id = $r["id"];
- if ($r["building"] != null && $r["building"] > 0){
- $this->building = new Building($r["building"]);
- }
- if (strlen($r["unit"]) > 0){
- $this->unit = new K_Unit($r["unit"], $complete);
- }
- $this->stars = $r["stars"];
- $this->level = $r["level"];
- $this->hp = $r["hp"];
- $this->attack = $r["attack"];
- $this->defense = $r["defense"];
- $this->speed = $r["speed"];
- $this->crit_rate = $r["crit_rate"];
- $this->crit_damage = $r["crit_damage"];
- $this->resistance = $r["resistance"];
- $this->accuracy = $r["accuracy"];
- $this->experience = $r["experience"];
- $this->exp_gained = $r["exp_gained"];
- $this->exp_gain_rate = $r["exp_gain_rate"];
- $this->costume = $r["costume"];
- $this->source = new K_Source($r["source"]);
- $this->create_time = date_create($r["create_time"]);
- $this->homunculus_name = $r["homunculus_name"];
- if ($complete){
- $this->load_stats();
- $this->load_teams();
- $this->check_storage();
- $this->lock = $r["lock"];
- $s =
- "SELECT level " .
- "FROM " .
- " unit_skill, " .
- " k_skill " .
- "WHERE " .
- " k_skill.id = unit_skill.skill AND " .
- " unit = '$this->id' " .
- "ORDER BY slot;";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- array_push($this->skill_levels, $r["level"]);
- }
- $s =
- "SELECT exp " .
- "FROM k_experience " .
- "WHERE " .
- " stars = $this->stars AND " .
- " level = $this->level;";
- $q = $db->query($s);
- $r = $q->fetchArray(SQLITE3_ASSOC);
- $this->experience_level = $r["exp"];
- $this->experience_level_up = $r["exp"] - $this->exp_gained;
- }
- if ($this->unit->homunculus){
- $this->title = $this->homunculus_name;
- }
- else{
- $this->title = $this->unit->title;
- }
- }
- }
- /**
- * Checks if the monster is in storage and sets {@see $in_storage} to
- * true or false.
- *
- * @global int Player ID.
- * @global resource Database connection.
- */
- public function check_storage(){
- global $UID;
- global $db;
- $s = "
- SELECT 1
- FROM
- unit,
- building
- WHERE
- unit.id = $this->id AND
- unit.uid = '$UID' AND
- building.uid = '$UID' AND
- unit.building = building.id AND
- building.building = '" . BUILDING_ID::MONSTER_STORAGE . "';
- ";
- $q = $db->query($s);
- if ($q->fetchArray(SQLITE3_ASSOC) == false){
- $this->in_storage = false;
- }
- else{
- $this->in_storage = true;
- }
- }
- /**
- * Calculates the stats gained from runes, artifacts and buildings.
- * Calculates the totalls and the effective stats.
- *
- * @gobal int User ID.
- * @global resource Database connection.
- */
- public function load_stats(){
- global $UID;
- global $db;
- // Runes
- $this->rune = [];
- $s =
- "SELECT id " .
- "FROM rune " .
- "WHERE assigned_to = '$this->id'; ";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- array_push($this->rune, new Rune($r["id"]));
- }
- foreach ($this->rune as $rune){
- $this->sum_rune_stat($rune->main_stat, $rune->main_stat_value);
- $this->sum_rune_stat($rune->innate_stat, $rune->innate_stat_value);
- $this->sum_rune_stat($rune->substat_1, $rune->substat_1_value + $rune->substat_1_grind);
- $this->sum_rune_stat($rune->substat_2, $rune->substat_2_value + $rune->substat_2_grind);
- $this->sum_rune_stat($rune->substat_3, $rune->substat_3_value + $rune->substat_3_grind);
- $this->sum_rune_stat($rune->substat_4, $rune->substat_4_value + $rune->substat_4_grind);
- }
- $this->rune_attack = ceil($this->rune_attack);
- $this->rune_defense = ceil($this->rune_defense);
- $this->rune_hp = ceil($this->rune_hp);
- // Artifacts
- $s =
- "SELECT id " .
- "FROM artifact " .
- "WHERE assigned_to = '$this->id'; ";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- $artifact = new Artifact($r["id"]);
- if ($artifact->type == ARTIFACT_TYPE::ARCHETYPE){
- $this->artifact_type = $artifact;
- }
- else{
- $this->artifact_element = $artifact;
- }
- switch($artifact->main_stat){
- case ARTIFACT_STAT_ID::ATK:
- $this->artifact_attack += $artifact->main_stat_value;
- break;
- case ARTIFACT_STAT_ID::DEF:
- $this->artifact_defense += $artifact->main_stat_value;
- break;
- case ARTIFACT_STAT_ID::HP:
- $this->artifact_hp += $artifact->main_stat_value;
- break;
- }
- }
- // Buildings
- $s = "
- SELECT
- affected_stat,
- bonus,
- element
- FROM
- k_decoration,
- k_decoration_level,
- decoration
- WHERE
- decoration.uid = '$UID' AND
- k_decoration.id = decoration.decoration AND
- decoration.decoration = k_decoration_level.decoration AND
- decoration.level = k_decoration_level.level AND
- area = " . EFFECT_AREA_ID::GENERAL . " AND
- affected_stat IN (" . STAT_ID::ATK . ", " . STAT_ID::DEF . ", " . STAT_ID::HP . ", " . STAT_ID::SPD . ", " . STAT_ID::CRIT_DMG . ");
- ";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- switch($r["affected_stat"]){
- case STAT_ID::DEF:
- $this->building_defense += ($this->defense * $r["bonus"] / 100);
- break;
- case STAT_ID::HP:
- $this->building_hp += ($this->hp * $r["bonus"] / 100);
- break;
- case STAT_ID::SPD:
- $this->building_speed += ($this->speed * $r["bonus"] / 100);
- break;
- case STAT_ID::CRIT_DMG:
- $this->building_crit_damage += $r["bonus"];
- break;
- case STAT_ID::ATK:
- if(strlen($r["element"]) > 0 && $r["element"] == $this->unit->element){
- $this->building_attack += ($this->attack * $r["bonus"] / 100);
- }
- else{
- $this->building_attack += ($this->attack * $r["bonus"] / 100);
- }
- break;
- }
- }
- $this->building_attack = ceil($this->building_attack);
- $this->building_defense = ceil($this->building_defense);
- $this->building_hp = ceil($this->building_hp);
- $this->building_speed = ceil($this->building_speed);
- $this->building_crit_damage = ceil($this->building_crit_damage);
- // Totals
- $this->total_hp = $this->hp + $this->rune_hp + $this->building_hp + $this->artifact_hp;
- $this->total_attack = $this->attack + $this->rune_attack + $this->building_attack + $this->artifact_attack;
- $this->total_defense = $this->defense + $this->rune_defense + $this->building_defense + $this->artifact_defense;
- $this->total_speed = $this->speed + $this->rune_speed + $this->building_speed;
- $this->total_crit_rate = $this->crit_rate + $this->rune_crit_rate + $this->building_crit_rate;
- $this->total_crit_damage = $this->crit_damage + $this->rune_crit_damage + $this->building_crit_damage;
- $this->total_accuracy = $this->accuracy + $this->rune_accuracy + $this->building_accuracy;
- $this->total_resistance = $this->resistance + $this->rune_resistance + $this->building_resistance;
-
- // Effective stats
- $ehp = 0;
- $edmg = 0;
-
- // Calculate effective_hp
- $hp = $this->total_hp;
- $def = $this->total_defense;
- $ehp = ceil(((($def * 3.5) + 1140) * $hp) / 1000);
- $this->effective_hp = $ehp;
- // Calculate effective_dmg
- $atk = $this->total_attack;
- $crr = $this->total_crit_rate;
- if ($crr > 100){
- $crr = 100;
- }
- $crd = $this->total_crit_damage;
- $edmg = ceil(($atk * (100 - $crr) / 100) + (($atk + ($atk * $crd / 100)) * $crr / 100));
- $this->effective_dmg = $edmg;
- }
- /**
- * Loads the {@see teams} array, with the teams the unit is on.
- *
- * @global int Player ID.
- * @global resource Database connection.
- */
- public function load_teams(){
- global $UID;
- global $db;
- $this->teams = [];
- $s = "
- SELECT DISTINCT team.id AS id
- FROM
- team,
- team_unit
- WHERE
- team.uid = '$UID' AND
- team.id = team_unit.team AND
- team_unit.unit = '" . $this->id . "';
- ";
- $q = $db->query($s);
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- array_push($this->teams, new Team($r["id"], false));
- }
- }
- /**
- * Calculates the stat increase by a rune property.
- *
- * @param string $stat Stat type.
- * @param int $value Stat increase value.
- */
- private function sum_rune_stat($stat, $value){
- switch ($stat){
- case RUNE_STAT_ID::ATK:
- $this->rune_attack += $value;
- break;
- case RUNE_STAT_ID::DEF:
- $this->rune_defense += $value;
- break;
- case RUNE_STAT_ID::HP:
- $this->rune_hp += $value;
- break;
- case RUNE_STAT_ID::SPD:
- $this->rune_speed += $value;
- break;
- case RUNE_STAT_ID::CRR:
- $this->rune_crit_rate += $value;
- break;
- case RUNE_STAT_ID::CRD:
- $this->rune_crit_damage += $value;
- break;
- case RUNE_STAT_ID::ACC:
- $this->rune_accuracy += $value;
- break;
- case RUNE_STAT_ID::RES:
- $this->rune_resistance += $value;
- break;
- case RUNE_STAT_ID::ATK_P:
- $this->rune_attack += ($this->attack * $value / 100);
- break;
- case RUNE_STAT_ID::DEF_P:
- $this->rune_defense += ($this->defense * $value / 100);
- break;
- case RUNE_STAT_ID::HP_P:
- $this->rune_hp += ($this->hp * $value / 100);
- break;
- }
- }
- /**
- * Checks if the unit skills are completly maxed.
- *
- * @return bool True if all skills maxed, false otherwise.
- */
- public function are_skills_maxed(){
- $maxed = true;
- for ($i = 0; $i < sizeof($this->unit->skill); $i ++){
- if ($this->unit->skill[$i]->max_level > $this->skill_levels[$i]){
- $maxed = false;
- break;
- }
- }
- return $maxed;
- }
- }
- ?>
|