ldc-gen-game-web/app/Http/Controllers/bo/InstantWinController.php
Romain Bonhomme de663e2fad Init
2020-10-21 15:26:21 +02:00

97 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers\bo;
use App\Http\Controllers\Controller;
use App\Models\InstantWin;
use App\Models\Prizes;
use App\Models\Settings;
class InstantWinController extends Controller
{
public function index() {
$nbInstantWin = InstantWin::all()->count();
$canGenerate = 0;
if (intval($nbInstantWin) > 0) {
$canGenerate = 0;
} else {
$nbLots = Prizes::all()->count();
if (intval($nbLots) > 0) {
$canGenerate = 1;
} else {
$canGenerate = 0;
}
}
$instantWin = InstantWin::orderBy('appear_date')->get();
return view('bo.instant-win.index', [
'can_generate' => $canGenerate,
'instant_win' => $instantWin
]);
}
public function instantWinGenerate() {
$start_date = Settings::latest()->value("start_date");
$end_date = Settings::latest()->value("end_date");
$lots = Prizes::all();
$begin = new \DateTime( $start_date );
$end = new \DateTime( $end_date );
$end = $end->modify( '+1 day' );
$interval = new \DateInterval('P1D');
$daterange = new \DatePeriod($begin, $interval ,$end);
$dateGlobal = [];
$lotsGlobal = [];
foreach ($lots as $k => $lot)
for ($i = 0; $i < $lot->quantity; $i++)
$lotsGlobal[] = ['id' => $lot->id, 'name' => $lot->label_one, 'hour' => str_pad(mt_rand(6, 22), 2, "0", STR_PAD_LEFT) . ":" . str_pad(mt_rand(0, 59), 2, "0", STR_PAD_LEFT) . ":00"];
shuffle($lotsGlobal);
foreach($daterange as $date)
array_push($dateGlobal, ['date' => $date->format("Y-m-d"), 'lots' => []]);
$res = $this->generate($lotsGlobal,$dateGlobal, 0);
foreach ($res as $date) {
foreach ($date['lots'] as $lot) {
$iw = new InstantWin();
$iw->prize_id = $lot['id'];
$iw->appear_date = date('Y-m-d H:i:s', strtotime($date['date'] . " " . $lot['hour']));
$iw->save();
}
}
$instantWins = InstantWin::all();
return json_encode($instantWins);
}
private function generate($lotsGlobal, $dateGlobal, $lotsDone){
$rKey = array_rand($dateGlobal, 1);
if ($lotsDone < count($dateGlobal)) {
if (count($dateGlobal[$rKey]['lots']) > 0) {
return $this->generate($lotsGlobal, $dateGlobal, $lotsDone);
}
}
array_push($dateGlobal[$rKey]['lots'], $lotsGlobal[$lotsDone++]);
if ($lotsDone < count($lotsGlobal)) {
return $this->generate($lotsGlobal, $dateGlobal, $lotsDone);
}
foreach ($dateGlobal as $k => $date)
usort($dateGlobal[$k]['lots'], [$this, 'sortFunction']);
return $dateGlobal;
}
private function sortFunction( $a, $b ) {
return strtotime($a["hour"]) - strtotime($b["hour"]);
}
}