FFXIV_Lodestone_Handler/app/Http/Controllers/RankCheckController.php

76 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\FreeCompanyRankedUser;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class RankCheckController extends Controller
{
public function getRankChanges(string $discordGuildId)
{
if (!in_array($discordGuildId, array_keys(config('chaosapi.free_company_ranked_user_discord_mapping')))) {
abort(404);
}
$lodestoneFreeCompanyId = config('chaosapi.free_company_ranked_user_discord_mapping.' . $discordGuildId);
$members = FreeCompanyRankedUser::where('freeCompanyId', $lodestoneFreeCompanyId)->get()->toArray();
$members = array_map(function ($e) use ($lodestoneFreeCompanyId) {
$e['lodestoneId'] = (string)$e['lodestoneId'];
$e['shouldBeRole'] = null;
$editable_roles = config('chaosapi.free_company_ranked_user.' . $lodestoneFreeCompanyId . '.rank_time');
// We don't want to edit council or mentor
if (!in_array($e['ingameRole'], array_keys($editable_roles))) return $e;
// Keep track of the highest role
$highest_role = $e['ingameRole'];
foreach ($editable_roles as $role => $min_days) {
if ($min_days === null) continue;
$min_date = Carbon::parse($e['firstSeen'])->addDays($min_days);
if ($min_date <= Carbon::now())
$highest_role = $role;
}
if (array_search($e['ingameRole'], array_keys($editable_roles)) < array_search($highest_role, array_keys($editable_roles)))
$e['shouldBeRole'] = $highest_role;
return $e;
}, $members);
$mostRecentTimestamp = array_reduce($members, function($c, $e) {
$lastSeen = Carbon::parse($e['lastSeen']);
if ($lastSeen > $c)
return $lastSeen;
return $c;
}, Carbon::createFromTimestamp(0));
$demotionTimestamp = $mostRecentTimestamp->subHours(config('chaosapi.helix.demotion_time_hours', 3));
$members = array_filter($members, function($e) use ($demotionTimestamp) {
if ($e['lastSeen'] > $demotionTimestamp) {
return true;
}
// User should be removed from our system regarding Helix
$fcRankedUser = FreeCompanyRankedUser::find($e['lodestoneId']);
if ($fcRankedUser !== null)
$fcRankedUser->delete();
return false;
});
// Ensure that the keys are still sequential
$members = array_values($members);
return [
'success' => true,
'error' => null,
'data' => $members
];
}
}