TCVis/app/Http/Controllers/HomeController.php

96 lines
2.9 KiB
PHP
Raw Normal View History

2018-12-21 12:45:08 +00:00
<?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;
}
2018-12-22 03:47:09 +00:00
public function statistics(Request $request) {
$dataPoints = Rank::count();
return view('statistics',['data_points' => $dataPoints]);
}
2018-12-21 12:45:08 +00:00
public function home(Request $request) {
$minutes = $request->since ?? 60*24*90;
2018-12-22 03:47:09 +00:00
$timepoints = Cache::get('timepoints');
$requested_date = $request->date_since;
if($requested_date) {
$requested_date = Carbon::parse($requested_date);
$timepoints = $timepoints->filter(function($item) use ($requested_date) {
return $item->gte($requested_date);
});
}
else if($minutes != "full") $timepoints = $timepoints->filter(function($item) use ($minutes) {
2018-12-21 12:45:08 +00:00
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);
2018-12-22 03:47:09 +00:00
if($request->take) $timelabels = $timelabels->take($request->take);
2018-12-21 12:45:08 +00:00
foreach($ranks as $roleName => $roleRank) {
2018-12-22 03:47:09 +00:00
$chartPoint = Cache::get('data_'.$roleName);
2018-12-21 12:45:08 +00:00
// dd($chartPoint);
if(!isset($chartPoint)) {
\App\MatsuriHime\Election::createSnapshot();
2018-12-22 03:47:09 +00:00
$chartPoint = Cache::get('data_'.$roleName);
2018-12-21 12:45:08 +00:00
}
$chart = new RankChart;
$chart->options([
'axisOptions' => [
'xIsSeries' => true,
'xAxisMode' => 'tick'
],
'lineOptions' => [
'regionFill' => true
],
2018-12-22 03:47:09 +00:00
'title' => $roleName.' ('.$dramaNames[$roleName].')'
2018-12-21 12:45:08 +00:00
]);
$chart->isNavigable(true);
$chart->labels($timelabels->toArray());
$chart->hideDots(true);
foreach($chartPoint as $idol => $dataPoints) {
2018-12-22 03:47:09 +00:00
$values = collect($dataPoints);
$values = $values->splice(count($values) - $take);
if($request->take) $values = $values->take($request->take);
2018-12-21 12:45:08 +00:00
$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 ]);
}
//
}