tippspiel/pfosten/app/leagues/League.php

243 lines
8.4 KiB
PHP
Raw Normal View History

2017-09-09 08:35:39 +02:00
<?php
2017-09-22 17:40:48 +02:00
namespace App\Leagues;
2017-09-09 08:35:39 +02:00
use Illuminate\Database\Eloquent\Model;
2017-09-22 17:40:48 +02:00
use App\Season;
2017-09-30 13:14:46 +02:00
use App\Round;
2017-10-11 11:14:23 +02:00
use App\User;
2017-09-22 17:40:48 +02:00
use DB;
2017-09-11 10:38:57 +02:00
use Log;
2017-09-09 08:35:39 +02:00
class League extends Model
{
2017-09-22 17:40:48 +02:00
const RANK_LEAGUE1 = 1;
const RANK_LEAGUE2 = 2;
const RANK_LEAGUE3 = 3;
const RANK_QUALIFICATION = 4;
2017-09-09 08:35:39 +02:00
protected $table ='leagues';
protected $fillable = [
2017-09-11 10:38:57 +02:00
'id','season_id','is_qualification','rank','subrank','start_round_id','round_count','name'
2017-09-09 08:35:39 +02:00
];
//
2017-09-11 10:38:57 +02:00
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) {
2017-09-22 17:40:48 +02:00
$members = DB::table('leagues_members')->where('league_id','=',$league_id)->get();
return sizeof($members);
}
2017-09-22 17:40:48 +02:00
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;
}
2017-09-30 13:14:46 +02:00
public static function cmpScore($score1, $score2) {
return ($score1[1] < $score2[1]);
}
2017-10-11 11:14:23 +02:00
public static function getRanking($rank, $seasonId=-1) {
2017-09-30 13:14:46 +02:00
$season = Season::getCurrent();
2017-10-11 11:14:23 +02:00
if ($seasonId > -1) { $season=Season::where('id','=',$seasonId)->first(); };
2017-09-30 13:14:46 +02:00
$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;
2017-10-11 11:14:23 +02:00
$tpl[1]=0; // league_score
2017-09-30 13:14:46 +02:00
$tpl[2]=0; // goals
$tpl[3]=0; // countergoals
$memberScores[$memberId] = $tpl;
}
$matches=Match::getMatchResults($league->id,-1);
foreach ($matches as $match) {
2017-10-02 10:49:15 +02:00
if (Round::isCompleted($match->round_id)) {
$memberScores[$match->user1_id][1] += $match->user1_league_score;
$memberScores[$match->user1_id][2] += $match->user1_rankscore; // goals
$memberScores[$match->user1_id][3] += $match->user2_rankscore; // countergoals
2017-10-02 10:49:15 +02:00
$memberScores[$match->user2_id][1] += $match->user2_league_score;
$memberScores[$match->user2_id][2] += $match->user2_rankscore; // goals
$memberScores[$match->user2_id][3] += $match->user1_rankscore; // countergoals
2017-10-02 10:49:15 +02:00
}
2017-09-30 13:14:46 +02:00
}
usort($memberScores, array('App\Leagues\League','cmpScore'));
$leagueScores[$league->id]=$memberScores;
}
return $leagueScores;
}
2017-10-11 11:14:23 +02:00
2017-10-16 11:19:57 +02:00
// Für Auf/Abstiegsberechnung - wird auf previous Season angewendet.
public static function getUnifiedRanking($rank, $seasonId=-1) {
$leagueScores = League::getRanking($rank, $seasonId);
$unifiedScores = array();
foreach($leagueScores as $leagueId => $singleLeagueScores) {
foreach ($singleLeagueScores as $placeNr => $singleLeagueScore) {
$userId=$singleLeagueScores[$placeNr][0];
$unifiedScores[$userId]=$singleLeagueScores[$placeNr];
}
}
usort($unifiedScores,array('App\Leagues\League','cmpScore'));
return $unifiedScores;
}
2017-10-11 11:14:23 +02:00
// Aktuelle Liga-Zuordnung auf Basis des Rangs in der letzten Liga
2017-10-16 11:19:57 +02:00
public static function setLeagueForPlayers()
2017-10-11 11:14:23 +02:00
{
2017-10-16 11:19:57 +02:00
$previousSeason = Season::getPrevious();
2017-10-11 11:14:23 +02:00
$currentSeason = Season::getCurrent();
2017-10-16 11:19:57 +02:00
if (!$previousSeason) {
2017-10-11 11:14:23 +02:00
dd("No last season handling implemented. Please fix/implement.");
//TODO: Implement a qualification round
2017-10-16 11:19:57 +02:00
return;
2017-10-11 11:14:23 +02:00
}
$users = User::getActives();
2017-10-16 11:19:57 +02:00
$usersLastSeason = User::getActives($previousSeason->id);
2017-10-11 11:14:23 +02:00
2017-10-16 11:19:57 +02:00
$league1 = League::getUnifiedRanking(1, $previousSeason->id);
$league2 = League::getUnifiedRanking( 2, $previousSeason->id);
$league3 = League::getUnifiedRanking( 3, $previousSeason->id);
//$lastLeagues = League::where('season_id','=',$previousSeason->id)->get();
2017-10-11 11:14:23 +02:00
foreach ($users as $key => $user) {
$found = false;
foreach ($usersLastSeason as $userLastSeason) {
if ($userLastSeason->id == $user->id) $found = true;
}
if (!$found) {
$users[$key]['rank']=League::RANK_LEAGUE3; // In der letzten Season unter den Actives nicht gefunden
} else {
2017-10-16 11:19:57 +02:00
2017-10-11 11:14:23 +02:00
// League1, 1-16: RANK_LEAGUE1
// League1, 17-22: RANK_LEAGUE2
// League2, 1-6: RANK_LEAGUE1
// League2, 7-16: RANK_LEAGUE2
// League2, 17-22: RANK_LEAGUE3
// League3, best6: RANK_LEAGUE2
// League3, 7-end: RANK_LEAGUE3
2017-10-16 11:19:57 +02:00
//
foreach($league1 as $index => $entry) {
$userId = $entry[0];
$score = $entry[1];
$goals = $entry[2];
$cGoals = $entry[3];
$leagueId = -1;
if ($user->id == $userId) {
$rank=-1;
if ($index<16) {
$rank=1;
$leagueId=$league1->id;
} else {
$rank=2;
$leagueId=$league2->id;
}
$leagueMember = LeagueMember::create([
'user_id' => $userId,
'league_id' => $leagueId
]);
$leagueMember->save();
}
}
foreach($league2 as $index => $entry) {
$userId = $entry[0];
$score = $entry[1];
$goals = $entry[2];
$cGoals = $entry[3];
$leagueId = -1;
if ($user->id == $userId) {
$rank=-1;
$leagueId=$league1->id;
if ($index<6) {
$rank=1;
} elseif ($index<16) {
$rank=2;
$leagueId=$league2->id;
} else {
$rank=3;
$leagueId=$league3->id;
}
$leagueMember = LeagueMember::create([
'user_id' => $userId,
'league_id' => $leagueId
]);
$leagueMember->save();
}
}
foreach($league3 as $index => $entry) {
$userId = $entry[0];
$score = $entry[1];
$goals = $entry[2];
$cGoals = $entry[3];
if ($user->id == $userId) {
$rank=-1;
if ($index<6) {
$rank=2;
$leagueId=$league2->id;
} else {
$rank=3;
$leagueId=$league3->id;
}
$leagueMember = LeagueMember::create([
'user_id' => $userId,
'league_id' => $leagueId
]);
$leagueMember->save();
}
}
2017-10-11 11:14:23 +02:00
}
}
}
2017-09-09 08:35:39 +02:00
}