FFXIV_Lodestone_Handler/app/Http/Controllers/LodestoneLinkController.php

225 lines
7.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\LinkCode;
use App\Services\CodeGeneratorService;
use App\Services\LodestoneApiService;
use App\Services\LodestoneCacheService;
use App\UserLink;
use Carbon\Carbon;
class LodestoneLinkController extends Controller
{
private $codeGeneratorService;
private $lodestoneCacheService;
private $lodestoneApiService;
public function __construct(
CodeGeneratorService $codeGeneratorService,
LodestoneCacheService $lodestoneCacheService,
LodestoneApiService $lodestoneApiService
)
{
$this->codeGeneratorService = $codeGeneratorService;
$this->lodestoneCacheService = $lodestoneCacheService;
$this->lodestoneApiService = $lodestoneApiService;
}
public function checkDiscordName(
string $server,
string $name,
int $discordGuildId,
int $discordUserId
) {
$character = $this->lodestoneCacheService->getCharacterByName($server, $name, $this->lodestoneApiService)->content;
// Check already linked
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
return [
'linked' => true,
'linkCode' => null,
'expires' => null
];
}
// Check code exists
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
$expirationTime = Carbon::now()->addMinutes(5);
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
$linkCode->expires = $expirationTime;
$linkCode->save();
} else {
if ($linkCode !== null)
$linkCode->delete();
$identifier = $this->codeGeneratorService->generateIdentifierDiscordName($server, $name, $discordGuildId, $discordUserId);
$code = $this->codeGeneratorService->generateCode(
'discord',
$identifier
);
$linkCode = LinkCode::create([
'lodestoneId' => $character['Character']['ID'],
'discordUserId' => $discordUserId,
'expires' => $expirationTime,
'code' => $code
]);
}
return [
'linked' => false,
'linkCode' => $linkCode->code,
'expires' => $linkCode->expires
];
}
public function checkDiscordId(
int $lodestoneId,
int $discordGuildId,
int $discordUserId
) {
$character = $this->lodestoneCacheService->getCharacterById($lodestoneId, $this->lodestoneApiService)->content;
// Check already linked
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
return [
'linked' => true,
'linkCode' => null,
'expires' => null
];
}
// Check code exists
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
$expirationTime = Carbon::now()->addMinutes(5);
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
$linkCode->expires = $expirationTime;
$linkCode->save();
} else {
if ($linkCode !== null)
$linkCode->delete();
$identifier = $this->codeGeneratorService->generateIdentifierDiscordId($lodestoneId, $discordGuildId, $discordUserId);
$code = $this->codeGeneratorService->generateCode(
'discord',
$identifier
);
$linkCode = LinkCode::create([
'lodestoneId' => $character['Character']['ID'],
'discordUserId' => $discordUserId,
'expires' => $expirationTime,
'code' => $code
]);
}
return [
'linked' => false,
'linkCode' => $linkCode->code,
'expires' => $linkCode->expires
];
}
public function checkWebsiteName(
string $server,
string $name,
string $hostname,
int $websiteUserId
) {
$character = $this->lodestoneCacheService->getCharacterByName($server, $name, $this->lodestoneApiService)->content;
// Check already linked
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
return [
'linked' => true,
'linkCode' => null,
'expires' => null
];
}
// Check code exists
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
$expirationTime = Carbon::now()->addMinutes(5);
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
$linkCode->expires = $expirationTime;
$linkCode->save();
} else {
if ($linkCode !== null)
$linkCode->delete();
$identifier = $this->codeGeneratorService->generateIdentifierWebsiteName($server, $name, $hostname, $websiteUserId);
$code = $this->codeGeneratorService->generateCode(
'discord',
$identifier
);
$linkCode = LinkCode::create([
'lodestoneId' => $character['Character']['ID'],
'websiteHostname' => $hostname,
'websiteUserId' => $websiteUserId,
'expires' => $expirationTime,
'code' => $code
]);
}
return [
'linked' => false,
'linkCode' => $linkCode->code,
'expires' => $linkCode->expires
];
}
public function checkWebsiteId(
int $lodestoneId,
string $hostname,
int $websiteUserId
) {
$character = $this->lodestoneCacheService->getCharacterById($lodestoneId, $this->lodestoneApiService)->content;
// Check already linked
if (UserLink::where('lodestoneId', $character['Character']['ID'])->count() > 0) {
return [
'linked' => true,
'linkCode' => null,
'expires' => null
];
}
// Check code exists
$linkCode = LinkCode::where('lodestoneId', $character['Character']['ID'])->first();
$expirationTime = Carbon::now()->addMinutes(5);
if ($linkCode !== null && $linkCode->expires > Carbon::now()) {
$linkCode->expires = $expirationTime;
$linkCode->save();
} else {
if ($linkCode !== null)
$linkCode->delete();
$identifier = $this->codeGeneratorService->generateIdentifierWebsiteId($lodestoneId, $hostname, $websiteUserId);
$code = $this->codeGeneratorService->generateCode(
'discord',
$identifier
);
$linkCode = LinkCode::create([
'lodestoneId' => $character['Character']['ID'],
'websiteHostname' => $hostname,
'websiteUserId' => $websiteUserId,
'expires' => $expirationTime,
'code' => $code
]);
}
return [
'linked' => false,
'linkCode' => $linkCode->code,
'expires' => $linkCode->expires
];
}
public function linkDiscord(
int $lodestoneId,
int $discordGuildId,
int $discordUserId
) {}
public function linkWebsite(
int $lodestoneId,
string $hostname,
int $websiteId
) {}
}