Skill.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Skill 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 . "Skill_Level.php");
  13. require_once(PATH::ENTITY . "Effect.php");
  14. /**
  15. * Monster skill.
  16. *
  17. * Represents a monster skill.
  18. *
  19. * @category Entity
  20. */
  21. class Skill extends Entity{
  22. /**
  23. * @var int Skill identifier.
  24. */
  25. private $id;
  26. /**
  27. * @var string Skill name.
  28. */
  29. private $name;
  30. /**
  31. * @var string Skill description.
  32. */
  33. private $description;
  34. /**
  35. * @var int Skill order.
  36. */
  37. private $slot;
  38. /**
  39. * @var int Skill cooldown turns.
  40. */
  41. private $cooltime;
  42. /**
  43. * @var int Number of hits.
  44. */
  45. private $hits;
  46. /**
  47. * @var bool Wether the skill is passive or not.
  48. */
  49. private $passive;
  50. /**
  51. * @var bool Wether the skill is AOE.
  52. */
  53. private $aoe;
  54. /**
  55. * @var int Skill max level.
  56. */
  57. private $max_level;
  58. /**
  59. * @var string Skill formula.
  60. */
  61. private $multiplier_formula;
  62. /**
  63. * @var Skill The skill upgrading this one.
  64. */
  65. private $upgrade;
  66. /**
  67. * @var bool Internal flag to check if the levels have been loaded into the entity.
  68. */
  69. private $flag_levels = false;
  70. /**
  71. * @var Skill_Level[] Available skill levels.
  72. */
  73. private $level = [];
  74. /**
  75. * @var bool Internal flag to check if the skill effects have been loaded into the entity.
  76. */
  77. private $flag_effects = false;
  78. /**
  79. * @var Effect[] Associated eeffects.
  80. */
  81. private $effect = [];
  82. /**
  83. * Constructor.
  84. *
  85. * Searches the database and retrieves the information about the
  86. * skill, populating it and it's items.
  87. *
  88. * @param int $id Skill identifier.
  89. */
  90. public function __construct($id){
  91. $statement = get_context()->get_db()->prepare("
  92. SELECT
  93. id,
  94. name,
  95. description,
  96. slot,
  97. cooltime,
  98. hits,
  99. passive,
  100. aoe,
  101. max_level,
  102. multiplier_formula
  103. FROM skill
  104. WHERE id = :id;
  105. ");
  106. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  107. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  108. if ($r){
  109. $this->id = $r["id"];
  110. $this->name = HTML::e($r["name"]);
  111. $this->description = HTML::e($r["description"]);
  112. $this->slot = $r["slot"];
  113. $this->cooltime = $r["cooltime"];
  114. $this->hits = $r["hits"];
  115. $this->passive = $r["passive"];
  116. $this->aoe = $r["aoe"];
  117. $this->max_level = $r["max_level"];
  118. $this->multiplier_formula = HTML::e($r["multiplier_formula"]);
  119. }
  120. }
  121. /**
  122. * Loads the levels into the entity.
  123. *
  124. * Must be called only once before trying to access the levels. Sets the flag
  125. * {@link Skill:$flag_levels} to true.
  126. */
  127. private function load_levels(){
  128. $statement = get_context()->get_db()->prepare("
  129. SELECT
  130. skill,
  131. level
  132. FROM skill_level
  133. WHERE skill = :skill
  134. ORDER BY level;
  135. ");
  136. $statement->bindValue(':skill', $this->id, SQLITE3_TEXT);
  137. $q = $statement->execute();
  138. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  139. array_push($this->level, new Skill_Level($r["skill"], $r["level"]));
  140. }
  141. $this->flag_levels = true;
  142. }
  143. /**
  144. * Loads the effects into the entity.
  145. *
  146. * Must be called only once before trying to access the effects. Sets the flag
  147. * {@link Skill:$flag_effects} to true.
  148. */
  149. private function load_effects(){
  150. $statement = get_context()->get_db()->prepare("
  151. SELECT DISTINCT effect
  152. FROM skill_effect
  153. WHERE skill = :skill;
  154. ");
  155. $statement->bindValue(':skill', $this->id, SQLITE3_TEXT);
  156. $q = $statement->execute();
  157. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  158. array_push($this->effect, new Effect($r["effect"]));
  159. }
  160. $this->flag_effects = true;
  161. }
  162. /**
  163. * Retrieves the skill identifier.
  164. *
  165. * @return int Skill id.
  166. */
  167. public function get_id(){
  168. return $this->id;
  169. }
  170. /**
  171. * Retrieves the skill name.
  172. *
  173. * @return string Sill name.
  174. */
  175. public function get_name(){
  176. return $this->name;
  177. }
  178. /**
  179. * Retrieves the skill description.
  180. *
  181. * @return string Skill description.
  182. */
  183. public function get_description(){
  184. return $this->description;
  185. }
  186. /**
  187. * Retrieves the skill slot.
  188. *
  189. * @return int The skill slot (1-4);
  190. */
  191. public function get_slot(){
  192. return $this->slot;
  193. }
  194. /**
  195. * Retrieves the skill cooldown time
  196. *
  197. * @return int Skill cooltime.
  198. */
  199. public function get_cooltime(){
  200. return $this->cooltime;
  201. }
  202. /**
  203. * Retrieves the number of times the skill hist.
  204. *
  205. * @return int Number of hits.
  206. */
  207. public function get_hits(){
  208. return $this->hits;
  209. }
  210. /**
  211. * Checks if the skill is passive.
  212. *
  213. * @return bool True for passive skills, false for active.
  214. */
  215. public function is_passive(){
  216. return $this->passive;
  217. }
  218. /**
  219. * Checks if the skills affects a group instead of a unit.
  220. *
  221. * @return bool True for AOE skills, false for single-target skills.
  222. */
  223. public function is_aoe(){
  224. return $this->aoe;
  225. }
  226. /**
  227. * Retrieves the maximum level of the skill.
  228. *
  229. * @return int Skill max level.
  230. */
  231. public function get_max_level(){
  232. return $this->max_level;
  233. }
  234. /**
  235. * Retrieves the skills mathematical formula.
  236. *
  237. * @return string Skill formula.
  238. */
  239. public function get_multiplier_formula(){
  240. return $this->multiplier_formula;
  241. }
  242. /**
  243. * Retrieves the skill this one upgrades to (ie, on awakening).
  244. *
  245. * @return Skill Upgraded skill, null if not upgradeable.
  246. */
  247. public function get_upgrade(){
  248. return $this->upgrade;
  249. }
  250. /**
  251. * Retrieves information about the skill levels.
  252. *
  253. * @return Skill_Level[] Levels of the skill.
  254. */
  255. public function get_levels(){
  256. if (! $this->flag_levels){
  257. $this->load_levels();
  258. }
  259. return $this->level;
  260. }
  261. /**
  262. * Retrieves the effects caused by the skill.
  263. *
  264. * @return Effect[] Effects caused by the skill.
  265. */
  266. public function get_effects(){
  267. if (! $this->flag_effects){
  268. $this->load_effects();
  269. }
  270. return $this->effect;
  271. }
  272. /**
  273. * Gets the path to the skill image.
  274. *
  275. * @return string Image URL, or a fixed unknown image.
  276. */
  277. public function get_image(){
  278. return APPLICATION::img("SKILL", $this->id);
  279. }
  280. }