/rpg/src/AppBundle/Manager/JSONValidatorManager.php

https://gitlab.com/EleonoraUA/rpg_server · PHP · 510 lines · 321 code · 32 blank · 157 comment · 91 complexity · 7575cb229120777aafaa11811bfb2c7b MD5 · raw file

  1. <?php
  2. namespace AppBundle\Manager;
  3. use AppBundle\Manager\Traits\BattleTrait;
  4. use AppBundle\Manager\Traits\QuestInfoFieldsTrait;
  5. use AppBundle\Manager\Traits\RequestResponseTypesTrait;
  6. use AppBundle\Manager\Traits\ShopFieldsTrait;
  7. use AppBundle\Manager\Traits\SkillFieldsTrait;
  8. use AppBundle\Manager\Traits\UserInfoFieldsTrait;
  9. /**
  10. * Class JSONValidatorManager
  11. *
  12. * @package AppBundle\Manager
  13. */
  14. class JSONValidatorManager
  15. {
  16. use RequestResponseTypesTrait;
  17. use BattleTrait;
  18. use QuestInfoFieldsTrait;
  19. use UserInfoFieldsTrait;
  20. use SkillFieldsTrait;
  21. use ShopFieldsTrait;
  22. const TOKEN_LENGTH = 43;
  23. /**
  24. * @var RedisManager
  25. * @access private
  26. */
  27. private $redisManager;
  28. /**
  29. * @return RedisManager
  30. */
  31. public function getRedisManager()
  32. {
  33. return $this->redisManager;
  34. }
  35. /**
  36. * JSONValidatorManager constructor.
  37. * @param RedisManager $redisManager
  38. */
  39. public function __construct(RedisManager $redisManager)
  40. {
  41. $this->redisManager = $redisManager;
  42. }
  43. /**
  44. * Returns decoded json if valid and null if not valid
  45. *
  46. * @param $json
  47. * @param $type
  48. *
  49. * @return mixed|null
  50. */
  51. public function getValidatedJsonFromRequest($json, $type)
  52. {
  53. $result = false;
  54. $data = json_decode($json, true);
  55. if (json_last_error() == JSON_ERROR_NONE) {
  56. switch ($type) {
  57. case $this->USER_REGISTER:
  58. $result = $this->isValidUserRegisterRequest($data);
  59. break;
  60. case $this->USER_LOGIN:
  61. $result = $this->isValidUserLoginRequest($data);
  62. break;
  63. case $this->DONE_QUEST_PROVE:
  64. $result = $this->isValidDoneQuestProveRequest($data);
  65. break;
  66. case $this->REVIEW_RESULT:
  67. $result = $this->isValidReviewResultRequest($data);
  68. break;
  69. case $this->SKILLS_REQUEST:
  70. $result = $this->isValidSkillsRequest($data);
  71. break;
  72. case $this->SKILL_ACTION:
  73. $result = $this->isValidSkillActionRequest($data);
  74. break;
  75. case $this->TAKE_QUEST:
  76. $result = $this->isValidTakeQuestRequest($data);
  77. break;
  78. case $this->GET_QUEST_REWARD:
  79. $result = $this->isValidGetQuestRewardRequest($data);
  80. break;
  81. case $this->ARENA_BATTLE_INIT:
  82. $result = $this->isValidArenaInitRequest($data);
  83. break;
  84. case $this->TOURNAMENT_BATTLE_INIT:
  85. $result = $this->isValidTournamentRequest($data);
  86. break;
  87. case $this->SELECT_SKILLS_REQUEST:
  88. $result = $this->isValidSelectSkillsRequest($data);
  89. break;
  90. case $this->BUY_SHOP_REQUEST:
  91. $result = $this->isValidBuyShopRequest($data);
  92. break;
  93. case $this->CHANGE_AVATAR_REQUEST:
  94. $result = $this->isValidChangeAvatarRequest($data);
  95. break;
  96. case $this->ADVENTURE_BATTLE_INIT:
  97. $result = $this->isValidBattleInitRequest($data);
  98. break;
  99. case $this->ADD_FRIEND_REQUEST:
  100. $result = $this->isValidAddFriendRequest($data);
  101. break;
  102. case $this->FRIEND_ACTION_REQUEST:
  103. $result = $this->isValidFriendActionRequest($data);
  104. break;
  105. case $this->TAKE_DUEL_QUEST:
  106. $result = $this->isValidTakeDuelQuestActionRequest($data);
  107. break;
  108. case $this->DONE_DUEL_PROVE:
  109. $result = $this->isValidDoneQuestDuelProveRequest($data);
  110. break;
  111. case $this->REVIEW_DUEL_RESULT:
  112. $result = $this->isValidReviewResultRequest($data);
  113. break;
  114. case $this->LOCATION_INFO_REQUEST:
  115. $result = $this->isValidLocationInfoRequest($data);
  116. break;
  117. default:
  118. $result = $this->isValidRequest($data);
  119. }
  120. }
  121. if ($result) {
  122. return $data;
  123. } else {
  124. return null;
  125. }
  126. }
  127. /**
  128. * Returns user_id by token
  129. *
  130. * @param $data
  131. *
  132. * @return string
  133. */
  134. public function getUserIdFromData($data)
  135. {
  136. $userId = $this->getRedisManager()->getUserIdByToken($data[$this->TOKEN]);
  137. return $userId;
  138. }
  139. public function isValidTournamentRequest($data)
  140. {
  141. $result = false;
  142. if (isset($data[$this->PLAYER_1]) && is_string($data[$this->PLAYER_1]) &&
  143. strlen($data[$this->PLAYER_1]) == self::TOKEN_LENGTH &&
  144. isset($data[$this->PLAYER_2]) && is_string($data[$this->PLAYER_2]) &&
  145. strlen($data[$this->PLAYER_2]) == self::TOKEN_LENGTH) {
  146. $result = true;
  147. }
  148. return $result;
  149. }
  150. private function isValidLocationInfoRequest($data)
  151. {
  152. $result = false;
  153. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  154. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  155. isset($data[$this->LOCATION_ID]) && is_int($data[$this->LOCATION_ID])) {
  156. $result = true;
  157. }
  158. return $result;
  159. }
  160. /**
  161. * Сhecks the validity of the USER_REGISTER request
  162. *
  163. * @param $data
  164. *
  165. * @return bool
  166. */
  167. private function isValidUserRegisterRequest($data)
  168. {
  169. $result = false;
  170. if (isset($data[$this->EMAIL]) && is_string($data[$this->EMAIL]) &&
  171. isset($data[$this->USERNAME]) && is_string($data[$this->USERNAME]) &&
  172. isset($data[$this->PASSWORD]) && is_string($data[$this->PASSWORD]) &&
  173. isset($data[$this->CHARACTER][$this->CHAR_CLASS_ID]) &&
  174. is_int($data[$this->CHARACTER][$this->CHAR_CLASS_ID]) &&
  175. isset($data[$this->CHARACTER][$this->CHAR_NAME]) &&
  176. is_string($data[$this->CHARACTER][$this->CHAR_NAME]) &&
  177. isset($data[$this->CHARACTER][$this->AVATAR_ID]) &&
  178. is_int($data[$this->CHARACTER][$this->AVATAR_ID])) {
  179. $result = true;
  180. }
  181. return $result;
  182. }
  183. /**
  184. * Сhecks the validity of the FRIEND_ACTION_REQUEST request
  185. *
  186. * @param $data
  187. *
  188. * @return bool
  189. */
  190. private function isValidFriendActionRequest($data)
  191. {
  192. $result = false;
  193. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  194. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  195. isset($data[$this->FRIEND_ID]) && is_int($data[$this->FRIEND_ID])) {
  196. $result = true;
  197. }
  198. return $result;
  199. }
  200. /**
  201. * Сhecks the validity of the BUY_SHOP request
  202. *
  203. * @param $data
  204. *
  205. * @return bool
  206. */
  207. private function isValidBuyShopRequest($data)
  208. {
  209. $result = false;
  210. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  211. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  212. isset($data[$this->UNIT_ID]) && is_int($data[$this->UNIT_ID])) {
  213. $result = true;
  214. }
  215. return $result;
  216. }
  217. /**
  218. * Сhecks the validity of the TAKE_DUEL_QUEST request
  219. *
  220. * @param $data
  221. *
  222. * @return bool
  223. */
  224. private function isValidTakeDuelQuestActionRequest($data)
  225. {
  226. $result = false;
  227. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  228. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  229. isset($data[$this->QUEST_ID]) && is_int($data[$this->QUEST_ID]) &&
  230. isset($data[$this->FRIEND_ID]) && is_int($data[$this->FRIEND_ID])) {
  231. $result = true;
  232. }
  233. return $result;
  234. }
  235. /**
  236. * Сhecks the validity of the ADD_FRIEND_REQUEST request
  237. *
  238. * @param $data
  239. *
  240. * @return bool
  241. */
  242. private function isValidAddFriendRequest($data)
  243. {
  244. $result = false;
  245. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  246. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  247. isset($data[$this->USERNAME]) && is_string($data[$this->USERNAME])) {
  248. $result = true;
  249. }
  250. return $result;
  251. }
  252. /**
  253. * Сhecks the validity of the SELECT_SKILLS request
  254. *
  255. * @param $data
  256. *
  257. * @return bool
  258. */
  259. private function isValidSelectSkillsRequest($data)
  260. {
  261. $result = false;
  262. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  263. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  264. isset($data[$this->CHAR_ID]) && is_int($data[$this->CHAR_ID]) &&
  265. isset($data[$this->SKILLS]) && is_array($data[$this->SKILLS])) {
  266. $result = true;
  267. }
  268. return $result;
  269. }
  270. /**
  271. * Сhecks the validity of the ARENA_INIT request
  272. *
  273. * @param $data
  274. *
  275. * @return bool
  276. */
  277. private function isValidArenaInitRequest($data)
  278. {
  279. $result = false;
  280. if (isset($data[$this->PLAYER_1]) && is_string($data[$this->PLAYER_1]) &&
  281. strlen($data[$this->PLAYER_1]) == self::TOKEN_LENGTH &&
  282. isset($data[$this->PLAYER_2]) && is_string($data[$this->PLAYER_2]) &&
  283. strlen($data[$this->PLAYER_2]) == self::TOKEN_LENGTH &&
  284. isset($data[$this->PLAYER_1_SKILLS]) && is_array($data[$this->PLAYER_1_SKILLS]) &&
  285. count($data[$this->PLAYER_1_SKILLS]) == $this->ARENA_SKILLS_NUMBER &&
  286. isset($data[$this->PLAYER_2_SKILLS]) && is_array($data[$this->PLAYER_2_SKILLS]) &&
  287. count($data[$this->PLAYER_2_SKILLS]) == $this->ARENA_SKILLS_NUMBER) {
  288. $result = true;
  289. }
  290. return $result;
  291. }
  292. /**
  293. * Сhecks the validity of the ADVENTURE_BATTLE_INIT request
  294. *
  295. * @param $data
  296. *
  297. * @return bool
  298. */
  299. private function isValidBattleInitRequest($data)
  300. {
  301. $result = false;
  302. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  303. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  304. isset($data[$this->STAGE_ID]) && is_numeric($data[$this->STAGE_ID])) {
  305. $result = true;
  306. }
  307. return $result;
  308. }
  309. /**
  310. * Сhecks the validity of the TAKE_QUEST request
  311. *
  312. * @param $data
  313. *
  314. * @return bool
  315. */
  316. private function isValidTakeQuestRequest($data)
  317. {
  318. $result = false;
  319. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  320. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  321. isset($data[$this->QUEST_ID]) && is_int($data[$this->QUEST_ID])) {
  322. $result = true;
  323. }
  324. return $result;
  325. }
  326. /**
  327. * Сhecks the validity of the GET_QUEST_REWARD request
  328. *
  329. * @param $data
  330. *
  331. * @return bool
  332. */
  333. private function isValidGetQuestRewardRequest($data)
  334. {
  335. $result = false;
  336. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  337. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  338. isset($data[$this->QUEST_ID]) && is_int($data[$this->QUEST_ID])) {
  339. $result = true;
  340. }
  341. return $result;
  342. }
  343. /**
  344. * Сhecks the validity of the SKILL_ACTION request
  345. *
  346. * @param $data
  347. *
  348. * @return bool
  349. */
  350. private function isValidSkillActionRequest($data)
  351. {
  352. $result = false;
  353. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  354. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  355. isset($data[$this->SKILL_ID])&& is_int($data[$this->SKILL_ID])) {
  356. $result = true;
  357. }
  358. return $result;
  359. }
  360. /**
  361. * Сhecks the validity of the SKILLS_REQUEST request
  362. *
  363. * @param $data
  364. *
  365. * @return bool
  366. */
  367. private function isValidSkillsRequest($data)
  368. {
  369. $result = false;
  370. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  371. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  372. isset($data[$this->CHAR_ID]) && is_int($data[$this->CHAR_ID])) {
  373. $result = true;
  374. }
  375. return $result;
  376. }
  377. /**
  378. * Сhecks the validity of the REVIEW_RESULT request
  379. *
  380. * @param $data
  381. *
  382. * @return bool
  383. */
  384. private function isValidReviewResultRequest($data)
  385. {
  386. $result = false;
  387. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  388. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  389. isset($data[$this->RESULT]) && is_bool($data[$this->RESULT])) {
  390. $result = true;
  391. }
  392. return $result;
  393. }
  394. /**
  395. * Сhecks the validity of the DONE_QUEST_PROVE request
  396. *
  397. * @param $data
  398. *
  399. * @return bool
  400. */
  401. private function isValidDoneQuestProveRequest($data)
  402. {
  403. $result = false;
  404. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  405. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  406. isset($data[$this->QUEST_ID]) && is_int($data[$this->QUEST_ID])) {
  407. $result = true;
  408. }
  409. return $result;
  410. }
  411. /**
  412. * Сhecks the validity of the DONE_DUEL_PROVE request
  413. *
  414. * @param $data
  415. *
  416. * @return bool
  417. */
  418. private function isValidDoneQuestDuelProveRequest($data)
  419. {
  420. $result = false;
  421. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  422. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  423. isset($data[$this->QUEST_ID]) && is_int($data[$this->QUEST_ID]) &&
  424. isset($data[$this->FRIEND_ID]) && is_int($data[$this->FRIEND_ID])) {
  425. $result = true;
  426. }
  427. return $result;
  428. }
  429. /**
  430. * Сhecks the validity of the CHANGE_AVATAR_REQUEST request
  431. *
  432. * @param $data
  433. *
  434. * @return bool
  435. */
  436. private function isValidChangeAvatarRequest($data)
  437. {
  438. $result = false;
  439. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  440. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH &&
  441. isset($data[$this->AVATAR_ID]) && is_int($data[$this->AVATAR_ID])) {
  442. $result = true;
  443. }
  444. return $result;
  445. }
  446. /**
  447. * Сhecks the validity of the USER_LOGIN request
  448. *
  449. * @param $data
  450. *
  451. * @return bool
  452. */
  453. private function isValidUserLoginRequest($data)
  454. {
  455. $result = false;
  456. if (isset($data[$this->EMAIL]) && is_string($data[$this->EMAIL]) &&
  457. isset($data[$this->PASSWORD]) && is_string($data[$this->PASSWORD])) {
  458. $result = true;
  459. }
  460. return $result;
  461. }
  462. /**
  463. * Сhecks the validity of the request
  464. *
  465. * @param $data
  466. *
  467. * @return bool
  468. */
  469. private function isValidRequest($data)
  470. {
  471. $result = false;
  472. if (isset($data[$this->TOKEN]) && is_string($data[$this->TOKEN]) &&
  473. strlen($data[$this->TOKEN]) == self::TOKEN_LENGTH) {
  474. $result = true;
  475. }
  476. return $result;
  477. }
  478. }