API Fortnite

parent 1dd093bb
Pipeline #1331 passed with stages
in 3 minutes and 6 seconds
......@@ -8,6 +8,7 @@ use Onyx\Destiny\Helpers\String\Text;
use Onyx\Fortnite\Helpers\Network\FortniteApiNetworkException;
use Onyx\Fortnite\Helpers\Network\Http;
use Onyx\Fortnite\Objects\Stats;
use Onyx\User;
use Onyx\XboxLive\Enums\Console;
/**
......@@ -15,6 +16,8 @@ use Onyx\XboxLive\Enums\Console;
*/
class Client extends Http
{
public $userId = null;
/**
* @param Account $account
* @param string $id
......@@ -40,6 +43,14 @@ class Client extends Http
throw new FortniteApiNetworkException();
}
/**
* @param User $user
*/
public function setPandaAuth(User $user)
{
$this->userId = $user->id;
}
/**
* @param Account $account
* @return Stats
......@@ -170,6 +181,12 @@ class Client extends Http
$statModel->inactiveCounter++;
}
// Add if Panda
if ($this->userId !== null) {
$statModel->user_id = $this->userId;
$this->userId = null;
}
return $statModel->saveOrFail();
}
......
<?php
namespace Onyx\Fortnite\Helpers\Bot;
use Onyx\Account;
use Onyx\Fortnite\Objects\Stats;
class MessageGenerator
{
/**
* @var array
*/
private static $ignoredAttributes = ['id', 'epic_id', 'account_id', 'user_id', 'solo_lastmodified',
'duo_lastmodified', 'squad_lastmodified', 'created_at', 'updated_at', 'inactiveCounter'];
/**
* @param Account $account
* @param Stats $old
* @param Stats $new
* @return string
*/
public static function buildOverwatchUpdateMessage(Account $account, Stats $old, Stats $new)
{
$msg = '';
$stats = [];
$random_keys = array_rand($old->getAttributes(), count($old->getAttributes()));
shuffle($random_keys);
foreach ($random_keys as $random_key) {
if (!in_array($random_key, self::$ignoredAttributes) && count($stats) < 3) {
$difference = $new->$random_key - $old->$random_key;
if ($difference != 0) {
$stats[$random_key] = $difference;
}
}
}
// If no stats were changed, just grab 3.
if (count($stats) === 0) {
foreach ($random_keys as $random_key) {
if (!in_array($random_key, self::$ignoredAttributes) && count($stats) < 3) {
$difference = $new->$random_key - $old->$random_key;
$stats[$random_key] = $difference;
}
}
}
$gameDifference = $new->getMatchesSum() - $old->getMatchesSum();
$msg .= '<strong>'.$account->gamertag.'</strong> stats have been updated in <strong>' . $gameDifference . '</strong> games.<br />';
if ($old->solo_top1 < $new->solo_top1) {
$msg .= 'Holy shit. We have a new SOLO TOP 1 (#1) FIRST PLACE' . '<br />';
}
if ($old->duo_top1 < $new->duo_top1) {
$msg .= 'Wow. 1st place in DUOS. NICE!!!!' . '<br />';
}
if ($old->squad_top1 < $new->squad_top1) {
$msg .= 'PANDAS GETTING ANOTHER SQUAD WIN. #PNDA' . '<br />';
}
$msg .= '<br />Random Stats:<br />';
foreach ($stats as $key => $difference) {
$msg .= ucfirst(str_replace('_', ' ', $key)).': ';
$msg .= $new->$key.' ('.sprintf('%+d', $difference).') <br />';
}
return $msg;
}
}
......@@ -49,6 +49,26 @@ abstract class Console
}
}
/**
* @param string $platform
* @return int
*/
public static function idFromString(string $platform): int
{
switch (strtolower($platform)) {
case 'xbl':
case 'xbox':
case 'xb1':
return self::Xbox;
case 'psn':
case 'playstation':
return self::PSN;
case 'pc':
default:
return self::PC;
}
}
/**
* @param int $id
* @return string
......
<?php
namespace PandaLove\Http\Controllers\Fortnite;
use Illuminate\Http\Request as Request;
use Illuminate\Routing\Redirector as Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\View\Factory as View;
use Onyx\Fortnite\Objects\Stats;
use Onyx\User;
use Onyx\XboxLive\Enums\Console;
use PandaLove\Http\Controllers\Controller;
class ApiV1Controller extends Controller
{
private $view;
private $request;
private $redirect;
public $inactiveCounter = 10;
protected $layout = 'layouts.master';
public function __construct(View $view, Redirect $redirect, Request $request)
{
parent::__construct();
$this->view = $view;
$this->request = $request;
$this->redirect = $redirect;
}
//---------------------------------------------------------------------------------
// Fortnite GET
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
// Fortnite POST
//---------------------------------------------------------------------------------
public function postSetup()
{
$all = $this->request->all();
if (isset($all['google_id']) && isset($all['gamertag']) && $all['gamertag'] != '') {
try {
$client = new \Onyx\Fortnite\Client();
/** @var User $user */
$user = User::where('google_id', $all['google_id'])
->firstOrFail();
// With the user, we need gamertag and platform.
if (! isset($all['platform'])) {
$all['platform'] = Console::Xbox;
} else {
$all['platform'] = Console::idFromString($all['platform']);
}
// Check if platform exists.
[$id, $account] = $client->getAccountByTag($all['gamertag'], $all['platform']);
$msg = 'Account (' . $id . ') was found. Added into system. `/bot fn` will work.';
$client->setPandaAuth($user);
$client->getAccountRoyaleStats($account, $id);
return Response::json([
'error' => false,
'msg' => $msg,
], 200);
} catch (\Exception $e) {
return $this->_error($e->getMessage());
}
}
return $this->_error('Missing property: Need google_id, gamertag & platform.');
}
public function postUpdate()
{
$all = $this->request->all();
if (isset($all['google_id'])) {
try {
$client = new \Onyx\Fortnite\Client();
/** @var User $user */
$user = User::where('google_id', $all['google_id'])
->firstOrFail();
/** @var Stats $oldStats */
$oldStats = clone $user->fortnite;
$client->updateAccount($user->fortnite->account);
/** @var Stats $newStats */
$newStats = Stats::where('id', $oldStats->id)->first();
$msg = \Onyx\Fortnite\Helpers\Bot\MessageGenerator::buildOverwatchUpdateMessage($newStats->account, $oldStats, $newStats);
return Response::json([
'error' => false,
'msg' => $msg,
], 200);
} catch (\Exception $e) {
return $this->_error($e->getMessage());
}
}
}
//---------------------------------------------------------------------------------
// Private Functions
//---------------------------------------------------------------------------------
private function _error($message)
{
return Response::json([
'error' => true,
'message' => $message,
], 200);
}
}
......@@ -18,6 +18,7 @@ class VerifyCsrfToken extends BaseVerifier
'h5/api/*',
'overwatch/api/*',
'destiny2/api/*',
'fortnite/api/*',
];
/**
......
......@@ -26,6 +26,7 @@ Route::get('/destiny2/profile/{console}/{gamertag}/{characterId?}', 'Destiny2\Pr
Route::controller('/xbox/api/v1', 'Xbox\ApiV1Controller');
//# Fortnite
Route::controller('/fortnite/api/v1', 'Fortnite\ApiV1Controller');
Route::controller('/fortnite/roster', 'Fortnite\RosterController');
Route::get('/fortnite/profile/check-for-update/{id}', 'Fortnite\ProfileController@checkForUpdate');
Route::get('/fortnite/profile/manual-update/{id}', 'Fortnite\ProfileController@manualUpdate');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment