tippspiel/pfosten/app/leagues/League.php
2017-10-02 10:49:15 +02:00

108 lines
3.3 KiB
PHP

<?php
namespace App\Leagues;
use Illuminate\Database\Eloquent\Model;
use App\Season;
use App\Round;
use DB;
use Log;
class League extends Model
{
const RANK_LEAGUE1 = 1;
const RANK_LEAGUE2 = 2;
const RANK_LEAGUE3 = 3;
const RANK_QUALIFICATION = 4;
protected $table ='leagues';
protected $fillable = [
'id','season_id','is_qualification','rank','subrank','start_round_id','round_count','name'
];
//
public static function neededLeagues($member_count, $round_count) {
$max_members_per_league=22;
if ( $round_count < $max_members_per_league-1 ) { $max_members_per_league = $round_count+1; }
if ($member_count <= $max_members_per_league) return $member_count;
$neededLeagues = intdiv($member_count, $max_members_per_league);
if ( $member_count - $max_members_per_league * $neededLeagues ) {
$neededLeagues+=1;
}
return $neededLeagues;
}
public static function getMemberCount($league_id) {
$members = DB::table('leagues_members')->where('league_id','=',$league_id)->get();
return sizeof($members);
}
public static function getNames() {
$season = Season::getCurrent();
$leagues = League::where('season_id','=',$season->id)->orderBy('rank')->orderBy('name')->get();
$leagueNames = array();
foreach($leagues as $league) {
$leagueNames[$league->id] = $league->name;
}
return $leagueNames;
}
public static function getStartRoundId($leagueId)
{
$league = League::where('id','=',$leagueId)->first();
return $league->start_round_id;
}
public static function cmpScore($score1, $score2) {
return ($score1[1] < $score2[1]);
}
public static function getRanking($rank) {
$season = Season::getCurrent();
$leagues = League::where('rank','=',$rank)->where('season_id','=', $season->id)->orderBy('id')->get();
error_log("LeagueCount:".sizeof($leagues));
$leagueScores=array();
foreach($leagues as $league) {
$memberIds = LeagueMember::getMembers($league->id);
$memberScores =array();
foreach($memberIds as $memberId) {
$tpl=array();
$tpl[0]=$memberId;
$tpl[1]=0;
$tpl[2]=0; // goals
$tpl[3]=0; // countergoals
$memberScores[$memberId] = $tpl;
}
$matches=Match::getMatchResults($league->id,-1);
foreach ($matches as $match) {
if (Round::isCompleted($match->round_id)) {
$memberScores[$match->user1_id][1] += $match->user1_league_score;
$memberScores[$match->user1_id][2] += $match->user1_rankscore;
$memberScores[$match->user1_id][3] += $match->user2_rankscore;
$memberScores[$match->user2_id][1] += $match->user2_league_score;
$memberScores[$match->user2_id][2] += $match->user2_rankscore;
$memberScores[$match->user2_id][3] += $match->user1_rankscore;
}
}
usort($memberScores, array('App\Leagues\League','cmpScore'));
$leagueScores[$league->id]=$memberScores;
}
return $leagueScores;
}
}