Calculate score of all tipps

This commit is contained in:
2018-06-06 21:32:48 +02:00
parent 8a72d9a3a0
commit 4b18d4aca0
3 changed files with 46 additions and 0 deletions

View File

@@ -49,4 +49,39 @@ class PagesController extends Controller
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)->get();
$diffmatch = abs($match->resualta - $match->resultb);
$difftipp = abs($tipp->resulta - $tipp->resultb);
if (winTeam($tipp->resulta, $tipp->resultb) == 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]);
}
}
}

View File

@@ -36,4 +36,14 @@ class Tipp extends Model
protected $hidden = [
//
];
public static function winTeam($resulta, $resultb) {
if ($resulta > $resultb) {
return 'a';
} else {
//if scores are even, b will be returned
//if match and tipp score is even, they'll both will be b
return 'b';
}
}
}

View File

@@ -20,3 +20,4 @@ Auth::routes();
Route::get('/dashboard', 'PagesController@dashboard');
Route::get('/tipp/create/{matchid}', 'PagesController@createTipp');
Route::post('/tipp/store', 'PagesController@storeTipp');
Route::get('/calculate', 'PagesController@calculateScore');