public function random()
 {
     $builds = WinrateBuild::lists('id');
     shuffle($builds);
     $build = WinrateBuild::find($builds[0]);
     $build->load('itemsets.itemset_blocks.items', 'itemsets.champion');
     $items = [];
     foreach ($build->itemsets[0]->itemset_blocks as $block) {
         if ($block->type == 0) {
             $build->itemsets[0]->items = $block->items;
         }
     }
     return $build;
 }
Пример #2
0
 public function winrate_builds($from, $to)
 {
     $champions = Champion::where('id', '>=', $from)->where('id', '<', $to)->get();
     $itemsets = [];
     foreach ($champions as $champion) {
         $html = file_get_html('http://champion.gg/champion/' . $champion->key . '/');
         // Get the roles first
         $roles = $html->find('.champion-profile ul li a h3');
         $rolecounter = 1;
         foreach ($roles as $rolef) {
             $role = $rolef->plaintext;
             $role = trim($role);
             $html = file_get_html('http://champion.gg/champion/' . $champion->key . '/' . $role);
             $mostfrequent = WinrateBuild::firstOrNew(['champion_id' => $champion->riot_id, 'bestrate' => false, 'lane' => $role]);
             $bestwinrate = WinrateBuild::firstOrNew(['champion_id' => $champion->riot_id, 'bestrate' => true, 'lane' => $role]);
             $stats = $html->find('.build-wrapper .build-text strong');
             $mostfrequent->winrate = $stats[0]->plaintext;
             $mostfrequent->games = $stats[1]->plaintext;
             $mostfrequent->order = $rolecounter;
             $mostfrequent->save();
             $bestwinrate->winrate = $stats[2]->plaintext;
             $bestwinrate->games = $stats[3]->plaintext;
             $bestwinrate->order = $rolecounter;
             $bestwinrate->save();
             $frequent_set = Itemset::firstOrNew(['name' => $role . ' ' . $champion->name . ' Frequent', 'champion_id' => $champion->riot_id]);
             $winrate_set = Itemset::firstOrNew(['name' => $role . ' ' . $champion->name . ' Winrate', 'champion_id' => $champion->riot_id]);
             // Skill order
             $ret = $html->find('.skill-order div .skill-selections');
             // Most frequent skill order
             $abilitykeys = ['Nope', 'Q', 'W', 'E', 'R', 'Nope', 'Q', 'W', 'E', 'R'];
             $abilitymax = [];
             for ($i = 1; $i < 5; $i++) {
                 $childs = $ret[$i]->children();
                 $counter = 0;
                 for ($j = 0; $j < 18; $j++) {
                     if ($childs[$j]->class == 'selected') {
                         if ($j == 0) {
                             $frequent_set->point1 = $abilitykeys[$i];
                         } elseif ($j == 1) {
                             $frequent_set->point2 = $abilitykeys[$i];
                         } elseif ($j == 2) {
                             $frequent_set->point3 = $abilitykeys[$i];
                         } elseif ($counter == 4) {
                             $abilitymax[$abilitykeys[$i]] = $j + 1;
                         }
                         $counter++;
                     }
                 }
             }
             asort($abilitymax);
             $maxkeys = array_keys($abilitymax);
             $frequent_set->max1 = $maxkeys[0];
             $frequent_set->max2 = $maxkeys[1];
             $frequent_set->max3 = $maxkeys[2];
             // Highest winrate skill order
             $abilitymax = [];
             for ($i = 6; $i < 10; $i++) {
                 $childs = $ret[$i]->children();
                 $counter = 0;
                 for ($j = 0; $j < 18; $j++) {
                     if ($childs[$j]->class == 'selected') {
                         if ($j == 0) {
                             $winrate_set->point1 = $abilitykeys[$i];
                         } elseif ($j == 1) {
                             $winrate_set->point2 = $abilitykeys[$i];
                         } elseif ($j == 2) {
                             $winrate_set->point3 = $abilitykeys[$i];
                         } elseif ($counter == 4) {
                             $abilitymax[$abilitykeys[$i]] = $j + 1;
                         }
                         $counter++;
                     }
                 }
             }
             asort($abilitymax);
             $maxkeys = array_keys($abilitymax);
             $winrate_set->max1 = $maxkeys[0];
             $winrate_set->max2 = $maxkeys[1];
             $winrate_set->max3 = $maxkeys[2];
             // Summoner spells
             $ret = $html->find('.col-xxs-12 .summoner-wrapper a img');
             $counter = 0;
             foreach ($ret as $summoner) {
                 // 0 & 1 - most frequent, 2 & 3 - highest winrate
                 $key = preg_replace('/\\/\\/ddragon.leagueoflegends.com\\/cdn\\/5.16.1\\/img\\/spell\\//', '', $summoner->src);
                 $key = preg_replace('/\\.png/', '', $key);
                 $spell = SummonerSpell::where('key', $key)->first();
                 if ($counter == 0) {
                     $frequent_set->summoner1 = $spell->riot_id;
                 } elseif ($counter == 1) {
                     $frequent_set->summoner2 = $spell->riot_id;
                 } elseif ($counter == 2) {
                     $winrate_set->summoner1 = $spell->riot_id;
                 } elseif ($counter == 3) {
                     $winrate_set->summoner2 = $spell->riot_id;
                 }
                 $counter++;
             }
             $mostfrequent->itemsets()->save($frequent_set);
             $bestwinrate->itemsets()->save($winrate_set);
             // Final builds
             $ret = $html->find('.build-wrapper a img');
             $items = [];
             for ($i = 0; $i < 6; $i++) {
                 $key = preg_replace('/\\/\\/ddragon.leagueoflegends.com\\/cdn\\/5.16.1\\/img\\/item\\//', '', $ret[$i]->src);
                 $key = preg_replace('/\\.png/', '', $key);
                 array_push($items, $key);
             }
             $blockFinal = ItemsetBlock::firstOrNew(['name' => 'Frequent Core Items (' . $stats[0]->plaintext . ' with ' . $stats[1]->plaintext . ' games)', 'type' => 0, 'itemset_id' => $frequent_set->id]);
             $frequent_set->itemset_blocks()->save($blockFinal);
             $items_count = [];
             foreach ($items as $value) {
                 if (isset($items_count[(string) $value])) {
                     $items_count[(string) $value] = $items_count[(string) $value] + 1;
                 } else {
                     $items_count[(string) $value] = 1;
                 }
             }
             $counter = 0;
             foreach ($items_count as $key => $value) {
                 $item = Item::where('riot_id', $key)->first();
                 if (!$blockFinal->items->contains($item->id)) {
                     $blockFinal->items()->save($item, ['count' => $value, 'order' => $counter]);
                 }
                 $counter++;
             }
             // Best Winrate Final Builds too!
             $items = [];
             for ($i = 6; $i < 12; $i++) {
                 $key = preg_replace('/\\/\\/ddragon.leagueoflegends.com\\/cdn\\/5.16.1\\/img\\/item\\//', '', $ret[$i]->src);
                 $key = preg_replace('/\\.png/', '', $key);
                 array_push($items, $key);
             }
             $blockFinal = ItemsetBlock::firstOrNew(['name' => 'Best Winrate Core Items (' . $stats[2]->plaintext . ' with ' . $stats[3]->plaintext . ' games)', 'type' => 0, 'itemset_id' => $winrate_set->id]);
             $winrate_set->itemset_blocks()->save($blockFinal);
             $items_count = [];
             foreach ($items as $value) {
                 if (isset($items_count[(string) $value])) {
                     $items_count[(string) $value] = $items_count[(string) $value] + 1;
                 } else {
                     $items_count[(string) $value] = 1;
                 }
             }
             $counter = 0;
             foreach ($items_count as $key => $value) {
                 $item = Item::where('riot_id', $key)->first();
                 if (!$blockFinal->items->contains($item->id)) {
                     $blockFinal->items()->save($item, ['count' => $value, 'order' => $counter]);
                 }
                 $counter++;
             }
             // Starting items
             $ret = $html->find('.build-wrapper');
             $imgs = $ret[2]->find('a img');
             $items = [];
             for ($i = 0; $i < count($imgs); $i++) {
                 $key = preg_replace('/\\/\\/ddragon.leagueoflegends.com\\/cdn\\/5.16.1\\/img\\/item\\//', '', $imgs[$i]->src);
                 $key = preg_replace('/\\.png/', '', $key);
                 if ($key == 2009 || $key == 2010) {
                     $key = 2003;
                 }
                 array_push($items, $key);
             }
             $skillOrderString = $frequent_set->point1 . $frequent_set->point2 . $frequent_set->point3 . ', ' . $frequent_set->max1 . ' > ' . $frequent_set->max2 . ' > ' . $frequent_set->max3;
             $blockStarting = ItemsetBlock::firstOrNew(['name' => 'Frequent Starters. Skill order: ' . $skillOrderString, 'type' => 1, 'itemset_id' => $frequent_set->id]);
             $frequent_set->itemset_blocks()->save($blockStarting);
             $startings_count = [];
             foreach ($items as $value) {
                 if (isset($startings_count[(string) $value])) {
                     $startings_count[(string) $value] = $startings_count[(string) $value] + 1;
                 } else {
                     $startings_count[(string) $value] = 1;
                 }
             }
             $counter = 0;
             foreach ($startings_count as $key => $value) {
                 $item = Item::where('riot_id', $key)->first();
                 if (!$blockStarting->items->contains($item->id)) {
                     $blockStarting->items()->save($item, ['count' => $value, 'order' => $counter]);
                 }
                 $counter++;
             }
             $items = [];
             $imgs = $ret[3]->find('a img');
             for ($i = 0; $i < count($imgs); $i++) {
                 $key = preg_replace('/\\/\\/ddragon.leagueoflegends.com\\/cdn\\/5.16.1\\/img\\/item\\//', '', $imgs[$i]->src);
                 $key = preg_replace('/\\.png/', '', $key);
                 if ($key == 2009 || $key == 2010) {
                     $key = 2003;
                 }
                 array_push($items, $key);
             }
             $skillOrderString = $winrate_set->point1 . $winrate_set->point2 . $winrate_set->point3 . ', ' . $winrate_set->max1 . ' > ' . $winrate_set->max2 . ' > ' . $winrate_set->max3;
             $blockStarting = ItemsetBlock::firstOrNew(['name' => 'Best Winrate Starters. Skill order: ' . $skillOrderString, 'type' => 1, 'itemset_id' => $winrate_set->id]);
             $winrate_set->itemset_blocks()->save($blockStarting);
             $startings_count = [];
             foreach ($items as $value) {
                 if (isset($startings_count[(string) $value])) {
                     $startings_count[(string) $value] = $startings_count[(string) $value] + 1;
                 } else {
                     $startings_count[(string) $value] = 1;
                 }
             }
             $counter = 0;
             foreach ($startings_count as $key => $value) {
                 $item = Item::where('riot_id', $key)->first();
                 if (!$blockStarting->items->contains($item->id)) {
                     $blockStarting->items()->save($item, ['count' => $value, 'order' => $counter]);
                 }
                 $counter++;
             }
             //return WinrateBuild::where(['champion_id' => $champion->riot_id, 'bestrate' => false, 'lane' => $role])->with('itemsets.itemset_blocks.items')->first();
             array_push($itemsets, $frequent_set->name);
             array_push($itemsets, $winrate_set->name);
             $rolecounter++;
         }
     }
     $next = url() . '/api/filldata/winrate-builds/' . $to . '/' . ($to + ($to - $from));
     return ['itemsets-done' => $itemsets, 'next' => $next];
 }