Start Algorithm for Match creation (incremental version)

This commit is contained in:
Torsten 2017-09-13 10:12:36 +02:00
parent 7c9d5eef1e
commit 82e60df35f
6 changed files with 8619 additions and 3 deletions

View file

@ -14,6 +14,7 @@ use App\Round;
use App\User;
use App\leagues\League;
use App\leagues\LeagueMember;
use Log;
use DB;
@ -80,7 +81,7 @@ class LeagueController extends Controller
$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.
$league_ids=array();
if ($league_mode=="Qualifikation") {
for ($league_nr=1; $league_nr<=$neededLeagues; $league_nr+=1) {
$league = League::create([
@ -93,10 +94,30 @@ class LeagueController extends Controller
'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;
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: Achtel-, Viertel-, Halb-, Finale require additional leagues; Same with Pokal, so createMissingLeagues method will be needed in the future
$league_size1= min(22,$active_member_count);
$league1 = League::create([
'is_qualification' => false,

View file

@ -407,7 +407,8 @@ class Tipp extends Model
}
}
if (sizeof($usersWithHighestDayScore) > 6) {
usort($usersWithHighestDayScore, 'cmpDayWin');
//usort($usersWithHighestDayScore, 'cmpDayWin');
usort($usersWithHighestDayScore, array('App\Tipp','cmpDayWin'));
}
$count = 0;
$cutOffScore = 0;

View file

@ -175,6 +175,7 @@ class User extends Authenticatable
return true;
}
// aktiv = hat die Saison bezahlt.
public static function getActives() {
$currentSeason=Season::getCurrent();
@ -199,10 +200,26 @@ class User extends Authenticatable
return $userlist;
}
public static function softDelete($user_id) {
$user = User::where('id','=',$user_id)->first();
if ($user) { $user->deleted = Carbon\Carbon::now();}
$user->save();
}
// Selektiert nur die "Heimspiele"
public static function getMatches($user_id, $league_id) {
$matches = DB::table('leagues_matches')->where('league_id', '=', $league_id)->where('user1_id','=',$user_id)->get();
return $matches;
}
public static function hasMatched($user1_id, $user2_id, $league_id) {
$matches = User::getMatches($user1_id, $league_id);
foreach($matches as $match) {
if ($match->user1_id == $user1_id && $match->user2_id == $user2_id) return true;
}
return false;
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace App\Leagues;
use Illuminate\Database\Eloquent\Model;
class LeagueMember extends Model
{
protected $table ='leagues_members';
protected $fillable = [
'id','user_id','league_id'
];
//
}

View file

@ -3,6 +3,8 @@
namespace App\leagues;
use Illuminate\Database\Eloquent\Model;
use App\leagues\League;
use App\User;
class Match extends Model
{
@ -10,5 +12,77 @@ class Match extends Model
protected $fillable = [
'id','league_id','user1_id','user2_idsubrank','start_round_id','name'
];
//
public static function createMissingMatchups() {
$currentSeason = Season::getCurrent();
$leagues = League::where('season_id','=',$currentSeason->id);
// TODO: Differentiate Matchup selection by League Type
// Liga 1
// Liga 2
// Ligen 3 1,2,3,...n
// Liga 4 (Qualifikation)
// Liga 5: Champions League
// Liga 7: Achtelfinale
// Liga 8: Viertelfinale
// Liga 9: Halbfinale
// Liga 10: Finale
foreach($leagues as $league) {
// Qualifikation
if ($league->rank == 4) {
$matches = array();
// Bereits bestehende Matches laden
DB::table('leagues_matches')->where('league_id','=',$league->id)->get();
$users = User::getActives();
foreach($users as $user1) {
$counter_users=getActives();
foreach($counter_users as $user2) {
if ($user1->id == $user2->id) continue;
if (!User::hasMatched($user1->id, $user2->id, $league->id)) {
// add Match
array_push($matches, [$user1, $user2]);
}
}
}
Log::info("Open Matches:");
foreach ($matches as $match) {
// Oh je, jetzt noch die Runde festlegen
/*
*
* Bestimmen, in welcher Runde das Spiel stattfinden soll.
* Es darf kein Heim & Rückspiel in der gleichen Runde sein,
* d.h. ein Spieler kommt in einer Runde nur einmal zu einem Match
*
* Stehen keine Runden mehr zur Verfügung (der Standardfall)
* muss die Vergabe beendet werden. Neue Runden kommen dann
* später im Verlauf der Halbsaison ins System.
*
*/
// SOOOO
// VIEL
// SPASS !!!
}
}
}
}
}