League Creation

This commit is contained in:
Torsten 2017-09-11 10:38:57 +02:00
parent 96101cb05d
commit 7c9d5eef1e
11 changed files with 160 additions and 46 deletions

View file

@ -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");
}
}

View file

@ -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'));
}
/*

View file

@ -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();

View file

@ -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;
}
}

View file

@ -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();
});

View file

@ -13,7 +13,7 @@
<div>
<div class="dropdown" id="dropdown1">
<button id="btn_startround" onclick="toggleDropdown('dropdown_startround'); return false;" class="dropbtn">{{$startRound['name']}}</button>
<button id="btn_startround" onclick="toggleDropdown('dropdown_startround'); return false;" class="dropbtn">{{$start_round_name}}</button>
<div id="dropdown_startround" class="dropdown-content">
@foreach($rounds as $round)
<a onclick="setStartRound('{{$round->name}}',{{$round->id}});return false;" >{{ $round->name }}</a>
@ -23,12 +23,12 @@
<h3>Anzahl Runden:</h3>
<input id="input_round_count"></input>
<input id="input_round_count" value="{{$round_count}}"></input>
<h3>Liga-Modus</h3>
<div class="dropdown">
<button id="btn_leaguemode" onclick="toggleDropdown('dropdown_leaguemode'); return false;" class="dropbtn">Modus einstellen</button>
<button id="btn_leaguemode" onclick="toggleDropdown('dropdown_leaguemode'); return false;" class="dropbtn">{{$league_mode}}</button>
<div id="dropdown_leaguemode" class="dropdown-content">
<a onclick="selectLeagueMode('Qualifikation'); return false;" href="">Qualifikation</a>
<a onclick="selectLeagueMode('Liga-Spiel'); return false;" href="">Liga-Spiel</a>
@ -37,37 +37,32 @@
<p>&nbsp;</p>
<div >
<button onclick="createLeague(); return false;" class="btn btn-primary">Liga neu erzeugen</button>
</div>
</div>
</div>
</section>
<script>
global_start_round_id={!! $start_round_id !!};
global_league_mode="{!! $league_mode !!}";
console.log(global_start_round_id);
console.log(global_league_mode);
function createLeague() {
console.log("create League");
//alert($global_selected_round_id);
var round_count=document.getElementById("input_round_count").value;
var lonk = '/league_settings/create_league?start_round_id='+global_start_round_id+"&round_count="+round_count+"&mode="+global_league_mode;
//window.location=link;
alert(lonk);
console.log(lonk);
var link = '/league_settings/create_league/start_round_id='+global_start_round_id+"/round_count="+round_count+"/league_mode="+global_league_mode;
//alert(link);
window.location=link;
}
function selectLeagueMode($modename) {

View file

@ -7,6 +7,8 @@
<div class="lead">Aktiv: <strong>{{ $currentSeason->name }}</strong></div>
@endif
<div><a href="/seasons/create" class="btn btn-primary">Neue Saison anlegen</a></div>
@if ($currentSeason)
<div>
<ul>
@ -15,11 +17,10 @@
@endforeach
</ul>
</div>
@endif
<div><a href="/seasons/create" class="btn btn-primary">Neue Saison anlegen</a></div>
@if ($currentSeason)
<div>
@ -141,6 +142,11 @@
</h4>
<h3>Liga:</h3>
@if($currentSeason)
<div>
<a class="btn btn-primary" href="/league_settings/season={{$currentSeason->id}}">Liga bearbeiten</a>
</div>
@endif
<div class="form-group" >
<h4>
<label style="float:left; width:200px;">Platz 1:</label>

View file

@ -4,7 +4,7 @@
<section class="jumbotron text-center container">
<!-- <div class="container"> -->
<div class="lead col-md-8" >aktive Spieler: {{ $total_user_count }} , davon bezahlt: {{$active_user_count}}</div>
<div class="lead col-md-8" >registrierte Spieler: {{ $total_user_count }} , davon bezahlt: {{$active_user_count}}</div>
<p>&nbsp;</p>

View file

@ -79,4 +79,5 @@ Route::get('/users/send_reset_links_from_settings','UsersController@send_reset_l
Route::get('/home', 'UsersController@main')->name('home');
Route::get('/league_settings/{season_id}','Leagues\LeagueController@index');
Route::get('/league_settings/season={season_id}','Leagues\LeagueController@index');
Route::get('/league_settings/create_league/start_round_id={start_round_id}/round_count={round_count}/league_mode={league_mode}','Leagues\LeagueController@createLeagues');

View file

@ -6,7 +6,6 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'AddLeagueInfoToSeasons' => $baseDir . '/database/migrations/2017_09_08_095815_add_league_info_to_seasons.php',
'AddReminderfieldToUsers' => $baseDir . '/database/migrations/2017_08_28_110358_add_reminderfield_to_users.php',
'App\\Console\\Kernel' => $baseDir . '/app/Console/Kernel.php',
'App\\Exceptions\\Handler' => $baseDir . '/app/Exceptions/Handler.php',

View file

@ -278,7 +278,6 @@ class ComposerStaticInita19fb0695e1b61b4feeb3cc3fbdd2feb
);
public static $classMap = array (
'AddLeagueInfoToSeasons' => __DIR__ . '/../..' . '/database/migrations/2017_09_08_095815_add_league_info_to_seasons.php',
'AddReminderfieldToUsers' => __DIR__ . '/../..' . '/database/migrations/2017_08_28_110358_add_reminderfield_to_users.php',
'App\\Console\\Kernel' => __DIR__ . '/../..' . '/app/Console/Kernel.php',
'App\\Exceptions\\Handler' => __DIR__ . '/../..' . '/app/Exceptions/Handler.php',