341 lines
12 KiB
PHP
Executable file
341 lines
12 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Season;
|
|
use App\Round;
|
|
use App\Game;
|
|
use App\Tipp;
|
|
use App\User;
|
|
|
|
use Auth;
|
|
use Log;
|
|
use DB;
|
|
|
|
use App\MyClasses\TimeLogger;
|
|
|
|
class TippsController extends Controller
|
|
{
|
|
//Duplicate from Tipps model
|
|
//TODO: Refactor usage to use only the Tipps version
|
|
public static function createTipps($user_id) {
|
|
|
|
$a=new TimeLogger("TippsController::createTipps()");
|
|
Log::info($a->getStartMessage());
|
|
|
|
$activeSeason = Season::getCurrent();
|
|
|
|
$rounds = Round::all()->where('season_id', '=', $activeSeason->id);
|
|
foreach($rounds as $round) {
|
|
$games=Game::all()->where('round_id','=',$round->id);
|
|
foreach($games as $game) {
|
|
// Tipp erzeugen wenn noch nicht vorhanden
|
|
$tipp = DB::table('tipps')->where([
|
|
['game_id','=',$game->id],
|
|
['user_id','=',$user_id]]
|
|
)->first();
|
|
if (! $tipp) {
|
|
//Log::info('Creating missing tip: game_id=' . $game->id . ", user_id=" . $user->id);
|
|
$tipp=Tipp::create([
|
|
'user_id' => $user_id,
|
|
'game_id'=> $game->id,
|
|
'score1'=> -1,
|
|
'score2'=> -1,
|
|
'joker'=> false
|
|
]);
|
|
$tipp->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
Log::info($a->getEndMessage());
|
|
|
|
}
|
|
|
|
|
|
//Duplicate from Tipps model
|
|
//TODO: Refactor usage to use only the Tipps version
|
|
public static function createAllTips(){
|
|
$a=new TimeLogger("TippsController::createAllTipps()");
|
|
Log::info($a->getStartMessage());
|
|
|
|
$users = DB::table('users')->where('deleted',null)->get();
|
|
foreach($users as $user) {
|
|
TippsController::createTipps($user->id);
|
|
}
|
|
Log::info($a->getEndMessage());
|
|
}
|
|
|
|
|
|
public function index()
|
|
{
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
$user = Auth::user();
|
|
|
|
$activeSeason = Season::getCurrent();
|
|
if (!$activeSeason) {
|
|
return redirect('/');
|
|
}
|
|
|
|
if (! User::payment_date($user->id, $activeSeason->id)) {
|
|
return view('users.no_payment');
|
|
}
|
|
|
|
$rounds = null;
|
|
|
|
TippsController::createTipps($user->id); // ggfs. fehlende Tipp Eintraege einfügen
|
|
|
|
$lastLockedRoundId=Round::getLastLockedRoundId();
|
|
|
|
$rounds = Round::where('season_id', '=', $activeSeason->id)->get();
|
|
$max_identical_jokertipps=$activeSeason->maxIdenticalJokertipps();
|
|
|
|
|
|
return view('tipps.index', compact('rounds', 'activeSeason', 'user', 'max_identical_jokertipps'));
|
|
}
|
|
|
|
|
|
public function edit($tipp_id) {
|
|
$tipp = Tipp::where('id','=',$tipp_id)->first();
|
|
$game = Game::where('id','=',$tipp->game_id)->first();
|
|
return view('tipps.edit',compact('tipp','game'));
|
|
}
|
|
|
|
public function edit4($round_id) {
|
|
$user=Auth::user();
|
|
|
|
Log::info("TippsController::edit4 for user-id ".$user->id);
|
|
|
|
Log::info("Multi fetch result for tipps");
|
|
$round=Round::where('id','=', $round_id)->first();
|
|
$games=Game::where('round_id', '=', $round_id)->get();
|
|
|
|
$tipps=Tipp::whereIn('game_id', [$games[0]->id, $games[1]->id, $games[2]->id, $games[3]->id])->where('user_id',$user->id)->get();
|
|
|
|
$tipp1=$tipps[0];
|
|
$game1=Game::where('id','=',$tipp1->game_id)->first();
|
|
$tipp2=$tipps[1];
|
|
$game2=Game::where('id','=',$tipp2->game_id)->first();
|
|
$tipp3=$tipps[2];
|
|
$game3=Game::where('id','=',$tipp3->game_id)->first();
|
|
$tipp4=$tipps[3];
|
|
$game4=Game::where('id','=',$tipp4->game_id)->first();
|
|
return view('tipps.edit4',compact('round','tipp1','tipp2','tipp3','tipp4', 'game1', 'game2', 'game3', 'game4'));
|
|
}
|
|
|
|
public function store() {
|
|
// Validate form
|
|
$this->validate(request(), [
|
|
'id' => 'required',
|
|
'score1' => 'required',
|
|
'score2' => 'required',
|
|
'round_id' => 'required'
|
|
]);
|
|
|
|
$user=Auth::user();
|
|
Log::info("TippsController::store for user-id ".$user->id);
|
|
// Fetch, modify, save Tipp
|
|
$tipp = Tipp::where('id', request('id'))->first();
|
|
$game = Game::where('id', $tipp->game_id)->first();
|
|
$round= Round::where('id',$game->round_id)->first();
|
|
//if (Round::hasLockedGame($round->id)) { return redirect('/tipps'); } // Für Schlauberger, die die Tippmaske auflassen.
|
|
|
|
$tipp->score1= request('score1');
|
|
$tipp->score2= request('score2');
|
|
|
|
if (request()->has('joker')) {
|
|
$tipp->joker=true;
|
|
Log::info("Request has joker");
|
|
} else {
|
|
$tipp->joker = false;
|
|
Log::info("Request has NO joker");
|
|
}
|
|
|
|
// Neuerung 2020-01-09: Joker nur speichern, wenn Ergebnis nicht schon zu oft "gejokert" wurde.
|
|
Log::info("Checking for joker");
|
|
if ($tipp->joker) {
|
|
Log::info("Joker found.");
|
|
$jokertipps=Tipp::getJokerUsageArray($user->id,$game->round_id);
|
|
$key=$tipp->score1.":".$tipp->score2;
|
|
Log::info("Key(Gamescore):".$key);
|
|
if (array_key_exists($key,$jokertipps)) {
|
|
Log::info("Key found in jokertipps array");
|
|
$value=$jokertipps[$key];
|
|
Log::info("Stored value:".$value);
|
|
$limit=Season::getCurrent()->maxIdenticalJokertipps();
|
|
if (intval($value)>=intval($limit)) {
|
|
Log::info("**JOKERLIMIT REACHED FOR GAMESCORE:".$key);
|
|
return redirect('/tipps/joker_impossible');
|
|
}
|
|
}
|
|
}
|
|
|
|
$tipp->save();
|
|
|
|
|
|
// Joker gesetzt ? Dann Jokerflag für alle Tipps der selben Runde entfernen
|
|
if ($tipp->joker){
|
|
//Log::info("Checking for joker strip");
|
|
$game=Game::where('id','=',$tipp->game_id)->first();
|
|
$round_id=$game->round_id;
|
|
|
|
$games = DB::table('games')->where('round_id', '=', request('round_id'))->get();
|
|
foreach ($games as $game) {
|
|
|
|
$tipp = Tipp::where('user_id', $user->id)->where('game_id', $game->id)->first();
|
|
//Log::info("Testing game_id " . $game->id);
|
|
if ($tipp->id != request('id')) {
|
|
//Log::info("Resetting game_id " . $game->id);
|
|
$tipp->joker = false;
|
|
$tipp->save();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tippLink="/tipps#";
|
|
$r=\App\Round::getFirstTippableRound();
|
|
if($r) $tippLink=$tippLink.$r->id;
|
|
|
|
// Redirect to the Rounds Index
|
|
return redirect($tippLink);
|
|
}
|
|
|
|
// Try to store all 4 tipps of the round one by one.
|
|
// If one fails, previous saves are kept, but user
|
|
// is redirected to edit4 page again.
|
|
public function store4() {
|
|
// Validate form
|
|
$this->validate(request(), [
|
|
'round_id' => 'required',
|
|
'game_id_1' => 'required',
|
|
'game_id_2' => 'required',
|
|
'game_id_3' => 'required',
|
|
'game_id_4' => 'required',
|
|
'tipp_id_1' => 'required',
|
|
'tipp_id_2' => 'required',
|
|
'tipp_id_3' => 'required',
|
|
'tipp_id_4' => 'required',
|
|
'score1_1' => 'required',
|
|
'score1_2' => 'required',
|
|
'score1_3' => 'required',
|
|
'score1_4' => 'required',
|
|
'score2_1' => 'required',
|
|
'score2_2' => 'required',
|
|
'score2_3' => 'required',
|
|
'score2_4' => 'required',
|
|
'round_id' => 'required'
|
|
]);
|
|
|
|
$round_id= request('round_id');
|
|
$game_id_1=request('game_id_1');
|
|
$game_id_2=request('game_id_2');
|
|
$game_id_3=request('game_id_3');
|
|
$game_id_4=request('game_id_4');
|
|
$tipp_id_1=request('tipp_id_1');
|
|
$tipp_id_2=request('tipp_id_2');
|
|
$tipp_id_3=request('tipp_id_3');
|
|
$tipp_id_4=request('tipp_id_4');
|
|
$score1_1=request('score1_1');
|
|
$score1_2=request('score1_2');
|
|
$score1_3=request('score1_3');
|
|
$score1_4=request('score1_4');
|
|
$score2_1=request('score2_1');
|
|
$score2_2=request('score2_2');
|
|
$score2_3=request('score2_3');
|
|
$score2_4=request('score2_4');
|
|
|
|
$joker_1=false;
|
|
$joker_2=false;
|
|
$joker_3=false;
|
|
$joker_4=false;
|
|
|
|
|
|
if (request()->has('joker_1')) {
|
|
$joker_1=true;
|
|
} elseif (request()->has('joker_2')) {
|
|
$joker_2=true;
|
|
} elseif (request()->has('joker_3')) {
|
|
$joker_3=true;
|
|
} elseif (request()->has('joker_4')) {
|
|
$joker_4=true;
|
|
}
|
|
|
|
// If store fails, the jokerlimit has been reached. Redirect to edit4 page
|
|
if (self::tryStore($tipp_id_1, $game_id_1, $round_id, $score1_1, $score2_1, $joker_1 ) == false) {
|
|
//return redirect('/tipps/edit4/'.$round_id);
|
|
return redirect('/tipps/joker_impossible');
|
|
|
|
}
|
|
|
|
// If store fails, the jokerlimit has been reached. Redirect to edit4 page
|
|
if (self::tryStore($tipp_id_2, $game_id_2, $round_id, $score1_2, $score2_2, $joker_2) == false) {
|
|
return redirect('/tipps/joker_impossible');
|
|
|
|
}
|
|
|
|
// If store fails, the jokerlimit has been reached. Redirect to edit4 page
|
|
if (self::tryStore($tipp_id_3, $game_id_3, $round_id, $score1_3, $score2_3, $joker_3 ) == false) {
|
|
return redirect('/tipps/joker_impossible');
|
|
|
|
}
|
|
|
|
// If store fails, the jokerlimit has been reached. Redirect to edit4 page
|
|
if (self::tryStore($tipp_id_4, $game_id_4, $round_id, $score1_4, $score2_4, $joker_4 ) == false) {
|
|
return redirect('/tipps/joker_impossible');
|
|
|
|
}
|
|
|
|
return redirect('/tipps/');
|
|
}
|
|
|
|
// Store tipp if valid.
|
|
public function tryStore($tipp_id, $game_id, $round_id, $score1, $score2, $has_joker ) {
|
|
Log::info("Starting tryStore");
|
|
$user=Auth::user();
|
|
|
|
$tipp = Tipp::where('id', $tipp_id)->first();
|
|
Log::info("tipp_id=".$tipp->id);
|
|
$game = Game::where('id', $game_id)->first();
|
|
$round= Round::where('id',$round_id)->first();
|
|
|
|
$tipp->joker=$has_joker;
|
|
|
|
// Neuerung 2020-01-09: Joker nur speichern, wenn Ergebnis nicht schon zu oft "gejokert" wurde.
|
|
if ($tipp->joker) {
|
|
$jokertipps=Tipp::getJokerUsageArray($user->id,$round_id);
|
|
$key=$tipp->score1.":".$tipp->score2;
|
|
Log::info("Key(Gamescore):".$key);
|
|
if (array_key_exists($key,$jokertipps)) {
|
|
//Log::info("Key found in jokertipps array");
|
|
|
|
$value=$jokertipps[$key];
|
|
Log::info("Stored value:".$value);
|
|
$limit=Season::getCurrent()->maxIdenticalJokertipps();
|
|
if (intval($value)>=intval($limit)) {
|
|
Log::info("**JOKERLIMIT REACHED FOR GAMESCORE:".$key);
|
|
Log::info("Returning false");
|
|
return false; // Returning false should make store4 method return to edit4 page.
|
|
}
|
|
}
|
|
}
|
|
Log::info("tryStore: Saving score ".$score1.":".$score2." for user-id ".$user->id);
|
|
$tipp->score1=$score1;
|
|
$tipp->score2=$score2;
|
|
$tipp->joker=$has_joker;
|
|
$tipp->save();
|
|
Log::info("Returning true");
|
|
return true; // Returning true should make store4 method return to general tipps (index) page.
|
|
}
|
|
|
|
|
|
// belongs to old single tipp functionality.
|
|
public static function joker_impossible() {
|
|
return view('tipps.joker_impossible');
|
|
|
|
}
|
|
|
|
|
|
}
|