tippspiel/pfosten/app/Http/Controllers/Leagues/LeagueController.php
2017-09-11 10:38:57 +02:00

147 lines
No EOL
5.2 KiB
PHP

<?php
namespace App\Http\Controllers\Leagues;
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
{
public function index($season_id) {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$season = Season::where('id','=',$season_id)->first();
if (!$season) return redirect("/seasons");
$start_round_name="Einstellen";
$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
// No leagues: Create Qualification League or Standard League System (including matches)
// necessary input: start_round(_id), round_count
// Delete all leagues & matches
//
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");
}
}