ldc-gen-game-web/app/Http/Controllers/Bo/Re7Controller.php
2023-12-07 15:15:08 +01:00

129 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Models\Prize;
use App\Models\Settings;
use App\Models\Reduction;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class Re7Controller extends Controller
{
/**
* Re7Controller
*
* @return void
*/
public function __construct()
{
$this->middleware(function (Request $request, \Closure $next) {
$this->authorize('fo.showRe7');
return $next($request);
});
}
/**
* Show Teaser page
*
* @return \Illuminate\Contracts\View\View
*/
public function teaser(): View
{
return view('theme::pages.teaser');
}
// * -- Participation
/**
* Show prize page
*
* @param \App\Models\Prize $prize
* @return \Illuminate\Contracts\View\View
*/
public function prize(Prize $prize): View
{
return view('theme::pages.participation.won', [
'prizeModel' => $prize
]);
}
/**
* Show discount page
*
* @return \Illuminate\Contracts\View\View
*/
public function br(): View
{
/** @var \App\Models\Settings */
$settings = app(Settings::class);
/** @var \App\Lib\Discount */
$discount = Arr::get(
$settings->web_coupons,
$this->getRandomWeightedElementIndex($settings->web_coupons)
);
$crmCoupon = new Reduction();
return view('theme::pages.participation.won', compact('discount', 'crmCoupon'));
}
/**
* Show loose page
*
* @return \Illuminate\Contracts\View\View
*/
public function loose(): View
{
return view('theme::pages.participation.loose');
}
// * -- END Participation
/**
* prizeMail
*
* @return \Illuminate\View\View
*/
public function prizeMail()
{
return view('theme::prize-mail');
}
/**
* reductionMail
*
* @return \Illuminate\View\View
*/
public function reductionMail()
{
return view('theme::reduction-mail');
}
/**
* Utility function for getting random values with weighting.
* Pass in an associative array, such as array('A'=>5, 'B'=>45, 'C'=>50)
* An array like this means that "A" has a 5% chance of being selected, "B" 45%, and "C" 50%.
* The return value is the array key, A, B, or C in this case. Note that the values assigned
* do not have to be percentages. The values are simply relative to each other. If one value
* weight was 2, and the other weight of 1, the value with the weight of 2 has about a 66%
* chance of being selected. Also note that weights should be integers.
*
* @param array<\App\Lib\Discount> $weightedValues
* @return integer The winning key
* @url https://stackoverflow.com/a/11872928/4355295
*/
private function getRandomWeightedElementIndex(array $weightedValues): int
{
mt_srand(time());
$rand = mt_rand(1, max((int)collect($weightedValues)->sum('percentage'), 1));
foreach ($weightedValues as $key => $value) {
$rand -= $value->percentage;
if ($rand <= 0) {
return $key;
}
}
}
}