111 lines
3.1 KiB
PHP
111 lines
3.1 KiB
PHP
<?php
|
|
/**
|
|
* Holds the osu!leaderboard backend.
|
|
* @package Sakura
|
|
*/
|
|
|
|
namespace Sakura;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Query\Builder;
|
|
use stdClass;
|
|
|
|
/**
|
|
* osu!leaderboard backend.
|
|
* @package Sakura
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
*/
|
|
class OsuLeaderboard
|
|
{
|
|
private const ENDPOINT = "https://osu.ppy.sh/api/";
|
|
|
|
protected $apiKey;
|
|
|
|
public function __construct(string $apiKey)
|
|
{
|
|
$this->apiKey = $apiKey;
|
|
}
|
|
|
|
public function purge(Carbon $before): void
|
|
{
|
|
DB::table('osu_leaderboard')
|
|
->where('updated_on', '<', $before)
|
|
->delete();
|
|
}
|
|
|
|
public function update($osu, User $user, int $mode = 0): void
|
|
{
|
|
// skip if the user hasn't been online for a month
|
|
if ($user->lastOnline < time() - 30 * 24 * 60 * 60) {
|
|
return;
|
|
}
|
|
|
|
$osu_user = $this->apiGetUser($osu, $mode);
|
|
|
|
if (!$osu_user || $osu_user->pp_raw < 1) {
|
|
return;
|
|
}
|
|
|
|
$exists = DB::table('osu_leaderboard')
|
|
->where(is_int($osu) ? 'osu_user_id' : 'osu_username', $osu)
|
|
->where('user_id', $user->id)
|
|
->where('osu_game_mode', $mode)
|
|
->count() > 1;
|
|
|
|
$data = [
|
|
'user_id' => $user->id,
|
|
'username' => $user->username,
|
|
|
|
'osu_user_id' => intval($osu_user->user_id),
|
|
'osu_username' => $osu_user->username,
|
|
|
|
'osu_game_mode' => $mode,
|
|
|
|
'osu_count_300' => intval($osu_user->count300),
|
|
'osu_count_100' => intval($osu_user->count100),
|
|
'osu_count_50' => intval($osu_user->count50),
|
|
|
|
'osu_count_ss' => intval($osu_user->count_rank_ss),
|
|
'osu_count_s' => intval($osu_user->count_rank_s),
|
|
'osu_count_a' => intval($osu_user->count_rank_a),
|
|
|
|
'osu_play_count' => intval($osu_user->playcount),
|
|
'osu_score_ranked' => intval($osu_user->ranked_score),
|
|
'osu_score_total' => intval($osu_user->total_score),
|
|
'osu_rank' => intval($osu_user->pp_rank),
|
|
|
|
'osu_country' => $osu_user->country,
|
|
'osu_rank_country' => intval($osu_user->pp_country_rank),
|
|
|
|
'osu_performance' => floatval($osu_user->pp_raw),
|
|
'osu_accuracy' => floatval($osu_user->accuracy),
|
|
'osu_level' => floatval($osu_user->level),
|
|
|
|
'updated_on' => Carbon::now(),
|
|
];
|
|
|
|
if ($exists) {
|
|
DB::table('osu_leaderboard')
|
|
->where('osu_user_id', $data->osu_user_id)
|
|
->where('user_id', $data->user_id)
|
|
->where('osu_game_mode', $data->mode)
|
|
->update($data);
|
|
} else {
|
|
DB::table('osu_leaderboard')
|
|
->insert($data);
|
|
}
|
|
}
|
|
|
|
private function apiGetUser($user, int $mode = 0): ?stdClass
|
|
{
|
|
$result = json_decode(
|
|
Net::request(static::ENDPOINT . "get_user?k={$this->apiKey}&u={$user}&m={$mode}")
|
|
);
|
|
|
|
if ($result === false || count($result) < 1) {
|
|
return null;
|
|
}
|
|
|
|
return $result[0];
|
|
}
|
|
}
|