Register services to deal with lodestone
This commit is contained in:
parent
e8ad668ff2
commit
43c5061011
30
app/Services/CodeGeneratorService.php
Normal file
30
app/Services/CodeGeneratorService.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
class CodeGeneratorService
|
||||||
|
{
|
||||||
|
public function generateCode(string $type, string $identifier) {
|
||||||
|
return "${type}|" . base64_encode(substr(Hash::make($identifier), -20, 20));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateIdentifierDiscordName($server, $name, $discordGuildId, $discordUserId) {
|
||||||
|
return $server . $name . $discordGuildId . $discordUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateIdentifierWebsiteName($server, $name, $hostname, $websiteId) {
|
||||||
|
return $server . $name . $hostname . $websiteId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateIdentifierDiscordId($lodestoneId, $discordGuildId, $discordUserId) {
|
||||||
|
return $lodestoneId . $discordGuildId . $discordUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateIdentifierWebsiteId($lodestoneId, $hostname, $websiteId) {
|
||||||
|
return $lodestoneId . $hostname . $websiteId;
|
||||||
|
}
|
||||||
|
}
|
||||||
61
app/Services/LodestoneApiService.php
Normal file
61
app/Services/LodestoneApiService.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
|
class LodestoneApiService
|
||||||
|
{
|
||||||
|
public function getCharacterById($id) {
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/character/'.$id);
|
||||||
|
return $this->completeCurlRequest($ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCharacterByName($server, $name) {
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/character/search?name='.$name.'&server='.$server);
|
||||||
|
$characterResults = $this->completeCurlRequest($ch);
|
||||||
|
|
||||||
|
if ($characterResults->Pagination->Results === 0)
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
|
||||||
|
return $this->getCharacterById($characterResults->Results[0]->ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFreeCompanyById($id) {
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/freecompany/'.$id);
|
||||||
|
return $this->completeCurlRequest($ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFreeCompanyByName($server, $name) {
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
curl_setopt($ch, CURLOPT_URL, 'https://xivapi.com/freecompany/search?name='.$name.'&server='.$server);
|
||||||
|
$characterResults = $this->completeCurlRequest($ch);
|
||||||
|
|
||||||
|
if ($characterResults->Pagination->Results === 0)
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
|
||||||
|
return $this->getFreeCompanyById($characterResults->Results[0]->ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function completeCurlRequest($ch)
|
||||||
|
{
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
if (curl_errno($ch)) {
|
||||||
|
throw new Exception('Error:' . curl_error($ch));
|
||||||
|
}
|
||||||
|
curl_close($ch);
|
||||||
|
return json_decode($result);
|
||||||
|
}
|
||||||
|
}
|
||||||
80
app/Services/LodestoneCacheService.php
Normal file
80
app/Services/LodestoneCacheService.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use App\LodestoneCache;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
|
class LodestoneCacheService
|
||||||
|
{
|
||||||
|
public function getCharacterById($id, $lodestoneApiService) {
|
||||||
|
$character = LodestoneCache::where('type', LodestoneCacheType::character)->where('lodestoneId', $id)->first();
|
||||||
|
if ($character !== null && $character->updated_at > $this->getCacheFreshTime()) return $character;
|
||||||
|
$fetchedCharacter = $lodestoneApiService->getCharacterById($id);
|
||||||
|
$character = LodestoneCache::updateOrCreate([
|
||||||
|
'type' => LodestoneCacheType::character,
|
||||||
|
'lodestoneId' => $fetchedCharacter->Character->ID,
|
||||||
|
'server' => $fetchedCharacter->Character->Server,
|
||||||
|
'name' => $fetchedCharacter->Character->Name,
|
||||||
|
],[
|
||||||
|
'content' => $fetchedCharacter
|
||||||
|
]);
|
||||||
|
return $character;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCharacterByName($server, $name, $lodestoneApiService) {
|
||||||
|
$character = LodestoneCache::where('type', LodestoneCacheType::character)->where('server', $server)->where('name', $name)->first();
|
||||||
|
if ($character !== null && $character->updated_at > $this->getCacheFreshTime()) return $character;
|
||||||
|
$fetchedCharacter = $lodestoneApiService->getCharacterByName($server, $name);
|
||||||
|
$character = LodestoneCache::updateOrCreate([
|
||||||
|
'type' => LodestoneCacheType::character,
|
||||||
|
'lodestoneId' => $fetchedCharacter->Character->ID,
|
||||||
|
'server' => $fetchedCharacter->Character->Server,
|
||||||
|
'name' => $fetchedCharacter->Character->Name,
|
||||||
|
],[
|
||||||
|
'content' => $fetchedCharacter
|
||||||
|
]);
|
||||||
|
return $character;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFreeCompanyById($id, $lodestoneApiService) {
|
||||||
|
$freeCompany = LodestoneCache::where('type', LodestoneCacheType::freeCompany)->where('lodestoneId', $id)->first();
|
||||||
|
if ($freeCompany !== null && $freeCompany->updated_at > $this->getCacheFreshTime()) return $freeCompany;
|
||||||
|
$fetchedFreeCompany = $lodestoneApiService->getFreeCompanyById($id);
|
||||||
|
$freeCompany = LodestoneCache::updateOrCreate([
|
||||||
|
'type' => LodestoneCacheType::freeCompany,
|
||||||
|
'lodestoneId' => $fetchedFreeCompany->FreeCompany->ID,
|
||||||
|
'server' => $fetchedFreeCompany->FreeCompany->Server,
|
||||||
|
'name' => $fetchedFreeCompany->FreeCompany->Name,
|
||||||
|
],[
|
||||||
|
'content' => $fetchedFreeCompany
|
||||||
|
]);
|
||||||
|
return $freeCompany;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFreeCompanyByName($server, $name, $lodestoneApiService) {
|
||||||
|
$freeCompany = LodestoneCache::where('type', LodestoneCacheType::freeCompany)->where('server', $server)->where('name', $name)->first();
|
||||||
|
if ($freeCompany !== null && $freeCompany->updated_at > $this->getCacheFreshTime()) return $freeCompany;
|
||||||
|
$fetchedFreeCompany = $lodestoneApiService->getFreeCompanyByName($server, $name);
|
||||||
|
$freeCompany = LodestoneCache::updateOrCreate([
|
||||||
|
'type' => LodestoneCacheType::freeCompany,
|
||||||
|
'lodestoneId' => $fetchedFreeCompany->FreeCompany->ID,
|
||||||
|
'server' => $fetchedFreeCompany->FreeCompany->Server,
|
||||||
|
'name' => $fetchedFreeCompany->FreeCompany->Name,
|
||||||
|
],[
|
||||||
|
'content' => $fetchedFreeCompany
|
||||||
|
]);
|
||||||
|
return $freeCompany;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCacheFreshTime() {
|
||||||
|
return Carbon::now()->subMinutes(config('chaosapi.cachetime', 60));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class LodestoneCacheType {
|
||||||
|
public const character = 0;
|
||||||
|
public const freeCompany = 1;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user