TCVis/app/Http/Controllers/HomeController.php
2018-12-21 12:45:08 +00:00

84 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Charts\RankChart;
use App\MatsuriHime\Election;
use App\Rank;
use Carbon\Carbon;
use Cache;
use Storage;
use Agent;
class HomeController extends Controller
{
public function getIdolDB() {
return json_decode(Storage::get('idols.json'));
}
public function getIdolColors(){
$colors = [];
$idolDB = collect($this->getIdolDB()->idols);
foreach($idolDB as $idol) {
$colors[$idol->name->kanji->value] = $idol->color;
}
// dd($colors);
return $colors;
}
public function home(Request $request) {
$minutes = $request->since ?? 60*24*90;
$timepoints = Rank::orderBy('summary_time')->get()->unique('summary_time')->pluck('summary_time');
if($minutes != "full") $timepoints = $timepoints->filter(function($item) use ($minutes) {
return $item->gte(Carbon::now()->addHours(9)->subMinutes($minutes+5));
});
$take = $timepoints->count();
$idolColors = $this->getIdolColors();
$dramaNames = Election::getDramaNames();
$ranks = Rank::all()->groupBy('role');
$charts = [];
$timelabels = [];
foreach($timepoints as $timepoint) {
$timelabels[] = $timepoint->toDateTimeString();
}
$timelabels = collect($timelabels);
$timelabels = $timelabels->splice(count($timelabels) - $take);
foreach($ranks as $roleName => $roleRank) {
$chartPoint = Cache::get('chart_point_'.$roleName);
// dd($chartPoint);
if(!isset($chartPoint)) {
\App\MatsuriHime\Election::createSnapshot();
$chartPoint = Cache::get('chart_point_'.$roleName);
}
$chart = new RankChart;
$chart->options([
'axisOptions' => [
'xIsSeries' => true,
'xAxisMode' => 'tick'
],
'lineOptions' => [
'regionFill' => true
],
]);
$chart->isNavigable(true);
$chart->labels($timelabels->toArray());
$chart->hideDots(true);
foreach($chartPoint as $idol => $dataPoints) {
$values = [];
foreach($timelabels as $timelabel) {
$values[] = $dataPoints[$timelabel] ?? 0;
}
$values = collect($values);
$idol_color = $idolColors[$idol];
$chart->dataSet($idol,'line',$values->toArray())->color($idol_color);
}
$chartData["drama"] = $dramaNames[$roleName];
$chartData["name"] = $roleName;
$chartData["chart"] = $chart;
$charts[] = $chartData;
}
return view('home', [ 'chart_groups' => collect($charts)->groupBy("drama"), 'since' => $request->since ]);
}
//
}