Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * //////////////////////////////////////////////////////////////////////////////////
- * // Authors:
- * // Lucas Reis Máximo Dias (thereis@live.com)
- * // Pierre Andreis Ortega (pierreandreisortega@gmail.com)
- * // Gabriel Prestes Dias (gabrielpdias@hotmail.com)
- * // 26/03/17 21:23
- * // class.MatchCurl.php
- * // Unauthorized copying of this file, via any medium is strictly prohibited
- * // * Proprietary and confidential
- * //////////////////////////////////////////////////////////////////////////////////
- */
- namespace App\Core\Matches;
- use App\Core\Curl;
- use Exception;
- use Main\Cache;
- use Main\Utils;
- /**
- * Class MatchCurl
- * @package App\Core\Matches
- */
- class MatchCurl extends Curl implements MatchInterface
- {
- /*---------------------------------------------------------------------*/
- /**
- * Saves a match.
- *
- * @param string $match The match
- * @param string $country The country
- * @param $match
- * @param $country
- * @return mixed|void
- */
- public function save_match($match, $country)
- {
- $utils_match = new MatchUtils();
- if (!isset($match['data'])) return;
- // save the matches
- foreach ($match['data'] as $datum) {
- // if there is a telemetry for the match
- if (isset($datum['relationships']['assets']['data'][0])) {
- $telemetry_id = $datum['relationships']['assets']['data'];
- // search into the telemetry array for the respective id
- $get_tel_id = array_search($telemetry_id[0]['id'], array_column($match['included'], 'id'));
- $match['included'][$get_tel_id]['match_id'] = $datum['id'];
- }
- // get rosters
- $roster_0 = $datum['relationships']['rosters']['data'][0]['id'];
- $roster_1 = $datum['relationships']['rosters']['data'][1]['id'];
- $get_roster_0 = array_search($roster_0, array_column($match['included'], 'id'));
- $get_roster_1 = array_search($roster_1, array_column($match['included'], 'id'));
- $match['included'][$get_roster_0]['match_id'] = $datum['id'];
- $match['included'][$get_roster_1]['match_id'] = $datum['id'];
- //assign match_id to rosters
- foreach ($match['included'][$get_roster_0]['relationships']['participants']['data'] as $key => $value) {
- $hey = array_search($value['id'], array_column($match['included'], 'id'));
- $match['included'][$hey]['roster_id'] = $value['id'];
- $match['included'][$hey]['createdAt'] = $datum['attributes']['createdAt'];
- $match['included'][$hey]['gameMode'] = $datum['attributes']['gameMode'];
- $match['included'][$hey]['match_id'] = $datum['id'];
- }
- //assign match_id to rosters
- foreach ($match['included'][$get_roster_1]['relationships']['participants']['data'] as $key => $value) {
- $hey = array_search($value['id'], array_column($match['included'], 'id'));
- $match['included'][$hey]['roster_id'] = $value['id'];
- $match['included'][$hey]['createdAt'] = $datum['attributes']['createdAt'];
- $match['included'][$hey]['gameMode'] = $datum['attributes']['gameMode'];
- $match['included'][$hey]['match_id'] = $datum['id'];
- }
- //$datum['patchVersion']
- //$datum['attributes']['patchVersion'] = $utils_match->getRealPath($datum['attributes']['createdAt']);
- // save the request into the database
- $this->save_request('match', $datum);
- }
- // save the roster
- foreach ($match['included'] as $datum) {
- switch ($datum['type']) {
- case 'roster':
- // $hey = array_search($datum['relationships']['player']['data']['id'], array_column($match['included'], 'id'));
- // $datum['attributes']['name'] = $match['included'][$hey]['attributes']['name'];
- $this->save_request('roster', $datum);
- break;
- case 'participant':
- // validate the json if no errors, after that will save the request into database
- //print_r($datum['relationships']['player']['data']['id']);
- if ($datum['attributes']['actor'] == "spectator") continue;
- $hey = array_search($datum['relationships']['player']['data']['id'], array_column($match['included'], 'id'));
- $datum['attributes']['name'] = $match['included'][$hey]['attributes']['name'];
- $datum['attributes']['shardId'] = $country;
- $datum['attributes']['actor'] = $utils_match->getRealNameHero_api($datum['attributes']['actor']);
- $datum['attributes']['shardId'] = $country;
- $this->save_request('participant', $datum);
- // validate the json if no errors, after that will save the request into database
- //$this->save_request('itemGrants', $datum);
- // validate the json if no errors, after that will save the request into database
- //$this->save_request('items', $datum);
- break;
- case 'player':
- $datum['attributes']['shardId'] = $country;
- // validate the json if no errors, after that will save the request into database
- $this->save_request('players_match', $datum);
- break;
- case 'team':
- // validate the json if no errors, after that will save the request into database
- //$this->save_request('team', $datum);
- break;
- case 'asset':
- // validate the json if no errors, after that will save the telemetry into the database
- $this->save_request('asset', $datum);
- break;
- }
- }
- }
- /**
- * Sends a match request.
- *
- * @param MatchUtils $country The country
- * @param <type> $method The method
- * @param <type> $query The query
- * @param null $type
- * @param string $force
- * @param $country
- * @param $method
- * @param $query
- * @return bool|string
- *
- * @throws Exception (description)
- */
- public function send_match_request($country, $method, $query, $type = NULL, $force = '')
- {
- try {
- // will use the utils class
- $utils_match = new MatchUtils();
- $utils = new Utils();
- $cache = new Cache('match');
- $assistant = new MatchAssistant();
- $i = 0;
- $kills = [];
- $deaths = [];
- $assists = [];
- $gold = [];
- $totalkills = [];
- $farm = [];
- $win = [];
- $time = [];
- $gameMode = [];
- $jkill = [];
- $ckill = [];
- $capdeaths = [];
- $jdeaths = [];
- $cdeaths = [];
- $capkill = [];
- $jassist = [];
- $cassist = [];
- $capassist = [];
- $cwin = [];
- $capwin = [];
- $jwin = [];
- $kraken = [];
- $aces = [];
- $turrets = [];
- $bluewins = [];
- $redwins = [];
- $rank_wins = [];
- $rank_kills = [];
- $rank_deaths = [];
- $rank_assists = [];
- $rank_games = 0;
- $heroes = [];
- $playedwin = [];
- $playedloss = [];
- // parse the $query params
- $url = $utils_match->valid_parse_url($query);
- // get the player name
- $player_id = $utils_match->get_player_id($url['filter']['playerNames']);
- if ($force == true) {
- $cache->delete($country . $url['filter']['playerNames'] . $type);
- }
- if ($cache->read($country . $url['filter']['playerNames'] . $type) == false) {
- $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
- // if player not exists
- if (empty($player_matches) || ($utils_match->verificar_cache($url['filter']['playerNames']) !== true && empty($type) || !empty($force))) {
- // save into the cache table the recent player
- $this->save_request('cache', $url['filter']['playerNames']);
- // transform the result in array
- $curl_result = $this->send_curl_request($country, $method, $query);
- // check if there is an error
- $match = $this->values_request($curl_result);
- // save into the database
- if ($match != false) $this->save_match($match, $country);
- // query the player matches
- $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
- }
- // if there is no match for the player
- if (!$player_matches) {
- throw new Exception('403: Found but no matches came from mysql' . var_dump($player_matches));
- }
- $json['Data']['LastUpdate'] = $utils_match->get_last_match_cache($url['filter']['playerNames']);
- foreach ($player_matches[0] as $item) {
- $gamemode[] = $utils_match->getRealMode($item['gameMode'], 'img_name');
- $kills[] = $item['kills'];
- $deaths[] = $item['deaths'];
- $assists[] = $item['assists'];
- $gold[] = $item['gold'];
- // This generate the stats
- if ($item['gameMode'] == 'ranked' && $item['patchVersion'] >= $this->summer_patch_version) {
- $rank_kills[] = $item['kills'];
- $rank_deaths[] = $item['deaths'];
- $rank_assists[] = $item['assists'];
- $rank_wins[] = $item['winner'];
- $rank_games++;
- }
- $heroes[$item['actor']]['Kills'][] = $item['kills'];
- $heroes[$item['actor']]['Assists'][] = $item['assists'];
- $heroes[$item['actor']]['Farm'][] = $item['farm'];
- $heroes[$item['actor']]['Wins'][] = $item['winner'];
- $heroes[$item['actor']]['Deaths'][] = $item['deaths'];
- $totalkills[] = $item['heroKills'];
- $farm[] = $item['farm'];
- $win[] = $item['winner'];
- $time[] = $item['duration'];
- $kraken[] = $item['krakenCaptures'];
- $aces[] = $item['acesEarned'];
- $turrets[] = $item['turretKills'];
- if ($item['side'] == "left/blue") {
- $bluewins[] = $item['winner'];
- } else if ($item['side'] == "right/red") {
- $redwins[] = $item['winner'];
- }
- // detect role
- if ($item['gameMode'] == 'ranked' || $item['gameMode'] == 'casual') {
- $role = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
- if ($role === "Jungler") {
- $jkill[] = $item['kills'];
- $jdeaths[] = $item['deaths'];
- $jassist[] = $item['assists'];
- $jwin[] = $item['winner'];
- } else if ($role === "Carry") {
- $ckill[] = $item['kills'];
- $cdeaths[] = $item['deaths'];
- $cassist[] = $item['assists'];
- $cwin[] = $item['winner'];
- } else if ($role === "Captain") {
- $capkill[] = $item['kills'];
- $capdeaths[] = $item['deaths'];
- $capassist[] = $item['assists'];
- $capwin[] = $item['winner'];
- }
- }
- $json['Data']['Matches'][$i]['player'] = $url['filter']['playerNames'];
- if (empty($json['Stats']['SkillTier'])) {
- $json['Stats']['SkillTier'] = $item['skillTier'];
- }
- $json['Data']['Matches'][$i]['MatchResult'] = $item['endGameReason'];
- $json['Data']['Matches'][$i]['Winner'] = $item['winner'];
- $json['Data']['Matches'][$i]['Side'] = $item['side'];
- $json['Data']['Matches'][$i]['skin'] = $item['skinKey'];
- $json['Data']['Matches'][$i]['duration'] = $utils_match->convertDuration($item['duration']);
- $json['Data']['Matches'][$i]['duration-seconds'] = $item['duration'];
- $json['Data']['Matches'][$i]['gmin'] = $utils_match->convertFtoMin($item['duration'], ($item['pgold'] * 1));
- $json['Data']['Matches'][$i]['csmin'] = $utils_match->convertFtoMin($item['duration'], $item['farm']);
- $json['Data']['Matches'][$i]['jungleKills'] = $item['jungleKills'];
- $json['Data']['Matches'][$i]['kills'] = $item['kills'];
- $json['Data']['Matches'][$i]['assists'] = $item['assists'];
- $json['Data']['Matches'][$i]['deaths'] = $item['deaths'];
- $death_kda = $item['deaths'] == 0 ? 1 : $item['deaths'];
- $calculate_kda = (($item['kills'] + $item['assists']) / $death_kda);
- $json['Data']['Matches'][$i]['kda'] = round($calculate_kda, 2);
- $json['Data']['Matches'][$i]['farm'] = $item['farm'];
- $json['Data']['Matches'][$i]['gold'] = (($item['pgold'] * 1) > 999 ? $utils_match->thousandsCurrencyFormat($item['pgold'] * 1) : $item['pgold'] * 1);
- $json['Data']['Matches'][$i]['actor'] = strtolower($utils_match->getRealNameHero($item['actor']));
- $rosters = $this->allRows("call side_rosters_match('{$item['id_match']}')");
- foreach ($rosters as $team) {
- $id_blue = $team[0]['BlueId'];
- $id_red = $team[0]['RedId'];
- $blueTeam = $utils_match->allRows("SELECT * FROM `matches_roster` WHERE `id_roster` = '{$id_blue}'");
- $redTeam = $utils_match->allRows("SELECT * FROM `matches_roster` WHERE `id_roster` = '{$id_red}'");
- $json['Data']['Matches'][$i]['BlueTeam']['Aces'] = $blueTeam['0']['0']['acesEarned'];
- $json['Data']['Matches'][$i]['BlueTeam']['Gold'] = $blueTeam['0']['0']['gold'];
- $json['Data']['Matches'][$i]['BlueTeam']['Kills'] = $blueTeam['0']['0']['heroKills'];
- $json['Data']['Matches'][$i]['BlueTeam']['KrakenCaptures'] = $blueTeam['0']['0']['krakenCaptures'];
- $json['Data']['Matches'][$i]['BlueTeam']['TurretsDestroyed'] = $blueTeam['0']['0']['turretKills'];
- $json['Data']['Matches'][$i]['BlueTeam']['TurretsRemaining'] = $blueTeam['0']['0']['turretsRemaining'];
- $json['Data']['Matches'][$i]['RedTeam']['Aces'] = $redTeam['0']['0']['acesEarned'];
- $json['Data']['Matches'][$i]['RedTeam']['Gold'] = $redTeam['0']['0']['gold'];
- $json['Data']['Matches'][$i]['RedTeam']['Kills'] = $redTeam['0']['0']['heroKills'];
- $json['Data']['Matches'][$i]['RedTeam']['KrakenCaptures'] = $redTeam['0']['0']['krakenCaptures'];
- $json['Data']['Matches'][$i]['RedTeam']['TurretsDestroyed'] = $redTeam['0']['0']['turretKills'];
- $json['Data']['Matches'][$i]['RedTeam']['TurretsRemaining'] = $redTeam['0']['0']['turretsRemaining'];
- // obtaining the participants
- $red_players = [$redTeam['0']['0']['id_participant1'], $redTeam['0']['0']['id_participant2'], $redTeam['0']['0']['id_participant3']];
- $blue_players = [$blueTeam['0']['0']['id_participant1'], $blueTeam['0']['0']['id_participant2'], $blueTeam['0']['0']['id_participant3']];
- // generating the stats for the participants
- $blue_participants = $assistant->create_participant_stats($blue_players, $item['side'], $url['filter']['playerNames'], "left/blue");
- $red_participants = $assistant->create_participant_stats($red_players, $item['side'], $url['filter']['playerNames'], "right/red");
- // registering inside the array
- $json['Data']['Matches'][$i]['BlueTeam']['Players'] = $blue_participants;
- $json['Data']['Matches'][$i]['RedTeam']['Players'] = $red_participants;
- // showing played win/loss
- $json['Data']['Matches'][$i]['RedTeam']['PlayedWin'] = $red_participants['PlayedWin'];
- $json['Data']['Matches'][$i]['RedTeam']['PlayedLoss'] = $red_participants['PlayedLoss'];
- $json['Data']['Matches'][$i]['BlueTeam']['PlayedWin'] = $blue_participants['PlayedWin'];
- $json['Data']['Matches'][$i]['BlueTeam']['PlayedLoss'] = $blue_participants['PlayedLoss'];
- // removing some unused arrays
- 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']);
- }
- if (isset($json['Data']['Matches'][$i]['RedTeam']) && isset($json['Data']['Matches'][$i]['BlueTeam'])) {
- // generating the array values
- foreach ($json['Data']['Matches'][$i]['RedTeam']['PlayedWin'] as $player) {
- $playedwin[] = $player;
- }
- // generating the array values
- foreach ($json['Data']['Matches'][$i]['RedTeam']['PlayedLoss'] as $player) {
- $playedloss[] = $player;
- }
- // generating the array values
- foreach ($json['Data']['Matches'][$i]['BlueTeam']['PlayedWin'] as $player) {
- $playedwin[] = $player;
- }
- // generating the array values
- foreach ($json['Data']['Matches'][$i]['BlueTeam']['PlayedLoss'] as $player) {
- $playedloss[] = $player;
- }
- $json['Data']['Matches'][$i]['TypeMatch'] = $utils_match->getRealMode($item['gameMode']);
- $json['Data']['Matches'][$i]['Role'] = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
- $json['Data']['Matches'][$i]['Patch'] = $item['patchVersion'];
- $json['Data']['Matches'][$i]['MatchDateTime'] = $item['createdAt'];
- $json['Data']['Matches'][$i]['MatchWhen'] = $utils_match->time_elapsed_string($item['createdAt']);
- $json['Data']['Matches'][$i]['MId'] = $item['id_match'];
- $itemsbought_game = json_decode($item['rawParticipant'], true);
- foreach ($itemsbought_game['attributes']['stats']['items'] as $itembought) {
- $json['Data']['Matches'][$i]['Items'][] = strtolower($utils_match->get_item_image_name($itembought));
- }
- $i++;
- } else unset($json['Data']['Matches'][$i]);
- }
- if ($i > 0) {
- //$top_heroes = $this->allRows("call topHeroes('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
- foreach ($heroes as $hero => $herod) {
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalKills'] = array_sum($herod['Kills']);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalDeaths'] = array_sum($herod['Deaths']);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalAssists'] = array_sum($herod['Assists']);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalFarm'] = array_sum($herod['Farm']);
- $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);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalVictory'] = array_sum($herod['Wins']);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalLoss'] = count($herod['Wins']) - array_sum($herod['Wins']);
- if (array_sum($herod['Wins']) != 0) {
- $winrate = round($utils_match->division(array_sum($herod['Wins']), count($herod['Wins'])) * 100);
- } else {
- $winrate = 0;
- }
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Winrate'] = $winrate;
- }
- $winrateheroes = [];
- $totalheroes = [];
- foreach ($json['Stats']['Heroes'] as $key => $value) {
- $winrateheroes[] = $value['Winrate'];
- $totalheroes[] = $value['totalMatches'];
- }
- array_multisort($totalheroes, SORT_DESC, $winrateheroes, SORT_DESC, $json['Stats']['Heroes']);
- // generating the stats
- if ($rank_games > 0) {
- $json['Stats']['Ranked']['winrate'] = round((array_sum($rank_wins) / $rank_games) * 100);
- $json['Stats']['Ranked']['KDA'] = round($utils_match->division(array_sum($rank_kills) + array_sum($rank_assists), array_sum($rank_deaths)), 2) * 100;
- $json['Stats']['Ranked']['Wins'] = array_sum($rank_wins);
- $json['Stats']['Ranked']['Loss'] = $rank_games - array_sum($rank_wins);
- }
- $json['Stats']['winrate'] = round((array_sum($win) / $i) * 100);
- $json['Stats']['KP'] = round(((array_sum($kills) + array_sum($assists)) / array_sum($totalkills)) * 100);
- $json['Stats']['KrakenCap'] = array_sum($kraken);
- $json['Stats']['totalAce'] = array_sum($aces);
- $json['Stats']['totalTurrets'] = array_sum($turrets);
- $json['Stats']['Blue']['Wins'] = array_sum($bluewins);
- $json['Stats']['Blue']['Total'] = count($bluewins);
- $json['Stats']['Blue']['Loss'] = count($bluewins) - array_sum($bluewins);
- $json['Stats']['Blue']['WinRate'] = round($utils_match->division(array_sum($bluewins), count($bluewins)), 2) * 100;
- $json['Stats']['Red']['Wins'] = array_sum($redwins);
- $json['Stats']['Red']['Total'] = count($redwins);
- $json['Stats']['Red']['Loss'] = count($redwins) - array_sum($redwins);
- $json['Stats']['Red']['WinRate'] = round($utils_match->division(array_sum($redwins), count($redwins)), 2) * 100;
- $json['Stats']['Kills'] = array_sum($kills);
- $json['Stats']['totalKills'] = array_sum($totalkills);
- $json['Stats']['Assists'] = array_sum($assists);
- $json['Stats']['Deaths'] = array_sum($deaths);
- $json['Stats']['TotalTime'] = array_sum($time);
- $json['Stats']['totalFarm'] = array_sum($farm);
- $json['Stats']['gold'] = array_sum($gold);
- $json['Stats']['totalWin'] = array_sum($win);
- $json['Stats']['totalLoss'] = $i - array_sum($win);
- $json['Stats']['KDA'] = round($utils_match->division(array_sum($kills) + array_sum($assists), array_sum($deaths)), 2) * 100;
- $json['Stats']['KillsAssisted'] = array_sum($kills) + array_sum($assists);
- $json['Stats']['KillsNotAssisted'] = array_sum($totalkills) - (array_sum($kills) + array_sum($assists));
- $json['Stats']['avgKill'] = str_replace('.', "", round(array_sum($kills) / $i, 2));
- $json['Stats']['avgAssist'] = str_replace('.', "", round(array_sum($assists) / $i, 2));
- $json['Stats']['avgDeaths'] = str_replace('.', "", round(array_sum($deaths) / $i, 2));
- $json['Stats']['totalMatches'] = $i;
- $json['Stats']['RecentResults'] = array_slice($win, 0, 6);
- $json['Stats']['Roles']['Captain']['Kills'] = round($utils_match->division(array_sum($capkill), count($capkill)), 1);
- $json['Stats']['Roles']['Jungler']['Kills'] = round($utils_match->division(array_sum($jkill), count($jkill)), 1);
- $json['Stats']['Roles']['Carry']['Kills'] = round($utils_match->division(array_sum($ckill), count($ckill)), 1);
- $json['Stats']['Roles']['Captain']['Deaths'] = round($utils_match->division(array_sum($capdeaths), count($capkill)), 1);
- $json['Stats']['Roles']['Jungler']['Deaths'] = round($utils_match->division(array_sum($jdeaths), count($jkill)), 1);
- $json['Stats']['Roles']['Carry']['Deaths'] = round($utils_match->division(array_sum($cdeaths), count($ckill)), 1);
- $json['Stats']['Roles']['Captain']['Assists'] = round($utils_match->division(array_sum($capassist), count($capkill)), 1);
- $json['Stats']['Roles']['Jungler']['Assists'] = round($utils_match->division(array_sum($jassist), count($jkill)), 1);
- $json['Stats']['Roles']['Carry']['Assists'] = round($utils_match->division(array_sum($cassist), count($ckill)), 1);
- $json['Stats']['Roles']['Captain']['KDA'] = round($utils_match->division(array_sum($capkill) + array_sum($capassist), array_sum($capdeaths)), 2);
- $json['Stats']['Roles']['Jungler']['KDA'] = round($utils_match->division(array_sum($jkill) + array_sum($jassist), array_sum($jdeaths)), 2);
- $json['Stats']['Roles']['Carry']['KDA'] = round($utils_match->division(array_sum($ckill) + array_sum($cassist), array_sum($cdeaths)), 2);
- $json['Stats']['Roles']['Captain']['Win'] = array_sum($capwin);
- $json['Stats']['Roles']['Jungler']['Win'] = array_sum($jwin);
- $json['Stats']['Roles']['Carry']['Win'] = array_sum($cwin);
- $json['Stats']['Roles']['Captain']['Games'] = count($capkill);
- $json['Stats']['Roles']['Jungler']['Games'] = count($jkill);
- $json['Stats']['Roles']['Carry']['Games'] = count($ckill);
- $json['Stats']['Roles']['Captain']['Winrate'] = round($utils_match->division(array_sum($capwin), count($capkill)), 2) * 100;
- $json['Stats']['Roles']['Jungler']['Winrate'] = round($utils_match->division(array_sum($jwin), count($jwin)), 2) * 100;
- $json['Stats']['Roles']['Carry']['Winrate'] = round($utils_match->division(array_sum($cwin), count($ckill)), 2) * 100;
- $playedwin = array_count_values(array_filter($playedwin));
- $gameMode = array_count_values(array_filter($gamemode));
- $playedloss = array_count_values(array_filter($playedloss));
- $json['Stats']['gameMode'] = $gameMode;
- foreach ($playedwin as $key => $value) {
- $json['Stats']['PlayedWith'][$key]['Win'] = $value;
- $json['Stats']['PlayedWith'][$key]['Loss'] = '0';
- $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
- $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
- }
- foreach ($playedloss as $key => $value) {
- if (!isset($json['Stats']['PlayedWith'][$key]['Win'])) {
- $json['Stats']['PlayedWith'][$key]['Win'] = 0;
- }
- $json['Stats']['PlayedWith'][$key]['Loss'] = $value;
- $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
- $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
- }
- // as known as
- $json['Stats']['AKA'] = $utils_match->aka($player_id, $url['filter']['playerNames']);
- $total = array();
- if (isset($json['Stats']['PlayedWith'])) {
- foreach ($json['Stats']['PlayedWith'] as $key => $value) {
- if ($value['Total'] <= 2) {
- unset($json['Stats']['PlayedWith'][$key]);
- } else {
- $total[] = $value['Total'];
- }
- }
- array_multisort($total, SORT_DESC, $json['Stats']['PlayedWith']);
- }
- $json['Stats']['SpringRating'] = $utils->get_spring_vpr($url['filter']['playerNames']);
- if (empty($type) && array_sum($rank_wins) >= 10 && $json['Stats']['SkillTier'] > 0) {
- $getranking = $utils->generateRating($json['Stats']['Ranked'], $json['Stats']['SkillTier'], $url['filter']['playerNames'], $country);
- $json['Stats']['Rating'] = $getranking['rating'];
- $json['Stats']['Ranking'] = $getranking['ranking'];
- }
- //save historical data
- if (!($type)) {
- $utils_match->saveHistorical($url['filter']['playerNames'], $country, $json['Stats']);
- }
- // saving into the cache
- $cache->save($country . $url['filter']['playerNames'] . $type . $force, json_encode($json));
- }
- }
- // reading the cache
- return $cache->read($country . $url['filter']['playerNames'] . $type . $force);
- }
- catch (Exception $exception) {
- // $this->query("INSERT INTO `website_logs` (`id`, `message`) VALUES (NULL, '{$exception->getMessage()}')");
- $curl_result = json_decode($curl_result, true);
- error_log($exception->getMessage() . $country . $url['filter']['playerNames'] . $type);
- $json['errors']['title'] = $exception->getMessage();
- $cache->save($country . $url['filter']['playerNames'] . $type, json_encode($json));
- return json_encode($json);
- }
- }
- public function send_tourney_request($country, $method, $query, $type = NULL, $force = '')
- {
- try {
- // will use the utils class
- $utils_match = new MatchUtils();
- $utils = new Utils();
- $assistant = new MatchAssistant();
- $i = 0;
- $kills = [];
- $deaths = [];
- $assists = [];
- $gold = [];
- $totalkills = [];
- $farm = [];
- $win = [];
- $time = [];
- $gameMode = [];
- $jkill = [];
- $ckill = [];
- $capdeaths = [];
- $jdeaths = [];
- $cdeaths = [];
- $capkill = [];
- $jassist = [];
- $cassist = [];
- $capassist = [];
- $cwin = [];
- $capwin = [];
- $jwin = [];
- $kraken = [];
- $aces = [];
- $turrets = [];
- $bluewins = [];
- $redwins = [];
- $rank_wins = [];
- $rank_kills = [];
- $rank_deaths = [];
- $rank_assists = [];
- $rank_games = 0;
- $heroes = [];
- $playedwin = [];
- $playedloss = [];
- // parse the $query params
- $url = $utils_match->valid_parse_url($query);
- // get the player name
- $player_id = $utils_match->get_player_id($url['filter']['playerNames']);
- $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
- // if player not exists
- if (empty($player_matches) || ($utils_match->verificar_cache($url['filter']['playerNames']) !== true && empty($type) || !empty($force))) {
- // save into the cache table the recent player
- $this->save_request('cache', $url['filter']['playerNames']);
- // transform the result in array
- $curl_result = $this->send_curl_request($country, $method, $query);
- // check if there is an error
- $match = $this->values_request($curl_result);
- // save into the database
- $this->save_match($match, $country);
- // query the player matches
- $player_matches = $this->allRows("call search_matches_namePlayer2('{$country}', '{$url['filter']['playerNames']}', '{$type}')");
- }
- foreach ($player_matches[0] as $item) {
- $gamemode[] = $utils_match->getRealMode($item['gameMode'], 'img_name');
- $kills[] = $item['kills'];
- $deaths[] = $item['deaths'];
- $assists[] = $item['assists'];
- $gold[] = $item['gold'];
- if ($item['gameMode'] == 'ranked') {
- $rank_kills[] = $item['kills'];
- $rank_deaths[] = $item['deaths'];
- $rank_assists[] = $item['assists'];
- $rank_wins[] = $item['winner'];
- $rank_games++;
- }
- $heroes[$item['actor']]['Kills'][] = $item['kills'];
- $heroes[$item['actor']]['Assists'][] = $item['assists'];
- $heroes[$item['actor']]['Farm'][] = $item['farm'];
- $heroes[$item['actor']]['Wins'][] = $item['winner'];
- $heroes[$item['actor']]['Deaths'][] = $item['deaths'];
- $totalkills[] = $item['heroKills'];
- $farm[] = $item['farm'];
- $win[] = $item['winner'];
- $time[] = $item['duration'];
- $kraken[] = $item['krakenCaptures'];
- $aces[] = $item['acesEarned'];
- $turrets[] = $item['turretKills'];
- // detect role
- if ($item['gameMode'] == 'ranked' || $item['gameMode'] == 'casual') {
- $role = $utils_match->detectRole($item['jungleKills'], $item['nonJungleMinionKills']);
- if ($role === "Jungler") {
- $jkill[] = $item['kills'];
- $jdeaths[] = $item['deaths'];
- $jassist[] = $item['assists'];
- $jwin[] = $item['winner'];
- } else if ($role === "Carry") {
- $ckill[] = $item['kills'];
- $cdeaths[] = $item['deaths'];
- $cassist[] = $item['assists'];
- $cwin[] = $item['winner'];
- } else if ($role === "Captain") {
- $capkill[] = $item['kills'];
- $capdeaths[] = $item['deaths'];
- $capassist[] = $item['assists'];
- $capwin[] = $item['winner'];
- }
- }
- $json['Data']['Matches'][$i]['player'] = $url['filter']['playerNames'];
- // if (empty($json['Stats']['SkillTier'])) {
- // $json['Stats']['SkillTier'] = $item['skillTier'];
- // }
- $death_kda = $item['deaths'] == 0 ? 1 : $item['deaths'];
- $calculate_kda = (($item['kills'] + $item['assists']) / $death_kda);
- $itemsbought_game = json_decode($item['rawParticipant'], true);
- $i++;
- }
- if ($i > 0) {
- $json['Stats']['name'] = $url['filter']['playerNames'];
- $json['Stats']['region'] = $country;
- // if ($rank_games > 0) {
- // $json['Stats']['Ranked']['winrate'] = round((array_sum($rank_wins) / $rank_games) * 100);
- // $json['Stats']['Ranked']['KDA'] = round($utils_match->division(array_sum($rank_kills) + array_sum($rank_assists), array_sum($rank_deaths)), 2) * 100;
- // $json['Stats']['Ranked']['Wins'] = array_sum($rank_wins);
- // $json['Stats']['Ranked']['Loss'] = $rank_games - array_sum($rank_wins);
- // }
- // $json['Stats']['winrate'] = round((array_sum($win) / $i) * 100);
- // $json['Stats']['KP'] = round(((array_sum($kills) + array_sum($assists)) / array_sum($totalkills)) * 100);
- // $json['Stats']['KrakenCap'] = array_sum($kraken);
- // $json['Stats']['totalAce'] = array_sum($aces);
- // $json['Stats']['totalTurrets'] = array_sum($turrets);
- // $json['Stats']['Blue']['Wins'] = array_sum($bluewins);
- // $json['Stats']['Blue']['Total'] = count($bluewins);
- // $json['Stats']['Blue']['Loss'] = count($bluewins) - array_sum($bluewins);
- // $json['Stats']['Blue']['WinRate'] = round($utils_match->division(array_sum($bluewins), count($bluewins)), 2) * 100;
- // $json['Stats']['Red']['Wins'] = array_sum($redwins);
- // $json['Stats']['Red']['Total'] = count($redwins);
- // $json['Stats']['Red']['Loss'] = count($redwins) - array_sum($redwins);
- // $json['Stats']['Red']['WinRate'] = round($utils_match->division(array_sum($redwins), count($redwins)), 2) * 100;
- // $json['Stats']['Kills'] = array_sum($kills);
- // $json['Stats']['totalKills'] = array_sum($totalkills);
- // $json['Stats']['Assists'] = array_sum($assists);
- // $json['Stats']['Deaths'] = array_sum($deaths);
- // $json['Stats']['TotalTime'] = array_sum($time);
- // $json['Stats']['totalFarm'] = array_sum($farm);
- // $json['Stats']['gold'] = array_sum($gold);
- // $json['Stats']['totalWin'] = array_sum($win);
- // $json['Stats']['totalLoss'] = $i - array_sum($win);
- $json['Stats']['KDA'] = (round($utils_match->division(array_sum($kills) + array_sum($assists), array_sum($deaths)), 2) * 100) / 100;
- // $json['Stats']['KillsAssisted'] = array_sum($kills) + array_sum($assists);
- // $json['Stats']['KillsNotAssisted'] = array_sum($totalkills) - (array_sum($kills) + array_sum($assists));
- // $json['Stats']['avgKill'] = str_replace('.', "", round(array_sum($kills) / $i, 2));
- // $json['Stats']['avgAssist'] = str_replace('.', "", round(array_sum($assists) / $i, 2));
- // $json['Stats']['avgDeaths'] = str_replace('.', "", round(array_sum($deaths) / $i, 2));
- $json['Stats']['csmin'] = $utils_match->convertFtoMinT(array_sum($time), array_sum($farm));
- // $json['Stats']['totalMatches'] = $i;
- // $json['Stats']['RecentResults'] = array_slice($win, 0, 6);
- $json['Stats']['VPR'] = $utils->get_current_vpr($url['filter']['playerNames']);
- $json['Stats']['VPRSpring'] = $utils->get_spring_vpr($url['filter']['playerNames']);
- $json['Stats']['RankLocal'] = $utils->get_rank_local($url['filter']['playerNames']);
- $json['Stats']['RankGlobal'] = $utils->get_rank_global($url['filter']['playerNames']);
- $database_total_matches = $i;
- foreach ($heroes as $hero => $herod) {
- $herokda = round($utils_match->division(array_sum($herod['Kills']) + array_sum($herod['Assists']), array_sum($herod['Deaths'])), 2);
- $herototalv = array_sum($herod['Wins']);
- $herototall = count($herod['Wins']) - array_sum($herod['Wins']);
- $herototalm = count($herod['Wins']);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalKills'] = array_sum($herod['Kills']);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalDeaths'] = array_sum($herod['Deaths']);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalAssists'] = array_sum($herod['Assists']);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalFarm'] = array_sum($herod['Farm']);
- // $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);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalVictory'] = array_sum($herod['Wins']);
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalLoss'] = count($herod['Wins']) - array_sum($herod['Wins']);
- //
- // if (array_sum($herod['Wins']) != 0) {
- // $winrate = round($utils_match->division(array_sum($herod['Wins']), count($herod['Wins'])) * 100);
- // } else {
- // $winrate = 0;
- // }
- //
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Winrate'] = $winrate;
- // Generate the match percentage
- $percentage = round(($herototalm / $database_total_matches) * 100);
- if ($percentage >= 5) {
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Name'] = $utils_match->getRealNameHero($hero);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Image'] = strtolower('http://vgpro.gg/assets/images/heroes/' . $utils_match->getRealNameHero($hero) . '.gif');
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['totalMatches'] = count($herod['Wins']);
- $hero_vpr = round(($herokda + ($herototalv - $herototall) / $herototalm), 2);
- $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['HeroVPR'] = $hero_vpr;
- // $json['Stats']['Heroes'][$utils_match->getRealNameHero($hero)]['Percentage'] = $percentage;
- }
- }
- $heroesvpr = [];
- foreach ($json['Stats']['Heroes'] as $key => $value) {
- $heroesvpr[] = $value['HeroVPR'];
- }
- // sorting by VPR
- array_multisort($heroesvpr, SORT_DESC, $json['Stats']['Heroes'], SORT_DESC, $json['Stats']['Heroes']);
- // Display only 3 heroes
- $totalheroes = 0;
- foreach ($json['Stats']['Heroes'] as $key => $hero) {
- if ($totalheroes < 3) {
- unset($hero['HeroVPR']);
- $json['Stats']['TopHeroes'][] = $hero;
- $totalheroes++;
- }
- }
- unset($json['Stats']['Heroes']);
- // $winrateheroes = [];
- // $totalheroes = [];
- //
- // foreach ($json['Stats']['Heroes'] as $key => $value) {
- // $winrateheroes[] = $value['HeroVPR'];
- ////
- // $totalheroes[] = $value['totalMatches'];
- // }
- //
- // array_multisort($totalheroes, SORT_DESC, $winrateheroes, SORT_DESC, $json['Stats']['Heroes']);
- // $json['Stats']['gameMode'] = $gameMode;
- //
- // $array_heroes = $json['Stats']['Heroes'];
- //
- // foreach($array_heroes as $hero){
- // print_r($hero);
- // }
- //
- // foreach ($playedwin as $key => $value) {
- // $json['Stats']['PlayedWith'][$key]['Win'] = $value;
- // $json['Stats']['PlayedWith'][$key]['Loss'] = '0';
- // $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
- // $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
- // }
- //
- // foreach ($playedloss as $key => $value) {
- // if (!isset($json['Stats']['PlayedWith'][$key]['Win'])) {
- // $json['Stats']['PlayedWith'][$key]['Win'] = 0;
- // }
- // $json['Stats']['PlayedWith'][$key]['Loss'] = $value;
- // $json['Stats']['PlayedWith'][$key]['Total'] = $json['Stats']['PlayedWith'][$key]['Win'] + $json['Stats']['PlayedWith'][$key]['Loss'];
- // $json['Stats']['PlayedWith'][$key]['Winrate'] = round(($json['Stats']['PlayedWith'][$key]['Win'] / $json['Stats']['PlayedWith'][$key]['Total']) * 100);
- // }
- //
- // // as known as
- // $json['Stats']['AKA'] = $utils_match->aka($player_id, $url['filter']['playerNames']);
- //
- // $total = [];
- //
- // if (isset($json['Stats']['PlayedWith'])) {
- // foreach ($json['Stats']['PlayedWith'] as $key => $value) {
- // if ($value['Total'] <= 2) {
- // unset($json['Stats']['PlayedWith'][$key]);
- // } else {
- // $total[] = $value['Total'];
- // }
- // }
- //
- // array_multisort($total, SORT_DESC, $json['Stats']['PlayedWith']);
- // }
- return ($json['Stats']);
- }
- }
- catch (Exception $exception) {
- // $this->query("INSERT INTO `website_logs` (`id`, `message`) VALUES (NULL, '{$exception->getMessage()}')");
- $curl_result = json_decode($curl_result, true);
- error_log($exception->getMessage() . $country . $url['filter']['playerNames'] . $type);
- $json['errors']['title'] = $exception->getMessage();
- // $cache->save($country . $url['filter']['playerNames'] . $type, json_encode($json));
- return json_encode($json);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement