2017-06-22 17:00:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use App\Season;
|
|
|
|
|
use App\Round;
|
|
|
|
|
use App\Game;
|
|
|
|
|
use App\Tipp;
|
2017-06-26 17:21:30 +02:00
|
|
|
use App\User;
|
2017-06-22 17:00:09 +02:00
|
|
|
|
|
|
|
|
use Auth;
|
|
|
|
|
use Log;
|
|
|
|
|
use DB;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TippsController extends Controller
|
|
|
|
|
{
|
2017-08-24 11:47:16 +02:00
|
|
|
//Duplicate from Tipps model
|
|
|
|
|
//TODO: Refactor usage to use only the Tipps version
|
2017-06-22 21:22:07 +02:00
|
|
|
public static function createTipps($user_id) {
|
2017-06-22 17:00:09 +02:00
|
|
|
$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],
|
2017-06-22 21:22:07 +02:00
|
|
|
['user_id','=',$user_id]]
|
2017-06-22 17:00:09 +02:00
|
|
|
)->first();
|
|
|
|
|
if (! $tipp) {
|
|
|
|
|
//Log::info('Creating missing tip: game_id=' . $game->id . ", user_id=" . $user->id);
|
|
|
|
|
$tipp=Tipp::create([
|
2017-06-22 21:22:07 +02:00
|
|
|
'user_id' => $user_id,
|
2017-06-22 17:00:09 +02:00
|
|
|
'game_id'=> $game->id,
|
|
|
|
|
'score1'=> -1,
|
|
|
|
|
'score2'=> -1,
|
|
|
|
|
'joker'=> false
|
|
|
|
|
]);
|
|
|
|
|
$tipp->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
//Duplicate from Tipps model
|
|
|
|
|
//TODO: Refactor usage to use only the Tipps version
|
2017-06-22 21:22:07 +02:00
|
|
|
public static function createAllTips(){
|
|
|
|
|
$users = DB::table('users')->where('deleted',null)->get();
|
|
|
|
|
foreach($users as $user) {
|
|
|
|
|
TippsController::createTipps($user->id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
public function index()
|
|
|
|
|
{
|
2017-06-26 17:21:30 +02:00
|
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
$activeSeason = Season::getCurrent();
|
2017-06-26 17:21:30 +02:00
|
|
|
if (!$activeSeason) {
|
|
|
|
|
return redirect('/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! User::payment_date($user->id, $activeSeason->id)) {
|
|
|
|
|
return view('users.no_payment');
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
$rounds = null;
|
|
|
|
|
|
2017-06-22 21:22:07 +02:00
|
|
|
TippsController::createTipps($user->id); // ggfs. fehlender Tipp Eintraege einfügen
|
2017-06-22 17:00:09 +02:00
|
|
|
|
2017-08-26 15:15:54 +02:00
|
|
|
$lastLockedRoundId=Round::getLastLockedRoundId();
|
2017-06-22 17:00:09 +02:00
|
|
|
|
2017-08-26 15:15:54 +02:00
|
|
|
$rounds = Round::where('season_id', '=', $activeSeason->id)->get();
|
2017-07-18 14:58:32 +02:00
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
return view('tipps.index', compact('rounds', 'activeSeason', 'user'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 store() {
|
|
|
|
|
// Validate form
|
|
|
|
|
$this->validate(request(), [
|
|
|
|
|
'id' => 'required',
|
|
|
|
|
'score1' => 'required',
|
|
|
|
|
'score2' => 'required',
|
|
|
|
|
'round_id' => 'required'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$user=Auth::user();
|
|
|
|
|
|
|
|
|
|
// Fetch, modify, save Tipp
|
|
|
|
|
$tipp = Tipp::where('id', request('id'))->first();
|
2017-08-04 16:51:28 +02:00
|
|
|
$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.
|
|
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
$tipp->score1= request('score1');
|
|
|
|
|
$tipp->score2= request('score2');
|
2017-06-26 17:21:30 +02:00
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
if (request()->has('joker')) {
|
2017-08-04 16:51:28 +02:00
|
|
|
$tipp->joker=true;
|
|
|
|
|
} else {
|
|
|
|
|
$tipp->joker = false;
|
2017-06-22 17:00:09 +02:00
|
|
|
}
|
2017-08-04 16:51:28 +02:00
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
$tipp->save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Joker gesetzt ? Dann Jokerflag für alle Tipps der selben Runde entfernen
|
|
|
|
|
if ($tipp->joker){
|
2017-06-26 17:21:30 +02:00
|
|
|
Log::info("Checking for joker strip");
|
|
|
|
|
$game=Game::where('id','=',$tipp->game_id)->first();
|
|
|
|
|
$round_id=$game->round_id;
|
2017-06-22 17:00:09 +02:00
|
|
|
|
2017-06-26 17:21:30 +02:00
|
|
|
$games = DB::table('games')->where('round_id', '=', request('round_id'))->get();
|
|
|
|
|
foreach ($games as $game) {
|
2017-06-22 17:00:09 +02:00
|
|
|
|
2017-06-26 17:21:30 +02:00
|
|
|
$tipp = Tipp::where('user_id', $user->id)->where('game_id', $game->id)->first();
|
|
|
|
|
Log::info("Testing game_id " . $game->id);
|
2017-06-22 17:00:09 +02:00
|
|
|
if ($tipp->id != request('id')) {
|
2017-06-26 17:21:30 +02:00
|
|
|
Log::info("Resetting game_id " . $game->id);
|
|
|
|
|
$tipp->joker = false;
|
2017-06-22 17:00:09 +02:00
|
|
|
$tipp->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-26 17:21:30 +02:00
|
|
|
|
2017-06-22 17:00:09 +02:00
|
|
|
}
|
2017-08-29 15:52:12 +02:00
|
|
|
$tippLink="/tipps#".\App\Round::getFirstTippableRound()->id;
|
2017-06-22 17:00:09 +02:00
|
|
|
// Redirect to the Rounds Index
|
2017-08-29 15:52:12 +02:00
|
|
|
return redirect($tippLink);
|
2017-06-22 17:00:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|