| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- /**
- * Record run entity file.
- *
- * Creates the entity and makes it available.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @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;
- }
- }
|