Qualification Pairing done

This commit is contained in:
Torsten 2017-09-23 12:19:46 +02:00
parent a66189ca00
commit e0d38fac38
4 changed files with 73 additions and 172 deletions

View file

@ -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'));
}

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -5,31 +5,35 @@
<div class="container">
<label>{{ $season->name }}</label>
<h2>Spielplan</h2>
<h1>Spielplan</h1>
<!-- Start Runden-Loop-->
@foreach($matchesTotal as $round_id => $roundGroup)
<h3>{{$roundNames[$round_id]}}</h3>
<table class="table-bordered col-lg-12">
<tr class="table-bordered">
<td class="table-bordered" style="width:150px;">Gruppe</td>
<td class="table-bordered" style="width:150px;">Spieler 1</td>
<td class="table-bordered" style="width:150px;">Spieler 2</td>
<td class="table-bordered" style="width:150px;">Ergebnis</td>
</tr>
<p>&nbsp;</p>
@foreach($matchesTotal[$round_id] as $match)
@if ($leagueNames[$match->league_id]=="Qualifikationsgruppe 4") <!-- Debug -->
<tr class="table-bordered">
<td>&nbsp;{{$leagueNames[$match->league_id]}}&nbsp;</td>
<td>&nbsp;{{$match->user1_id}}-{{$match->user1_name}}&nbsp;</td>
<td>&nbsp;{{$match->user2_id}}-{{$match->user2_name}}&nbsp;</td>
<td>0:0</td>
</tr>
@endif
@endforeach
<h2>{{$roundNames[$round_id]}}</h2>
</table>
<!-- Start Liga-Loop-->
@foreach($matchesTotal[$round_id] as $league_id => $matchGroup)
<h3>{{$leagueNames[$league_id]}}</h3>
<table class="table-bordered col-lg-12">
<tr class="table-bordered">
<td class="table-bordered" style="width:150px;">Heim</td>
<td class="table-bordered" style="width:150px;">Gast</td>
<td class="table-bordered" style="width:150px;">Ergebnis</td>
</tr>
<!-- Start Matchgruppen-Loop-->
@foreach($matchesTotal[$round_id][$league_id] as $match)
<tr class="table-bordered">
<td>&nbsp;{{$match->user1_id}}-{{$match->user1_name}}&nbsp;</td>
<td>&nbsp;{{$match->user2_id}}-{{$match->user2_name}}&nbsp;</td>
<td>0:0</td>
</tr>
@endforeach
</table>
<p>&nbsp;</p>
@endforeach
@endforeach
</div>
</section>