Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.76 KB | None | 0 0
  1. <?php
  2. /**
  3. * //////////////////////////////////////////////////////////////////////////////////
  4. * // Authors:
  5. * // Lucas Reis Máximo Dias (thereis@live.com)
  6. * // Pierre Andreis Ortega (pierreandreisortega@gmail.com)
  7. * // Gabriel Prestes Dias (gabrielpdias@hotmail.com)
  8. * // 26/03/17 21:23
  9. * // class.MatchCurl.php
  10. * // Unauthorized copying of this file, via any medium is strictly prohibited
  11. * // * Proprietary and confidential
  12. * //////////////////////////////////////////////////////////////////////////////////
  13. */
  14.  
  15. namespace App\Core\Matches;
  16.  
  17. use App\Core\Curl;
  18. use Exception;
  19. use Main\Cache;
  20. use Main\Utils;
  21.  
  22. /**
  23. * Class MatchCurl
  24. * @package App\Core\Matches
  25. */
  26. class MatchCurl extends Curl implements MatchInterface
  27. {
  28.  
  29. /*---------------------------------------------------------------------*/
  30. /**
  31. * Saves a match.
  32. *
  33. * @param string $match The match
  34. * @param string $country The country
  35. * @param $match
  36. * @param $country
  37. * @return mixed|void
  38. */
  39. public function save_match($match, $country)
  40. {
  41. $utils_match = new MatchUtils();
  42.  
  43. if (!isset($match['data'])) return;
  44. // save the matches
  45. foreach ($match['data'] as $datum) {
  46.  
  47. // if there is a telemetry for the match
  48. if (isset($datum['relationships']['assets']['data'][0])) {
  49.  
  50. $telemetry_id = $datum['relationships']['assets']['data'];
  51.  
  52. // search into the telemetry array for the respective id
  53. $get_tel_id = array_search($telemetry_id[0]['id'], array_column($match['included'], 'id'));
  54.  
  55. $match['included'][$get_tel_id]['match_id'] = $datum['id'];
  56. }
  57.  
  58. // get rosters
  59. $roster_0 = $datum['relationships']['rosters']['data'][0]['id'];
  60. $roster_1 = $datum['relationships']['rosters']['data'][1]['id'];
  61.  
  62. $get_roster_0 = array_search($roster_0, array_column($match['included'], 'id'));
  63. $get_roster_1 = array_search($roster_1, array_column($match['included'], 'id'));
  64.  
  65. $match['included'][$get_roster_0]['match_id'] = $datum['id'];
  66. $match['included'][$get_roster_1]['match_id'] = $datum['id'];
  67.  
  68. //assign match_id to rosters
  69. foreach ($match['included'][$get_roster_0]['relationships']['participants']['data'] as $key => $value) {
  70. $hey = array_search($value['id'], array_column($match['included'], 'id'));
  71. $match['included'][$hey]['roster_id'] = $value['id'];
  72. $match['included'][$hey]['createdAt'] = $datum['attributes']['createdAt'];
  73. $match['included'][$hey]['gameMode'] = $datum['attributes']['gameMode'];
  74. $match['included'][$hey]['match_id'] = $datum['id'];
  75. }
  76. //assign match_id to rosters
  77. foreach ($match['included'][$get_roster_1]['relationships']['participants']['data'] as $key => $value) {
  78. $hey = array_search($value['id'], array_column($match['included'], 'id'));
  79. $match['included'][$hey]['roster_id'] = $value['id'];
  80. $match['included'][$hey]['createdAt'] = $datum['attributes']['createdAt'];
  81. $match['included'][$hey]['gameMode'] = $datum['attributes']['gameMode'];
  82. $match['included'][$hey]['match_id'] = $datum['id'];
  83. }
  84.  
  85. //$datum['patchVersion']
  86. //$datum['attributes']['patchVersion'] = $utils_match->getRealPath($datum['attributes']['createdAt']);
  87. // save the request into the database
  88. $this->save_request('match', $datum);
  89. }
  90.  
  91. // save the roster
  92. foreach ($match['included'] as $datum) {
  93. switch ($datum['type']) {
  94. case 'roster':
  95.  
  96. // $hey = array_search($datum['relationships']['player']['data']['id'], array_column($match['included'], 'id'));
  97. // $datum['attributes']['name'] = $match['included'][$hey]['attributes']['name'];
  98.  
  99.  
  100. $this->save_request('roster', $datum);
  101. break;
  102.  
  103. case 'participant':
  104. // validate the json if no errors, after that will save the request into database
  105. //print_r($datum['relationships']['player']['data']['id']);
  106. if ($datum['attributes']['actor'] == "spectator") continue;
  107.  
  108. $hey = array_search($datum['relationships']['player']['data']['id'], array_column($match['included'], 'id'));
  109.  
  110. $datum['attributes']['name'] = $match['included'][$hey]['attributes']['name'];
  111.  
  112.  
  113. $datum['attributes']['shardId'] = $country;
  114. $datum['attributes']['actor'] = $utils_match->getRealNameHero_api($datum['attributes']['actor']);
  115.  
  116. $datum['attributes']['shardId'] = $country;
  117.  
  118. $this->save_request('participant', $datum);
  119. // validate the json if no errors, after that will save the request into database
  120. //$this->save_request('itemGrants', $datum);
  121. // validate the json if no errors, after that will save the request into database
  122. //$this->save_request('items', $datum);
  123. break;
  124.  
  125. case 'player':
  126. $datum['attributes']['shardId'] = $country;
  127. // validate the json if no errors, after that will save the request into database
  128.  
  129. $this->save_request('players_match', $datum);
  130. break;
  131. case 'team':
  132. // validate the json if no errors, after that will save the request into database
  133. //$this->save_request('team', $datum);
  134. break;
  135. case 'asset':
  136. // validate the json if no errors, after that will save the telemetry into the database
  137. $this->save_request('asset', $datum);
  138. break;
  139. }
  140. }
  141. }
  142.  
  143. /**
  144. * Sends a match request.
  145. *
  146. * @param MatchUtils $country The country
  147. * @param <type> $method The method
  148. * @param <type> $query The query
  149. * @param null $type
  150. * @param string $force
  151. * @param $country
  152. * @param $method
  153. * @param $query
  154. * @return bool|string
  155. *
  156. * @throws Exception (description)
  157. */
  158. public function send_match_request($country, $method, $query, $type = NULL, $force = '')
  159. {
  160.  
  161. try {
  162. // will use the utils class
  163. $utils_match = new MatchUtils();
  164. $utils = new Utils();
  165. $cache = new Cache('match');
  166.  
  167. $assistant = new MatchAssistant();
  168.  
  169. $i = 0;
  170. $kills = [];
  171. $deaths = [];
  172. $assists = [];
  173. $gold = [];
  174. $totalkills = [];
  175. $farm = [];
  176. $win = [];
  177. $time = [];
  178. $gameMode = [];
  179. $jkill = [];
  180. $ckill = [];
  181. $capdeaths = [];
  182. $jdeaths = [];
  183. $cdeaths = [];
  184. $capkill = [];
  185. $jassist = [];
  186. $cassist = [];
  187. $capassist = [];
  188. $cwin = [];
  189. $capwin = [];
  190. $jwin = [];
  191. $kraken = [];
  192. $aces = [];
  193. $turrets = [];
  194. $bluewins = [];
  195. $redwins = [];
  196. $rank_wins = [];
  197. $rank_kills = [];
  198. $rank_deaths = [];
  199. $rank_assists = [];
  200. $rank_games = 0;
  201. $heroes = [];
  202. $playedwin = [];
  203. $playedloss = [];
  204.  
  205. // parse the $query params
  206. $url = $utils_match->valid_parse_url($query);
  207.  
  208. // get the player name
  209. $player_id = $utils_match->get_player_id($url['filter']['playerNames']);
  210.  
  211. if ($force == true) {
  212. $cache->delete($country . $url['filter']['playerNames'] . $type);
  213. }
  214.  
  215. if ($cache->read($country . $url['filter']['playerNames'] . $type) == false) {
  216.  
  217. $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  218.  
  219. // if player not exists
  220. if (empty($player_matches) || ($utils_match->verificar_cache($url['filter']['playerNames']) !== true && empty($type) || !empty($force))) {
  221.  
  222. // save into the cache table the recent player
  223. $this->save_request('cache', $url['filter']['playerNames']);
  224.  
  225. // transform the result in array
  226. $curl_result = $this->send_curl_request($country, $method, $query);
  227.  
  228. // check if there is an error
  229. $match = $this->values_request($curl_result);
  230.  
  231. // save into the database
  232. if ($match != false) $this->save_match($match, $country);
  233.  
  234. // query the player matches
  235. $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  236. }
  237.  
  238. // if there is no match for the player
  239. if (!$player_matches) {
  240. throw new Exception('403: Found but no matches came from mysql' . var_dump($player_matches));
  241. }
  242.  
  243. $json['Data']['LastUpdate'] = $utils_match->get_last_match_cache($url['filter']['playerNames']);
  244.  
  245. foreach ($player_matches[0] as $item) {
  246.  
  247. $gamemode[] = $utils_match->getRealMode($item['gameMode'], 'img_name');
  248.  
  249. $kills[] = $item['kills'];
  250. $deaths[] = $item['deaths'];
  251. $assists[] = $item['assists'];
  252. $gold[] = $item['gold'];
  253.  
  254. // This generate the stats
  255. if ($item['gameMode'] == 'ranked' && $item['patchVersion'] >= $this->summer_patch_version) {
  256. $rank_kills[] = $item['kills'];
  257. $rank_deaths[] = $item['deaths'];
  258. $rank_assists[] = $item['assists'];
  259. $rank_wins[] = $item['winner'];
  260.  
  261. $rank_games++;
  262. }
  263.  
  264.  
  265. $heroes[$item['actor']]['Kills'][] = $item['kills'];
  266. $heroes[$item['actor']]['Assists'][] = $item['assists'];
  267. $heroes[$item['actor']]['Farm'][] = $item['farm'];
  268. $heroes[$item['actor']]['Wins'][] = $item['winner'];
  269. $heroes[$item['actor']]['Deaths'][] = $item['deaths'];
  270.  
  271.  
  272. $totalkills[] = $item['heroKills'];
  273.  
  274. $farm[] = $item['farm'];
  275. $win[] = $item['winner'];
  276. $time[] = $item['duration'];
  277. $kraken[] = $item['krakenCaptures'];
  278. $aces[] = $item['acesEarned'];
  279. $turrets[] = $item['turretKills'];
  280.  
  281. if ($item['side'] == "left/blue") {
  282. $bluewins[] = $item['winner'];
  283. } else if ($item['side'] == "right/red") {
  284. $redwins[] = $item['winner'];
  285. }
  286.  
  287. // detect role
  288. if ($item['gameMode'] == 'ranked' || $item['gameMode'] == 'casual') {
  289. $role = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
  290. if ($role === "Jungler") {
  291. $jkill[] = $item['kills'];
  292. $jdeaths[] = $item['deaths'];
  293. $jassist[] = $item['assists'];
  294. $jwin[] = $item['winner'];
  295. } else if ($role === "Carry") {
  296. $ckill[] = $item['kills'];
  297. $cdeaths[] = $item['deaths'];
  298. $cassist[] = $item['assists'];
  299. $cwin[] = $item['winner'];
  300. } else if ($role === "Captain") {
  301. $capkill[] = $item['kills'];
  302. $capdeaths[] = $item['deaths'];
  303. $capassist[] = $item['assists'];
  304. $capwin[] = $item['winner'];
  305. }
  306. }
  307.  
  308.  
  309. $json['Data']['Matches'][$i]['player'] = $url['filter']['playerNames'];
  310. if (empty($json['Stats']['SkillTier'])) {
  311. $json['Stats']['SkillTier'] = $item['skillTier'];
  312. }
  313.  
  314. $json['Data']['Matches'][$i]['MatchResult'] = $item['endGameReason'];
  315. $json['Data']['Matches'][$i]['Winner'] = $item['winner'];
  316. $json['Data']['Matches'][$i]['Side'] = $item['side'];
  317. $json['Data']['Matches'][$i]['skin'] = $item['skinKey'];
  318. $json['Data']['Matches'][$i]['duration'] = $utils_match->convertDuration($item['duration']);
  319. $json['Data']['Matches'][$i]['duration-seconds'] = $item['duration'];
  320. $json['Data']['Matches'][$i]['gmin'] = $utils_match->convertFtoMin($item['duration'], ($item['pgold'] * 1));
  321. $json['Data']['Matches'][$i]['csmin'] = $utils_match->convertFtoMin($item['duration'], $item['farm']);
  322. $json['Data']['Matches'][$i]['jungleKills'] = $item['jungleKills'];
  323. $json['Data']['Matches'][$i]['kills'] = $item['kills'];
  324. $json['Data']['Matches'][$i]['assists'] = $item['assists'];
  325. $json['Data']['Matches'][$i]['deaths'] = $item['deaths'];
  326.  
  327. $death_kda = $item['deaths'] == 0 ? 1 : $item['deaths'];
  328.  
  329. $calculate_kda = (($item['kills'] + $item['assists']) / $death_kda);
  330.  
  331. $json['Data']['Matches'][$i]['kda'] = round($calculate_kda, 2);
  332. $json['Data']['Matches'][$i]['farm'] = $item['farm'];
  333.  
  334. $json['Data']['Matches'][$i]['gold'] = (($item['pgold'] * 1) > 999 ? $utils_match->thousandsCurrencyFormat($item['pgold'] * 1) : $item['pgold'] * 1);
  335.  
  336. $json['Data']['Matches'][$i]['actor'] = strtolower($utils_match->getRealNameHero($item['actor']));
  337.  
  338. $rosters = $this->allRows("call side_rosters_match('{$item['id_match']}')");
  339.  
  340. foreach ($rosters as $team) {
  341.  
  342. $id_blue = $team[0]['BlueId'];
  343. $id_red = $team[0]['RedId'];
  344.  
  345. $blueTeam = $utils_match->allRows("SELECT * FROM `matches_roster` WHERE `id_roster` = '{$id_blue}'");
  346. $redTeam = $utils_match->allRows("SELECT * FROM `matches_roster` WHERE `id_roster` = '{$id_red}'");
  347. $json['Data']['Matches'][$i]['BlueTeam']['Aces'] = $blueTeam['0']['0']['acesEarned'];
  348. $json['Data']['Matches'][$i]['BlueTeam']['Gold'] = $blueTeam['0']['0']['gold'];
  349. $json['Data']['Matches'][$i]['BlueTeam']['Kills'] = $blueTeam['0']['0']['heroKills'];
  350. $json['Data']['Matches'][$i]['BlueTeam']['KrakenCaptures'] = $blueTeam['0']['0']['krakenCaptures'];
  351. $json['Data']['Matches'][$i]['BlueTeam']['TurretsDestroyed'] = $blueTeam['0']['0']['turretKills'];
  352. $json['Data']['Matches'][$i]['BlueTeam']['TurretsRemaining'] = $blueTeam['0']['0']['turretsRemaining'];
  353.  
  354. $json['Data']['Matches'][$i]['RedTeam']['Aces'] = $redTeam['0']['0']['acesEarned'];
  355. $json['Data']['Matches'][$i]['RedTeam']['Gold'] = $redTeam['0']['0']['gold'];
  356. $json['Data']['Matches'][$i]['RedTeam']['Kills'] = $redTeam['0']['0']['heroKills'];
  357. $json['Data']['Matches'][$i]['RedTeam']['KrakenCaptures'] = $redTeam['0']['0']['krakenCaptures'];
  358. $json['Data']['Matches'][$i]['RedTeam']['TurretsDestroyed'] = $redTeam['0']['0']['turretKills'];
  359. $json['Data']['Matches'][$i]['RedTeam']['TurretsRemaining'] = $redTeam['0']['0']['turretsRemaining'];
  360.  
  361.  
  362. // obtaining the participants
  363. $red_players = [$redTeam['0']['0']['id_participant1'], $redTeam['0']['0']['id_participant2'], $redTeam['0']['0']['id_participant3']];
  364. $blue_players = [$blueTeam['0']['0']['id_participant1'], $blueTeam['0']['0']['id_participant2'], $blueTeam['0']['0']['id_participant3']];
  365.  
  366. // generating the stats for the participants
  367. $blue_participants = $assistant->create_participant_stats($blue_players, $item['side'], $url['filter']['playerNames'], "left/blue");
  368. $red_participants = $assistant->create_participant_stats($red_players, $item['side'], $url['filter']['playerNames'], "right/red");
  369.  
  370. // registering inside the array
  371. $json['Data']['Matches'][$i]['BlueTeam']['Players'] = $blue_participants;
  372. $json['Data']['Matches'][$i]['RedTeam']['Players'] = $red_participants;
  373.  
  374. // showing played win/loss
  375. $json['Data']['Matches'][$i]['RedTeam']['PlayedWin'] = $red_participants['PlayedWin'];
  376. $json['Data']['Matches'][$i]['RedTeam']['PlayedLoss'] = $red_participants['PlayedLoss'];
  377. $json['Data']['Matches'][$i]['BlueTeam']['PlayedWin'] = $blue_participants['PlayedWin'];
  378. $json['Data']['Matches'][$i]['BlueTeam']['PlayedLoss'] = $blue_participants['PlayedLoss'];
  379.  
  380. // removing some unused arrays
  381. unset($json['Data']['Matches'][$i]['RedTeam']['Players']['PlayedWin'], $json['Data']['Matches'][$i]['RedTeam']['Players']['PlayedLoss'], $json['Data']['Matches'][$i]['BlueTeam']['Players']['PlayedWin'], $json['Data']['Matches'][$i]['BlueTeam']['Players']['PlayedLoss']);
  382. }
  383.  
  384.  
  385. if (isset($json['Data']['Matches'][$i]['RedTeam']) && isset($json['Data']['Matches'][$i]['BlueTeam'])) {
  386.  
  387. // generating the array values
  388. foreach ($json['Data']['Matches'][$i]['RedTeam']['PlayedWin'] as $player) {
  389. $playedwin[] = $player;
  390. }
  391.  
  392. // generating the array values
  393. foreach ($json['Data']['Matches'][$i]['RedTeam']['PlayedLoss'] as $player) {
  394. $playedloss[] = $player;
  395. }
  396.  
  397. // generating the array values
  398. foreach ($json['Data']['Matches'][$i]['BlueTeam']['PlayedWin'] as $player) {
  399. $playedwin[] = $player;
  400. }
  401.  
  402. // generating the array values
  403. foreach ($json['Data']['Matches'][$i]['BlueTeam']['PlayedLoss'] as $player) {
  404. $playedloss[] = $player;
  405. }
  406.  
  407. $json['Data']['Matches'][$i]['TypeMatch'] = $utils_match->getRealMode($item['gameMode']);
  408. $json['Data']['Matches'][$i]['Role'] = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
  409. $json['Data']['Matches'][$i]['Patch'] = $item['patchVersion'];
  410. $json['Data']['Matches'][$i]['MatchDateTime'] = $item['createdAt'];
  411. $json['Data']['Matches'][$i]['MatchWhen'] = $utils_match->time_elapsed_string($item['createdAt']);
  412. $json['Data']['Matches'][$i]['MId'] = $item['id_match'];
  413.  
  414. $itemsbought_game = json_decode($item['rawParticipant'], true);
  415.  
  416. foreach ($itemsbought_game['attributes']['stats']['items'] as $itembought) {
  417. $json['Data']['Matches'][$i]['Items'][] = strtolower($utils_match->get_item_image_name($itembought));
  418. }
  419.  
  420. $i++;
  421.  
  422. } else unset($json['Data']['Matches'][$i]);
  423. }
  424.  
  425. if ($i > 0) {
  426.  
  427. //$top_heroes = $this->allRows("call topHeroes('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  428.  
  429. foreach ($heroes as $hero => $herod) {
  430.  
  431. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalKills'] = array_sum($herod['Kills']);
  432. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalDeaths'] = array_sum($herod['Deaths']);
  433. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalAssists'] = array_sum($herod['Assists']);
  434. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalFarm'] = array_sum($herod['Farm']);
  435. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['kda'] = round($utils_match->division(array_sum($herod['Kills']) + array_sum($herod['Assists']), array_sum($herod['Deaths'])), 2);
  436. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
  437. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalVictory'] = array_sum($herod['Wins']);
  438. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalLoss'] = count($herod['Wins']) - array_sum($herod['Wins']);
  439.  
  440. if (array_sum($herod['Wins']) != 0) {
  441. $winrate = round($utils_match->division(array_sum($herod['Wins']), count($herod['Wins'])) * 100);
  442. } else {
  443. $winrate = 0;
  444. }
  445.  
  446. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Winrate'] = $winrate;
  447.  
  448. }
  449. $winrateheroes = [];
  450. $totalheroes = [];
  451. foreach ($json['Stats']['Heroes'] as $key => $value) {
  452. $winrateheroes[] = $value['Winrate'];
  453.  
  454. $totalheroes[] = $value['totalMatches'];
  455. }
  456. array_multisort($totalheroes, SORT_DESC, $winrateheroes, SORT_DESC, $json['Stats']['Heroes']);
  457.  
  458. // generating the stats
  459. if ($rank_games > 0) {
  460. $json['Stats']['Ranked']['winrate'] = round((array_sum($rank_wins) / $rank_games) * 100);
  461. $json['Stats']['Ranked']['KDA'] = round($utils_match->division(array_sum($rank_kills) + array_sum($rank_assists), array_sum($rank_deaths)), 2) * 100;
  462. $json['Stats']['Ranked']['Wins'] = array_sum($rank_wins);
  463. $json['Stats']['Ranked']['Loss'] = $rank_games - array_sum($rank_wins);
  464. }
  465.  
  466. $json['Stats']['winrate'] = round((array_sum($win) / $i) * 100);
  467. $json['Stats']['KP'] = round(((array_sum($kills) + array_sum($assists)) / array_sum($totalkills)) * 100);
  468. $json['Stats']['KrakenCap'] = array_sum($kraken);
  469. $json['Stats']['totalAce'] = array_sum($aces);
  470. $json['Stats']['totalTurrets'] = array_sum($turrets);
  471. $json['Stats']['Blue']['Wins'] = array_sum($bluewins);
  472. $json['Stats']['Blue']['Total'] = count($bluewins);
  473. $json['Stats']['Blue']['Loss'] = count($bluewins) - array_sum($bluewins);
  474. $json['Stats']['Blue']['WinRate'] = round($utils_match->division(array_sum($bluewins), count($bluewins)), 2) * 100;
  475. $json['Stats']['Red']['Wins'] = array_sum($redwins);
  476. $json['Stats']['Red']['Total'] = count($redwins);
  477. $json['Stats']['Red']['Loss'] = count($redwins) - array_sum($redwins);
  478. $json['Stats']['Red']['WinRate'] = round($utils_match->division(array_sum($redwins), count($redwins)), 2) * 100;
  479. $json['Stats']['Kills'] = array_sum($kills);
  480. $json['Stats']['totalKills'] = array_sum($totalkills);
  481. $json['Stats']['Assists'] = array_sum($assists);
  482. $json['Stats']['Deaths'] = array_sum($deaths);
  483.  
  484. $json['Stats']['TotalTime'] = array_sum($time);
  485. $json['Stats']['totalFarm'] = array_sum($farm);
  486. $json['Stats']['gold'] = array_sum($gold);
  487. $json['Stats']['totalWin'] = array_sum($win);
  488. $json['Stats']['totalLoss'] = $i - array_sum($win);
  489. $json['Stats']['KDA'] = round($utils_match->division(array_sum($kills) + array_sum($assists), array_sum($deaths)), 2) * 100;
  490. $json['Stats']['KillsAssisted'] = array_sum($kills) + array_sum($assists);
  491. $json['Stats']['KillsNotAssisted'] = array_sum($totalkills) - (array_sum($kills) + array_sum($assists));
  492. $json['Stats']['avgKill'] = str_replace('.', "", round(array_sum($kills) / $i, 2));
  493. $json['Stats']['avgAssist'] = str_replace('.', "", round(array_sum($assists) / $i, 2));
  494. $json['Stats']['avgDeaths'] = str_replace('.', "", round(array_sum($deaths) / $i, 2));
  495. $json['Stats']['totalMatches'] = $i;
  496.  
  497. $json['Stats']['RecentResults'] = array_slice($win, 0, 6);
  498.  
  499. $json['Stats']['Roles']['Captain']['Kills'] = round($utils_match->division(array_sum($capkill), count($capkill)), 1);
  500. $json['Stats']['Roles']['Jungler']['Kills'] = round($utils_match->division(array_sum($jkill), count($jkill)), 1);
  501. $json['Stats']['Roles']['Carry']['Kills'] = round($utils_match->division(array_sum($ckill), count($ckill)), 1);
  502. $json['Stats']['Roles']['Captain']['Deaths'] = round($utils_match->division(array_sum($capdeaths), count($capkill)), 1);
  503. $json['Stats']['Roles']['Jungler']['Deaths'] = round($utils_match->division(array_sum($jdeaths), count($jkill)), 1);
  504. $json['Stats']['Roles']['Carry']['Deaths'] = round($utils_match->division(array_sum($cdeaths), count($ckill)), 1);
  505. $json['Stats']['Roles']['Captain']['Assists'] = round($utils_match->division(array_sum($capassist), count($capkill)), 1);
  506. $json['Stats']['Roles']['Jungler']['Assists'] = round($utils_match->division(array_sum($jassist), count($jkill)), 1);
  507. $json['Stats']['Roles']['Carry']['Assists'] = round($utils_match->division(array_sum($cassist), count($ckill)), 1);
  508. $json['Stats']['Roles']['Captain']['KDA'] = round($utils_match->division(array_sum($capkill) + array_sum($capassist), array_sum($capdeaths)), 2);
  509. $json['Stats']['Roles']['Jungler']['KDA'] = round($utils_match->division(array_sum($jkill) + array_sum($jassist), array_sum($jdeaths)), 2);
  510. $json['Stats']['Roles']['Carry']['KDA'] = round($utils_match->division(array_sum($ckill) + array_sum($cassist), array_sum($cdeaths)), 2);
  511. $json['Stats']['Roles']['Captain']['Win'] = array_sum($capwin);
  512. $json['Stats']['Roles']['Jungler']['Win'] = array_sum($jwin);
  513. $json['Stats']['Roles']['Carry']['Win'] = array_sum($cwin);
  514. $json['Stats']['Roles']['Captain']['Games'] = count($capkill);
  515. $json['Stats']['Roles']['Jungler']['Games'] = count($jkill);
  516. $json['Stats']['Roles']['Carry']['Games'] = count($ckill);
  517. $json['Stats']['Roles']['Captain']['Winrate'] = round($utils_match->division(array_sum($capwin), count($capkill)), 2) * 100;
  518. $json['Stats']['Roles']['Jungler']['Winrate'] = round($utils_match->division(array_sum($jwin), count($jwin)), 2) * 100;
  519. $json['Stats']['Roles']['Carry']['Winrate'] = round($utils_match->division(array_sum($cwin), count($ckill)), 2) * 100;
  520.  
  521. $playedwin = array_count_values(array_filter($playedwin));
  522. $gameMode = array_count_values(array_filter($gamemode));
  523. $playedloss = array_count_values(array_filter($playedloss));
  524.  
  525. $json['Stats']['gameMode'] = $gameMode;
  526.  
  527. foreach ($playedwin as $key => $value) {
  528. $json['Stats']['PlayedWith'][$key]['Win'] = $value;
  529. $json['Stats']['PlayedWith'][$key]['Loss'] = '0';
  530. $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
  531. $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
  532. }
  533.  
  534. foreach ($playedloss as $key => $value) {
  535. if (!isset($json['Stats']['PlayedWith'][$key]['Win'])) {
  536. $json['Stats']['PlayedWith'][$key]['Win'] = 0;
  537. }
  538. $json['Stats']['PlayedWith'][$key]['Loss'] = $value;
  539. $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
  540. $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
  541. }
  542.  
  543. // as known as
  544. $json['Stats']['AKA'] = $utils_match->aka($player_id, $url['filter']['playerNames']);
  545.  
  546. $total = array();
  547.  
  548. if (isset($json['Stats']['PlayedWith'])) {
  549. foreach ($json['Stats']['PlayedWith'] as $key => $value) {
  550. if ($value['Total'] <= 2) {
  551. unset($json['Stats']['PlayedWith'][$key]);
  552. } else {
  553. $total[] = $value['Total'];
  554. }
  555. }
  556.  
  557. array_multisort($total, SORT_DESC, $json['Stats']['PlayedWith']);
  558. }
  559.  
  560. $json['Stats']['SpringRating'] = $utils->get_spring_vpr($url['filter']['playerNames']);
  561.  
  562. if (empty($type) && array_sum($rank_wins) >= 10 && $json['Stats']['SkillTier'] > 0) {
  563.  
  564. $getranking = $utils->generateRating($json['Stats']['Ranked'], $json['Stats']['SkillTier'], $url['filter']['playerNames'], $country);
  565.  
  566. $json['Stats']['Rating'] = $getranking['rating'];
  567. $json['Stats']['Ranking'] = $getranking['ranking'];
  568. }
  569.  
  570. //save historical data
  571. if (!($type)) {
  572. $utils_match->saveHistorical($url['filter']['playerNames'], $country, $json['Stats']);
  573. }
  574.  
  575. // saving into the cache
  576.  
  577. $cache->save($country . $url['filter']['playerNames'] . $type . $force, json_encode($json));
  578. }
  579.  
  580. }
  581.  
  582. // reading the cache
  583. return $cache->read($country . $url['filter']['playerNames'] . $type . $force);
  584.  
  585. }
  586. catch (Exception $exception) {
  587. // $this->query("INSERT INTO `website_logs` (`id`, `message`) VALUES (NULL, '{$exception->getMessage()}')");
  588. $curl_result = json_decode($curl_result, true);
  589. error_log($exception->getMessage() . $country . $url['filter']['playerNames'] . $type);
  590. $json['errors']['title'] = $exception->getMessage();
  591. $cache->save($country . $url['filter']['playerNames'] . $type, json_encode($json));
  592. return json_encode($json);
  593. }
  594. }
  595.  
  596. public function send_tourney_request($country, $method, $query, $type = NULL, $force = '')
  597. {
  598.  
  599. try {
  600. // will use the utils class
  601. $utils_match = new MatchUtils();
  602. $utils = new Utils();
  603.  
  604. $assistant = new MatchAssistant();
  605.  
  606. $i = 0;
  607. $kills = [];
  608. $deaths = [];
  609. $assists = [];
  610. $gold = [];
  611. $totalkills = [];
  612. $farm = [];
  613. $win = [];
  614. $time = [];
  615. $gameMode = [];
  616. $jkill = [];
  617. $ckill = [];
  618. $capdeaths = [];
  619. $jdeaths = [];
  620. $cdeaths = [];
  621. $capkill = [];
  622. $jassist = [];
  623. $cassist = [];
  624. $capassist = [];
  625. $cwin = [];
  626. $capwin = [];
  627. $jwin = [];
  628. $kraken = [];
  629. $aces = [];
  630. $turrets = [];
  631. $bluewins = [];
  632. $redwins = [];
  633. $rank_wins = [];
  634. $rank_kills = [];
  635. $rank_deaths = [];
  636. $rank_assists = [];
  637. $rank_games = 0;
  638. $heroes = [];
  639. $playedwin = [];
  640. $playedloss = [];
  641.  
  642. // parse the $query params
  643. $url = $utils_match->valid_parse_url($query);
  644.  
  645. // get the player name
  646. $player_id = $utils_match->get_player_id($url['filter']['playerNames']);
  647.  
  648. $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  649.  
  650. // if player not exists
  651. if (empty($player_matches) || ($utils_match->verificar_cache($url['filter']['playerNames']) !== true && empty($type) || !empty($force))) {
  652.  
  653. // save into the cache table the recent player
  654. $this->save_request('cache', $url['filter']['playerNames']);
  655.  
  656. // transform the result in array
  657. $curl_result = $this->send_curl_request($country, $method, $query);
  658.  
  659. // check if there is an error
  660. $match = $this->values_request($curl_result);
  661.  
  662. // save into the database
  663. $this->save_match($match, $country);
  664.  
  665. // query the player matches
  666. $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  667. }
  668.  
  669. foreach ($player_matches[0] as $item) {
  670.  
  671. $gamemode[] = $utils_match->getRealMode($item['gameMode'], 'img_name');
  672.  
  673. $kills[] = $item['kills'];
  674. $deaths[] = $item['deaths'];
  675. $assists[] = $item['assists'];
  676. $gold[] = $item['gold'];
  677.  
  678. if ($item['gameMode'] == 'ranked') {
  679. $rank_kills[] = $item['kills'];
  680. $rank_deaths[] = $item['deaths'];
  681. $rank_assists[] = $item['assists'];
  682. $rank_wins[] = $item['winner'];
  683. $rank_games++;
  684. }
  685.  
  686. $heroes[$item['actor']]['Kills'][] = $item['kills'];
  687. $heroes[$item['actor']]['Assists'][] = $item['assists'];
  688. $heroes[$item['actor']]['Farm'][] = $item['farm'];
  689. $heroes[$item['actor']]['Wins'][] = $item['winner'];
  690. $heroes[$item['actor']]['Deaths'][] = $item['deaths'];
  691.  
  692. $totalkills[] = $item['heroKills'];
  693.  
  694. $farm[] = $item['farm'];
  695. $win[] = $item['winner'];
  696. $time[] = $item['duration'];
  697. $kraken[] = $item['krakenCaptures'];
  698. $aces[] = $item['acesEarned'];
  699. $turrets[] = $item['turretKills'];
  700.  
  701. // detect role
  702. if ($item['gameMode'] == 'ranked' || $item['gameMode'] == 'casual') {
  703. $role = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
  704. if ($role === "Jungler") {
  705. $jkill[] = $item['kills'];
  706. $jdeaths[] = $item['deaths'];
  707. $jassist[] = $item['assists'];
  708. $jwin[] = $item['winner'];
  709. } else if ($role === "Carry") {
  710. $ckill[] = $item['kills'];
  711. $cdeaths[] = $item['deaths'];
  712. $cassist[] = $item['assists'];
  713. $cwin[] = $item['winner'];
  714. } else if ($role === "Captain") {
  715. $capkill[] = $item['kills'];
  716. $capdeaths[] = $item['deaths'];
  717. $capassist[] = $item['assists'];
  718. $capwin[] = $item['winner'];
  719. }
  720. }
  721.  
  722. $json['Data']['Matches'][$i]['player'] = $url['filter']['playerNames'];
  723.  
  724. // if (empty($json['Stats']['SkillTier'])) {
  725. // $json['Stats']['SkillTier'] = $item['skillTier'];
  726. // }
  727.  
  728. $death_kda = $item['deaths'] == 0 ? 1 : $item['deaths'];
  729.  
  730. $calculate_kda = (($item['kills'] + $item['assists']) / $death_kda);
  731.  
  732. $itemsbought_game = json_decode($item['rawParticipant'], true);
  733.  
  734. $i++;
  735. }
  736.  
  737. if ($i > 0) {
  738.  
  739. $json['Stats']['name'] = $url['filter']['playerNames'];
  740. $json['Stats']['region'] = $country;
  741.  
  742. // if ($rank_games > 0) {
  743. // $json['Stats']['Ranked']['winrate'] = round((array_sum($rank_wins) / $rank_games) * 100);
  744. // $json['Stats']['Ranked']['KDA'] = round($utils_match->division(array_sum($rank_kills) + array_sum($rank_assists), array_sum($rank_deaths)), 2) * 100;
  745. // $json['Stats']['Ranked']['Wins'] = array_sum($rank_wins);
  746. // $json['Stats']['Ranked']['Loss'] = $rank_games - array_sum($rank_wins);
  747. // }
  748.  
  749. // $json['Stats']['winrate'] = round((array_sum($win) / $i) * 100);
  750. // $json['Stats']['KP'] = round(((array_sum($kills) + array_sum($assists)) / array_sum($totalkills)) * 100);
  751. // $json['Stats']['KrakenCap'] = array_sum($kraken);
  752. // $json['Stats']['totalAce'] = array_sum($aces);
  753. // $json['Stats']['totalTurrets'] = array_sum($turrets);
  754.  
  755. // $json['Stats']['Blue']['Wins'] = array_sum($bluewins);
  756. // $json['Stats']['Blue']['Total'] = count($bluewins);
  757. // $json['Stats']['Blue']['Loss'] = count($bluewins) - array_sum($bluewins);
  758. // $json['Stats']['Blue']['WinRate'] = round($utils_match->division(array_sum($bluewins), count($bluewins)), 2) * 100;
  759. // $json['Stats']['Red']['Wins'] = array_sum($redwins);
  760. // $json['Stats']['Red']['Total'] = count($redwins);
  761. // $json['Stats']['Red']['Loss'] = count($redwins) - array_sum($redwins);
  762. // $json['Stats']['Red']['WinRate'] = round($utils_match->division(array_sum($redwins), count($redwins)), 2) * 100;
  763. // $json['Stats']['Kills'] = array_sum($kills);
  764. // $json['Stats']['totalKills'] = array_sum($totalkills);
  765. // $json['Stats']['Assists'] = array_sum($assists);
  766. // $json['Stats']['Deaths'] = array_sum($deaths);
  767.  
  768. // $json['Stats']['TotalTime'] = array_sum($time);
  769. // $json['Stats']['totalFarm'] = array_sum($farm);
  770. // $json['Stats']['gold'] = array_sum($gold);
  771. // $json['Stats']['totalWin'] = array_sum($win);
  772. // $json['Stats']['totalLoss'] = $i - array_sum($win);
  773. $json['Stats']['KDA'] = (round($utils_match->division(array_sum($kills) + array_sum($assists), array_sum($deaths)), 2) * 100) / 100;
  774. // $json['Stats']['KillsAssisted'] = array_sum($kills) + array_sum($assists);
  775. // $json['Stats']['KillsNotAssisted'] = array_sum($totalkills) - (array_sum($kills) + array_sum($assists));
  776. // $json['Stats']['avgKill'] = str_replace('.', "", round(array_sum($kills) / $i, 2));
  777. // $json['Stats']['avgAssist'] = str_replace('.', "", round(array_sum($assists) / $i, 2));
  778. // $json['Stats']['avgDeaths'] = str_replace('.', "", round(array_sum($deaths) / $i, 2));
  779. $json['Stats']['csmin'] = $utils_match->convertFtoMinT(array_sum($time), array_sum($farm));
  780. // $json['Stats']['totalMatches'] = $i;
  781.  
  782. // $json['Stats']['RecentResults'] = array_slice($win, 0, 6);
  783.  
  784.  
  785. $json['Stats']['VPR'] = $utils->get_current_vpr($url['filter']['playerNames']);
  786. $json['Stats']['VPRSpring'] = $utils->get_spring_vpr($url['filter']['playerNames']);
  787. $json['Stats']['RankLocal'] = $utils->get_rank_local($url['filter']['playerNames']);
  788. $json['Stats']['RankGlobal'] = $utils->get_rank_global($url['filter']['playerNames']);
  789.  
  790. $database_total_matches = $i;
  791.  
  792. foreach ($heroes as $hero => $herod) {
  793.  
  794. $herokda = round($utils_match->division(array_sum($herod['Kills']) + array_sum($herod['Assists']), array_sum($herod['Deaths'])), 2);
  795.  
  796. $herototalv = array_sum($herod['Wins']);
  797. $herototall = count($herod['Wins']) - array_sum($herod['Wins']);
  798. $herototalm = count($herod['Wins']);
  799.  
  800. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalKills'] = array_sum($herod['Kills']);
  801. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalDeaths'] = array_sum($herod['Deaths']);
  802. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalAssists'] = array_sum($herod['Assists']);
  803. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalFarm'] = array_sum($herod['Farm']);
  804. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['kda'] = round($utils_match->division(array_sum($herod['Kills']) + array_sum($herod['Assists']), array_sum($herod['Deaths'])), 2);
  805. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
  806. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalVictory'] = array_sum($herod['Wins']);
  807. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalLoss'] = count($herod['Wins']) - array_sum($herod['Wins']);
  808. //
  809. // if (array_sum($herod['Wins']) != 0) {
  810. // $winrate = round($utils_match->division(array_sum($herod['Wins']), count($herod['Wins'])) * 100);
  811. // } else {
  812. // $winrate = 0;
  813. // }
  814. //
  815. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Winrate'] = $winrate;
  816.  
  817. // Generate the match percentage
  818. $percentage = round(($herototalm / $database_total_matches) * 100);
  819.  
  820. if ($percentage >= 5) {
  821.  
  822. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Name'] = $utils_match->getRealNameHero($hero);
  823. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Image'] = strtolower('http://vgpro.gg/assets/images/heroes/' . $utils_match->getRealNameHero($hero) . '.gif');
  824.  
  825. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
  826. $hero_vpr = round(($herokda + ($herototalv - $herototall) / $herototalm), 2);
  827. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['HeroVPR'] = $hero_vpr;
  828.  
  829. // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Percentage'] = $percentage;
  830. }
  831.  
  832. }
  833.  
  834. $heroesvpr = [];
  835.  
  836. foreach ($json['Stats']['Heroes'] as $key => $value) {
  837. $heroesvpr[] = $value['HeroVPR'];
  838. }
  839.  
  840. // sorting by VPR
  841. array_multisort($heroesvpr, SORT_DESC, $json['Stats']['Heroes'], SORT_DESC, $json['Stats']['Heroes']);
  842.  
  843. // Display only 3 heroes
  844. $totalheroes = 0;
  845. foreach ($json['Stats']['Heroes'] as $key => $hero) {
  846. if ($totalheroes < 3) {
  847. unset($hero['HeroVPR']);
  848. $json['Stats']['TopHeroes'][] = $hero;
  849. $totalheroes++;
  850. }
  851. }
  852. unset($json['Stats']['Heroes']);
  853.  
  854.  
  855. // $winrateheroes = [];
  856. // $totalheroes = [];
  857. //
  858. // foreach ($json['Stats']['Heroes'] as $key => $value) {
  859. // $winrateheroes[] = $value['HeroVPR'];
  860. ////
  861. // $totalheroes[] = $value['totalMatches'];
  862. // }
  863. //
  864. // array_multisort($totalheroes, SORT_DESC, $winrateheroes, SORT_DESC, $json['Stats']['Heroes']);
  865.  
  866. // $json['Stats']['gameMode'] = $gameMode;
  867. //
  868. // $array_heroes = $json['Stats']['Heroes'];
  869. //
  870. // foreach($array_heroes as $hero){
  871. // print_r($hero);
  872. // }
  873.  
  874. //
  875. // foreach ($playedwin as $key => $value) {
  876. // $json['Stats']['PlayedWith'][$key]['Win'] = $value;
  877. // $json['Stats']['PlayedWith'][$key]['Loss'] = '0';
  878. // $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
  879. // $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
  880. // }
  881. //
  882. // foreach ($playedloss as $key => $value) {
  883. // if (!isset($json['Stats']['PlayedWith'][$key]['Win'])) {
  884. // $json['Stats']['PlayedWith'][$key]['Win'] = 0;
  885. // }
  886. // $json['Stats']['PlayedWith'][$key]['Loss'] = $value;
  887. // $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
  888. // $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
  889. // }
  890. //
  891. // // as known as
  892. // $json['Stats']['AKA'] = $utils_match->aka($player_id, $url['filter']['playerNames']);
  893. //
  894. // $total = [];
  895. //
  896. // if (isset($json['Stats']['PlayedWith'])) {
  897. // foreach ($json['Stats']['PlayedWith'] as $key => $value) {
  898. // if ($value['Total'] <= 2) {
  899. // unset($json['Stats']['PlayedWith'][$key]);
  900. // } else {
  901. // $total[] = $value['Total'];
  902. // }
  903. // }
  904. //
  905. // array_multisort($total, SORT_DESC, $json['Stats']['PlayedWith']);
  906. // }
  907.  
  908. return ($json['Stats']);
  909. }
  910.  
  911. }
  912. catch (Exception $exception) {
  913. // $this->query("INSERT INTO `website_logs` (`id`, `message`) VALUES (NULL, '{$exception->getMessage()}')");
  914. $curl_result = json_decode($curl_result, true);
  915. error_log($exception->getMessage() . $country . $url['filter']['playerNames'] . $type);
  916. $json['errors']['title'] = $exception->getMessage();
  917. // $cache->save($country . $url['filter']['playerNames'] . $type, json_encode($json));
  918. return json_encode($json);
  919. }
  920. }
  921. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement