Skip to content

Commit

Permalink
cleaning code , renaming builds to snapshots and fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanD87 committed Mar 4, 2018
1 parent a514bcb commit d6c906d
Show file tree
Hide file tree
Showing 24 changed files with 768 additions and 730 deletions.
3 changes: 3 additions & 0 deletions .env.example
Expand Up @@ -33,3 +33,6 @@ PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

POE_VERSION=3.2
POE_LEAGUES='Abyss,Hardcore Abyss,SSF Abyss,SSF Abyss HC,10 Day Mayhem'
8 changes: 4 additions & 4 deletions app/Account.php
Expand Up @@ -28,7 +28,7 @@ public function ladderChars()

protected function getNewAccInfo($acc){
// $html = HtmlDomParser::file_get_html('https://www.pathofexile.com/account/view-profile/'. $acc .'/characters');
//problem with HtmlDomParser::file_get_html with php 7.1
//problem with HtmlDomParser::file_get_html with php 7.1
//get with Guzzle and then str_get_html
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://www.pathofexile.com/',
Expand All @@ -37,7 +37,7 @@ protected function getNewAccInfo($acc){
$response = $client->request('GET', '/account/view-profile/'. $acc .'/characters');
$body = $response->getBody()->getContents();
$html = HtmlDomParser::str_get_html($body);


//filter for accoutn name
$name = $html->find('.container-content h2',0);
Expand Down Expand Up @@ -79,7 +79,7 @@ public function updateLastChar(){
private function getLastCharFrom($html){
if($html==null){
// $html = HtmlDomParser::file_get_html('https://www.pathofexile.com/account/view-profile/'. $this->name .'/characters');
//problem with HtmlDomParser::file_get_html with php 7.1
//problem with HtmlDomParser::file_get_html with php 7.1
//get with Guzzle and then str_get_html

$client = new \GuzzleHttp\Client([
Expand Down Expand Up @@ -136,7 +136,7 @@ public function getItemsFor($char){
//get cahce if no cache get from poe api
$acc=$this->name;
$key='items/'.$acc.'/'.$char;
$items_response = \Cache::remember($key, 5, function () use ($acc,$char){
$items_response = \Cache::remember($key, config('app.poe_cache_time'), function () use ($acc,$char){
$client = new \GuzzleHttp\Client();
//make Requests to PathOfExile website to retrieve Character Items
try {
Expand Down
91 changes: 0 additions & 91 deletions app/Build.php

This file was deleted.

90 changes: 37 additions & 53 deletions app/Http/Controllers/ApiController.php
Expand Up @@ -10,13 +10,12 @@ class ApiController extends CacheController
{
public function getItems(Request $request)
{

if (!$request->has('account') && !$request->has('character')) {
return;
}
$b = explode('::', $request->input('account'));
if($b[0] == 'build'){
return \App\Build::find($b[1])->item_data['items'];
return \App\Snapshot::where('hash','=',$b[1])->get()->item_data['items'];
}
$acc=$request->input('account');
$char=$request->input('character');
Expand All @@ -34,9 +33,13 @@ public function getStats(Request $request)

$b = explode('::', $acc);
if ($b[0] == 'build') {
return \App\Build::find($b[1])->getStats();
$build=\App\Snapshot::where('hash','=',$b[1])->first();
if(!$build){
return [];
}
return $build->getStats();
}
//getItemsCache() is from parant class CacheController
//getStatsCache() is from parant class CacheController
return $this->getStatsCache($acc, $char);
}

Expand Down Expand Up @@ -82,12 +85,11 @@ public function getFavsChars()

public function getLadder(Request $request)
{

if ($request->has('searchFilter')) {
$respond = \App\LadderCharacter::with('account')->whereHas('account', function ($query) use (&$request) {
$query->where('name', 'like', '%'.$request->input ('searchFilter').'%');
})->orWhere('name', 'like', '%'.$request->input ('searchFilter').'%')->paginate();

return $respond;
}

Expand Down Expand Up @@ -116,7 +118,7 @@ public function getLadder(Request $request)
return $respond;
}

if ($request->has('leagueFilter')) {
if ($request->has('leagueFilter')) {
$respond = \App\LadderCharacter::with('account')->where('league', '=', $request->input('leagueFilter'))
->orderBy('rank', 'asc')->paginate();
return $respond;
Expand Down Expand Up @@ -161,7 +163,6 @@ public function getXML(Request $request)
$itemsData = $this->getItemsCache($acc, $char, true);
$treeJson = $this->getTreeCache($acc, $char);
$pob = new PobXMLBuilder($itemsData, $treeJson);


// show XML ---->
// Header('Content-type: text/xml');
Expand All @@ -170,59 +171,42 @@ public function getXML(Request $request)
return $pob->encodedXML();
}

public function defaultBuild(Request $request)
public function getBuild($hash)
{
$id = $request->input('id');
$name = $request->input('name');

$build = \App\Build::where('id', '=', $id)->where('name', '=', $name)->first();

$build = \App\Snapshot::where('hash', '=', $hash)->first();
return $build;
}

public function saveBuild(Request $request)
{
$client = new \GuzzleHttp\Client();
$treeData = $client->request(
'GET',
'https://www.pathofexile.com/character-window/get-passive-skills',
[
'query' => [
'accountName' => $request->input('account'),
'character' => $request->input('char')
]
]
);
$treeData = (string)$treeData->getBody();

$itemData = $client->request(
'GET',
'https://www.pathofexile.com/character-window/get-items',
[
'query' => [
'accountName' => $request->input('account'),
'character' => $request->input('char')
]
]
);
$itemData = (string)$itemData->getBody();
$acc=$request->input('account');
$char=$request->input('char');

//getItemsCache() true to get the whole respons
$itemData = $this->getItemsCache($acc, $char, true);
$itemData = json_encode($itemData);
$treeData = $this->getTreeCache($acc, $char);
$treeData = json_encode($treeData);

$hash = md5($treeData.'/'.$itemData);
$snapshot = \App\Snapshot::where('hash', '=', $hash)->first();
if(!$snapshot){
$version = env('POE_VERSION');
$original_char = $acc .'/'. $char;
$snapshot = \App\Snapshot::create([
'hash' => $hash,
'tree_data' => $treeData,
'item_data' => $itemData,
'original_char' => $original_char,
'poe_version' => $version
]);
$snapshot->original_level = $snapshot->item_data['character']['level'];
$snapshot->save();
}

$currentLeagues = explode(',', env('POE_LEAGUES'));
$buildName = str_replace(' ', '_', $request->input('name'));
$build = \App\Build::create([
'name' => $buildName,
'tree_data' => $treeData,
'item_data' => $itemData,
'poe_version' => $currentLeagues[0]
]);
$build->stats = $build->getStats();
$build->save();

$favStore = $build->item_data['character'];
$favStore['name'] = $buildName;
$favStore = $snapshot->item_data['character'];
$favStore['league'] = 'localBuild';
$favStore['buildId'] = $build->id;

$favStore['buildId'] = $snapshot->hash;
return $favStore;
}
}
47 changes: 4 additions & 43 deletions app/Http/Controllers/CacheController.php
Expand Up @@ -20,7 +20,7 @@ class CacheController extends BaseController
//get chars for acc from cache if no get from poe site
protected function getCharsCache($acc)
{
$chars = \Cache::remember($acc, 5, function () use ($acc) {
$chars = \Cache::remember($acc, config('app.poe_cache_time'), function () use ($acc) {
$client = new \GuzzleHttp\Client();
try {
$response = $client->request(
Expand Down Expand Up @@ -57,7 +57,6 @@ protected function getCharsCache($acc)
return $chars;
}


//get items for acc/char from cache if no get from poe site
protected function getItemsCache($acc, $char, $response=false)
{
Expand All @@ -83,43 +82,10 @@ protected function getStatsCache($acc, $char)
}
// dd(config('app.poe_cache_time'));
$stats = \Cache::remember($key, config('app.poe_cache_time'), function () use ($acc,$dbAcc,$char,$itemsRes) {
$stManager = new Stats_Manager;

//add Stats from Items

//filter for secondary items and flasks
$banItems = ['Flask', 'Weapon2', 'Offhand2'];
if (isset($_GET['offHand'])) {
$banItems = ['Flask', 'Weapon', 'Offhand'];
}

$filterItems = array_filter($itemsRes['items'], function($item) use (&$banItems) {
return !in_array($item['inventoryId'], $banItems);
});

$stManager->addItems($filterItems);

if ($dbAcc->last_character==$itemsRes['character']['name']) {
$dbAcc->updateLastCharInfo($itemsRes);
}
//end Stats from Items

//add Stats from Tree
//make Requests to PathOfExile website to retrieve Pasive Tree

$treeNodesJewels = new CharacterTreePoints;
$nodes = $treeNodesJewels->getPoints($this->getTreeCache($acc, $char));
$stManager->addTreeNode($nodes);

//add Stats Base for Class and Level
$character = $itemsRes['character'];
$baseStat = new Base_Stats;
$baseStat = $baseStat->getStats($character['level'], $character['classId']);
foreach ($baseStat as $stat) {
$stManager->addBaseMods($stat);
}
//end Stats Base

$stManager = new Stats_Manager($itemsRes, $this->getTreeCache($acc, $char));
return $stManager->getStats();
});

Expand All @@ -142,15 +108,10 @@ public function getTreeCache($acc, $char)
]
]
);

return json_decode((string)$responseTree->getBody(), true);
});
}

protected function getLocalItems(Request $request)
{
$jsonAcc = \Storage::disk('accounts')->get($request->input('accName') . '.txt');
$acc = json_decode($jsonAcc);
return response()->json($acc);
}

}

0 comments on commit d6c906d

Please sign in to comment.