PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/rpg/src/AppBundle/Manager/SkillAPIManager.php

https://gitlab.com/EleonoraUA/rpg_server
PHP | 374 lines | 233 code | 27 blank | 114 comment | 29 complexity | 233a54bac25ade3239503bda0113c456 MD5 | raw file
  1. <?php
  2. /**
  3. * Class File SkillAPIManager
  4. *
  5. * PHP version 5.5
  6. *
  7. * @package AppBundle\Manager
  8. */
  9. namespace AppBundle\Manager;
  10. use AppBundle\Manager\Traits\BattleTrait;
  11. use AppBundle\Manager\Traits\ErrorCodesTrait;
  12. use AppBundle\Entity\Personage;
  13. use AppBundle\Entity\Skill;
  14. use AppBundle\Entity\CharToSkill;
  15. use AppBundle\Manager\Traits\SkillFieldsTrait;
  16. use AppBundle\Manager\Traits\UserInfoFieldsTrait;
  17. /**
  18. * Class SkillAPIManager is used to store logic for skills
  19. *
  20. * @package AppBundle\Manager
  21. */
  22. class SkillAPIManager extends Manager
  23. {
  24. use ErrorCodesTrait;
  25. use UserInfoFieldsTrait;
  26. use SkillFieldsTrait;
  27. use BattleTrait;
  28. /**
  29. * Get information about personage's skills
  30. *
  31. * @param Personage $personage
  32. *
  33. * @return array
  34. */
  35. public function getSkillsProfile($personage)
  36. {
  37. $skills = $this->getEm()->getRepository('AppBundle:CharToSkill')->findBy(array($this->CHAR => $personage));
  38. $skills_profile = array();
  39. foreach ($skills as $skill) {
  40. $skill_profile[$this->SKILL_ID] = $skill->getSkill()->getId();
  41. $skill_profile[$this->IS_SELECTED_SKILL] = $skill->getIsSelected();
  42. $skill_profile[$this->REQUIRED_LVL] = $skill->getSkill()->getRequiredLvl();
  43. $skills_profile[] = $skill_profile;
  44. }
  45. return $skills_profile;
  46. }
  47. /**
  48. * Get all skills of personage
  49. *
  50. * @param Personage $personage personage entity
  51. *
  52. * @access public
  53. *
  54. * @return array
  55. */
  56. public function getSkillsOfPersonage($personage)
  57. {
  58. $em = $this->getEm();
  59. $char_skills = $em->getRepository('AppBundle:CharToSkill')->findBy(array($this->CHAR => $personage));
  60. $skills = array();
  61. foreach ($char_skills as $char_skill) {
  62. array_push($skills, $char_skill);
  63. }
  64. return $skills;
  65. }
  66. /**
  67. * Get skills for personage when arena initialize
  68. *
  69. * @param array $skillIds
  70. *
  71. * @return array
  72. */
  73. public function getArenaSkillsForPersonage($skillIds)
  74. {
  75. return $this->getEm()->getRepository('AppBundle:Skill')->findSkillsBySkillsIds($skillIds);
  76. }
  77. /**
  78. * Get selected skills of personage
  79. *
  80. * @param Personage $personage personage entity
  81. *
  82. * @access public
  83. *
  84. * @return array
  85. */
  86. public function getSelectedSkillsOfPersonage($personage)
  87. {
  88. $em = $this->getEm();
  89. $char_skills = $em
  90. ->getRepository('AppBundle:CharToSkill')
  91. ->findBy(array($this->CHAR => $personage, $this->IS_SELECTED => true));
  92. $skills = array();
  93. foreach ($char_skills as $char_skill) {
  94. array_push($skills, $char_skill->getSkill());
  95. }
  96. return $skills;
  97. }
  98. /**
  99. * Check if skill can be used
  100. *
  101. * @param Personage $personage
  102. *
  103. * @param Skill $skill
  104. *
  105. * @return bool
  106. */
  107. public function skillCanBeUsed($personage, $skill)
  108. {
  109. if (!$personage->getClass()->getSkills()->contains($skill)) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. /**
  115. * Add skill to personage
  116. *
  117. * @param Personage $personage
  118. *
  119. * @param Skill $skill
  120. *
  121. * @return bool
  122. */
  123. public function setSkillToPersonage($personage, $skill)
  124. {
  125. if (!$personage->getClass()->getSkills()->contains($skill)) {
  126. return false;
  127. }
  128. if ($this->getEm()
  129. ->getRepository('AppBundle:CharToSkill')
  130. ->findOneBy(array($this->CHAR => $personage, $this->SKILL => $skill))) {
  131. return false;
  132. }
  133. $charToSkill = new CharToSkill($personage, $skill, false);
  134. $this->getEm()->persist($charToSkill);
  135. $this->getEm()->flush();
  136. return true;
  137. }
  138. /**
  139. * Get skill by skill_id
  140. *
  141. * @param $skillId
  142. *
  143. * @return null|object
  144. */
  145. public function getSkillByID($skillId)
  146. {
  147. $skill = $this->getEm()->getRepository('AppBundle:Skill')->findOneBy(array($this->ID => $skillId));
  148. return $skill;
  149. }
  150. /**
  151. * Count selected skills in database
  152. *
  153. * @param $allPersonageSkills
  154. * @param $skills
  155. *
  156. * @return int
  157. */
  158. public function countChanges($allPersonageSkills, $skills)
  159. {
  160. $countChanges = 0;
  161. foreach ($allPersonageSkills as $skill) {
  162. if (in_array($skill->getSkill()->getId(), $skills)) {
  163. $skill->setIsSelected(1);
  164. $countChanges++;
  165. } else {
  166. $skill->setIsSelected(0);
  167. }
  168. $this->getEm()->persist($skill);
  169. }
  170. return $countChanges;
  171. }
  172. /**
  173. * Validate arena skills
  174. *
  175. * @param Personage $personage
  176. *
  177. * @param array $skillIds
  178. *
  179. * @return bool
  180. */
  181. public function arenaSkillsAreValid($personage, $skillIds)
  182. {
  183. if (count($skillIds) != $this->ARENA_SKILLS_NUMBER) {
  184. return false;
  185. }
  186. $skills = $this->getEm()->getRepository('AppBundle:Skill')->findSkillsBySkillsIds($skillIds);
  187. if (count($skills) != $this->ARENA_SKILLS_NUMBER) {
  188. return false;
  189. }
  190. foreach ($skills as $skill) {
  191. if (!$this->skillCanBeUsed($personage, $skill)) {
  192. return false;
  193. }
  194. }
  195. return true;
  196. }
  197. /**
  198. * Create a response for getSkillByIdAction
  199. *
  200. * @param $skillId
  201. *
  202. * @return array
  203. */
  204. public function getResponseForSkillByIdAction($skillId)
  205. {
  206. $skill = $this
  207. ->getEm()
  208. ->getRepository('AppBundle:Skill')
  209. ->findOneBy(array($this->ID => $skillId));
  210. if (!$skill) {
  211. return array(
  212. $this->STATUS => $this->SKILL_WITH_SUCH_ID_NOT_FOUND
  213. );
  214. } else {
  215. $response[$this->SKILL] = array(
  216. $this->NAME => $skill->getName(),
  217. $this->DESCRIPTION =>$skill->getDescription(),
  218. $this->MULTIPLIER => $skill->getMultiplier()
  219. );
  220. }
  221. $response[$this->STATUS] = $this->STATUS_OK;
  222. return $response;
  223. }
  224. /**
  225. * Create a response for selectSkillsAction
  226. *
  227. * @param $data
  228. *
  229. * @return array
  230. */
  231. public function getResponseForSelectSkillsAction($data)
  232. {
  233. global $kernel;
  234. $validatedResponse = $kernel->getContainer()->get('app.manager.validator')->getPersonageFromData($data);
  235. if (!$validatedResponse[$this->STATUS] == $this->STATUS_OK) {
  236. return $validatedResponse;
  237. }
  238. $personage = $validatedResponse[$this->PERSONAGE];
  239. $allPersonageSkills = $this->getSkillsOfPersonage($personage);
  240. if (!$allPersonageSkills) {
  241. return array(
  242. $this->STATUS => $this->PERSONAGE_HAS_NO_ANY_SKILLS
  243. );
  244. }
  245. $skillsId = $data[$this->SKILLS];
  246. if (!$skillsId) {
  247. return array(
  248. $this->STATUS => $this->EMPTY_SKILLS_TO_SELECT
  249. );
  250. }
  251. if (count($skillsId) > $personage->getActiveSkillsBagSize()) {
  252. return array(
  253. $this->STATUS => $this->EXCEED_ACTIVE_SKILLS_BAG_SIZE
  254. );
  255. }
  256. $skills = array_unique($skillsId);
  257. foreach ($skillsId as $skillId) {
  258. $skill = $this->getSkillByID($skillId);
  259. if (!$skill) {
  260. return array(
  261. $this->STATUS => $this->SKILL_WITH_SUCH_ID_NOT_FOUND
  262. );
  263. }
  264. if ($skill->getRequiredLvl() > $personage->getLvl()) {
  265. return array(
  266. $this->STATUS => $this->LEVEL_OF_PERSONAGE_LESS_THAN_SKILLS
  267. );
  268. }
  269. }
  270. $countChanges = $this->countChanges($allPersonageSkills, $skills);
  271. if ($countChanges < count($skills)) {
  272. return array(
  273. $this->STATUS => $this->PERSONAGE_HAS_NO_SUCH_SKILLS
  274. );
  275. }
  276. $this->getEm()->flush();
  277. return array(
  278. $this->STATUS => $this->STATUS_OK
  279. );
  280. }
  281. /**
  282. * Create a response with status and arena skills of personage
  283. *
  284. * @param $data
  285. *
  286. * @return array
  287. */
  288. public function getResponseForArenaSkillsAction($data)
  289. {
  290. global $kernel;
  291. $validatedResponse = $kernel->getContainer()->get('app.manager.validator')->getPersonageFromData($data);
  292. if (!$validatedResponse[$this->STATUS] == $this->STATUS_OK) {
  293. return $validatedResponse;
  294. }
  295. $personage = $validatedResponse[$this->PERSONAGE];
  296. $skills = $personage->getClass()->getSkills();
  297. if (!$skills) {
  298. return array(
  299. $this->STATUS => $this->SKILLS_NOT_FOUND
  300. );
  301. } else {
  302. foreach ($skills as $skill) {
  303. $skillsArray[] = $skill->getId();
  304. }
  305. }
  306. shuffle($skillsArray);
  307. if (count($skills) > $this->NUMBER_OF_SKILLS_FOR_ARENA) {
  308. $skillsArray = array_slice($skillsArray, 0, $this->NUMBER_OF_SKILLS_FOR_ARENA);
  309. }
  310. $response[$this->SKILLS] = $skillsArray;
  311. $response[$this->STATUS] = $this->STATUS_OK;
  312. return $response;
  313. }
  314. /**
  315. * Create a response with status all skills of personage
  316. *
  317. * @param $data
  318. *
  319. * @return mixed
  320. */
  321. public function getResponseForSkillsAction($data)
  322. {
  323. global $kernel;
  324. $validatedResponse = $kernel->getContainer()->get('app.manager.validator')->getPersonageFromData($data);
  325. if (!$validatedResponse[$this->STATUS] == $this->STATUS_OK) {
  326. return $validatedResponse;
  327. }
  328. $personage = $validatedResponse[$this->PERSONAGE];
  329. $skills = $this->getSkillsOfPersonage($personage);
  330. if (empty($skills)) {
  331. $response[$this->SKILLS] = array();
  332. } else {
  333. foreach ($skills as $skill) {
  334. $response[$this->SKILLS][] = array(
  335. $this->SKILL_ID => $skill->getSkill()->getId(),
  336. $this->IS_SELECTED_SKILL => $skill->getIsSelected()
  337. );
  338. }
  339. }
  340. $response[$this->STATUS] = $this->STATUS_OK;
  341. return $response;
  342. }
  343. public function getEffectsForSkill($skillId)
  344. {
  345. return $this->getEm()->getRepository('AppBundle:SkillToEffect')->findEffectsBySkill($skillId);
  346. }
  347. }