TCVis/app/Http/Controllers/HomeController.php

179 lines
7.1 KiB
PHP
Raw Permalink 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'));
}
2019-01-02 19:18:53 +00:00
public function getIdolColor($idol) {
$idolColors = $this->getIdolColors();
return $idolColors[explode("\n",$idol)[0]];
}
2018-12-21 12:45:08 +00:00
public function getIdolColors(){
2019-01-02 19:18:53 +00:00
if(!Cache::get('idol_colors')) {
2018-12-21 12:45:08 +00:00
$colors = [];
$idolDB = collect($this->getIdolDB()->idols);
foreach($idolDB as $idol) {
$colors[$idol->name->kanji->value] = $idol->color;
}
2019-01-02 19:18:53 +00:00
Cache::forever('idol_colors',$colors);
}
// return $colors;
return Cache::get('idol_colors');
2018-12-21 12:45:08 +00:00
}
2018-12-22 03:47:09 +00:00
public function statistics(Request $request) {
$dataPoints = Rank::count();
return view('statistics',['data_points' => $dataPoints]);
2019-01-02 19:18:53 +00:00
}
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;
2018-12-22 03:47:09 +00:00
}
2018-12-21 12:45:08 +00:00
public function home(Request $request) {
2019-01-02 19:18:53 +00:00
$idolColors = $this->getIdolColors();
$minutes = $request->since ?? 60*24;
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();
$dramaNames = Election::getDramaNames();
2019-01-02 19:18:53 +00:00
$ranks = collect($dramaNames)->keys();
2018-12-21 12:45:08 +00:00
$charts = [];
2019-01-02 19:18:53 +00:00
$timelabels = Cache::get('timelabels');
$timelabels = $timelabels->take(-$take)->values();
2018-12-22 03:47:09 +00:00
if($request->take) $timelabels = $timelabels->take($request->take);
2019-01-02 19:18:53 +00:00
foreach($ranks as $roleName) {
$chartPoint = null;
$chartPoint = collect(Cache::get('data_'.$roleName))->keys();
$colors = [];
foreach($chartPoint as $point) {
$colors[] = $this->getIdolColor($point);
2018-12-21 12:45:08 +00:00
}
2019-01-02 19:18:53 +00:00
$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);
2018-12-21 12:45:08 +00:00
}
2019-01-02 19:18:53 +00:00
$chart =$this->newChart($timelabels,$roleName,$request,$request->take,$dramaNames,$colors,$request->enlarge);
2018-12-21 12:45:08 +00:00
$chartData["drama"] = $dramaNames[$roleName];
$chartData["name"] = $roleName;
$chartData["chart"] = $chart;
$charts[] = $chartData;
}
2019-01-02 19:18:53 +00:00
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();
2018-12-21 12:45:08 +00:00
}
//
}