Qualification Pairing done
This commit is contained in:
parent
a66189ca00
commit
e0d38fac38
4 changed files with 73 additions and 172 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue