FFXIV_Lodestone_Handler/app/Http/Controllers/HelixRankCheckController.php
2020-09-23 19:42:27 +02:00

67 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\FreeCompanyRankedUser;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class HelixRankCheckController extends Controller
{
public function getRankChanges()
{
$members = FreeCompanyRankedUser::all()->toArray();
$members = array_map(function ($e) {
$e['lodestoneId'] = (string)$e['lodestoneId'];
$e['shouldBeRole'] = null;
$editable_roles = config('chaosapi.helix.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;
});
return [
'success' => true,
'error' => null,
'data' => $members
];
}
}