diff --git a/pfosten/app/Http/Controllers/Leagues/LeagueController.php b/pfosten/app/Http/Controllers/Leagues/LeagueController.php index fa1663b4..d03de876 100644 --- a/pfosten/app/Http/Controllers/Leagues/LeagueController.php +++ b/pfosten/app/Http/Controllers/Leagues/LeagueController.php @@ -7,10 +7,15 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Http\Controllers\Leagues; + use Auth; use App\Season; use App\Round; +use App\User; + use App\leagues\League; +use Log; +use DB; class LeagueController extends Controller { @@ -20,23 +25,29 @@ class LeagueController extends Controller if (!Auth::user()->admin) { return redirect("/"); } $season = Season::where('id','=',$season_id)->first(); - $rounds = Round::where('season_id','=',$season_id)->orderBy('id')->get(); - - $league = League::where('season_id','=',$season_id)->first(); + if (!$season) return redirect("/seasons"); $start_round_name="Einstellen"; - $start_round_id=-1; - if ($league) { - if ($league->start_round_id) { - $startRound = Round::where('id','=',$league->start_round_id)->first(); - $start_round_name = $startRound->name; - $start_round_id = $startRound->id; - } - } - $startRound=array(); - $startRound['id'] = $start_round_id; - $startRound['name'] = $start_round_name; + $rounds = Round::where('season_id','=',$season_id)->orderBy('id')->get(); + if (!$rounds) return redirect("/seasons"); + + $round_count = 18; + $start_round_name="Einstellen"; + $start_round_id=$rounds[0]->id; + $league_mode="Einstellen"; + + $league = League::where('season_id','=',$season_id)->first(); + if ($league) { + $startround = Round::where('season_id', '=', $season_id)->orderBy('id')->first(); + $start_round_id = $startround->id; + + $startRound = Round::where('id','=',$league->start_round_id)->first(); + $start_round_name = $startRound->name; + $start_round_id = $startRound->id; + $round_count = $league->round_count; + if($league->is_qualification) { $league_mode="Qualifikation"; } else { $league_mode="Liga-Spiel"; } + } // Fetch existing leagues @@ -47,7 +58,90 @@ class LeagueController extends Controller // - - return view('leagues.index', compact('season','rounds','startRound')); + 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); + + // Todo: Erzeugen der Ligen. Beachten: Matchup-Generierung in separater Method, da inkrementell. + 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(); + + } + + } else { + $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 1', + 'rank' => '1', + '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(); + + } + } + } + } + + return redirect("/seasons"); + } +} \ No newline at end of file diff --git a/pfosten/app/Http/Controllers/UsersController.php b/pfosten/app/Http/Controllers/UsersController.php index 5ad9bd92..57f955bd 100755 --- a/pfosten/app/Http/Controllers/UsersController.php +++ b/pfosten/app/Http/Controllers/UsersController.php @@ -98,6 +98,7 @@ class UsersController extends Controller $activeUsers = User::getActives(); $currentSeason=Season::getCurrent(); + $active_user_count = sizeof( $activeUsers ); $total_user_count = sizeof( DB::table('payments')->where('season_id',$currentSeason->id)->get() ); @@ -120,6 +121,8 @@ class UsersController extends Controller } } + + return view('users.index', compact('users','active_user_count','total_user_count')); } /* diff --git a/pfosten/app/User.php b/pfosten/app/User.php index fcce8f59..74693444 100644 --- a/pfosten/app/User.php +++ b/pfosten/app/User.php @@ -110,6 +110,8 @@ class User extends Authenticatable public static function toDBDate($deDate) { + if (!$deDate) return ""; + $values = explode(" ", $deDate); $year = explode(".",$values[0])[2]; @@ -181,7 +183,9 @@ class User extends Authenticatable $payref = array(); foreach($payments as $payment) { - $payref[$payment->user_id]='ok'; + if ($payment->paid_date) { + $payref[$payment->user_id] = 'ok'; + } } $userlist=array(); diff --git a/pfosten/app/leagues/League.php b/pfosten/app/leagues/League.php index cb89e96d..a8dd98c3 100644 --- a/pfosten/app/leagues/League.php +++ b/pfosten/app/leagues/League.php @@ -3,12 +3,25 @@ namespace App\leagues; use Illuminate\Database\Eloquent\Model; +use Log; class League extends Model { protected $table ='leagues'; protected $fillable = [ - 'id','season_id','rank','subrank','start_round_id','name' + '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; + } } diff --git a/pfosten/database/migrations/2017_09_08_095721_create_leagues_table.php b/pfosten/database/migrations/2017_09_08_095721_create_leagues_table.php index c6ed15a7..6ba41ebf 100644 --- a/pfosten/database/migrations/2017_09_08_095721_create_leagues_table.php +++ b/pfosten/database/migrations/2017_09_08_095721_create_leagues_table.php @@ -21,7 +21,7 @@ class CreateLeaguesTable extends Migration $table->string('name'); $table->boolean('is_qualification'); $table->integer('start_round_id'); - $table->integer('round_count'); + $table->integer('round_count')->default(18); $table->timestamps(); }); diff --git a/pfosten/resources/views/leagues/index.blade.php b/pfosten/resources/views/leagues/index.blade.php index a79288ba..8511bd7e 100644 --- a/pfosten/resources/views/leagues/index.blade.php +++ b/pfosten/resources/views/leagues/index.blade.php @@ -13,7 +13,7 @@