2018-12-21 12:45:08 +00:00
|
|
|
<?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();
|
|
|
|
if($existingSnapshot == 0) {
|
2018-12-22 03:47:09 +00:00
|
|
|
$idols = [];
|
2018-12-21 12:45:08 +00:00
|
|
|
foreach($ladder->data[0] as $data) {
|
|
|
|
$rank = new Rank();
|
|
|
|
$rank->role = $ladder->name;
|
|
|
|
$rank->character = $data->idol_name;
|
2018-12-22 03:47:09 +00:00
|
|
|
$idols[] = $data->idol_name;
|
2018-12-21 12:45:08 +00:00
|
|
|
$rank->score = $data->score;
|
|
|
|
$rank->summary_time = Carbon::parse($ladder->summaryTime);
|
|
|
|
$rank->save();
|
|
|
|
}
|
2018-12-22 03:47:09 +00:00
|
|
|
$remnant_idols = Rank::where('role',$ladder->name)->whereNotIn('character',$idols)->orderBy('summary_time','desc')->get()->groupBy('character');
|
|
|
|
foreach($remnant_idols as $remnant_char => $remnant) {
|
|
|
|
$rank = new Rank();
|
|
|
|
$rank->role = $ladder->name;
|
|
|
|
$rank->character = $remnant_char;
|
|
|
|
$rank->score = $remnant->first()->score;
|
|
|
|
$rank->summary_time = Carbon::parse($ladder->summaryTime);
|
|
|
|
$rank->save();
|
|
|
|
\Log::error($remnant_char.' '.$remnant->first()->score);
|
|
|
|
}
|
2018-12-21 12:45:08 +00:00
|
|
|
} else {
|
|
|
|
\Log::info("snapshot has been taken for " . (string)$ladder->summaryTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::commit();
|
2018-12-22 03:47:09 +00:00
|
|
|
Election::generateChartPointsNew();
|
|
|
|
}
|
|
|
|
public static function generateChartPointsNew() {
|
|
|
|
$ranks = Rank::orderBy('summary_time')->get()->groupBy('role');
|
|
|
|
$timepoints = Rank::distinct('summary_time')->orderBy('summary_time')->pluck('summary_time');
|
|
|
|
Cache::forever('timepoints',$timepoints);
|
|
|
|
foreach($ranks as $roleName => $roleRank) {
|
|
|
|
$dataPoints = [];
|
|
|
|
$ladder = $roleRank->sortBy('summary_time')->groupBy('character')->sortByDesc(function($rank,$key) {
|
|
|
|
return $rank->last()->score;
|
|
|
|
});
|
|
|
|
foreach($ladder as $character => $rank) {
|
|
|
|
$standing = $rank;
|
|
|
|
|
|
|
|
$standing = $standing->pad(-$timepoints->count(),(int)0);
|
|
|
|
$dataPoints[$character] = $standing->pluck('score');
|
|
|
|
}
|
|
|
|
Cache::forever('data_'.$roleName,$dataPoints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static function generateChartPoints() {
|
2018-12-21 12:45:08 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|