2020-06-09 09:57:00 +02:00
< ? php
namespace App\Leagues ;
use Illuminate\Database\Eloquent\Model ;
use App\Season ;
use App\Round ;
use App\User ;
use App\Leagues\LeagueMember ;
use DB ;
use Log ;
use App\MyClasses ;
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 ;
2020-09-09 16:55:52 +02:00
if ( $member_count <= $max_members_per_league ) return 1 ;
2020-06-09 09:57:00 +02:00
$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 ;
}
2022-06-12 00:22:19 +02:00
public static function getIds () {
$season = Season :: getCurrent ();
$leagues = League :: where ( 'season_id' , '=' , $season -> id ) -> get ();
$leagueIds = array ();
foreach ( $leagues as $league ) {
$leagueIds [] = $league -> id ; // push id onto array
}
return $leagueIds ;
}
2020-06-09 09:57:00 +02:00
public static function getStartRoundId ( $leagueId )
{
$league = League :: where ( 'id' , '=' , $leagueId ) -> first ();
return $league -> start_round_id ;
}
public static function cmpScore ( $score1 , $score2 ) {
// [0] = user-id
// [1] = league_score
// [4] = rank
$_score1 = $score1 [ 1 ];
$goals1 = $score1 [ 2 ];
$cGoals1 = $score1 [ 3 ];
$rank1 = - 1 ;
if ( array_key_exists ( 4 , $score1 )) { $rank1 = $score1 [ 4 ]; }
$_score2 = $score2 [ 1 ];
$goals2 = $score2 [ 2 ];
$cGoals2 = $score2 [ 3 ];
$rank2 = - 1 ;
if ( array_key_exists ( 4 , $score2 )) { $rank2 = $score2 [ 4 ]; }
$diff1 = $goals1 - $cGoals1 ;
$diff2 = $goals2 - $cGoals2 ;
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 ;
if ( $_score1 == $_score2 ) {
if ( $diff1 == $diff2 ) {
if ( $goals1 == $goals2 ) {
return $cGoals1 > $cGoals2 ;
}
return ( $goals1 < $goals2 );
}
return ( $diff1 < $diff2 );
}
return ( $_score1 < $_score2 );
}
public static function getRanking ( $rank , $seasonId =- 1 ) {
$season = Season :: getCurrent ();
if ( $seasonId > - 1 ) { $season = Season :: where ( 'id' , '=' , $seasonId ) -> first (); };
$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 ;
$tpl [ 1 ] = 0 ; // league_score
$tpl [ 2 ] = 0 ; // goals
$tpl [ 3 ] = 0 ; // countergoals
$memberScores [ $memberId ] = $tpl ;
}
$lastLockedRoundId = Round :: getLastLockedRoundId ();
2020-09-09 16:55:52 +02:00
//Log::info("LASTLOCKKEDROUNDID=".$lastLockedRoundId);
2020-06-09 09:57:00 +02:00
$matches = Match :: getMatchResults ( $league -> id , - 1 );
foreach ( $matches as $match ) {
if ( Round :: isCompleted ( $match -> round_id ) || $match -> round_id == $lastLockedRoundId ) {
$memberScores [ $match -> user1_id ][ 1 ] += $match -> user1_league_score ;
2020-09-09 16:55:52 +02:00
//error_log("+++ ".$match->user1_id." +".$match->user1_league_score);
2020-06-09 09:57:00 +02:00
$memberScores [ $match -> user1_id ][ 2 ] += $match -> user1_rankscore ; // goals
$memberScores [ $match -> user1_id ][ 3 ] += $match -> user2_rankscore ; // countergoals
$memberScores [ $match -> user2_id ][ 1 ] += $match -> user2_league_score ;
2020-09-09 16:55:52 +02:00
//error_log("+++ ".$match->user2_id." +".$match->user2_league_score);
2020-06-09 09:57:00 +02:00
$memberScores [ $match -> user2_id ][ 2 ] += $match -> user2_rankscore ; // goals
$memberScores [ $match -> user2_id ][ 3 ] += $match -> user1_rankscore ; // countergoals
}
}
usort ( $memberScores , array ( 'App\Leagues\League' , 'cmpScore' ));
$leagueScores [ $league -> id ] = $memberScores ;
2020-09-09 16:55:52 +02:00
//error_log("League::getRanking leagueid / userid / score");
2020-06-09 09:57:00 +02:00
foreach ( $memberScores as $entry ) {
2020-09-09 16:55:52 +02:00
//error_log($league->id." / ".$entry[0]." / ".$entry[1]);
2020-06-09 09:57:00 +02:00
}
2020-09-09 16:55:52 +02:00
//error_log("---");
2020-06-09 09:57:00 +02:00
}
return $leagueScores ;
}
// Für Auf/Abstiegsberechnung - wird auf previous Season angewendet.
// 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)
public static function getUnifiedRanking ( $rank , $seasonId =- 1 ) {
//Log::info("getUnified Ranking [last season], rank=".$rank." seasonId=".$seasonId);
$leagueScores = League :: getRanking ( $rank , $seasonId );
$unifiedScores = array ();
foreach ( $leagueScores as $leagueId => $singleLeagueScores ) {
foreach ( $singleLeagueScores as $placeNr => $singleLeagueScore ) {
$userId = $singleLeagueScores [ $placeNr ][ 0 ];
$scoreWithPos = $singleLeagueScores [ $placeNr ];
$scoreWithPos [ 4 ] = $placeNr + 1 ;
$unifiedScores [ $userId ] = $scoreWithPos ;
}
}
usort ( $unifiedScores , array ( 'App\Leagues\League' , 'cmpScore' ));
//Log::info("getUnifiedRanking result, $rank=".$rank.", SeasonId=".$seasonId);
//Log::info($unifiedScores);
return $unifiedScores ;
}
// Nur für Auf und Abstiegsberechnung.
// Schreibt den zukünftigen Rank auf Index4
public static function getTotalRankingOverall ( $seasonId =- 1 ) {
$a = new MyClasses\TimeLogger ( " League::getTotalRankingOverall (seasonId= " . $seasonId . " ) " );
Log :: info ( $a -> getStartMessage ());
$userNames = User :: getNames ();
$totalRanking = array ();
$hasRank4 = false ;
foreach ([ 1 , 2 , 3 , 4 ] as $rank ) {
foreach ( League :: getUnifiedRanking ( $rank , $seasonId ) as $rankEntry ) {
// Tuple: [0] = userId, [1] = league_score [2] = goals, [3] = counter_goals [4] = LigaPos
$tpl = array ();
$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)
if ( $rank == 4 ) { $hasRank4 = true ;}
// only add to list if user has paid for season
if ( User :: is_season_paid ( $tpl [ 0 ]) == " Ja " ) {
array_push ( $totalRanking , $tpl );
}
}
}
usort ( $totalRanking , array ( 'App\Leagues\League' , 'cmpScore' ));
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 ;
}
// kein Rank4 ==> Ligaspiel ==> Auf & Abstieg wird angewendet.
if ( $hasRank4 ) {
Log :: info ( " Rank4 gesetzt, Quali, keine Vorsaison, Auf/Abstieg wird übersprungen " );
} else {
Log :: info ( " Rank4 nicht gesetzt, keine Quali, Auf/Abstieg wird berechnet " );
// 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
$totalRanking [ 16 ][ 4 ] = 2 ;
$uid = $totalRanking [ 16 ][ 0 ];
Log :: info ( " Abstieg in Liga 2: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 17 ][ 4 ] = 2 ;
$uid = $totalRanking [ 17 ][ 0 ];
Log :: info ( " Abstieg in Liga 2: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 18 ][ 4 ] = 2 ;
$uid = $totalRanking [ 18 ][ 0 ];
Log :: info ( " Abstieg in Liga 2: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 19 ][ 4 ] = 2 ;
$uid = $totalRanking [ 19 ][ 0 ];
2020-09-09 16:55:52 +02:00
//Log::info("Abstieg in Liga 2: ["+$uid+"] ".$userNames[$uid]);
2020-06-09 09:57:00 +02:00
$totalRanking [ 20 ][ 4 ] = 2 ;
$uid = $totalRanking [ 20 ][ 0 ];
Log :: info ( " Abstieg in Liga 2: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 21 ][ 4 ] = 2 ;
$uid = $totalRanking [ 21 ][ 0 ];
Log :: info ( " Abstieg in Liga 2: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 22 ][ 4 ] = 1 ;
$uid = $totalRanking [ 22 ][ 0 ];
Log :: info ( " Aufstieg in Liga 1: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 23 ][ 4 ] = 1 ;
$uid = $totalRanking [ 23 ][ 0 ];
Log :: info ( " Aufstieg in Liga 1: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 24 ][ 4 ] = 1 ;
$uid = $totalRanking [ 24 ][ 0 ];
Log :: info ( " Aufstieg in Liga 1: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 25 ][ 4 ] = 1 ;
$uid = $totalRanking [ 25 ][ 0 ];
Log :: info ( " Aufstieg in Liga 1: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 26 ][ 4 ] = 1 ;
$uid = $totalRanking [ 26 ][ 0 ];
Log :: info ( " Aufstieg in Liga 1: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 27 ][ 4 ] = 1 ;
$uid = $totalRanking [ 27 ][ 0 ];
Log :: info ( " Aufstieg in Liga 1: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 38 ][ 4 ] = 3 ;
$uid = $totalRanking [ 38 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 39 ][ 4 ] = 3 ;
$uid = $totalRanking [ 39 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 40 ][ 4 ] = 3 ;
$uid = $totalRanking [ 40 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 41 ][ 4 ] = 3 ;
$uid = $totalRanking [ 41 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 42 ][ 4 ] = 3 ;
$uid = $totalRanking [ 42 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 43 ][ 4 ] = 3 ;
$uid = $totalRanking [ 43 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 44 ][ 4 ] = 2 ;
$uid = $totalRanking [ 44 ][ 0 ];
Log :: info ( " Aufstieg in Liga 2: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 45 ][ 4 ] = 2 ;
$uid = $totalRanking [ 45 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 46 ][ 4 ] = 2 ;
$uid = $totalRanking [ 46 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 47 ][ 4 ] = 2 ;
$uid = $totalRanking [ 47 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 48 ][ 4 ] = 2 ;
$uid = $totalRanking [ 48 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
$totalRanking [ 49 ][ 4 ] = 2 ;
$uid = $totalRanking [ 49 ][ 0 ];
Log :: info ( " Abstieg in Liga 3: [ " . $uid . " ] " . $userNames [ $uid ]);
usort ( $totalRanking , array ( 'App\Leagues\League' , 'cmpScore' ));
}
Log :: info ( $a -> getEndMessage ());
//error_log($a->getEndMessage());
return $totalRanking ;
}
public static function getNewUsers ( $seasonId =- 1 ) {
$users = User :: getActives ( $seasonId );
$previousSeason = Season :: getPrevious ( $seasonId );
$old_users = User :: getActives ( $previousSeason -> id );
// Structure of unified list entry
/*
$tpl = array ();
$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)
*/
$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 , 0 , 0 , 0 , 3 , 0 ];
array_push ( $new_users , $entry );
}
}
}
return $new_users ;
}
// 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 ) {
Log :: info ( " Leagues \ League::createLeagues, start_round_id= " . $start_round_id . " , round_count= " . $round_count );
$currentSeason = Season :: getCurrent ();
$previousSeason = Season :: getPrevious ();
$userNames = User :: getNames ();
// 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 );
$neededLeagues3 = League :: neededLeagues ( $active_member_count , $round_count );
Log :: info ( " Benötigte Liga-Gruppen: " . $neededLeagues3 );
$league_ids = array ();
//TODO: Wenn es keine Vorsaison gibt -> Qualifikation
//TODO: Ansonsten: Standard-Ligen (1,2,3) erzeugen. Liga 1 und 2 sind auf 22 Spieler festgelegt.
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.
Log :: info ( " League::createLeagues => No previous season detected. " );
for ( $league_nr = 1 ; $league_nr <= $neededLeagues3 ; $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 ();
2020-09-09 16:55:52 +02:00
Log :: info ( " Saving user_id " . $user -> id );
2020-06-09 09:57:00 +02:00
$league_index += 1 ;
if ( sizeof ( $league_ids ) == $league_index ) { $league_index = 0 ; }
}
} else {
// 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
Log :: info ( " League::createLeagues => Previous season detected. Creating Leagues 1, 2 and 3 " );
// Erzeugung der Ligen
$createdLeagues = array ();
$neededLeagues3 = 0 ;
$league_size1 = min ( 22 , $active_member_count );
2020-09-09 16:55:52 +02:00
Log :: info ( " Creating League 'Spielerliga 1' " );
2020-06-09 09:57:00 +02:00
$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
]);
array_push ( $createdLeagues , $league1 );
$league_size2 = min ( 22 , $active_member_count - $league_size1 );
if ( $league_size2 > 0 ) {
2020-09-09 16:55:52 +02:00
Log :: info ( " Creating League 'Spielerliga 2' " );
2020-06-09 09:57:00 +02:00
$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
]);
array_push ( $createdLeagues , $league2 );
$league_size3 = $active_member_count - $league_size1 - $league_size2 ;
2020-09-09 16:55:52 +02:00
Log :: info ( " Size of league 3 = " . $league_size3 );
2020-06-09 09:57:00 +02:00
// Make neededLeagues3 visible outside of scope
$neededLeagues3 =- 1 ;
if ( $league_size3 > 0 ) {
$neededLeagues3 = League :: neededLeagues ( $league_size3 , $round_count );
2020-09-09 16:55:52 +02:00
Log :: info ( " Needed 3-Leagues for league_size= " . $league_size3 . " and round_count= " . $round_count . " is: " . $neededLeagues3 );
2020-06-09 09:57:00 +02:00
for ( $league_nr = 1 ; $league_nr <= $neededLeagues3 ; $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 ();
array_push ( $createdLeagues , $league );
}
}
Log :: info ( " League::createLeagues Creating unified List from previous season (i call League::getTotalRankingOverall) " );
$unifiedList = League :: getTotalRankingOverall ( $previousSeason -> id ); // WICHTIG !! getTotalRankingOverall implentiert Auf- und Abstieg !!
Log : info ( " League::createLeagues 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 ];
$leagueScore = $listEntry [ 1 ];
if ( User :: is_season_paid ( $userId ) && ! User :: isDeleted ( $userId )) {
$count += 1 ;
array_push ( $unifiedList2 , $listEntry );
Log :: info ( $count . " .) [ " . $userId . " ] " . $userNames [ $userId ] . " " . $leagueScore . " Punkte " );
} else {
Log :: info ( $count . " .) [ " . $userId . " ] " . $userNames [ $userId ] . " [DELETED] " );
}
}
$unifiedList = $unifiedList2 ;
// Jetzt neue Spieler hinzufügen, die letzte Saison noch nicht dabei waren, und deshalb in getTotalRankingOverall nicht enthalten sind.
Log :: info ( " League::createLeagues Adding new users not within previous season " );
foreach ( League :: getNewUsers () as $entry ) {
array_push ( $unifiedList , $entry );
$userId = $entry [ 0 ];
Log :: info ( " adding new user not in previous season: ( [ " . $userId . " ] " . $userNames [ $userId ] . " ) " );
}
// Wenn es für Liga 3 mehr als eine Gruppe gibt, dann die Reihenfolge der Members randomisieren.
if ( $neededLeagues3 > 1 ) {
$numberOfSwaps = 50 ;
// Structure of unified list entry
/*
$tpl = array ();
$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)
*/
Log :: info ( " League 3 entries before shuffling: " );
foreach ( $unifiedList as $entry ) {
if ( $entry [ 4 ] == 3 ) {
Log :: info ( " ( " . $entry [ 0 ] . " ) " . $userNames [ $entry [ 0 ]]);
}
}
$league3entries = array ();
foreach ( $unifiedList as $pos => $entry ) {
if ( $entry [ 4 ] == 3 ) {
array_push ( $league3entries , $entry );
}
}
shuffle ( $league3entries );
$pos = 0 ;
foreach ( $unifiedList as $key => $entry ) {
if ( $entry [ 4 ] == 3 ) {
$unifiedList [ $key ] = $league3entries [ $pos ];
$pos += 1 ;
}
}
Log :: info ( " League 3 entries after shuffling: " );
foreach ( $unifiedList as $entry ) {
if ( $entry [ 4 ] == 3 ) { Log :: info ( " ( " . $entry [ 0 ] . " ) " . $userNames [ $entry [ 0 ]]); }
}
}
$currentCount = 1 ;
$nr = 1 ;
$leagueNr = 1 ;
$neededSize3 = 0 ;
$subLeague = 1 ; // for rank 3 leagues only
$leagueCount = 1 ;
$leagueIndex = 0 ;
Log :: info ( " League::createLeagues 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 ), $neededLeagues3 ); // $neededLeagues = needed Groups for rank 3 leagues
if ( ( $active_member_count - 44 ) % 2 ) {
$neededSize3 += 1 ;
}
Log :: info ( " League::createLeagues Needed min/switch size for rank 3 leagues: " . $neededSize3 );
}
if ( $nr > 45 && $leagueCount > $neededSize3 ) {
$subLeague += 1 ;
$leagueCount = 1 ;
$leagueIndex += 1 ;
}
Log :: info ( " League::createLeagues " . " # " . $leagueCount . " Liga " . $leagueNr . " - " . $subLeague . " ) " . $nr . " " . $entry [ 0 ] . " ( " . $userNames [ $entry [ 0 ]] . " ) " );
$league = $createdLeagues [ $leagueIndex ];
$leagueMember = LeagueMember :: create ([
'user_id' => $entry [ 0 ],
'league_id' => $league -> id
]);
2020-09-09 16:55:52 +02:00
Log :: info ( " ( " . $entry [ 0 ] . " ) " . $userNames [ $entry [ 0 ]] . " => leagueIndex= " . $leagueIndex );
2020-06-09 09:57:00 +02:00
$leagueMember -> save ();
$leagueCount += 1 ;
$nr ++ ;
}
}
}
}
// Aktuelle Liga-Zuordnung auf Basis des Rangs in der letzten Liga
// 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.
public static function setLeagueForPlayers ()
/*
* setLeagueForPlayers__NEW_QUALIFICATION case1
* setLeagueForPlayers__FROM_QUALIFICATION case2
* setLeagueForPlayers__FROM_LEAGUES case3
*/
{
$previousSeason = Season :: getPrevious ();
$currentSeason = Season :: getCurrent ();
$users = User :: getActives ();
if ( ! $previousSeason ) {
// 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*/
}
// 2 Möglichkeiten:
// 1.) previousSeason hat rank=4 => Qualification Round
// 2.) sonst aus .
$usersLastSeason = User :: getActives ( $previousSeason -> id );
$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();
2020-09-09 16:55:52 +02:00
// WAS MACHT DIESER CODE ?!
2020-06-09 09:57:00 +02:00
/*
foreach ( $users as $index => $user ) {
$found = false ;
foreach ( $usersLastSeason as $userLastSeason ) {
if ( $userLastSeason -> id == $user -> id ) $found = true ;
}
if ( ! $found ) {
$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 ();
} else {
// 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
//
//alte Ligen durchsuchen und entsprechend einordnen
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 ();
}
}
}
}
2020-09-09 16:55:52 +02:00
*/
// WAS MACHT DIESER CODE? --ENDE
2020-06-09 09:57:00 +02:00
}
}