2020-10-29 15:40:23 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\CodesEAN;
|
|
|
|
|
use App\Models\Prizes;
|
|
|
|
|
use App\Models\Settings;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class EnterCodeController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index() {
|
2020-10-30 16:12:14 +01:00
|
|
|
// TODO : Compté les participations du user en cours pour voir s'il peut jouer, sinon rediriger vers participation-exhausted
|
|
|
|
|
$settings = Settings::find(1)->select('use_ean_codes', 'nb_participation_player')->first();
|
|
|
|
|
// return view('front.basic.participation-exhausted', compact('settings'));
|
2020-10-29 15:40:23 +01:00
|
|
|
return view('front.basic.enter-code', compact('settings'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function participate(Request $request) {
|
|
|
|
|
$code = $request->code;
|
2020-11-02 16:18:06 +01:00
|
|
|
$codeEan = null;
|
2020-10-29 15:40:23 +01:00
|
|
|
$settings = Settings::select('use_ean_codes')->first();
|
|
|
|
|
if ($settings->use_ean_codes) {
|
|
|
|
|
$codeEan = $request->code_ean;
|
|
|
|
|
$eanExist = CodesEAN::where('code', $codeEan)->count();
|
2020-11-02 16:18:06 +01:00
|
|
|
if ($eanExist === 0)
|
2020-10-29 15:40:23 +01:00
|
|
|
return redirect()->back()->withErrors('Le code EAN entré est incorrect, veuillez réessayer.');
|
|
|
|
|
}
|
2020-11-02 16:18:06 +01:00
|
|
|
$gagne = true;
|
2020-10-29 15:40:23 +01:00
|
|
|
// TODO : Traitement du code, instant gagnant etc
|
|
|
|
|
if ($gagne) {
|
2020-10-30 16:12:14 +01:00
|
|
|
$prizeId = $request->prize;
|
|
|
|
|
$prize = Prizes::findOrFail($prizeId);
|
|
|
|
|
// TODO : Check que le participant à bien gagné avant de renvoyé la vue, sinon 404
|
|
|
|
|
return view('front.basic.' . $prize->gain_view->file_name, compact('prize'));
|
2020-10-29 15:40:23 +01:00
|
|
|
} else {
|
|
|
|
|
// Page perdu si jeu non 100% gagnant, reduction sinon
|
|
|
|
|
$perdant = false;
|
|
|
|
|
$nbDefaultPrize = Prizes::where('is_default', 1)->count();
|
2020-11-02 16:18:06 +01:00
|
|
|
if ($nbDefaultPrize === 0)
|
2020-10-29 15:40:23 +01:00
|
|
|
$perdant = true;
|
|
|
|
|
|
|
|
|
|
if ($perdant) {
|
2020-10-30 16:12:14 +01:00
|
|
|
return view('front.basic.loose');
|
2020-10-29 15:40:23 +01:00
|
|
|
} else {
|
2020-10-30 16:12:14 +01:00
|
|
|
$prizes = Prizes::where('is_default', 1)->get();
|
|
|
|
|
// TODO : Gérer quel prix par défaut on retourne avec un pourcentage
|
|
|
|
|
return view('front.basic.reduction', compact('prizes'));
|
2020-10-29 15:40:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|