Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.83 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. * Class MatchCurl
  23. * @package App\Core\Matches
  24. */
  25. class MatchCurl extends Curl implements MatchInterface
  26. {
  27.  
  28. /*---------------------------------------------------------------------*/
  29. /**
  30. * Saves a match.
  31. *
  32. * @param string $match The match
  33. * @param string $country The country
  34. * @param $match
  35. * @param $country
  36. */
  37. public function save_match($match, $country)
  38. {
  39. $utils_match = new MatchUtils();
  40. // save the matches
  41. foreach ($match['data'] as $datum) {
  42.  
  43. // if there is a telemetry for the match
  44. if (isset($datum['relationships']['assets']['data'][0])) {
  45.  
  46. $telemetry_id = $datum['relationships']['assets']['data'];
  47.  
  48. // search into the telemetry array for the respective id
  49. $get_tel_id = array_search($telemetry_id[0]['id'], array_column($match['included'], 'id'));
  50.  
  51. $match['included'][$get_tel_id]['match_id'] = $datum['id'];
  52. }
  53.  
  54. // get rosters
  55. $roster_0 = $datum['relationships']['rosters']['data'][0]['id'];
  56. $roster_1 = $datum['relationships']['rosters']['data'][1]['id'];
  57.  
  58. $get_roster_0 = array_search($roster_0, array_column($match['included'], 'id'));
  59. $get_roster_1 = array_search($roster_1, array_column($match['included'], 'id'));
  60.  
  61. $match['included'][$get_roster_0]['match_id'] = $datum['id'];
  62. $match['included'][$get_roster_1]['match_id'] = $datum['id'];
  63.  
  64. //assign match_id to rosters
  65. foreach ($match['included'][$get_roster_0]['relationships']['participants']['data'] as $key => $value) {
  66. $hey = array_search($value['id'], array_column($match['included'], 'id'));
  67. $match['included'][$hey]['roster_id'] = $value['id'];
  68. $match['included'][$hey]['createdAt'] = $datum['attributes']['createdAt'];
  69. $match['included'][$hey]['gameMode'] = $datum['attributes']['gameMode'];
  70. $match['included'][$hey]['match_id'] = $datum['id'];
  71. }
  72. //assign match_id to rosters
  73. foreach ($match['included'][$get_roster_1]['relationships']['participants']['data'] as $key => $value) {
  74. $hey = array_search($value['id'], array_column($match['included'], 'id'));
  75. $match['included'][$hey]['roster_id'] = $value['id'];
  76. $match['included'][$hey]['createdAt'] = $datum['attributes']['createdAt'];
  77. $match['included'][$hey]['gameMode'] = $datum['attributes']['gameMode'];
  78. $match['included'][$hey]['match_id'] = $datum['id'];
  79. }
  80.  
  81. //$datum['patchVersion']
  82. //$datum['attributes']['patchVersion'] = $utils_match->getRealPath($datum['attributes']['createdAt']);
  83. // save the request into the database
  84. $this->save_request('match', $datum);
  85. }
  86.  
  87. // save the roster
  88. foreach ($match['included'] as $datum) {
  89. switch ($datum['type']) {
  90. case 'roster':
  91.  
  92. // $hey = array_search($datum['relationships']['player']['data']['id'], array_column($match['included'], 'id'));
  93. // $datum['attributes']['name'] = $match['included'][$hey]['attributes']['name'];
  94.  
  95.  
  96. $this->save_request('roster', $datum);
  97. break;
  98.  
  99. case 'participant':
  100. // validate the json if no errors, after that will save the request into database
  101. //print_r($datum['relationships']['player']['data']['id']);
  102. $hey = array_search($datum['relationships']['player']['data']['id'], array_column($match['included'], 'id'));
  103.  
  104. $datum['attributes']['name'] = $match['included'][$hey]['attributes']['name'];
  105.  
  106.  
  107. $datum['attributes']['shardId'] = $country;
  108. $datum['attributes']['actor'] = $utils_match->getRealNameHero_api($datum['attributes']['actor']);
  109.  
  110. $datum['attributes']['shardId'] = $country;
  111.  
  112.  
  113. $this->save_request('participant', $datum);
  114. // validate the json if no errors, after that will save the request into database
  115. //$this->save_request('itemGrants', $datum);
  116. // validate the json if no errors, after that will save the request into database
  117. //$this->save_request('items', $datum);
  118. break;
  119.  
  120. case 'player':
  121. $datum['attributes']['shardId'] = $country;
  122. // validate the json if no errors, after that will save the request into database
  123.  
  124. $this->save_request('players_match', $datum);
  125. break;
  126. case 'team':
  127. // validate the json if no errors, after that will save the request into database
  128. //$this->save_request('team', $datum);
  129. break;
  130. case 'asset':
  131. // validate the json if no errors, after that will save the telemetry into the database
  132. $this->save_request('asset', $datum);
  133. break;
  134. }
  135. }
  136. }
  137.  
  138. /**
  139. * Sends a match request.
  140. *
  141. * @param MatchUtils $country The country
  142. * @param <type> $method The method
  143. * @param <type> $query The query
  144. * @param null $type
  145. * @param string $force
  146. * @param $country
  147. * @param $method
  148. * @param $query
  149. * @return bool|string
  150. *
  151. * @throws Exception (description)
  152. */
  153. public function send_match_request($country, $method, $query, $type = NULL, $force = '')
  154. {
  155.  
  156. try {
  157. // will use the utils class
  158. $utils_match = new MatchUtils();
  159. $utils = new Utils();
  160. $cache = new Cache('match');
  161.  
  162. $assistant = new MatchAssistant();
  163.  
  164. $i = 0;
  165. $kills = [];
  166. $deaths = [];
  167. $assists = [];
  168. $gold = [];
  169. $totalkills = [];
  170. $farm = [];
  171. $win = [];
  172. $time = [];
  173. $gameMode = [];
  174. $jkill = [];
  175. $ckill = [];
  176. $capdeaths = [];
  177. $jdeaths = [];
  178. $cdeaths = [];
  179. $capkill = [];
  180. $jassist = [];
  181. $cassist = [];
  182. $capassist = [];
  183. $cwin = [];
  184. $capwin = [];
  185. $jwin = [];
  186. $kraken = [];
  187. $aces = [];
  188. $turrets = [];
  189. $bluewins = [];
  190. $redwins = [];
  191. $rank_wins = [];
  192. $rank_kills = [];
  193. $rank_deaths = [];
  194. $rank_assists = [];
  195. $rank_games = 0;
  196. $heroes = [];
  197. $playedwin = [];
  198. $playedloss = [];
  199.  
  200. // parse the $query params
  201. $url = $utils_match->valid_parse_url($query);
  202.  
  203. // get the player name
  204. $player_id = $utils_match->get_player_id($url['filter']['playerNames']);
  205.  
  206. if ($force == true) {
  207. $cache->delete($country . $url['filter']['playerNames'] . $type);
  208. }
  209.  
  210. if ($cache->read($country . $url['filter']['playerNames'] . $type) == false) {
  211.  
  212. $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  213.  
  214. // if player not exists
  215. if (empty($player_matches) || ($utils_match->verificar_cache($url['filter']['playerNames']) !== true && empty($type) || !empty($force))) {
  216.  
  217. // save into the cache table the recent player
  218. $this->save_request('cache', $url['filter']['playerNames']);
  219.  
  220. // transform the result in array
  221. $curl_result = $this->send_curl_request($country, $method, $query);
  222.  
  223. // check if there is an error
  224. $match = $this->values_request($curl_result);
  225.  
  226. // save into the database
  227. $this->save_match($match, $country);
  228.  
  229. // query the player matches
  230. $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  231. }
  232.  
  233. // if there is no match for the player
  234. if (!$player_matches) {
  235. throw new Exception('403: Found but no matches came from mysql'.var_dump($player_matches));
  236. }
  237.  
  238. $json['Data']['LastUpdate'] = $utils_match->get_last_match_cache($url['filter']['playerNames']);
  239.  
  240. foreach ($player_matches[0] as $item) {
  241. $gamemode[] = $utils_match->getRealMode($item['gameMode'], 'img_name');
  242.  
  243. $kills[] = $item['kills'];
  244. $deaths[] = $item['deaths'];
  245. $assists[] = $item['assists'];
  246. $gold[] = $item['gold'];
  247.  
  248. if ($item['gameMode'] == 'ranked') {
  249. $rank_kills[] = $item['kills'];
  250. $rank_deaths[] = $item['deaths'];
  251. $rank_assists[] = $item['assists'];
  252. $rank_wins[] = $item['winner'];
  253. $rank_games++;
  254.  
  255. }
  256.  
  257.  
  258. $heroes[$item['actor']]['Kills'][] = $item['kills'];
  259. $heroes[$item['actor']]['Assists'][] = $item['assists'];
  260. $heroes[$item['actor']]['Farm'][] = $item['farm'];
  261. $heroes[$item['actor']]['Wins'][] = $item['winner'];
  262. $heroes[$item['actor']]['Deaths'][] = $item['deaths'];
  263.  
  264.  
  265. $totalkills[] = $item['heroKills'];
  266.  
  267. $farm[] = $item['farm'];
  268. $win[] = $item['winner'];
  269. $time[] = $item['duration'];
  270. $kraken[] = $item['krakenCaptures'];
  271. $aces[] = $item['acesEarned'];
  272. $turrets[] = $item['turretKills'];
  273.  
  274. if ($item['side'] == "left/blue") {
  275. $bluewins[] = $item['winner'];
  276. } else if ($item['side'] == "right/red") {
  277. $redwins[] = $item['winner'];
  278. }
  279.  
  280. // detect role
  281. if ($item['gameMode'] == 'ranked' || $item['gameMode'] == 'casual') {
  282. $role = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
  283. if ($role === "Jungler") {
  284. $jkill[] = $item['kills'];
  285. $jdeaths[] = $item['deaths'];
  286. $jassist[] = $item['assists'];
  287. $jwin[] = $item['winner'];
  288. } else if ($role === "Carry") {
  289. $ckill[] = $item['kills'];
  290. $cdeaths[] = $item['deaths'];
  291. $cassist[] = $item['assists'];
  292. $cwin[] = $item['winner'];
  293. } else if ($role === "Captain") {
  294. $capkill[] = $item['kills'];
  295. $capdeaths[] = $item['deaths'];
  296. $capassist[] = $item['assists'];
  297. $capwin[] = $item['winner'];
  298. }
  299. }
  300.  
  301.  
  302. $json['Data']['Matches'][$i]['player'] = $url['filter']['playerNames'];
  303. if (empty($json['Stats']['SkillTier'])) {
  304. $json['Stats']['SkillTier'] = $item['skillTier'];
  305. }
  306.  
  307. $json['Data']['Matches'][$i]['MatchResult'] = $item['endGameReason'];
  308. $json['Data']['Matches'][$i]['Winner'] = $item['winner'];
  309. $json['Data']['Matches'][$i]['Side'] = $item['side'];
  310. $json['Data']['Matches'][$i]['skin'] = $item['skinKey'];
  311. $json['Data']['Matches'][$i]['duration'] = $utils_match->convertDuration($item['duration']);
  312. $json['Data']['Matches'][$i]['duration-seconds'] = $item['duration'];
  313. $json['Data']['Matches'][$i]['gmin'] = $utils_match->convertFtoMin($item['duration'], ($item['pgold'] * 1));
  314. $json['Data']['Matches'][$i]['csmin'] = $utils_match->convertFtoMin($item['duration'], $item['farm']);
  315. $json['Data']['Matches'][$i]['jungleKills'] = $item['jungleKills'];
  316. $json['Data']['Matches'][$i]['kills'] = $item['kills'];
  317. $json['Data']['Matches'][$i]['assists'] = $item['assists'];
  318. $json['Data']['Matches'][$i]['deaths'] = $item['deaths'];
  319.  
  320. $death_kda = $item['deaths'] == 0 ? 1 : $item['deaths'];
  321.  
  322. $calculate_kda = (($item['kills'] + $item['assists']) / $death_kda);
  323.  
  324. $json['Data']['Matches'][$i]['kda'] = round($calculate_kda, 2);
  325. $json['Data']['Matches'][$i]['farm'] = $item['farm'];
  326.  
  327. $json['Data']['Matches'][$i]['gold'] = (($item['pgold'] * 1) > 999 ? $utils_match->thousandsCurrencyFormat($item['pgold'] * 1) : $item['pgold'] * 1);
  328.  
  329. $json['Data']['Matches'][$i]['actor'] = strtolower($utils_match->getRealNameHero($item['actor']));
  330.  
  331. $rosters = $this->allRows("call side_rosters_match('{$item['id_match']}')");
  332.  
  333. foreach ($rosters as $team) {
  334.  
  335. $id_blue = $team[0]['BlueId'];
  336. $id_red = $team[0]['RedId'];
  337.  
  338. $blueTeam = $utils_match->allRows("SELECT * FROM `matches_roster` WHERE `id_roster` = '{$id_blue}'");
  339. $redTeam = $utils_match->allRows("SELECT * FROM `matches_roster` WHERE `id_roster` = '{$id_red}'");
  340. $json['Data']['Matches'][$i]['BlueTeam']['Aces'] = $blueTeam['0']['0']['acesEarned'];
  341. $json['Data']['Matches'][$i]['BlueTeam']['Gold'] = $blueTeam['0']['0']['gold'];
  342. $json['Data']['Matches'][$i]['BlueTeam']['Kills'] = $blueTeam['0']['0']['heroKills'];
  343. $json['Data']['Matches'][$i]['BlueTeam']['KrakenCaptures'] = $blueTeam['0']['0']['krakenCaptures'];
  344. $json['Data']['Matches'][$i]['BlueTeam']['TurretsDestroyed'] = $blueTeam['0']['0']['turretKills'];
  345. $json['Data']['Matches'][$i]['BlueTeam']['TurretsRemaining'] = $blueTeam['0']['0']['turretsRemaining'];
  346.  
  347. $json['Data']['Matches'][$i]['RedTeam']['Aces'] = $redTeam['0']['0']['acesEarned'];
  348. $json['Data']['Matches'][$i]['RedTeam']['Gold'] = $redTeam['0']['0']['gold'];
  349. $json['Data']['Matches'][$i]['RedTeam']['Kills'] = $redTeam['0']['0']['heroKills'];
  350. $json['Data']['Matches'][$i]['RedTeam']['KrakenCaptures'] = $redTeam['0']['0']['krakenCaptures'];
  351. $json['Data']['Matches'][$i]['RedTeam']['TurretsDestroyed'] = $redTeam['0']['0']['turretKills'];
  352. $json['Data']['Matches'][$i]['RedTeam']['TurretsRemaining'] = $redTeam['0']['0']['turretsRemaining'];
  353.  
  354.  
  355. // obtaining the participants
  356. $red_players = [$redTeam['0']['0']['id_participant1'], $redTeam['0']['0']['id_participant2'], $redTeam['0']['0']['id_participant3']];
  357. $blue_players = [$blueTeam['0']['0']['id_participant1'], $blueTeam['0']['0']['id_participant2'], $blueTeam['0']['0']['id_participant3']];
  358.  
  359. // generating the stats for the participants
  360. $blue_participants = $assistant->create_participant_stats($blue_players, $item['side'], $url['filter']['playerNames'], "left/blue");
  361. $red_participants = $assistant->create_participant_stats($red_players, $item['side'], $url['filter']['playerNames'], "right/red");
  362.  
  363. // registering inside the array
  364. $json['Data']['Matches'][$i]['BlueTeam']['Players'] = $blue_participants;
  365. $json['Data']['Matches'][$i]['RedTeam']['Players'] = $red_participants;
  366.  
  367. // showing played win/loss
  368. $json['Data']['Matches'][$i]['RedTeam']['PlayedWin'] = $red_participants['PlayedWin'];
  369. $json['Data']['Matches'][$i]['RedTeam']['PlayedLoss'] = $red_participants['PlayedLoss'];
  370. $json['Data']['Matches'][$i]['BlueTeam']['PlayedWin'] = $blue_participants['PlayedWin'];
  371. $json['Data']['Matches'][$i]['BlueTeam']['PlayedLoss'] = $blue_participants['PlayedLoss'];
  372.  
  373. // removing some unused arrays
  374. 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']);
  375. }
  376.  
  377.  
  378. // generating the array values
  379. foreach ($json['Data']['Matches'][$i]['RedTeam']['PlayedWin'] as $player) {
  380. $playedwin[] = $player;
  381. }
  382.  
  383. // generating the array values
  384. foreach ($json['Data']['Matches'][$i]['RedTeam']['PlayedLoss'] as $player) {
  385. $playedloss[] = $player;
  386. }
  387.  
  388. // generating the array values
  389. foreach ($json['Data']['Matches'][$i]['BlueTeam']['PlayedWin'] as $player) {
  390. $playedwin[] = $player;
  391. }
  392.  
  393. // generating the array values
  394. foreach ($json['Data']['Matches'][$i]['BlueTeam']['PlayedLoss'] as $player) {
  395. $playedloss[] = $player;
  396. }
  397.  
  398. $json['Data']['Matches'][$i]['TypeMatch'] = $utils_match->getRealMode($item['gameMode']);
  399. $json['Data']['Matches'][$i]['Role'] = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
  400. $json['Data']['Matches'][$i]['Patch'] = $item['patchVersion'];
  401. $json['Data']['Matches'][$i]['MatchDateTime'] = $item['createdAt'];
  402. $json['Data']['Matches'][$i]['MatchWhen'] = $utils_match->time_elapsed_string($item['createdAt']);
  403. $json['Data']['Matches'][$i]['MId'] = $item['id_match'];
  404.  
  405. $itemsbought_game = json_decode($item['rawParticipant'], true);
  406.  
  407. foreach ($itemsbought_game['attributes']['stats']['items'] as $itembought) {
  408. $json['Data']['Matches'][$i]['Items'][] = strtolower($utils_match->get_item_image_name($itembought));
  409. }
  410.  
  411. $i++;
  412. }
  413.  
  414. if ($i > 0) {
  415.  
  416. //$top_heroes = $this->allRows("call topHeroes('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
  417.  
  418. foreach ($heroes as $hero => $herod) {
  419.  
  420. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalKills'] = array_sum($herod['Kills']);
  421. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalDeaths'] = array_sum($herod['Deaths']);
  422. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalAssists'] = array_sum($herod['Assists']);
  423. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalFarm'] = array_sum($herod['Farm']);
  424. $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);
  425. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
  426. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalVictory'] = array_sum($herod['Wins']);
  427. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalLoss'] = count($herod['Wins']) - array_sum($herod['Wins']);
  428.  
  429. if (array_sum($herod['Wins']) != 0) {
  430. $winrate = round($utils_match->division(array_sum($herod['Wins']), count($herod['Wins'])) * 100);
  431. } else {
  432. $winrate = 0;
  433. }
  434.  
  435. $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Winrate'] = $winrate;
  436.  
  437. }
  438. $winrateheroes = [];
  439. $totalheroes = [];
  440. foreach ($json['Stats']['Heroes'] as $key => $value) {
  441. $winrateheroes[] = $value['Winrate'];
  442.  
  443. $totalheroes[] = $value['totalMatches'];
  444. }
  445. array_multisort($totalheroes, SORT_DESC, $winrateheroes, SORT_DESC, $json['Stats']['Heroes']);
  446. // generating the stats
  447. if ($rank_games > 0) {
  448. $json['Stats']['Ranked']['winrate'] = round((array_sum($rank_wins) / $rank_games) * 100);
  449. $json['Stats']['Ranked']['KDA'] = round($utils_match->division(array_sum($rank_kills) + array_sum($rank_assists), array_sum($rank_deaths)), 2) * 100;
  450. $json['Stats']['Ranked']['Wins'] = array_sum($rank_wins);
  451. $json['Stats']['Ranked']['Loss'] = $rank_games - array_sum($rank_wins);
  452. }
  453.  
  454. $json['Stats']['winrate'] = round((array_sum($win) / $i) * 100);
  455. $json['Stats']['KP'] = round(((array_sum($kills) + array_sum($assists)) / array_sum($totalkills)) * 100);
  456. $json['Stats']['KrakenCap'] = array_sum($kraken);
  457. $json['Stats']['totalAce'] = array_sum($aces);
  458. $json['Stats']['totalTurrets'] = array_sum($turrets);
  459. $json['Stats']['Blue']['Wins'] = array_sum($bluewins);
  460. $json['Stats']['Blue']['Total'] = count($bluewins);
  461. $json['Stats']['Blue']['Loss'] = count($bluewins) - array_sum($bluewins);
  462. $json['Stats']['Blue']['WinRate'] = round($utils_match->division(array_sum($bluewins), count($bluewins)), 2) * 100;
  463. $json['Stats']['Red']['Wins'] = array_sum($redwins);
  464. $json['Stats']['Red']['Total'] = count($redwins);
  465. $json['Stats']['Red']['Loss'] = count($redwins) - array_sum($redwins);
  466. $json['Stats']['Red']['WinRate'] = round($utils_match->division(array_sum($redwins), count($redwins)), 2) * 100;
  467. $json['Stats']['Kills'] = array_sum($kills);
  468. $json['Stats']['totalKills'] = array_sum($totalkills);
  469. $json['Stats']['Assists'] = array_sum($assists);
  470. $json['Stats']['Deaths'] = array_sum($deaths);
  471.  
  472. $json['Stats']['TotalTime'] = array_sum($time);
  473. $json['Stats']['totalFarm'] = array_sum($farm);
  474. $json['Stats']['gold'] = array_sum($gold);
  475. $json['Stats']['totalWin'] = array_sum($win);
  476. $json['Stats']['totalLoss'] = $i - array_sum($win);
  477. $json['Stats']['KDA'] = round($utils_match->division(array_sum($kills) + array_sum($assists), array_sum($deaths)), 2) * 100;
  478. $json['Stats']['KillsAssisted'] = array_sum($kills) + array_sum($assists);
  479. $json['Stats']['KillsNotAssisted'] = array_sum($totalkills) - (array_sum($kills) + array_sum($assists));
  480. $json['Stats']['avgKill'] = str_replace('.', "", round(array_sum($kills) / $i, 2));
  481. $json['Stats']['avgAssist'] = str_replace('.', "", round(array_sum($assists) / $i, 2));
  482. $json['Stats']['avgDeaths'] = str_replace('.', "", round(array_sum($deaths) / $i, 2));
  483. $json['Stats']['totalMatches'] = $i;
  484. $json['Stats']['RecentResults'] = array_slice($win, 0, 6);
  485.  
  486. $json['Stats']['Roles']['Captain']['Kills'] = round($utils_match->division(array_sum($capkill), count($capkill)), 1);
  487. $json['Stats']['Roles']['Jungler']['Kills'] = round($utils_match->division(array_sum($jkill), count($jkill)), 1);
  488. $json['Stats']['Roles']['Carry']['Kills'] = round($utils_match->division(array_sum($ckill), count($ckill)), 1);
  489. $json['Stats']['Roles']['Captain']['Deaths'] = round($utils_match->division(array_sum($capdeaths), count($capkill)), 1);
  490. $json['Stats']['Roles']['Jungler']['Deaths'] = round($utils_match->division(array_sum($jdeaths), count($jkill)), 1);
  491. $json['Stats']['Roles']['Carry']['Deaths'] = round($utils_match->division(array_sum($cdeaths), count($ckill)), 1);
  492. $json['Stats']['Roles']['Captain']['Assists'] = round($utils_match->division(array_sum($capassist), count($capkill)), 1);
  493. $json['Stats']['Roles']['Jungler']['Assists'] = round($utils_match->division(array_sum($jassist), count($jkill)), 1);
  494. $json['Stats']['Roles']['Carry']['Assists'] = round($utils_match->division(array_sum($cassist), count($ckill)), 1);
  495. $json['Stats']['Roles']['Captain']['KDA'] = round($utils_match->division(array_sum($capkill) + array_sum($capassist), array_sum($capdeaths)), 2);
  496. $json['Stats']['Roles']['Jungler']['KDA'] = round($utils_match->division(array_sum($jkill) + array_sum($jassist), array_sum($jdeaths)), 2);
  497. $json['Stats']['Roles']['Carry']['KDA'] = round($utils_match->division(array_sum($ckill) + array_sum($cassist), array_sum($cdeaths)), 2);
  498. $json['Stats']['Roles']['Captain']['Win'] = array_sum($capwin);
  499. $json['Stats']['Roles']['Jungler']['Win'] = array_sum($jwin);
  500. $json['Stats']['Roles']['Carry']['Win'] = array_sum($cwin);
  501. $json['Stats']['Roles']['Captain']['Games'] = count($capkill);
  502. $json['Stats']['Roles']['Jungler']['Games'] = count($jkill);
  503. $json['Stats']['Roles']['Carry']['Games'] = count($ckill);
  504. $json['Stats']['Roles']['Captain']['Winrate'] = round($utils_match->division(array_sum($capwin), count($capkill)), 2) * 100;
  505. $json['Stats']['Roles']['Jungler']['Winrate'] = round($utils_match->division(array_sum($jwin), count($jwin)), 2) * 100;
  506. $json['Stats']['Roles']['Carry']['Winrate'] = round($utils_match->division(array_sum($cwin), count($ckill)), 2) * 100;
  507.  
  508. $playedwin = array_count_values(array_filter($playedwin));
  509. $gameMode = array_count_values(array_filter($gamemode));
  510. $playedloss = array_count_values(array_filter($playedloss));
  511.  
  512. $json['Stats']['gameMode'] = $gameMode;
  513.  
  514. foreach ($playedwin as $key => $value) {
  515. $json['Stats']['PlayedWith'][$key]['Win'] = $value;
  516. $json['Stats']['PlayedWith'][$key]['Loss'] = '0';
  517. $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
  518. $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
  519. }
  520.  
  521. foreach ($playedloss as $key => $value) {
  522. if (!isset($json['Stats']['PlayedWith'][$key]['Win'])) {
  523. $json['Stats']['PlayedWith'][$key]['Win'] = 0;
  524. }
  525. $json['Stats']['PlayedWith'][$key]['Loss'] = $value;
  526. $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
  527. $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
  528. }
  529.  
  530. // as known as
  531. $json['Stats']['AKA'] = $utils_match->aka($player_id, $url['filter']['playerNames']);
  532.  
  533. $total = array();
  534.  
  535. if (isset($json['Stats']['PlayedWith'])) {
  536. foreach ($json['Stats']['PlayedWith'] as $key => $value) {
  537. if ($value['Total'] <= 2) {
  538. unset($json['Stats']['PlayedWith'][$key]);
  539. } else {
  540. $total[] = $value['Total'];
  541. }
  542. }
  543.  
  544. array_multisort($total, SORT_DESC, $json['Stats']['PlayedWith']);
  545. }
  546. if (empty($type)) {
  547. if (array_sum($rank_wins) >= 10 && $json['Stats']['SkillTier'] > 0) {
  548. $getranking = $utils->generateRating($json['Stats']['Ranked'], $json['Stats']['SkillTier'], $url['filter']['playerNames'], $country);
  549. $json['Stats']['Rating'] = $getranking['rating'];
  550. $json['Stats']['Ranking'] = $getranking['ranking'];
  551. }
  552.  
  553. }
  554. //save historical data
  555. if(!($type)){
  556. $utils_match->saveHistorical($url['filter']['playerNames'], $country, $json['Stats']);
  557. }
  558.  
  559. // saving into the cache
  560.  
  561. $cache->save($country . $url['filter']['playerNames'] . $type . $force, json_encode($json));
  562. }
  563.  
  564. }
  565.  
  566. // reading the cache
  567. return $cache->read($country . $url['filter']['playerNames'] . $type . $force);
  568.  
  569. } catch (Exception $exception) {
  570. // $this->query("INSERT INTO `website_logs` (`id`, `message`) VALUES (NULL, '{$exception->getMessage()}')");
  571. $curl_result = json_decode($curl_result, true);
  572. error_log($exception->getMessage() . $country . $url['filter']['playerNames'] . $type);
  573. $json['errors']['title'] = $exception->getMessage();
  574. $cache->save($country . $url['filter']['playerNames'] . $type, json_encode($json));
  575. return json_encode($json);
  576. }
  577. }
  578. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement