* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ require_once(PATH::ENTITY . "Entity.php"); require_once(PATH::ENTITY . "Unit.php"); require_once(PATH::ENTITY . "Monster.php"); require_once(PATH::ENTITY . "Area.php"); /** * A record run. * * Represents the best run in an area, as in the in-game log book. * * @category Entity */ class Record extends Entity{ /** * @var int Player identifier. */ private $player; /** * @var Area Area. */ private $area; /** * @var int Stage. */ private $stage; /** * @var int Clear time, milliseconds. */ private $time; /** * @var int Score in battles that are rated (WB, rift dungeons...). */ private $score; /** * @var String Rank in battles that are rated (WB, rift dungeons...). */ private $rank; /** * @var bool Internal flag to check if the run party has been loaded into the entity. */ private $flag_party = false; /** * @var Unit[]|Monster[] Units or monsters used. * If the unit is still owned, it will be a instance of Unit. * If not, it will be a instance of Monster. */ private $party = []; /** * @var int ID of the leader unit or monster. */ private $leader; /** * @var int[] List of IDs of the frontline units or monsters. */ private $frontline = []; /** * Constructor. * * Searches the database and retrieves the information about the * record, populating it and it's items. * * @param int $player_id Selected player identifier. * @param int $area_type Area type identifier. * @param int $area Area identifier. */ public function __construct($player_id, $area_type, $area){ $statement = get_context()->get_db()->prepare(" SELECT player, area_type, area, stage, time, score, rank FROM record WHERE player = :player AND area_type = :area_type AND area = :area; "); $statement->bindValue(':player', $player_id, SQLITE3_TEXT); $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT); $statement->bindValue(':area', $area, SQLITE3_TEXT); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r){ $this->player = $r["player"]; $this->area = new Area($r["area"], $r["area_type"]); $this->stage = $r["stage"]; $this->time = $r["time"]; if (intval($r["score"]) > 0){ $this->score = $r["score"]; } if (strlen($r["rank"]) > 0){ $this->rank = $r["rank"]; } } } /** * Loads the record units into the entity. * * Must be called only once before trying to get the party units, leader or positions. Sets * the flag {@link Record:$flag_party} to true. */ private function load_party(){ $statement = get_context()->get_db()->prepare(" SELECT unit, monster, (SELECT count(id) FROM unit WHERE unit.id = record_party.unit) AS owned, leader, front FROM record_party WHERE player = :player AND area_type = :area_type AND area = :area ORDER BY front DESC, leader DESC; "); $statement->bindValue(':player', $this->player, SQLITE3_TEXT); $statement->bindValue(':area_type', $this->area->get_type(), SQLITE3_TEXT); $statement->bindValue(':area', $this->get_area()->get_id(), SQLITE3_TEXT); $q = $statement->execute(); while ($r = $q->fetchArray(SQLITE3_ASSOC)){ if ($r["owned"] > 0){ array_push($this->party, new Unit($r["unit"], false)); if ($r["leader"] == 1){ $this->leader = $r["unit"]; } if ($r["front"] == 1){ array_push($this->frontline, $r["unit"]); } } else{ array_push($this->party, new Monster($r["monster"], false)); if ($r["leader"] == 1){ $this->leader = $r["monster"]; } if ($r["front"] == 1){ array_push($this->frontline, $r["monster"]); } } } } /** * Retrieves the record holder's player identifier. * * @return int Player ID. */ public function get_player(){ return $this->player; } /** * Retrieves the area of the record * * @return Area Record area. */ public function get_area(){ return $this->area; } /** * Retrieves the stage of the record. * * @return int Record stage. */ public function get_stage(){ return $this->stage; } /** * Retrieves the time of the record. * * @return int record time, in milliseconds. */ public function get_time(){ return $this->time; } /** * Retrieves the score of the record. * * @return int Record score. Null if it's not a score based record. */ public function get_score(){ return $this->score; } /** * Retrieves the rank reached on the record. * * @return string Rank, in human readable form. Null if not a rank based record. */ public function get_rank(){ return $this->rank; } /** * Retrieves the party used in the record. If one of the units is not still owned, it will get * the base monster instead. * * @return (Unit|Monster)[] Party units or monsters. */ public function get_party(){ if (! $this->flag_party){ $this->load_party(); } return $this->party; } /** * Retrieves the party leader identifier. If the leader unit is not still owned, it will get * the base monster identifierinstead. * * @return int Leader unit or monster ID. */ public function get_leader(){ if (! $this->flag_party){ $this->load_party(); } return $this->leader; } /** * Retrieves the identifiers of the party members in the frontline. If one of the units is not * still owned, it will get the base monster instead. * * @return int Unit or Monster identifiers. */ public function get_frontline(){ if (! $this->flag_party){ $this->load_party(); } return $this->frontline; } }