From e0d38fac38d5095be51eb1d7bc1722dfc5d19169 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 23 Sep 2017 12:19:46 +0200 Subject: [PATCH] Qualification Pairing done --- .../Controllers/Leagues/LeaguesController.php | 12 +- pfosten/app/leagues/LeagueMember.php | 6 +- pfosten/app/leagues/Match.php | 181 ++++-------------- .../resources/views/leagues/matches.blade.php | 46 +++-- 4 files changed, 73 insertions(+), 172 deletions(-) diff --git a/pfosten/app/Http/Controllers/Leagues/LeaguesController.php b/pfosten/app/Http/Controllers/Leagues/LeaguesController.php index 1e1b2953..235987b5 100644 --- a/pfosten/app/Http/Controllers/Leagues/LeaguesController.php +++ b/pfosten/app/Http/Controllers/Leagues/LeaguesController.php @@ -166,7 +166,7 @@ class LeaguesController extends Controller } } - Match::createMissingMatchups(); + Match::createMatchups(); return redirect("/seasons"); } @@ -177,7 +177,7 @@ class LeaguesController extends Controller $season=Season::getCurrent(); $roundNames = Round::getNames(); $leagueNames = League::getNames(); - $leagues = League::where('season_id','=',$season->id)->where('rank','=', $league_rank)->get(); + $leagues = League::where('season_id','=',$season->id)->where('rank','=', $league_rank)->orderBy('id')->get(); $matchesTotal = array(); @@ -188,13 +188,17 @@ class LeaguesController extends Controller if (!key_exists($match->round_id, $matchesTotal)) { $matchesTotal[$match->round_id]=array(); } - $lg = $matchesTotal[$match->round_id]; + if (!key_exists($league->id, $matchesTotal[$match->round_id] )) { + $matchesTotal[$match->round_id][$league->id]=array(); + } + $lg = $matchesTotal[$match->round_id][$league->id]; array_push($lg, $match); - $matchesTotal[$match->round_id] = $lg; + $matchesTotal[$match->round_id][$league->id] = $lg; } } } + return view ('leagues.matches', compact('season', 'matchesTotal','leagues','leagueNames','roundNames')); } diff --git a/pfosten/app/leagues/LeagueMember.php b/pfosten/app/leagues/LeagueMember.php index 08218f8a..9de01727 100644 --- a/pfosten/app/leagues/LeagueMember.php +++ b/pfosten/app/leagues/LeagueMember.php @@ -16,7 +16,11 @@ class LeagueMember extends Model public static function getMembers($league_id) { $members =LeagueMember::where('league_id','=',$league_id)->orderBy('id')->get(); - return $members; + $userList=array(); + foreach($members as $member) { + array_push($userList,$member->user_id); + } + return $userList; } public static function playsInRound($userId, $roundId, $leagueId) { diff --git a/pfosten/app/leagues/Match.php b/pfosten/app/leagues/Match.php index 42c84c81..6b46f9d4 100644 --- a/pfosten/app/leagues/Match.php +++ b/pfosten/app/leagues/Match.php @@ -20,153 +20,32 @@ class Match extends Model 'id','league_id','round_id','user1_id','user2_id' ]; - - // https://en.wikipedia.org/wiki/Round-robin_tournament - public static function createMissingMatchups() - { Log::info("Match::Creating missing matchups()"); - + public static function createMatchups() + { Log::info("Match::Creating matchups()"); $currentSeason = Season::getCurrent(); $leagues = League::where('season_id', '=', $currentSeason->id)->get(); - - $roundNames=Round::getNames(); - $leagueNames=League::getNames(); - foreach ($leagues as $league) { - if ($league->rank == League::RANK_QUALIFICATION) { - $matches = array(); - - $members = LeagueMember::getMembers($league->id); - foreach ($members as $member) { - $counterMembers = LeagueMember::getMembers($league->id); - - foreach ($counterMembers as $counterMember) { - if ($member->user_id == $counterMember->user_id) continue; - // Die Paarung darf nicht schon in der DB existieren, egal welche Runde - // hasMatched bezieht sich auf bereits eingetragene matches - // Spieler darf gegen den Gegner in der Saison nicht schon gespielt haben - - $exists = false; - // Prüfung ob Paarung bereits in matches abgespeichert ist - if (array_key_exists($league->id, $matches)) { - foreach ($matches[$league->id] as $match) { - if ($match[0] == $member->user_id && $match[1] == $counterMember->user_id) { - $exists = true; - } - if ($match[1] == $member->user_id && $match[0] == $counterMember->user_id) { - $exists = true; - } - } - } - - // hasMatched prüft abgespeicherte Matches in DB (leagues_matches) - if (!Match::hasMatched($member->user_id, $counterMember->user_id, $league->id) && !$exists ) { - if (!array_key_exists($league->id, $matches)) { - $matches[$league->id] = array(); - } - // add Match - array_push($matches[$league->id], [$member->user_id, $counterMember->user_id,0]); // 0 = unplatziert - Log::info("ADD MATCH PAIRING: Liga " . $league->id . " => " . $member->user_id . " vs " . $counterMember->user_id); - } - } - } - - // Wir haben jetzt in $matches die nach Liga unterteilten noch zu platzierenden Matches - - //Log::info("Starting placement"); - // Auffüllen der noch freien Runden mit den verfügbaren matches. $rounds = Round::where('id','>=',$league->start_round_id)->get(); - - - - + $roundNr=1; + $neededRoundCount = Match::getNumberOfNeededRounds($league->id); foreach ($rounds as $round) { - - if ($leagueNames[$league->id]=="Qualifikationsgruppe 4" && $roundNames[$round->id]=="Runde 2") { Log::info("Analyzing Round 2");} - - //Log::info("LeagueID=".$league->id.", Trying to fill roundid ".$round->id); - $count=0; - if ($leagueNames[$league->id]=="Qualifikationsgruppe 4" && $roundNames[$round->id]=="Runde 2") { - Log::info("Available Matchups:"); + $pairings = Match::getPairings($league->id, $roundNr); + // Save pairings + foreach($pairings as $pair) { + Log::info("Saving match. ".$pair[0]." vs ".$pair[1]." roundid=".$round->id); + $newMatch = Match::create([ + 'league_id' => $league->id, + 'round_id' => $round->id, + 'user1_id' => $pair[0], + 'user2_id' => $pair[1] + ]); + $newMatch->save(); } - shuffle($matches[$league->id]); - foreach($matches[$league->id] as $key => $m) { - - - - if ($m[2]==0) { $count += 1; - if ($leagueNames[$league->id]=="Qualifikationsgruppe 4" && $roundNames[$round->id]=="Runde 2") { - Log::info($m[0]." vs ".$m[1]); - } - - } - } - //Log::info("Unplaced matches:".$count); - - $existingRoundMatches = Match::where('league_id','=',$league->id)->where('round_id','=',$round->id)->get(); - //Log::info("round ".$round->id." Existing matchCount:".sizeof($existingRoundMatches)); - - $membercount = League::getMemberCount($league->id); - $maxMatchCount = intdiv($membercount,2); //TODO: Ungerade Anzahl = +1 - - - // Gibt es noch Einträge in $matches[$league->id] ? - $hasUnplacedMatches=false; - foreach($matches[$league->id] as $m) { - if ($m[2]==0) { $hasUnplacedMatches=true; break;} - } - if ( !$hasUnplacedMatches ) { - if ($leagueNames[$league->id]=="Qualifikationsgruppe 4" && $roundNames[$round->id]=="Runde 2") { - Log::info("Ending Placement for Round 2 because roster is full"); - } - break; - } - - foreach ($matches[$league->id] as $index => $match) { - if ($match[2]==1) continue; - - // Anzahl an Matches pro Runde schon erreicht ? - $existingRoundMatches = Match::where('league_id','=',$league->id)->where('round_id','=',$round->id)->get(); - - //Log::info("Existing matches in roundid ".$round->id.": ".sizeof($existingRoundMatches)); - if (sizeof($existingRoundMatches) > $maxMatchCount) { - - //Log::info("Skipping to next round, because Placement roster is full"); - continue; - } - - $conflict=false; - foreach ($existingRoundMatches as $exMatch) { - $mName=$matches[$league->id][$index][0]." vs ".$matches[$league->id][$index][1]; - if ($exMatch->user1_id == $match[0] ) { $conflict=true; } - else if ($exMatch->user2_id == $match[0]) { $conflict=true; } - else if ($exMatch->user1_id == $match[1]) { $conflict=true; } - else if ($exMatch->user2_id == $match[1]) { $conflict=true; } - } - - if (!$conflict) { - $matches[$league->id][$index][2]=1; - Log::info("Saving match. ".$matches[$league->id][$index][0]." vs ".$matches[$league->id][$index][1]); - $newMatch = Match::create([ - 'league_id' => $league->id, - 'round_id' => $round->id, - 'user1_id' => $matches[$league->id][$index][0], - 'user2_id' => $matches[$league->id][$index][1] - ]); - $newMatch->save(); - } - } - - + $roundNr += 1; + if ($roundNr > $neededRoundCount) break; } - $count=0; - foreach($matches[$league->id] as $key => $m) { - if ($m[2]==0) { $count += 1; } - } - - Log::info("Exiting LeagueId ".$league->id. " with ".$count." unplaced matches"); - } } @@ -174,7 +53,6 @@ class Match extends Model public static function hasMatched($user1_id, $user2_id, $league_id) { - $matches = DB::table('leagues_matches') ->where('league_id', '=', $league_id) ->where('user1_id', '=', $user1_id) @@ -184,7 +62,6 @@ class Match extends Model return true; } - $matches = DB::table('leagues_matches') ->where('league_id', '=', $league_id) ->where('user1_id', '=', $user2_id) @@ -220,18 +97,18 @@ class Match extends Model // Hier könnte man die Punkte auswerten. } - return $matches; } public static function getPairings($leagueId, $roundNr) { $members = LeagueMember::getMembers($leagueId); - $pairingCount = intdiv(sizeof($members),2); + shuffle($members); + Log::info("Number of members:".sizeof($members)); + $pairingCount = Match::getNumberOfPairingsPerRound($leagueId); $roundRobin=array(); - - foreach($members as $member) { - array_push($roundRobin, $member->id); + foreach($members as $userId) { + array_push($roundRobin, $userId); } while ($roundNr>1) { @@ -258,8 +135,20 @@ class Match extends Model error_log("pairings[".$idx."][".$slot."]=".$userId); } - return $pairings; // Array, der User1/User2 Tupel enthält } + public static function getNumberOfPairingsPerRound($leagueId) { + $members = LeagueMember::getMembers($leagueId); + $pairingsPerRound = intdiv(sizeof($members),2); + if (sizeof($members)-$pairingsPerRound*2) { + $pairingsPerRound+=1; + } + return $pairingsPerRound; + } + + public static function getNumberOfNeededRounds($leagueId) { + $members = LeagueMember::getMembers($leagueId); + return sizeof($members)-1; + } } diff --git a/pfosten/resources/views/leagues/matches.blade.php b/pfosten/resources/views/leagues/matches.blade.php index 72bf6327..a415cb85 100755 --- a/pfosten/resources/views/leagues/matches.blade.php +++ b/pfosten/resources/views/leagues/matches.blade.php @@ -5,31 +5,35 @@
-

Spielplan

+

Spielplan

@foreach($matchesTotal as $round_id => $roundGroup) -

{{$roundNames[$round_id]}}

- - - - - - - -

 

- @foreach($matchesTotal[$round_id] as $match) - @if ($leagueNames[$match->league_id]=="Qualifikationsgruppe 4") - - - - - - - @endif - @endforeach +

{{$roundNames[$round_id]}}

-
GruppeSpieler 1Spieler 2Ergebnis
 {{$leagueNames[$match->league_id]}}  {{$match->user1_id}}-{{$match->user1_name}}  {{$match->user2_id}}-{{$match->user2_name}} 0:0
+ + @foreach($matchesTotal[$round_id] as $league_id => $matchGroup) +

{{$leagueNames[$league_id]}}

+ + + + + + + + + + @foreach($matchesTotal[$round_id][$league_id] as $match) + + + + + + @endforeach + +
HeimGastErgebnis
 {{$match->user1_id}}-{{$match->user1_name}}  {{$match->user2_id}}-{{$match->user2_name}} 0:0
+

 

+ @endforeach @endforeach