id)->get(); foreach ($leagues as $league) { if ($league->rank == League::RANK_QUALIFICATION) { $rounds = Round::where('id','>=',$league->start_round_id)->get(); $roundNr=1; $neededRoundCount = Match::getNumberOfNeededRounds($league->id); $allPairings=Match::getAllPairings($league->id, sizeof($rounds)); Log::info("all pairings:"); Log::info($allPairings); foreach ($rounds as $round) { $pairings = $allPairings[$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(); } $roundNr += 1; if ($roundNr > $neededRoundCount) break; } } } } 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) ->where('user2_id', '=', $user2_id) ->get(); if (sizeof($matches)) { return true; } $matches = DB::table('leagues_matches') ->where('league_id', '=', $league_id) ->where('user1_id', '=', $user2_id) ->where('user2_id', '=', $user1_id) ->get(); if (sizeof($matches)) { return true; } return false; } // Selektiert nur die "Heimspiele" public static function getMatches($user_id, $league_id) { $matches = DB::table('leagues_matches')->where('league_id', '=', $league_id)->where('user1_id','=',$user_id)->get(); return $matches; } public static function getLeagueMatches($league_id) { //$matches = Match::where('league_id','=',$league_id)->orderBy('round_id')->get(); $matches = DB::table('leagues_matches')->where('league_id','=',$league_id)->orderBy('round_id')->get(); $season = Season::getCurrent(); $roundnames = Round::getNames(); $usernames = User::getNames(); foreach($matches as $match) { $match->round_name = $roundnames[$match->round_id]; $match->user1_name = $usernames[$match->user1_id]; $match->user2_name = $usernames[$match->user2_id]; // Punktauswertung in Match::getMatchResults($leagueId, $maxRoundId); } return $matches; } public static function getAllPairings($leagueId, $roundCount) { $members = LeagueMember::getMembers($leagueId); shuffle($members); //error_log("Number of members:".sizeof($members)); $pairingCount = Match::getNumberOfPairingsPerRound($leagueId); $all = array(); for ($currentRoundNr=1; $currentRoundNr<= $roundCount; $currentRoundNr++) { $roundRobin = array(); foreach ($members as $userId) { array_push($roundRobin, $userId); } //error_log("RoundNr ".$currentRoundNr." Array start:"); //error_log(implode(",",$roundRobin)); $_roundNr = $currentRoundNr; while ($_roundNr >= 1) { // Round-Robin shift: First element stays, rest rotates $first = array_shift($roundRobin); // Erstes Element sichern & entfernen $last = array_pop($roundRobin); // Letztes Element sichern & entfernen array_unshift($roundRobin, $last); // Vorne einfügen array_unshift($roundRobin, $first); // Vorne einfügen $_roundNr--; } //error_log("RoundNr ".$currentRoundNr." Array end:"); //error_log(implode(",",$roundRobin)); $idx = 0; $slot = 0; $pairings = array(); foreach ($roundRobin as $index => $userId) { $idx = $index; if ($index >= $pairingCount) { $idx = 2 * $pairingCount - $index - 1; $slot = 1; } $pairings[$idx][$slot] = $userId; error_log($idx.",".$slot." => ".$userId); } //error_log("Pairings for RoundNr ".$currentRoundNr.":"); $strPairings=""; //foreach($pairings as $pairing) { //error_log(">".$pairing[0].":".$pairing[1]); //$strPairings = $strPairings." ".$pairing[0].":".$pairing[1]; //} //error_log($strPairings); //error_log("-------"); $all[$currentRoundNr]=$pairings; } //foreach ($all as $roundNr => $pairings) { // foreach ($pairings as $pairing) //error_log("round_".$roundNr.":user_".$pairing[0]."!:user_".$pairing[1]."!"); //} //return null; return $all; // $all[$roundnr]= Array of Tuples ( UserId1, UserId2 ) } 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; } public static function getMatchResults($leagueId, $maxRoundId=-1) { $roundtipps = Tipp::getRoundTipps($maxRoundId); $roundtipps = $roundtipps["roundtipps"]; $matches = Match::getLeagueMatches($leagueId, $maxRoundId); $season = Season::getCurrent(); if ($maxRoundId==-1) { $maxRoundId = Round::getLastRoundId($season->id); } foreach($matches as $key => $match) { $user1_key = $match->round_id."_".$match->user1_id; $user2_key = $match->round_id."_".$match->user2_id; if (Round::isCompleted($match->round_id)) { $user1_score = $roundtipps[$user1_key]["rankscore"]; $user2_score = $roundtipps[$user2_key]["rankscore"]; $matches[$key]->user1_rankscore = $user1_score; $matches[$key]->user2_rankscore = $user2_score; //$matches[$key]->user1_hits = $roundtipps[$user1_key][] if ($user1_score > $user2_score) { $matches[$key]->user1_league_score = 3; $matches[$key]->user2_league_score = 0; } else if ($user1_score < $user2_score){ $matches[$key]->user1_league_score = 0; $matches[$key]->user2_league_score = 3; } else { $matches[$key]->user1_league_score = 1; $matches[$key]->user2_league_score = 1; } } else { // Untipped/future round $matches[$key]->user1_league_score = 0; $matches[$key]->user2_league_score = 0; $matches[$key]->user1_rankscore = -1; $matches[$key]->user2_rankscore = -1; } } return $matches; } public static function getMatchResultsForUser($userId, $maxRoundId=-1) { $season = Season::getCurrent(); $leagues = League::where('season_id','=',$season->id)->get(); $matchList=array(); foreach($leagues as $league) { $member=LeagueMember::where('league_id','=',$league->id)->where('user_id','=',$userId)->first(); if ($member) { // $userId ist member dieser League $matchResults = Match::getMatchResults($league->id,$maxRoundId); foreach($matchResults as $result) { if ($result->user1_id == $userId || $result->user2_id==$userId) { array_push($matchList, $result); } } } } return $matchList; } }