Record.php 6.5 KB

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