Record.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Record run entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::ENTITY . "Entity.php");
  12. require_once(PATH::ENTITY . "Unit.php");
  13. require_once(PATH::ENTITY . "Monster.php");
  14. require_once(PATH::ENTITY . "Area.php");
  15. /**
  16. * A record run.
  17. *
  18. * Represents the best run in an area, as in the in-game log book.
  19. *
  20. * @category Entity
  21. */
  22. class Record extends Entity{
  23. /**
  24. * @var int Player identifier.
  25. */
  26. private $player;
  27. /**
  28. * @var Area Area.
  29. */
  30. private $area;
  31. /**
  32. * @var int Stage.
  33. */
  34. private $stage;
  35. /**
  36. * @var int Clear time, milliseconds.
  37. */
  38. private $time;
  39. /**
  40. * @var int Score in battles that are rated (WB, rift dungeons...).
  41. */
  42. private $score;
  43. /**
  44. * @var String Rank in battles that are rated (WB, rift dungeons...).
  45. */
  46. private $rank;
  47. /**
  48. * @var bool Internal flag to check if the run party has been loaded into the entity.
  49. */
  50. private $flag_party = false;
  51. /**
  52. * @var Unit[]|Monster[] Units or monsters used.
  53. * If the unit is still owned, it will be a instance of Unit.
  54. * If not, it will be a instance of Monster.
  55. */
  56. private $party = [];
  57. /**
  58. * @var int ID of the leader unit or monster.
  59. */
  60. private $leader;
  61. /**
  62. * @var int[] List of IDs of the frontline units or monsters.
  63. */
  64. private $frontline = [];
  65. /**
  66. * Constructor.
  67. *
  68. * Searches the database and retrieves the information about the
  69. * record, populating it and it's items.
  70. *
  71. * @param int $player_id Selected player identifier.
  72. * @param int $area_type Area type identifier.
  73. * @param int $area Area identifier.
  74. */
  75. public function __construct($player_id, $area_type, $area){
  76. $statement = get_context()->get_db()->prepare("
  77. SELECT
  78. player,
  79. area_type,
  80. area,
  81. stage,
  82. time,
  83. score,
  84. rank
  85. FROM record
  86. WHERE
  87. player = :player AND
  88. area_type = :area_type AND
  89. area = :area;
  90. ");
  91. $statement->bindValue(':player', $player_id, SQLITE3_TEXT);
  92. $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT);
  93. $statement->bindValue(':area', $area, SQLITE3_TEXT);
  94. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  95. if ($r){
  96. $this->player = $r["player"];
  97. $this->area = new Area($r["area"], $r["area_type"]);
  98. $this->stage = $r["stage"];
  99. $this->time = $r["time"];
  100. if (intval($r["score"]) > 0){
  101. $this->score = $r["score"];
  102. }
  103. if (strlen($r["rank"]) > 0){
  104. $this->rank = $r["rank"];
  105. }
  106. }
  107. }
  108. /**
  109. * Loads the record units into the entity.
  110. *
  111. * Must be called only once before trying to get the party units, leader or positions. Sets
  112. * the flag {@link Record:$flag_party} to true.
  113. */
  114. private function load_party(){
  115. $statement = get_context()->get_db()->prepare("
  116. SELECT
  117. unit,
  118. monster,
  119. (SELECT count(id) FROM unit WHERE unit.id = record_party.unit) AS owned,
  120. leader,
  121. front
  122. FROM record_party
  123. WHERE
  124. player = :player AND
  125. area_type = :area_type AND
  126. area = :area
  127. ORDER BY
  128. front DESC,
  129. leader DESC;
  130. ");
  131. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  132. $statement->bindValue(':area_type', $this->area->get_type(), SQLITE3_TEXT);
  133. $statement->bindValue(':area', $this->get_area()->get_id(), SQLITE3_TEXT);
  134. $q = $statement->execute();
  135. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  136. if ($r["owned"] > 0){
  137. array_push($this->party, new Unit($r["unit"], false));
  138. if ($r["leader"] == 1){
  139. $this->leader = $r["unit"];
  140. }
  141. if ($r["front"] == 1){
  142. array_push($this->frontline, $r["unit"]);
  143. }
  144. }
  145. else{
  146. array_push($this->party, new Monster($r["monster"], false));
  147. if ($r["leader"] == 1){
  148. $this->leader = $r["monster"];
  149. }
  150. if ($r["front"] == 1){
  151. array_push($this->frontline, $r["monster"]);
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * Retrieves the record holder's player identifier.
  158. *
  159. * @return int Player ID.
  160. */
  161. public function get_player(){
  162. return $this->player;
  163. }
  164. /**
  165. * Retrieves the area of the record
  166. *
  167. * @return Area Record area.
  168. */
  169. public function get_area(){
  170. return $this->area;
  171. }
  172. /**
  173. * Retrieves the stage of the record.
  174. *
  175. * @return int Record stage.
  176. */
  177. public function get_stage(){
  178. return $this->stage;
  179. }
  180. /**
  181. * Retrieves the time of the record.
  182. *
  183. * @return int record time, in milliseconds.
  184. */
  185. public function get_time(){
  186. return $this->time;
  187. }
  188. /**
  189. * Retrieves the score of the record.
  190. *
  191. * @return int Record score. Null if it's not a score based record.
  192. */
  193. public function get_score(){
  194. return $this->score;
  195. }
  196. /**
  197. * Retrieves the rank reached on the record.
  198. *
  199. * @return string Rank, in human readable form. Null if not a rank based record.
  200. */
  201. public function get_rank(){
  202. return $this->rank;
  203. }
  204. /**
  205. * Retrieves the party used in the record. If one of the units is not still owned, it will get
  206. * the base monster instead.
  207. *
  208. * @return (Unit|Monster)[] Party units or monsters.
  209. */
  210. public function get_party(){
  211. if (! $this->flag_party){
  212. $this->load_party();
  213. }
  214. return $this->party;
  215. }
  216. /**
  217. * Retrieves the party leader identifier. If the leader unit is not still owned, it will get
  218. * the base monster identifierinstead.
  219. *
  220. * @return int Leader unit or monster ID.
  221. */
  222. public function get_leader(){
  223. if (! $this->flag_party){
  224. $this->load_party();
  225. }
  226. return $this->leader;
  227. }
  228. /**
  229. * Retrieves the identifiers of the party members in the frontline. If one of the units is not
  230. * still owned, it will get the base monster instead.
  231. *
  232. * @return int Unit or Monster identifiers.
  233. */
  234. public function get_frontline(){
  235. if (! $this->flag_party){
  236. $this->load_party();
  237. }
  238. return $this->frontline;
  239. }
  240. }