Team.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Team 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 . "Area.php");
  13. require_once(PATH::ENTITY . "Unit.php");
  14. /**
  15. * A Team.
  16. *
  17. * Represents an object from the table 'unit'.
  18. *
  19. * @category Entity
  20. */
  21. class Team extends Entity{
  22. /**
  23. * @var int Owner's player identifier.
  24. */
  25. private $player;
  26. /**
  27. * @var int Team identifier.
  28. */
  29. private $id;
  30. /**
  31. * @var string Team name.
  32. */
  33. private $name;
  34. /**
  35. * @var string Team description.
  36. */
  37. private $description;
  38. private $flag_units = false;
  39. /**
  40. * @var \Unit[] Units in the team.
  41. */
  42. private $unit = [];
  43. /**
  44. * @var int ID of the leader unit.
  45. */
  46. private $leader;
  47. /**
  48. * @var int[] List of IDs of the frontline units.
  49. */
  50. private $frontline = [];
  51. /**
  52. * @var Area Area the team is designed for.
  53. */
  54. private $area;
  55. /**
  56. * @var int Stage the team is designed for. It can be null.
  57. */
  58. private $stage;
  59. /**
  60. * @var int difficulty the team is designed for. It can be null.
  61. *
  62. * @see DIFFICULTY_ID
  63. */
  64. private $difficulty;
  65. /**
  66. * @var int Team score for the optmizer
  67. */
  68. private $score;
  69. /**
  70. * Constructor.
  71. *
  72. * Searches the database and retrieves the information about the
  73. * monster, populating it and it's items.
  74. *
  75. * @param int $id Monster identifier.
  76. * @param bool $complete If false, it won't load the units.
  77. */
  78. public function __construct($id){
  79. $statement = get_context()->get_db()->prepare("
  80. SELECT
  81. player,
  82. id,
  83. name,
  84. description,
  85. area_type,
  86. area,
  87. stage,
  88. difficulty,
  89. score
  90. FROM team
  91. WHERE id = :id;
  92. ");
  93. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  94. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  95. if ($r){
  96. $this->player = $r["player"];
  97. $this->id = $r["id"];
  98. $this->name = HTML::e($r["name"]);
  99. $this->description = HTML::e($r["description"]);
  100. $this->area = new Area($r["area"], $r["area_type"]);
  101. $this->stage = $r["stage"];
  102. $this->score = $r["score"];
  103. }
  104. }
  105. private function load_units(){
  106. $statement = get_context()->get_db()->prepare("
  107. SELECT
  108. unit,
  109. leader,
  110. front
  111. FROM team_unit
  112. WHERE team = :team
  113. ORDER BY
  114. front DESC,
  115. leader DESC;
  116. ");
  117. $statement->bindValue(':team', $this->id, SQLITE3_TEXT);
  118. $q = $statement->execute();
  119. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  120. array_push($this->unit, new Unit($r["unit"], false));
  121. if ($r["leader"] == 1){
  122. $this->leader = $r["unit"];
  123. }
  124. if ($r["front"] == 1){
  125. array_push($this->frontline, $r["unit"]);
  126. }
  127. }
  128. $this->flag_units = true;
  129. }
  130. /**
  131. * Retrieves the account's player identifier.
  132. *
  133. * @return number Player ID.
  134. */
  135. public function get_player(){
  136. return $this->player;
  137. }
  138. /**
  139. * Retrieves the team identifier.
  140. *
  141. * @return number Team ID.
  142. */
  143. public function get_id(){
  144. return $this->id;
  145. }
  146. /**
  147. * Retrieves the team name.
  148. *
  149. * @return string Team name.
  150. */
  151. public function get_name(){
  152. return $this->name;
  153. }
  154. /**
  155. * Retrieves the team description.
  156. *
  157. * @return string Team description (can be an empty string).
  158. */
  159. public function get_description(){
  160. return $this->description;
  161. }
  162. /**
  163. * Retrieves the units in the team.
  164. *
  165. * @return Unit[] Units in the team.
  166. */
  167. public function get_units(){
  168. if (! $this->flag_units){
  169. $this->load_units();
  170. }
  171. return $this->unit;
  172. }
  173. /**
  174. * Retrieves the leader unit's identifier.
  175. *
  176. * @return number Leader unit ID.
  177. */
  178. public function get_leader(){
  179. return $this->leader;
  180. }
  181. /**
  182. * Retrieves the identifiers of the units in the frontline.
  183. *
  184. * @return number[] ID of units in the frontline.
  185. */
  186. public function get_frontline(){
  187. return $this->frontline;
  188. }
  189. /**
  190. * Retrieves the area the team is set for.
  191. *
  192. * @return Area Team area.
  193. */
  194. public function get_area(){
  195. return $this->area;
  196. }
  197. /**
  198. * Retrieves the stage the team is intended to.
  199. *
  200. * @return number Team stage, can be null.
  201. */
  202. public function get_stage(){
  203. return $this->stage;
  204. }
  205. /**
  206. * Retrieves the difficulty of the area the team is intended for.
  207. *
  208. * @see DIFFICULTY_ID
  209. *
  210. * @return number Difficulty ID, can be null.
  211. */
  212. public function get_difficulty(){
  213. return $this->difficulty;
  214. }
  215. /**
  216. * Retrieves the score of the team for the optimizer.
  217. *
  218. * @return number Team score.
  219. */
  220. public function get_score(){
  221. return $this->score;
  222. }
  223. }
  224. ?>