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
2017-11-02 16:41:07 +01:00
use App\Leagues\LeagueMember ;
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 ;
}
2017-09-18 08:28:42 +02:00
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 ();
2017-09-18 08:28:42 +02:00
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 ;
}
2017-09-26 07:57:39 +02:00
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 ) {
2017-10-25 10:42:05 +02:00
// [0] = user-id
2017-11-02 16:41:07 +01:00
// [4] = rank
$_score1 = $score1 [ 1 ];
2017-10-25 10:42:05 +02:00
$goals1 = $score1 [ 2 ];
$cGoals1 = $score1 [ 3 ];
2017-11-02 16:41:07 +01:00
$rank1 = - 1 ;
if ( array_key_exists ( 4 , $score1 )) { $rank1 = $score1 [ 4 ]; }
$_score2 = $score2 [ 1 ];
2017-10-25 10:42:05 +02:00
$goals2 = $score2 [ 2 ];
$cGoals2 = $score2 [ 3 ];
2017-11-02 16:41:07 +01:00
$rank2 = - 1 ;
if ( array_key_exists ( 4 , $score2 )) { $rank2 = $score2 [ 4 ]; }
2017-10-25 10:42:05 +02:00
$diff1 = $goals1 - $cGoals1 ;
$diff2 = $goals2 - $cGoals2 ;
2017-11-07 16:59:58 +01:00
if ( $rank1 > $rank2 ) return true ;
if ( $rank1 < $rank2 ) return false ;
if ( $_score1 == $_score2 ) {
if ( $diff1 == $diff2 ) {
if ( $goals1 == $goals2 ) {
return $cGoals1 > $cGoals2 ;
}
return ( $goals1 < $goals2 );
}
return ( $diff1 < $diff2 );
}
return ( $_score1 < $_score2 );
}
// Funktioniert nur auf bereits sortierter Liste, nur für Selektion Gruppe 3 Auf & Abstieg
public static function cmpScoreForPos ( $score1 , $score2 ) {
// [0] = user-id
// [4] = rank
$_score1 = $score1 [ 1 ];
$goals1 = $score1 [ 2 ];
$cGoals1 = $score1 [ 3 ];
$rank1 = $score1 [ 4 ];
$pos1 = $score1 [ 5 ];
$_score2 = $score2 [ 1 ];
$goals2 = $score2 [ 2 ];
$cGoals2 = $score2 [ 3 ];
$rank2 = $score2 [ 4 ];
$pos2 = $score2 [ 5 ];
$diff1 = $goals1 - $cGoals1 ;
$diff2 = $goals2 - $cGoals2 ;
if ( $rank1 > $rank2 ) return true ;
if ( $rank1 < $rank2 ) return false ;
if ( $pos1 > $pos2 ) return true ;
if ( $pos1 < $pos2 ) return false ;
2017-11-02 16:41:07 +01:00
2017-10-25 10:42:05 +02:00
if ( $_score1 == $_score2 ) {
if ( $diff1 == $diff2 ) {
if ( $goals1 == $goals2 ) {
return $cGoals1 > $cGoals2 ;
}
return ( $goals1 < $goals2 );
}
return ( $diff1 < $diff2 );
}
return ( $_score1 < $_score2 );
2017-09-30 13:14:46 +02:00
}
2017-10-25 10:42:05 +02:00
2017-11-07 16:59:58 +01:00
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 ();
$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 ;
2017-10-12 11:40:25 +02:00
$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 ;
2017-10-12 11:40:25 +02:00
$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.
2017-11-30 11:07:27 +01:00
// getUnifiedRanking bezieht sich (nur) auf Gruppen-Übergreifende Listung (also alle Untergruppen)
// Rank = -1 bedeutet über alle Ranks (benötigt für Auf- und Abstieg über alle Ligen)
2017-10-12 11:40:25 +02:00
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 ];
2017-11-07 16:59:58 +01:00
$scoreWithPos = $singleLeagueScores [ $placeNr ];
$scoreWithPos [ 4 ] = $placeNr + 1 ;
$unifiedScores [ $userId ] = $scoreWithPos ;
2017-10-12 11:40:25 +02:00
}
}
usort ( $unifiedScores , array ( 'App\Leagues\League' , 'cmpScore' ));
return $unifiedScores ;
}
2017-11-07 16:59:58 +01:00
// Nur für Auf und Abstiegsberechnung.
// Schreibt den zukünftigen Rank auf Index4
2017-11-02 16:41:07 +01:00
public static function getTotalRankingOverall ( $seasonId =- 1 ) {
$totalRanking = array ();
2017-11-03 14:20:10 +01:00
$hasRank4 = false ;
2017-11-02 16:41:07 +01:00
foreach ([ 1 , 2 , 3 , 4 ] as $rank ) {
foreach ( League :: getUnifiedRanking ( $rank , $seasonId ) as $rankEntry ) {
2017-11-07 16:59:58 +01:00
// Tuple: [0] = userId, [1] = league_score [2] = goals, [3] = counter_goals [4] = LigaPos
2017-11-02 16:41:07 +01:00
$tpl = array ();
2017-11-07 16:59:58 +01:00
$tpl [ 0 ] = $rankEntry [ 0 ]; // userId
$tpl [ 1 ] = $rankEntry [ 1 ]; // league_score
$tpl [ 2 ] = $rankEntry [ 2 ]; // goals
$tpl [ 3 ] = $rankEntry [ 3 ]; // countergoals
$tpl [ 4 ] = $rank ; // league_rank (1,2,3, 4=quali)
$tpl [ 5 ] = $rankEntry [ 4 ]; // league_pos (for multiple leagues per rank, usually ranks 3 and 4)
2017-11-03 14:20:10 +01:00
if ( $rank == 4 ) { $hasRank4 = true ;}
2017-11-30 11:07:27 +01:00
// only add to list if user has paid for season
if ( User :: is_season_paid ( $tpl [ 0 ]) == " Ja " ) {
array_push ( $totalRanking , $tpl );
}
2017-11-02 16:41:07 +01:00
}
}
2017-11-03 14:20:10 +01:00
2017-11-02 16:41:07 +01:00
usort ( $totalRanking , array ( 'App\Leagues\League' , 'cmpScore' ));
2017-11-07 16:59:58 +01:00
usort ( $totalRanking , array ( 'App\Leagues\League' , 'cmpScoreForPos' )); // Dieser Sort funktioniert nur auf bereits sortierter Liste !! Nur für GruppenRank=3
// Neuen Rang auf Index 4 schreiben
foreach ( $totalRanking as $index => $entry ) {
$rank = 3 ;
if ( $index < 44 ) { $rank = 2 ; }
if ( $index < 22 ) { $rank = 1 ; }
$totalRanking [ $index ][ 4 ] = $rank ;
}
2017-11-03 14:20:10 +01:00
// kein Rank4 ==> Ligaspiel ==> Auf & Abstieg wird angewendet.
if ( ! $hasRank4 ) {
// Liga 1/2: Swap rank 17-22 <> 23-28 ==> index 16-21 to rank=2, index 22-27 to rank=1
// Liga 2/3: Swap rank 39-44 <> 45-50 ==> index 38-43 to rank=3, index 44-49 to rank=2
2017-11-07 16:59:58 +01:00
2017-11-03 14:20:10 +01:00
$totalRanking [ 16 ][ 4 ] = 2 ;
$totalRanking [ 17 ][ 4 ] = 2 ;
$totalRanking [ 18 ][ 4 ] = 2 ;
$totalRanking [ 19 ][ 4 ] = 2 ;
$totalRanking [ 20 ][ 4 ] = 2 ;
$totalRanking [ 21 ][ 4 ] = 2 ;
$totalRanking [ 22 ][ 4 ] = 1 ;
$totalRanking [ 23 ][ 4 ] = 1 ;
$totalRanking [ 24 ][ 4 ] = 1 ;
$totalRanking [ 25 ][ 4 ] = 1 ;
$totalRanking [ 26 ][ 4 ] = 1 ;
$totalRanking [ 27 ][ 4 ] = 1 ;
$totalRanking [ 38 ][ 4 ] = 3 ;
$totalRanking [ 39 ][ 4 ] = 3 ;
$totalRanking [ 40 ][ 4 ] = 3 ;
$totalRanking [ 41 ][ 4 ] = 3 ;
$totalRanking [ 42 ][ 4 ] = 3 ;
$totalRanking [ 43 ][ 4 ] = 3 ;
$totalRanking [ 44 ][ 4 ] = 2 ;
$totalRanking [ 45 ][ 4 ] = 2 ;
$totalRanking [ 46 ][ 4 ] = 2 ;
$totalRanking [ 47 ][ 4 ] = 2 ;
$totalRanking [ 48 ][ 4 ] = 2 ;
$totalRanking [ 49 ][ 4 ] = 2 ;
usort ( $totalRanking , array ( 'App\Leagues\League' , 'cmpScore' ));
}
2017-11-02 16:41:07 +01:00
return $totalRanking ;
}
2017-11-13 17:22:28 +01:00
public static function getNewUsers ( $seasonId =- 1 ) {
$users = User :: getActives ( $seasonId );
$previousSeason = Season :: getPrevious ( $seasonId );
$old_users = User :: getActives ( $previousSeason -> id );
$new_users = array ();
foreach ( $users as $user ) {
$exists = 0 ;
foreach ( $old_users as $old_user ) {
if ( $old_user -> id == $user -> id ) { $exists = 1 ;}
}
if ( ! $exists ) {
if ( User :: payment_date ( $user -> id , $seasonId )) {
$entry = [ $user -> id ];
array_push ( $new_users , $entry );
}
}
}
return $new_users ;
}
2017-10-25 10:42:05 +02:00
// ausfaktoriert aus LeaguesController::createLeagues()
// TODO: $round_count ist nur im Quali-Modus relevant und steuert, wieviele Quali-Gruppen benötigt werden.
// TODO: Sobald es Liga1 und Liga2 gibt, wird der round_count immer 21 sein, da Liga1 und Liga2 auf
// TODO: 22 Mitspieler festgelegt sind.
public static function createLeagues ( $start_round_id , $round_count ) {
2017-11-30 11:07:27 +01:00
Log :: info ( " Leagues \ League::createLeagues, start_round_id= " . $start_round_id . " , round_count= " . $round_count );
2017-10-25 10:42:05 +02:00
$currentSeason = Season :: getCurrent ();
2017-10-29 07:13:52 +01:00
$previousSeason = Season :: getPrevious ();
2017-10-25 10:42:05 +02:00
2017-11-30 11:07:27 +01:00
$userNames = User :: getNames ();
2017-10-25 10:42:05 +02:00
// Eventuell vorhandene Ligen loeschen
$leagues = League :: where ( 'season_id' , '=' , $currentSeason -> id ) -> get ();
if ( $leagues ) {
foreach ( $leagues as $league ) {
DB :: table ( 'leagues_matches' ) -> where ( 'league_id' , '=' , $league -> id ) -> delete ();
DB :: table ( 'leagues_members' ) -> where ( 'league_id' , '=' , $league -> id ) -> delete ();
DB :: table ( 'leagues' ) -> where ( 'id' , '=' , $league -> id ) -> delete ();
}
}
$active_member_count = sizeof ( User :: getActives ());
Log :: info ( " Aktive Spieler: " . $active_member_count );
$neededLeagues = League :: neededLeagues ( $active_member_count , $round_count );
2017-11-02 16:41:07 +01:00
Log :: info ( " Benötigte Liga-Gruppen: " . $neededLeagues );
2017-10-25 10:42:05 +02:00
$league_ids = array ();
//TODO: Wenn es keine Vorsaison gibt -> Qualifikation
2017-10-29 07:13:52 +01:00
//TODO: Ansonsten: Standard-Ligen (1,2,3) erzeugen. Liga 1 und 2 sind auf 22 Spieler festgelegt.
2017-10-25 10:42:05 +02:00
2017-10-29 07:13:52 +01:00
if ( ! $previousSeason ) {
// Alle Spieler werden gleichmässig auf Qualifikationsgruppen aufgeteilt
// Dieser Modus wird gewählt, wenn es keine Vorsaison gibt, es also
// die erste Saison im System ist.
2017-11-02 16:41:07 +01:00
Log :: info ( " League::createLeagues => No previous season detected. " );
2017-10-25 10:42:05 +02:00
for ( $league_nr = 1 ; $league_nr <= $neededLeagues ; $league_nr += 1 ) {
$league = League :: create ([
'is_qualification' => true ,
'name' => 'Qualifikationsgruppe ' . $league_nr ,
'rank' => '4' ,
'subrank' => $league_nr ,
'round_count' => $round_count ,
'season_id' => $currentSeason -> id ,
'start_round_id' => $start_round_id
]);
$league -> save ();
$league_ids [ $league_nr - 1 ] = $league -> id ;
}
// Distribute members
$users = User :: getActives ();
shuffle ( $users );
$league_index = 0 ;
Log :: info ( " Distributing Members between groups " );
while ( sizeof ( $users ) > 0 ) {
$user = array_pop ( $users );
$leagueMember = LeagueMember :: create ([
'user_id' => $user -> id ,
'league_id' => $league_ids [ $league_index ]
]);
$leagueMember -> save ();
2017-11-13 17:22:28 +01:00
error_log ( " Saving user_id " . $user -> id );
2017-10-25 10:42:05 +02:00
$league_index += 1 ;
if ( sizeof ( $league_ids ) == $league_index ) { $league_index = 0 ; }
}
} else {
2017-10-29 07:13:52 +01:00
// Es gibt eine Vorsaison.
// - 1.) Ligen erzeugen
// - 2.) unified Liste bauen
// - 3.) Wenn Vorsaison keine Quali war, dann Auf & Abstieg umsetzen ( unfified Liste als Basis )
// - 4.) Member den Ligen zuordnen
2017-11-30 11:07:27 +01:00
Log :: info ( " League::createLeagues => Previous season detected. Creating Leagues 1, 2 and 3 " );
2017-10-25 10:42:05 +02:00
2017-10-29 07:13:52 +01:00
// Erzeugung der Ligen
2017-11-02 16:41:07 +01:00
$createdLeagues = array ();
$neededLeagues = 0 ;
2017-10-25 10:42:05 +02:00
$league_size1 = min ( 22 , $active_member_count );
$league1 = League :: create ([
'is_qualification' => false ,
'name' => 'Spielerliga 1' ,
'rank' => '1' ,
'subrank' => '1' ,
'round_count' => $round_count ,
'season_id' => $currentSeason -> id ,
'start_round_id' => $start_round_id
]);
2017-11-02 16:41:07 +01:00
array_push ( $createdLeagues , $league1 );
2017-10-25 10:42:05 +02:00
$league_size2 = min ( 22 , $active_member_count - $league_size1 );
if ( $league_size2 > 0 ) {
$league2 = League :: create ([
'is_qualification' => false ,
'name' => 'Spielerliga 2' ,
'rank' => '2' ,
'subrank' => '1' ,
'round_count' => $round_count ,
'season_id' => $currentSeason -> id ,
'start_round_id' => $start_round_id
]);
2017-11-02 16:41:07 +01:00
array_push ( $createdLeagues , $league2 );
2017-10-25 10:42:05 +02:00
$league_size3 = $active_member_count - $league_size1 - $league_size2 ;
if ( $league_size3 > 0 ) {
$neededLeagues = League :: neededLeagues ( $league_size3 , $round_count );
for ( $league_nr = 1 ; $league_nr <= $neededLeagues ; $league_nr += 1 ) {
$league = League :: create ([
'is_qualification' => false ,
'name' => 'Spielerliga 3, Gruppe ' . $league_nr ,
'rank' => '3' ,
'subrank' => $league_nr ,
'round_count' => $round_count ,
'season_id' => $currentSeason -> id ,
'start_round_id' => $start_round_id
]);
$league -> save ();
2017-11-02 16:41:07 +01:00
array_push ( $createdLeagues , $league );
2017-10-25 10:42:05 +02:00
}
}
2017-11-30 11:07:27 +01:00
Log :: info ( " Creating unified List from previous season " );
2017-11-03 14:20:10 +01:00
$unifiedList = League :: getTotalRankingOverall ( $previousSeason -> id ); // WICHTIG !! getTotalRankingOverall implentiert Auf- und Abstieg !!
2017-11-13 17:22:28 +01:00
2017-11-30 11:07:27 +01:00
Log : info ( " Unified List: " );
$userNames = User :: getNames ();
$unifiedList2 = array (); // We will only transfer users over to here that have paid the current season and are not deleted.
$count = 0 ;
foreach ( $unifiedList as $listEntry ) {
$userId = $listEntry [ 0 ];
if ( User :: is_season_paid ( $userId ) && ! User :: isDeleted ( $userId )) {
$count += 1 ;
array_push ( $unifiedList2 , $listEntry );
Log :: info ( $count . " .) [ " . $userId . " ] " . $userNames [ $userId ]);
} else {
Log :: info ( $count . " .) [ " . $userId . " ] " . $userNames [ $userId ] . " [DELETED] " );
}
}
$unifiedList = $unifiedList2 ;
2017-11-13 17:22:28 +01:00
// Jetzt neue Spieler hinzufügen, die letzte Saison noch nicht dabei waren, und deshalb in getTotalRankingOverall nicht enthalten sind.
2017-11-30 11:07:27 +01:00
Log :: info ( " Adding new users not within previous season " );
2017-11-13 17:22:28 +01:00
foreach ( League :: getNewUsers () as $entry ) {
array_push ( $unifiedList , $entry );
2017-11-30 11:07:27 +01:00
$userId = $entry [ 0 ];
Log :: info ( " ( [ " . $userId . " ] " . $userNames [ $userId ] . " ) " );
2017-11-13 17:22:28 +01:00
}
2017-11-02 16:41:07 +01:00
$currentCount = 1 ;
$nr = 1 ;
$leagueNr = 1 ;
$neededSize3 = 0 ;
$subLeague = 1 ; // for rank 3 leagues only
$leagueCount = 1 ;
$leagueIndex = 0 ;
Log :: info ( " unified list size= " . sizeof ( $unifiedList ));
foreach ( $unifiedList as $entry ) {
if ( $nr == 23 ) { $leagueNr = 2 ; $leagueCount = 1 ; $leagueIndex = 1 ; }
if ( $nr == 45 ) {
$leagueNr = 3 ; $leagueCount = 1 ; $leagueIndex = 2 ;
$neededSize3 = intdiv (( $active_member_count - 44 ), $neededLeagues ); // $neededLeagues = needed Groups for rank 3 leagues
2017-11-13 17:22:28 +01:00
if ( ( $active_member_count - 44 ) % 2 ) {
$neededSize3 += 1 ;
}
2017-11-02 16:41:07 +01:00
Log :: info ( " Needed min/switch size for rank 3 leagues: " . $neededSize3 );
}
if ( $nr > 45 && $leagueCount > $neededSize3 ) {
$subLeague += 1 ;
$leagueCount = 1 ;
$leagueIndex += 1 ;
}
2017-11-30 11:07:27 +01:00
Log :: info ( " # " . $leagueCount . " Liga " . $leagueNr . " - " . $subLeague . " ) " . $nr . " " . $entry [ 0 ] . " ( " . $userNames [ $entry [ 0 ]] . " ) " );
2017-11-02 16:41:07 +01:00
$league = $createdLeagues [ $leagueIndex ];
$leagueMember = LeagueMember :: create ([
'user_id' => $entry [ 0 ],
'league_id' => $league -> id
]);
$leagueMember -> save ();
$leagueCount += 1 ;
$nr ++ ;
}
2017-10-25 10:42:05 +02:00
}
}
}
2017-10-12 11:40:25 +02:00
2017-10-11 11:14:23 +02:00
// Aktuelle Liga-Zuordnung auf Basis des Rangs in der letzten Liga
2017-10-25 10:42:05 +02:00
// Oder des Rangs in der Qualifikation
// oder erstellung des Quali Rangs wenn es keine previous Season gibt.
// Liga-Zugehörigkeit festlegen. Setzt voraus, dass die Ligen bereits erzeugt wurden.
2017-10-16 11:19:57 +02:00
public static function setLeagueForPlayers ()
2017-10-25 10:42:05 +02:00
/*
* setLeagueForPlayers__NEW_QUALIFICATION case1
* setLeagueForPlayers__FROM_QUALIFICATION case2
* setLeagueForPlayers__FROM_LEAGUES case3
*/
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-25 10:42:05 +02:00
$users = User :: getActives ();
2017-10-11 11:14:23 +02:00
2017-10-16 11:19:57 +02:00
if ( ! $previousSeason ) {
2017-10-25 10:42:05 +02:00
// case 1: first season ever, no previous season exists, qualification (league-rank=4)
} elseif ( Season :: hasQualification ( $previousSeason -> id )){
// case 2: previous season was qualification season (rank 4)
} else { /*case 3 - default: determine league-ranks 1,2,3 from previous Season rank verwenden*/
2017-10-11 11:14:23 +02:00
}
2017-10-25 10:42:05 +02:00
// 2 Möglichkeiten:
// 1.) previousSeason hat rank=4 => Qualification Round
// 2.) sonst aus .
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
2017-10-25 10:42:05 +02:00
/*
foreach ( $users as $index => $user ) {
2017-10-11 11:14:23 +02:00
$found = false ;
foreach ( $usersLastSeason as $userLastSeason ) {
if ( $userLastSeason -> id == $user -> id ) $found = true ;
}
if ( ! $found ) {
2017-10-25 10:42:05 +02:00
$users [ $index ][ 'rank' ] = League :: RANK_LEAGUE3 ; // In der letzten Season unter den Actives nicht gefunden
$leagueMember = LeagueMember :: create ([
'user_id' => $user -> id ,
'league_id' => $league3 -> id
]);
$leagueMember -> save ();
2017-10-11 11:14:23 +02:00
} 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
//
2017-10-25 10:42:05 +02:00
//alte Ligen durchsuchen und entsprechend einordnen
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-10-25 10:42:05 +02:00
*/
2017-10-11 11:14:23 +02:00
}
2017-10-25 10:42:05 +02:00
2017-09-09 08:35:39 +02:00
}