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

201 lines
5.4 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-07 16:59:17 +01:00
use App\Mail\Won\DiscountWonMail;
use App\Mail\Won\PrizeWonMail;
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 16:59:17 +01:00
use Illuminate\Http\Response;
use Illuminate\Mail\Mailables\Address;
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-07 15:27:48 +01:00
/**
* Show Teaser page
*
* @return \Illuminate\Contracts\View\View
*/
public function endGame(): View
{
return view('theme::pages.end-game');
}
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-07 16:59:17 +01:00
public function discount(): 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-07 16:59:17 +01:00
* View won discount mail
2023-08-24 12:59:51 +00:00
*
2023-12-07 16:59:17 +01:00
* @param \App\Models\Prize $prize
* @return \Illuminate\Contracts\View\View
*/
public function mailPrize(Prize $prize): View
{
return view('back::pages.re7-preview-mail', [
'render_url' => route('re7.mail-print', [
'type' => 'prize',
'prize' => $prize
])
]);
}
/**
* View won discount mail
*
* @return \Illuminate\Contracts\View\View
2022-08-11 10:26:29 +02:00
*/
2023-12-07 16:59:17 +01:00
public function mailDiscount(): View
{
2023-12-07 16:59:17 +01:00
return view('back::pages.re7-preview-mail', [
'render_url' => route('re7.mail-print', [
'type' => 'discount'
])
]);
}
2022-08-11 10:26:29 +02:00
/**
2023-12-07 16:59:17 +01:00
* Print a specific mail
2023-08-24 12:59:51 +00:00
*
2023-12-07 16:59:17 +01:00
* @param string $type
* @param \App\Models\Prize|null $prize
* @return \Illuminate\Http\Response
2022-08-11 10:26:29 +02:00
*/
2023-12-07 16:59:17 +01:00
public function mailPrint(string $type, Prize|null $prize = null): Response
{
2023-12-07 16:59:17 +01:00
/** @var \App\Models\User */
$user = auth('backend')->user();
/** @var \App\Models\Settings */
$settings = app(Settings::class);
switch ($type) {
case 'discount':
/** @var \App\Lib\Discount */
$discount = Arr::get(
$settings->web_coupons,
$this->getRandomWeightedElementIndex($settings->web_coupons)
);
return response((new DiscountWonMail(
new Address(
$user->email,
$user->first_name
),
$settings->name,
$discount,
config('app.url')
))->render());
case 'prize':
return response((new PrizeWonMail(
new Address(
$user->email,
$user->first_name
),
$settings->name,
$prize,
config('app.url')
))->render());
default:
return response('', 404);
} //end switch
}
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
}
}
}
}