TCVis/app/Http/Controllers/HomeController.php
2019-01-02 19:18:53 +00:00

179 lines
7.1 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 getIdolColor($idol) {
$idolColors = $this->getIdolColors();
return $idolColors[explode("\n",$idol)[0]];
}
public function getIdolColors(){
if(!Cache::get('idol_colors')) {
$colors = [];
$idolDB = collect($this->getIdolDB()->idols);
foreach($idolDB as $idol) {
$colors[$idol->name->kanji->value] = $idol->color;
}
Cache::forever('idol_colors',$colors);
}
// return $colors;
return Cache::get('idol_colors');
}
public function statistics(Request $request) {
$dataPoints = Rank::count();
return view('statistics',['data_points' => $dataPoints]);
}
public function nukes(Request $request) {
$nukes = Cache::get('nukes');
return view('nukes',["nukes" => $nukes]);
}
public function newChart($timelabels,$roleName,$request,$amount,$dramaNames,$colors,$enlarge = false) {
$chart = new RankChart;
$height = 400;
if($enlarge) $height = 1000;
$options = [
'axisOptions' => [
'xIsSeries' => true,
'xAxisMode' => 'tick'
],
'lineOptions' => [
'regionFill' => true,
],
'title' => $roleName.' ('.($roleName == 'global' ? 'Global' : $dramaNames[$roleName]).')',
'colors' => $colors,
'height' => $height,
];
$chart->options($options);
$chart->isNavigable(true);
$chart->labels($timelabels->toArray());
$chart->hideDots(true);
$api = url('/api/chart/'.$roleName."?date_since=".$request->date_since.'&since='.$request->since.'&take='.$request->take);
$chart->load($api);
return $chart;
}
public function home(Request $request) {
$idolColors = $this->getIdolColors();
$minutes = $request->since ?? 60*24;
$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) {
return $item->gte(Carbon::now()->addHours(9)->subMinutes($minutes+5));
});
$take = $timepoints->count();
$dramaNames = Election::getDramaNames();
$ranks = collect($dramaNames)->keys();
$charts = [];
$timelabels = Cache::get('timelabels');
$timelabels = $timelabels->take(-$take)->values();
if($request->take) $timelabels = $timelabels->take($request->take);
foreach($ranks as $roleName) {
$chartPoint = null;
$chartPoint = collect(Cache::get('data_'.$roleName))->keys();
$colors = [];
foreach($chartPoint as $point) {
$colors[] = $this->getIdolColor($point);
}
$chart =$this->newChart($timelabels,$roleName,$request,$request->take,$dramaNames,$colors,$request->enlarge);
$chartData["drama"] = $dramaNames[$roleName];
$chartData["name"] = $roleName;
$chartData["chart"] = $chart;
$charts[] = $chartData;
}
return view('home', [ 'chart_groups' => collect($charts)->groupBy("drama"), 'since' => $request->since, 'enlarge' => $request->enlarge , 'date_since' => $request->date_since, 'take' => $request->take]);
}
public function homeNew(Request $request) {
$idolColors = $this->getIdolColors();
$minutes = $request->since ?? 60*24;
$snap_time = Cache::get('snap_time');
$end_date = $snap_time->toDateTimeString();
$start_date = ( $request->date_since != "" ? $request->date_since : $snap_time->subMinutes($request->since ?? 24*60)->toDateTimeString());
$timerange = \App\MatsuriHime\Election::generateChartPointsDate($start_date,$end_date);
$timepoints = Cache::get('timepoints_'.$timerange);
$take = $timepoints->count();
$dramaNames = Election::getDramaNames();
$ranks = collect($dramaNames)->keys();
$charts = [];
$timelabels = Cache::get('timelabels_'.$timerange);
$timelabels = $timelabels->take(-$take)->values();
if($request->take) $timelabels = $timelabels->take($request->take);
foreach($ranks as $roleName) {
$chartPoint = null;
$chartPoint = collect(Cache::get('data_'.$roleName.'_'.$timerange))->keys();
$colors = [];
foreach($chartPoint as $point) {
$colors[] = $this->getIdolColor($point);
}
$chart =$this->newChart($timelabels,$roleName,$request,$request->take,$dramaNames,$colors,$request->enlarge);
$chartData["drama"] = $dramaNames[$roleName];
$chartData["name"] = $roleName;
$chartData["chart"] = $chart;
$charts[] = $chartData;
}
return view('home', [ 'chart_groups' => collect($charts)->groupBy("drama"), 'since' => $request->since, 'enlarge' => $request->enlarge , 'date_since' => $request->date_since, 'take' => $request->take]);
}
public function viewChart($chart_name, Request $request) {
$minutes = $request->since ?? 60*24;
$minutes = $request->since ?? 60*24;
$snap_time = Cache::get('snap_time');
$end_date = $snap_time->toDateTimeString(); $start_date = ( $request->date_since != "" ? $request->date_since : $snap_time->subMinutes($request->since ?? 24*60)->toDateTimeString());
$timerange = \App\MatsuriHime\Election::generateChartPointsDate($start_date,$end_date);
$timepoints = Cache::get('timepoints_'.$timerange);
$take = $timepoints->count();
$idolColors = $this->getIdolColors();
$dramaNames = Election::getDramaNames();
$charts = [];
$timelabels = Cache::get('timelabels_'.$timerange);
$timelabels = $timelabels->take(-$take)->values();
if($request->take) $timelabels = $timelabels->take($request->take);
$roleName = $chart_name;
$roleRank = "Global";
$chartPoint = collect(Cache::get('data_'.$roleName.'_'.$timerange))->keys();
$colors = [];
foreach($chartPoint as $point) {
$colors[] = $this->getIdolColor($point);
}
$chart =$this->newChart($timelabels,$roleName,$request,$request->take,$dramaNames,$colors,$request->enlarge);
$chartData["drama"] = ($roleName == 'global' ? 'Global' : $dramaNames[$roleName]);
$chartData["name"] = $roleName;
$chartData["chart"] = $chart;
$charts[] = $chartData;
return view('home', [ 'chart_groups' => collect($charts)->groupBy("drama"), 'since' => $request->since, 'enlarge' => $request->enlarge , 'date_since' => $request->date_since, 'take' => $request->take, 'chart_name' => $chart_name]);
}
public function viewChartApi($chart_name, Request $request) {
$snap_time = Cache::get('snap_time');
$end_date = $snap_time->toDateTimeString();
$start_date = ( $request->date_since != "" ? $request->date_since : $snap_time->subMinutes($request->since ?? 24*60)->toDateTimeString());
$timerange = \App\MatsuriHime\Election::generateChartPointsDate($start_date,$end_date);
$take = $request->since;
$roleName = $chart_name;
$chartPoint = Cache::get('data_'.$roleName.'_'.$timerange);
$chart = new RankChart;
foreach($chartPoint as $idol => $dataPoints) {
$values = $dataPoints;
if($request->take) $values = $dataPoints->take($request->take)->values();
$chart->dataSet($idol,'line',$values->toArray());
}
return $chart->api();
}
//
}