ldc-gen-game-web/app/Http/Controllers/Bo/InstantWinController.php

127 lines
3.7 KiB
PHP
Raw Normal View History

2020-10-21 15:26:21 +02:00
<?php
namespace App\Http\Controllers\Bo;
2020-10-21 15:26:21 +02:00
use App\Http\Controllers\Controller;
use App\Models\InstantWin;
use App\Models\Prize;
2020-10-21 15:26:21 +02:00
use App\Models\Settings;
class InstantWinController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
2021-08-18 17:08:02 +02:00
{
2020-11-13 15:31:40 +01:00
$nbInstantWin = InstantWin::count();
2021-08-18 17:08:02 +02:00
$canGenerate = 0;
2020-10-21 15:26:21 +02:00
if (intval($nbInstantWin) > 0) {
$canGenerate = 0;
} else {
$nbLots = Prize::count();
2020-10-21 15:26:21 +02:00
if (intval($nbLots) > 0) {
$canGenerate = 1;
} else {
$canGenerate = 0;
}
}
$instantWin = InstantWin::orderBy('appear_date')->get();
2023-11-20 17:22:24 +01:00
return view('back::pages.instant-win.index', [
2020-10-21 15:26:21 +02:00
'can_generate' => $canGenerate,
'instant_win' => $instantWin
]);
}
/**
* Returns generated instant win.
*
* @return string|false
*/
2021-08-18 17:08:02 +02:00
public function instantWinGenerate()
{
2020-10-21 15:26:21 +02:00
$start_date = Settings::latest()->value("start_date");
2021-08-18 17:08:02 +02:00
$end_date = Settings::latest()->value("end_date");
$lots = Prize::all();
2020-10-21 15:26:21 +02:00
2021-08-18 17:08:02 +02:00
$begin = new \DateTime($start_date);
$end = new \DateTime($end_date);
$interval = new \DateInterval('P1D');
$daterange = new \DatePeriod($begin, $interval, $end);
2020-10-21 15:26:21 +02:00
$dateGlobal = [];
$lotsGlobal = [];
2021-08-18 17:08:02 +02:00
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"
];
2021-08-18 17:08:02 +02:00
}
}
2020-10-21 15:26:21 +02:00
shuffle($lotsGlobal);
2021-08-18 17:08:02 +02:00
foreach ($daterange as $date) {
2020-10-21 15:26:21 +02:00
array_push($dateGlobal, ['date' => $date->format("Y-m-d"), 'lots' => []]);
2021-08-18 17:08:02 +02:00
}
2020-10-21 15:26:21 +02:00
2021-08-18 17:08:02 +02:00
$res = $this->generate($lotsGlobal, $dateGlobal, 0);
2020-10-21 15:26:21 +02:00
foreach ($res as $date) {
foreach ($date['lots'] as $lot) {
2021-08-18 17:08:02 +02:00
$iw = new InstantWin();
$iw->prize_id = $lot['id'];
2020-10-21 15:26:21 +02:00
$iw->appear_date = date('Y-m-d H:i:s', strtotime($date['date'] . " " . $lot['hour']));
$iw->save();
}
}
$instantWins = InstantWin::all();
return json_encode($instantWins);
}
/**
* Generate instant win.
*
* @param array $lotsGlobal
* @param array $dateGlobal
* @param integer $lotsDone
* @return array
*/
private function generate(array $lotsGlobal, array $dateGlobal, int $lotsDone): array
2021-08-18 17:08:02 +02:00
{
2020-10-21 15:26:21 +02:00
$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);
}
2021-08-18 17:08:02 +02:00
foreach ($dateGlobal as $k => $date) {
2020-10-21 15:26:21 +02:00
usort($dateGlobal[$k]['lots'], [$this, 'sortFunction']);
2021-08-18 17:08:02 +02:00
}
2020-10-21 15:26:21 +02:00
return $dateGlobal;
}
/**
* Callback to sort 2 items by their hour.
*
* @param array $a
* @param array $b
* @return integer|float
*/
private function sortFunction(array $a, array $b): int|float
2021-08-18 17:08:02 +02:00
{
2020-10-21 15:26:21 +02:00
return strtotime($a["hour"]) - strtotime($b["hour"]);
}
}