From 7a33f5f8897adea688b97ec22f4ca7aa8a76f443 Mon Sep 17 00:00:00 2001 From: Torsten Date: Thu, 22 Jun 2017 17:00:09 +0200 Subject: [PATCH] First version almost done --- .../app/Http/Controllers/RoundsController.php | 5 +- .../app/Http/Controllers/TippController.php | 10 -- .../app/Http/Controllers/TippsController.php | 128 ++++++++++++++++++ .../app/Http/Controllers/UsersController.php | 17 ++- pfosten/app/Tipp.php | 41 +++++- pfosten/config/app.php | 4 +- .../2017_06_19_112632_create_tipps_table.php | 8 +- .../resources/views/layouts/footer.blade.php | 3 +- .../resources/views/layouts/layout.blade.php | 2 +- pfosten/resources/views/layouts/nav.blade.php | 4 +- pfosten/resources/views/tipps/edit.blade.php | 34 +++++ pfosten/resources/views/tipps/index.blade.php | 35 +++++ pfosten/resources/views/users/edit.blade.php | 5 +- pfosten/routes/web.php | 6 +- 14 files changed, 274 insertions(+), 28 deletions(-) delete mode 100644 pfosten/app/Http/Controllers/TippController.php create mode 100644 pfosten/app/Http/Controllers/TippsController.php create mode 100644 pfosten/resources/views/tipps/edit.blade.php create mode 100644 pfosten/resources/views/tipps/index.blade.php diff --git a/pfosten/app/Http/Controllers/RoundsController.php b/pfosten/app/Http/Controllers/RoundsController.php index 0b10b23a..6a9f4c13 100644 --- a/pfosten/app/Http/Controllers/RoundsController.php +++ b/pfosten/app/Http/Controllers/RoundsController.php @@ -23,8 +23,7 @@ class RoundsController extends Controller } return view('rounds.index', compact('rounds','activeSeason')); - //return view('rounds.index'); - //return view ('welcome'); + } @@ -95,7 +94,7 @@ class RoundsController extends Controller // Sign them in // Redirect to the home page - return redirect()->home(); + return redirect('/rounds'); } public function editname($round_id) diff --git a/pfosten/app/Http/Controllers/TippController.php b/pfosten/app/Http/Controllers/TippController.php deleted file mode 100644 index d01dfb2c..00000000 --- a/pfosten/app/Http/Controllers/TippController.php +++ /dev/null @@ -1,10 +0,0 @@ -id); +// Log::info("** Start: createTipps for " . $activeSeason->name . "**"); + + $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(); + } + } + } + + + } + + public function index() + { + $activeSeason = Season::getCurrent(); + $rounds = null; + + $user = Auth::user(); + + + $this->createTipps(); // ggfs. fehlender Tipp Eintraege einfügen + + + if ($activeSeason) { + $activeSeasonId = Season::getCurrent()->id; + $rounds = Round::all()->where('season_id', '=', $activeSeasonId); + } + + 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(); + $tipp->score1= request('score1'); + $tipp->score2= request('score2'); + if (request()->has('joker')) { + $tipp->joker=true; + } else { + $tipp->joker=false; + } + $tipp->save(); + + + // Joker gesetzt ? Dann Jokerflag für alle Tipps der selben Runde entfernen + if ($tipp->joker){ + $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(); + + if ($tipp->id != request('id')) { + $tipp->joker=false; + $tipp->save(); + } + } + } + + // Redirect to the Rounds Index + return redirect('/tipps'); + } + + + +} diff --git a/pfosten/app/Http/Controllers/UsersController.php b/pfosten/app/Http/Controllers/UsersController.php index 3efd65bd..8c282657 100644 --- a/pfosten/app/Http/Controllers/UsersController.php +++ b/pfosten/app/Http/Controllers/UsersController.php @@ -49,6 +49,8 @@ class UsersController extends Controller public function store() { $season_id = Season::getCurrent()->id; + + // Validate form $this->validate(request(), [ 'id' => 'required', @@ -63,13 +65,20 @@ class UsersController extends Controller $user->name=request('name'); $user->email=request('email'); - if (request()->has('admin')) { $user->admin=request('admin'); } + if (request()->has('admin')) { + $user->admin = true; + + } else { + $user->admin=false; + } + + // + $user->save(); + //dd(request('id'), $season_id, request('paid')); + Payment::set_payment_date(request('id'), $season_id, request('paid')); - if (request()->has('paid')) { - Payment::set_payment_date(request('id'), $season_id, request('paid')); - } // Redirect to the home page return redirect('/users'); diff --git a/pfosten/app/Tipp.php b/pfosten/app/Tipp.php index 552f92d9..4aad69cc 100644 --- a/pfosten/app/Tipp.php +++ b/pfosten/app/Tipp.php @@ -3,8 +3,47 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use DB; class Tipp extends Model { - // + protected $fillable = [ + 'id', 'user_id', 'game_id','score1','score2','joker' + ]; + + public static function getTipp($game_id, $user_id) { + return DB::table('tipps')->where([ + ['user_id','=',$user_id], + ['game_id','=',$game_id] + ])->first(); + } + + public static function getScore($tipp_id) { + $tipp = Tipp::where('id',$tipp_id)->first(); + $game = Game::where('id',$tipp->game_id)->first(); + + $tippTendenz = $tipp->score1 - $tipp->score2; + if ($tippTendenz > 0) {$tippTendenz=1;} + if ($tippTendenz < 0) {$tippTendenz=-1;} + + $gameTendenz = $game->score1 - $game->score2; + if ($gameTendenz<0) {$gameTendenz=-1;} + if ($gameTendenz>0) {$gameTendenz=1;} + + $score=0; + + if ($tippTendenz == $gameTendenz) { $score=1; } + + if ( ($tipp->score1 == $game->score1) && ($tipp->score2 == $game->score2) ) { + $score=2; + } + + if ($tipp->joker==true) { + $score = $score *2; + } + + if ($game->score1 == -1) { $score=0; } + + return $score; + } } diff --git a/pfosten/config/app.php b/pfosten/config/app.php index 42f311d2..76656969 100644 --- a/pfosten/config/app.php +++ b/pfosten/config/app.php @@ -64,7 +64,9 @@ return [ | */ - 'timezone' => 'UTC', + /*'timezone' => 'UTC',*/ + + 'timezone' => 'Europe/Berlin', /* |-------------------------------------------------------------------------- diff --git a/pfosten/database/migrations/2017_06_19_112632_create_tipps_table.php b/pfosten/database/migrations/2017_06_19_112632_create_tipps_table.php index dd554d31..cdc41224 100644 --- a/pfosten/database/migrations/2017_06_19_112632_create_tipps_table.php +++ b/pfosten/database/migrations/2017_06_19_112632_create_tipps_table.php @@ -15,9 +15,11 @@ class CreateTippsTable extends Migration { Schema::create('tipps', function (Blueprint $table) { $table->increments('id'); - $table->integer('user_id'); - $table->integer('game_id'); - $table->boolean('joker'); + $table->integer('user_id')->hasOne('App\User'); + $table->integer('game_id')->hasOne('App\Game'); + $table->integer('score1'); + $table->integer('score2'); + $table->boolean('joker')->default(false); $table->timestamps(); }); } diff --git a/pfosten/resources/views/layouts/footer.blade.php b/pfosten/resources/views/layouts/footer.blade.php index af136790..cb2619b2 100644 --- a/pfosten/resources/views/layouts/footer.blade.php +++ b/pfosten/resources/views/layouts/footer.blade.php @@ -1,6 +1,7 @@