Calculate score of all tipps
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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');
|
||||
|
Reference in New Issue
Block a user