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

130 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2023-12-01 11:45:12 +01:00
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
2023-12-05 18:41:58 +01:00
use App\Models\Prize;
use App\Models\Settings;
use App\Models\Reduction;
2023-12-01 11:45:12 +01:00
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
2023-12-07 15:15:08 +01:00
use Illuminate\Support\Arr;
2023-08-24 12:59:51 +00:00
class Re7Controller extends Controller
{
2022-08-11 10:26:29 +02:00
/**
2023-12-01 11:45:12 +01:00
* Re7Controller
2023-08-24 12:59:51 +00:00
*
2023-12-01 11:45:12 +01:00
* @return void
2022-08-11 10:26:29 +02:00
*/
2023-12-01 11:45:12 +01:00
public function __construct()
{
2023-12-01 11:45:12 +01:00
$this->middleware(function (Request $request, \Closure $next) {
$this->authorize('fo.showRe7');
return $next($request);
});
}
2023-12-05 12:33:25 +01:00
/**
* Show Teaser page
*
* @return \Illuminate\Contracts\View\View
*/
public function teaser(): View
{
return view('theme::pages.teaser');
}
2023-12-05 18:41:58 +01:00
// * -- Participation
2022-08-11 10:26:29 +02:00
/**
2023-12-05 18:41:58 +01:00
* Show prize page
2023-08-24 12:59:51 +00:00
*
2023-12-05 18:41:58 +01:00
* @param \App\Models\Prize $prize
* @return \Illuminate\Contracts\View\View
2022-08-11 10:26:29 +02:00
*/
2023-12-05 18:41:58 +01:00
public function prize(Prize $prize): View
{
2023-12-05 18:41:58 +01:00
return view('theme::pages.participation.won', [
'prizeModel' => $prize
]);
}
2022-08-11 10:26:29 +02:00
/**
2023-12-05 18:41:58 +01:00
* Show discount page
2023-08-24 12:59:51 +00:00
*
2023-12-05 18:41:58 +01:00
* @return \Illuminate\Contracts\View\View
2022-08-11 10:26:29 +02:00
*/
2023-12-05 18:41:58 +01:00
public function br(): View
{
2023-12-05 18:41:58 +01:00
/** @var \App\Models\Settings */
2023-12-07 15:15:08 +01:00
$settings = app(Settings::class);
/** @var \App\Lib\Discount */
$discount = Arr::get(
$settings->web_coupons,
$this->getRandomWeightedElementIndex($settings->web_coupons)
);
2023-12-05 18:41:58 +01:00
$crmCoupon = new Reduction();
2023-12-07 15:15:08 +01:00
return view('theme::pages.participation.won', compact('discount', 'crmCoupon'));
}
2022-08-11 10:26:29 +02:00
/**
2023-12-05 18:41:58 +01:00
* Show loose page
2023-08-24 12:59:51 +00:00
*
2023-12-05 18:41:58 +01:00
* @return \Illuminate\Contracts\View\View
2022-08-11 10:26:29 +02:00
*/
2023-12-05 18:41:58 +01:00
public function loose(): View
{
2023-12-05 18:41:58 +01:00
return view('theme::pages.participation.loose');
}
2023-12-05 18:41:58 +01:00
// * -- END Participation
2022-08-11 10:26:29 +02:00
/**
2023-12-05 18:41:58 +01:00
* prizeMail
2023-08-24 12:59:51 +00:00
*
2022-08-11 10:26:29 +02:00
* @return \Illuminate\View\View
*/
2023-12-05 18:41:58 +01:00
public function prizeMail()
{
2023-12-05 18:41:58 +01:00
return view('theme::prize-mail');
}
2022-08-11 10:26:29 +02:00
/**
2023-12-05 18:41:58 +01:00
* reductionMail
2023-08-24 12:59:51 +00:00
*
2022-08-11 10:26:29 +02:00
* @return \Illuminate\View\View
*/
2023-12-05 18:41:58 +01:00
public function reductionMail()
{
2023-12-05 18:41:58 +01:00
return view('theme::reduction-mail');
}
2023-12-05 18:41:58 +01:00
2022-08-11 10:26:29 +02:00
/**
2023-12-07 15:15:08 +01:00
* 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.
2023-08-24 12:59:51 +00:00
*
2023-12-07 15:15:08 +01:00
* @param array<\App\Lib\Discount> $weightedValues
* @return integer The winning key
* @url https://stackoverflow.com/a/11872928/4355295
2022-08-11 10:26:29 +02:00
*/
2023-12-07 15:15:08 +01:00
private function getRandomWeightedElementIndex(array $weightedValues): int
2021-08-18 17:08:02 +02:00
{
2023-12-07 15:15:08 +01:00
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;
2021-08-18 17:08:02 +02:00
}
}
}
}