Files
tippspiel/tippspiel/app/Http/Controllers/PagesController.php

89 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Team;
use App\Match;
use App\Tipp;
use App\Http\Requests\TippCreateRequest;
use Auth;
class PagesController extends Controller
{
function dashboard() {
if (!Auth::check()) {
return redirect('/');
}
$teams = Team::all();
$matches = Match::all();
$tipps = Tipp::where('user', Auth::user()->id)->get();
//dd($tipps->where('matchid', 1)->pluck('resulta')->first());
return view('dashboard')->with([
'teams' => $teams,
'matches' => $matches,
'tipps' => $tipps,
]);
}
function createTipp($matchid) {
$teams = Team::all();
$matches = Match::findOrFail($matchid);
$teama = $teams->find($matches->teama)->name;
$teamb = $teams->find($matches->teamb)->name;
return view('tipp/create')->with([
'teama' => $teama,
'teamb' => $teamb,
'matchid' => $matchid,
]);
}
function storeTipp(TippCreateRequest $request) {
$tipp = new Tipp($request->all());
$tipp->user = Auth::user()->id;
$tipp->save();
return redirect('/dashboard');
}
function calculateScore() {
//Score:
//2 --> correct winner chosen
//3 --> correct goal difference
//4 --> correct score
//Get all Tipps
$tipps = Tipp::all();
//Get all Matches
$matches = Match::all();
//Iterate over all tipps
foreach ($tipps as $tipp) {
$score = 0;
$match = $matches->find($tipp->matchid)->first();
$diffmatch = abs($match->resulta - $match->resultb);
$difftipp = abs($tipp->resulta - $tipp->resultb);
if (Tipp::winTeam($tipp->resulta, $tipp->resultb) == Tipp::winTeam($match->resulta, $match->resultb)) {
$score = 2;
}
if ($diffmatch == $difftipp) {
$score = 3;
}
if ($tipp->resulta == $match->resulta && $tipp->resultb == $match->resultb) {
$score = 4;
}
$tipp->update(['score' => $score]);
}
redirect('/dashboard');
}
}