This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Console/Command/OsuLeaderboardUpdateCommand.php

76 lines
1.9 KiB
PHP
Raw Normal View History

2016-12-11 18:51:45 +00:00
<?php
/**
* Holds the osu-leaderboard-update command controller.
* @package Sakura
*/
namespace Sakura\Console\Command;
use Carbon\Carbon;
use CLIFramework\Command;
use Sakura\DB;
use Sakura\OsuLeaderboard;
use Sakura\User;
/**
* Connects to the osu!api and stores new data.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class OsuLeaderboardUpdateCommand extends Command
{
/**
* A quick description of this command.
* @return string.
*/
public function brief(): string
{
return 'Connects to the osu!api and stores new data.';
}
/**
* Command.
*/
public function execute(): void
{
$this->getLogger()->writeln('Updating osu! leaderboard...');
$api_key = config('osu.api_key');
if ($api_key === null) {
$this->getLogger()->writeln("Your api key isn't set in the config, get one at https://osu.ppy.sh/p/api.");
return;
}
$game_modes = [
'osu!',
'osu!taiko',
'osu!catch',
'osu!mania',
];
$osulb = new OsuLeaderboard($api_key);
$start = Carbon::now();
$users = DB::table('users')
->whereNotNull('user_osu')
->get(['user_id']);
foreach ($game_modes as $mode => $name) {
$this->getLogger()->writeln(PHP_EOL . "=> {$name}");
foreach ($users as $user) {
$user = User::construct($user->user_id);
$user_display = $user->osu . ($user->username !== $user->osu ? " ({$user->username})" : '');
$this->getLogger()->writeln("==> Updating {$name} scores for {$user_display}...");
$osulb->update($user->osu, $user, $mode);
}
}
$this->getLogger()->writeln(PHP_EOL . "Purging inactive data...");
$osulb->purge($start);
}
}