75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\MatsuriHime;
|
||
|
|
||
|
use Guzzle;
|
||
|
use Carbon\Carbon;
|
||
|
use DB;
|
||
|
use Cache;
|
||
|
use App\Rank;
|
||
|
|
||
|
class Election
|
||
|
{
|
||
|
public static function getDramaNames() {
|
||
|
if(!Cache::has('drama_names')) {
|
||
|
$response = Guzzle::get("https://api.matsurihi.me/mltd/v1/election")->getBody()->getContents();
|
||
|
$dramas = json_decode($response);
|
||
|
$dramas = $dramas->dramas;
|
||
|
$drama_list = [];
|
||
|
foreach($dramas as $drama) {
|
||
|
$drama_roles = [];
|
||
|
foreach($drama->roles as $role) {
|
||
|
$drama_list[$role->name] = $drama->name;
|
||
|
}
|
||
|
}
|
||
|
Cache::forever('drama_names',$drama_list);
|
||
|
}
|
||
|
return Cache::get('drama_names');
|
||
|
}
|
||
|
public static function getRanking() {
|
||
|
$response = Guzzle::get("https://api.matsurihi.me/mltd/v1/election/current");
|
||
|
return json_decode($response->getBody()->getContents());
|
||
|
}
|
||
|
public static function createSnapshot() {
|
||
|
$rankings = Election::getRanking();
|
||
|
$hasSnapshot = true;
|
||
|
DB::beginTransaction();
|
||
|
foreach($rankings as $ladder) {
|
||
|
$existingSnapshot = Rank::where('role',$ladder->name)->where('summary_time',Carbon::parse($ladder->summaryTime))->count();
|
||
|
$hasSnapshot = $hasSnapshot && ($existingSnapshot > 0);
|
||
|
if($existingSnapshot == 0) {
|
||
|
foreach($ladder->data[0] as $data) {
|
||
|
$rank = new Rank();
|
||
|
$rank->role = $ladder->name;
|
||
|
$rank->character = $data->idol_name;
|
||
|
$rank->score = $data->score;
|
||
|
$rank->summary_time = Carbon::parse($ladder->summaryTime);
|
||
|
$rank->save();
|
||
|
}
|
||
|
} else {
|
||
|
\Log::info("snapshot has been taken for " . (string)$ladder->summaryTime);
|
||
|
}
|
||
|
}
|
||
|
DB::commit();
|
||
|
if($hasSnapshot == 1) return;
|
||
|
$ranks = Rank::all()->groupBy('role');
|
||
|
foreach($ranks as $roleName => $roleRank) {
|
||
|
$chartPoint = [];
|
||
|
$idols = $roleRank->unique('character')->pluck('character');
|
||
|
$timepoints = $roleRank->sortBy('summary_time')->unique('summary_time')->pluck('summary_time');
|
||
|
foreach($idols as $idol) {
|
||
|
foreach($timepoints as $timepoint) {
|
||
|
$chartPoint[$idol][$timepoint->toDateTimeString()] = 0;
|
||
|
}
|
||
|
foreach($roleRank->where('character',$idol) as $point) {
|
||
|
$chartPoint[$idol][$point->summary_time->toDateTimeString()] = $point->score;
|
||
|
}
|
||
|
}
|
||
|
Cache::forever('chart_point_'.$roleName,$chartPoint);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|