This commit is contained in:
Torsten 2017-10-25 10:42:05 +02:00
parent ed0c445bfa
commit 30f6157606
15 changed files with 248 additions and 169 deletions

View file

@ -65,114 +65,9 @@ class LeaguesController extends Controller
return view('leagues.index', compact('season','rounds','start_round_id','start_round_name','league_mode','round_count'));
}
public function createLeagues($start_round_id, $round_count, $league_mode) {
$currentSeason=Season::getCurrent();
// 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);
Log::info("Benötigte Quali-Gruppen: ".$neededLeagues);
$league_ids=array();
Log::info("League Mode:".$league_mode);
if ($league_mode=="Qualifikation") {
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();
$league_index+=1;
if (sizeof($league_ids)==$league_index) { $league_index=0; }
}
} else {
// ToDo: Branch Qualifikation/Liga entfällt in Zukunft, da Quali der Fallback für den Fall ist, dass keine vorherige Saison existiert.
// Pokal und Play-Off findet hier nicht statt, da diese Wettbewerbe die Matches abgängig von existierenden Vorrunden-Ergebnissen erzeugen,
// und daher zwangsläufig inkrementell sein müssen.
$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
]);
$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
]);
$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();
}
}
// TODO: LeagueMembers zu den Leagues hinzufügen, abhängig vom zuzuordnenden Platz
}
}
public function createLeagues($start_round_id, $round_count) {
League::createLeagues($start_round_id, $round_count);
Match::createMatchups();
return redirect("/seasons");

View file

@ -18,24 +18,17 @@ class SeasonsController extends Controller
{
public function index() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$seasons = DB::table('seasons')->get();
$currentSeason=Season::getCurrent();
$quotas = Quota::getQuotas($currentSeason->id);
if (!$quotas) {
Quota::createDefaultQuota($currentSeason->id);
$quotas = Quota::getQuotas($currentSeason->id);
}
$active_user_count = sizeof( User::getActives() );
$quotas = Quota::getQuotas($currentSeason->id);
$payouts = Quota::getPayouts($currentSeason->id);
$currentSeasonId=-1;
if ($currentSeason) {
$quotas = Quota::getQuotas($currentSeason->id);
$payouts = Quota::getPayouts($currentSeason->id);
}
return view('seasons.index', compact('seasons','currentSeason','active_user_count','quotas','payouts'));
}

View file

@ -52,8 +52,10 @@ class UsersController extends Controller
$nextRoundName="";
$nextRound = Round::getFirstTippableRound();
$nextRoundName = "";
$nextRoundLockTime = "";
$diffToLock ="";
$hourDiffToLock = 999;
if ($nextRound) {
$nextRoundName = $nextRound->name;
$nextRoundLockTime = $nextRound->getLockTime();
@ -98,6 +100,7 @@ class UsersController extends Controller
$activeUsers = User::getActives();
$currentSeason=Season::getCurrent();
if (!$currentSeason) { return redirect('/'); }
$active_user_count = sizeof( $activeUsers );
$total_user_count = sizeof( DB::table('payments')->where('season_id',$currentSeason->id)->get() );
@ -121,8 +124,6 @@ class UsersController extends Controller
}
}
return view('users.index', compact('users','active_user_count','total_user_count'));
}
/*
@ -201,8 +202,7 @@ class UsersController extends Controller
if($max_round_id==-1) {
$max_round_id = Round::getLastLockedRoundId();
if ($max_round_id==-1) {
$sql="select DATE_FORMAT(date_sub(min(games.kickoff), interval 1 hour),'%d.%m.%Y %H:%i') as start_date from rounds,games where rounds.season_id=".$season->id." and games.round_id=rounds.id;";
dd($sql);
$sql="select DATE_FORMAT(date_sub(min(games.kickoff), interval 1 hour),'%d.%m.%Y %H:%i') as start_date from rounds,games where rounds.season_id=".$seasonId." and games.round_id=rounds.id;";
$date = DB::select($sql);
if (count($date)>0) {

View file

@ -59,8 +59,10 @@ class Round extends Model
}
public static function getFirstIncompleteRound() {
$season_id = Season::getCurrent()->id;
$rounds = Round::where('season_id','=',$season_id)->orderBy('id')->get();
$season = Season::getCurrent();
if (!$season) return -1;
$rounds = Round::where('season_id','=',$season->id)->orderBy('id')->get();
foreach($rounds as $round) {
if (!Round::isCompleted($round->id)) return $round->id;
}
@ -69,6 +71,8 @@ class Round extends Model
public static function getNextRoundId($roundId) {
$currentSeason = Season::getCurrent();
if (!$currentSeason) return -1;
$rounds = Round::where('season_id','=',$currentSeason->id)->where('id','>=',$roundId)->get();
foreach($rounds as $round) {
if ($round->id == $roundId) { continue; }
@ -79,6 +83,7 @@ class Round extends Model
public static function getLastLockedRoundId() {
$season = Season::getCurrent();
if (!$season) return -1;
$rounds=Round::where('season_id','=',$season->id)->orderBy('id')->get();
$lastLockedRoundId=-1;
foreach ($rounds as $round) {
@ -89,8 +94,15 @@ class Round extends Model
return $lastLockedRoundId;
}
public static function getFirstTippableRoundId() {
$round = Round::getFirstTippableRound();
if (!$round) return -1;
return $round->id;
}
public static function getFirstTippableRound() {
$season = Season::getCurrent();
if (!$season) return null;
$rounds=Round::where('season_id','=',$season->id)->orderBy('id')->get();
$locked = false;
foreach ($rounds as $round) {

View file

@ -272,7 +272,7 @@ class Tipp extends Model
$round_ids=array();
// Write sql result to roundtipps (score per round & user)
if ($max_round_id==-1) { $max_round_id=Round::getLastLockedRoundId(); }
if ($max_round_id==-1) { $max_round_id=Round::getLastCompleteRoundId(); }
foreach ($result as $entry) {
$round_id = $entry->round_id;
if ($max_round_id >-1 && $round_id>$max_round_id) break;

View file

@ -180,9 +180,10 @@ class User extends Authenticatable
// aktiv = hat die Saison bezahlt.
public static function getActives($seasonId=-1) {
$currentSeason=Season::getCurrent();
if ($seasonId != -1) { $currentSeason = Season::where('id','=',$seasonId); }
if (!$currentSeason) return array();
$users = User::all();
$payments = Payment::where('season_id','=',$currentSeason->id)->get();

View file

@ -62,11 +62,33 @@ class League extends Model
}
public static function cmpScore($score1, $score2) {
return ($score1[1] < $score2[1]);
// [0] = user-id
$_score1 = $score1[1];
$goals1 = $score1[2];
$cGoals1 = $score1[3];
$_score2 = $score2[1];
$goals2 = $score2[2];
$cGoals2 = $score2[3];
$diff1 = $goals1-$cGoals1;
$diff2 = $goals2-$cGoals2;
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(); };
@ -122,21 +144,155 @@ class League extends Model
return $unifiedScores;
}
// 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) {
$currentSeason=Season::getCurrent();
// 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);
Log::info("Benötigte Quali-Gruppen: ".$neededLeagues);
$league_ids=array();
//TODO: Den Leaguemode kann die Methode selbst rausfinden.
//TODO: Wenn es keine Vorsaison gibt -> Qualifikation
//TODO: Ansonsten: Ligen erzeugen.
$league_mode="Qualifikation";
if ($league_mode=="Qualifikation") {
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();
$league_index+=1;
if (sizeof($league_ids)==$league_index) { $league_index=0; }
}
} else {
// ToDo: Branch Qualifikation/Liga entfällt in Zukunft, da Quali der Fallback für den Fall ist, dass keine vorherige Saison existiert.
// Pokal und Play-Off findet hier nicht statt, da diese Wettbewerbe die Matches abgängig von existierenden Vorrunden-Ergebnissen erzeugen,
// und daher zwangsläufig inkrementell sein müssen.
$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
]);
$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
]);
$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();
}
}
// TODO: LeagueMembers zu den Leagues hinzufügen, abhängig vom zuzuordnenden Platz
}
}
}
// 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) {
dd("No last season handling implemented. Please fix/implement.");
//TODO: Implement a qualification round
return;
// 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*/
}
$users = User::getActives();
// 2 Möglichkeiten:
// 1.) previousSeason hat rank=4 => Qualification Round
// 2.) sonst aus .
$usersLastSeason = User::getActives($previousSeason->id);
$league1 = League::getUnifiedRanking(1, $previousSeason->id);
@ -144,7 +300,14 @@ class League extends Model
$league3 = League::getUnifiedRanking( 3, $previousSeason->id);
//$lastLeagues = League::where('season_id','=',$previousSeason->id)->get();
foreach ($users as $key => $user) {
/*
foreach ($users as $index => $user) {
$found = false;
foreach ($usersLastSeason as $userLastSeason) {
@ -152,7 +315,12 @@ class League extends Model
}
if (!$found) {
$users[$key]['rank']=League::RANK_LEAGUE3; // In der letzten Season unter den Actives nicht gefunden
$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
@ -163,6 +331,8 @@ class League extends Model
// 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];
@ -238,5 +408,7 @@ class League extends Model
}
}
*/
}
}

View file

@ -23,7 +23,11 @@ class Match extends Model
// https://en.wikipedia.org/wiki/Round-robin_tournament
public static function createMatchups()
{ Log::info("Match::Creating matchups()");
{ // should be restricted to league ranks 1,2,3 and 4. All other leagues will have to use some
// incremental matchmaking system.
Log::info("Match::Creating matchups()");
$currentSeason = Season::getCurrent();
$leagues = League::where('season_id', '=', $currentSeason->id)->get();
foreach ($leagues as $league) {