Team.php 5.6 KB

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