tippspiel/pfosten/app/leagues/Match.php

216 lines
7.4 KiB
PHP

<?php
namespace App\Leagues;
use Illuminate\Database\Eloquent\Model;
use App\Leagues\League;
use App\Leagues\LeagueMember;
use App\User;
use App\Round;
use App\Season;
use App\Tipp;
use Log;
use DB;
class Match extends Model
{
protected $table ='leagues_matches';
protected $fillable = [
'id','league_id','round_id','user1_id','user2_id'
];
// https://en.wikipedia.org/wiki/Round-robin_tournament
public static function createMatchups()
{ Log::info("Match::Creating matchups()");
$currentSeason = Season::getCurrent();
$leagues = League::where('season_id', '=', $currentSeason->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);
foreach ($rounds as $round) {
$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();
}
$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::getResults($leagueId, $maxRoundId);
}
return $matches;
}
public static function getPairings($leagueId, $roundNr) {
$members = LeagueMember::getMembers($leagueId);
shuffle($members);
Log::info("Number of members:".sizeof($members));
$pairingCount = Match::getNumberOfPairingsPerRound($leagueId);
$roundRobin=array();
foreach($members as $userId) {
array_push($roundRobin, $userId);
}
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("Members: ".sizeof($roundRobin));
foreach ($roundRobin as $index => $userId) {
error_log($index." => ".$userId);
}
$idx=0;
$slot=0;
$pairings=array();
foreach ($roundRobin as $index => $userId) {
$idx=$index;
if ($index >= $pairingCount){ $idx -= $pairingCount; $slot=1;}
$pairings[$idx][$slot]=$userId;
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;
}
public static function getMatchResults($leagueId, $maxRoundId=-1) {
$roundtipps = Tipp::getRoundTipps($maxRoundId);
$roundtipps = $roundtipps["roundtipps"];
$matches = Match::getLeagueMatches($leagueId, $maxRoundId);
if ($maxRoundId==-1) {$maxRoundId=Round::getLastCompleteRoundId();}
foreach($matches as $key => $match) {
if (!Round::isCompleted($match->round_id)) continue;
$user1_key = $match->round_id."_".$match->user1_id;
$user2_key = $match->round_id."_".$match->user2_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;
}
}
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;
}
}