tippspiel/pfosten/app/Http/Controllers/Leagues/LeaguesController.php

295 lines
10 KiB
PHP
Raw Normal View History

2017-09-09 08:35:39 +02:00
<?php
namespace App\Http\Controllers\Leagues;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Leagues;
2017-09-11 10:38:57 +02:00
2017-09-09 08:35:39 +02:00
use Auth;
use App\Season;
use App\Round;
2017-09-11 10:38:57 +02:00
use App\User;
2017-09-22 17:40:48 +02:00
use App\Leagues\League;
use App\Leagues\LeagueMember;
use App\Leagues\Match;
use Illuminate\Support\Facades\Input;
2017-09-11 10:38:57 +02:00
use Log;
use DB;
2017-09-09 08:35:39 +02:00
use App\MyClasses\TimeLogger;
2017-09-22 17:40:48 +02:00
class LeaguesController extends Controller
2017-09-09 08:35:39 +02:00
{
2017-09-22 17:40:48 +02:00
public function index() {
$a=new TimeLogger("LeaguesController.index");
Log::info($a->getStartMessage());
2017-09-22 17:40:48 +02:00
$season=Season::getCurrent();
2017-09-09 08:35:39 +02:00
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
2017-09-22 17:40:48 +02:00
$season = Season::where('id','=',$season->id)->first();
2017-09-11 10:38:57 +02:00
if (!$season) return redirect("/seasons");
2017-09-09 08:35:39 +02:00
2017-09-11 10:38:57 +02:00
$start_round_name="Einstellen";
2017-09-22 17:40:48 +02:00
$rounds = Round::where('season_id','=',$season->id)->orderBy('id')->get();
2017-09-11 10:38:57 +02:00
if (!$rounds) return redirect("/seasons");
2017-09-09 08:35:39 +02:00
2017-09-11 10:38:57 +02:00
$round_count = 18;
2017-09-09 08:35:39 +02:00
$start_round_name="Einstellen";
2017-09-11 10:38:57 +02:00
$start_round_id=$rounds[0]->id;
$league_mode="Einstellen";
2017-09-22 17:40:48 +02:00
$league = League::where('season_id','=',$season->id)->first();
2017-09-09 08:35:39 +02:00
if ($league) {
2017-09-22 17:40:48 +02:00
$startround = Round::where('season_id', '=', $season->id)->orderBy('id')->first();
2017-09-11 10:38:57 +02:00
$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"; }
2017-09-09 08:35:39 +02:00
}
// 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
//
Log::info($a->getEndMessage());
2017-09-11 10:38:57 +02:00
return view('leagues.index', compact('season','rounds','start_round_id','start_round_name','league_mode','round_count'));
}
2017-10-25 10:42:05 +02:00
public function createLeagues($start_round_id, $round_count) {
Log::info("CREATE_LEAGUES");
$a=new TimeLogger("LeaguesController::createLeagues()");
Log::info($a->getStartMessage());
2017-11-30 11:07:27 +01:00
$users=User::getActives();
if (sizeof($users)==0){
Log::info($a->getEndMessage());
return view("leagues.no_users");
}
2017-10-25 10:42:05 +02:00
League::createLeagues($start_round_id, $round_count);
2017-10-29 07:13:52 +01:00
Log::info("LeaguesController::createLeagues completed.");
2017-09-23 12:19:46 +02:00
Match::createMatchups();
2017-09-22 17:40:48 +02:00
Log::info($a->getEndMessage());
2017-09-11 10:38:57 +02:00
return redirect("/seasons");
2017-09-09 08:35:39 +02:00
}
2017-09-22 17:40:48 +02:00
2017-11-03 14:20:10 +01:00
public function showLeagueRounds() {
2017-09-22 17:40:48 +02:00
if (Auth::guest()) { return redirect("/"); }
$a=new TimeLogger("LeaguesController::showLeagueRounds()");
Log::info($a->getStartMessage());
2017-09-22 17:40:48 +02:00
$season=Season::getCurrent();
2017-10-02 10:49:15 +02:00
2017-09-22 17:40:48 +02:00
$roundNames = Round::getNames();
$leagueNames = League::getNames();
2017-11-10 15:32:54 +01:00
$leagues = League::where('season_id','=',$season->id)->where('rank','<=', 4)->orderBy('id')->get();
2017-09-22 17:40:48 +02:00
$matchesTotal = array();
foreach ($leagues as $league) {
$matches = Match::getMatchResults($league->id, -1);
2017-09-22 17:40:48 +02:00
foreach ($matches as $match) {
if (!key_exists($match->round_id, $matchesTotal)) {
$matchesTotal[$match->round_id]=array();
}
if (!key_exists($league->id, $matchesTotal[$match->round_id] )) {
$matchesTotal[$match->round_id][$league->id]=array();
2017-09-22 17:40:48 +02:00
}
$lg = $matchesTotal[$match->round_id][$league->id];
array_push($lg, $match);
$matchesTotal[$match->round_id][$league->id] = $lg;
2017-09-22 17:40:48 +02:00
}
2017-09-30 13:14:46 +02:00
}
Log::info($a->getEndMessage());
2017-09-23 12:19:46 +02:00
2017-09-22 17:40:48 +02:00
return view ('leagues.matches', compact('season', 'matchesTotal','leagues','leagueNames','roundNames'));
}
2022-06-03 11:58:26 +02:00
public static function showLeagueRanking() {
$a=new TimeLogger("LeaguesController::showLeagueRanking()");
Log::info($a->getStartMessage());
$season = Season::getCurrent();
//$leagues = array_reverse(League::getRanking($rank));
2017-09-30 13:14:46 +02:00
$leagueNames = League::getNames();
$userNames = User::getNames();
2017-11-13 17:22:28 +01:00
2017-11-10 15:32:54 +01:00
$leagues = League::where('season_id','=',$season->id)->where('rank','<=', 4)->orderBy('id')->get();
if (!sizeof($leagues)) {
Log::info($a->getEndMessage());
return redirect("/");
}
2017-09-30 13:14:46 +02:00
$totalRanking = League::getTotalRankingOverall(); // Nur für Auf/Abstiegsinfo benötigt.
$rankInfo=array();
2017-11-07 16:59:58 +01:00
foreach($totalRanking as $index => $entry) {
$userId=$entry[0];
2017-11-07 16:59:58 +01:00
$rankInfo[$userId]=$totalRanking[$index][4];
}
2017-11-07 16:59:58 +01:00
$cols=array();
$cols[1]="greenyellow";
$cols[2]="orange";
$cols[3]="lightgrey";
$cols[4]="";
$leagueScores=array();
foreach([1,2,3,4] as $rank) {
$leagueScores[$rank] = League::getRanking($rank);
}
2017-11-07 16:59:58 +01:00
Log::info($a->getEndMessage());
//error_log($a->getEndMessage());
return view ('leagues.chart',compact('season', 'leagueNames', 'userNames','leagueScores','rankInfo','rank','cols'));
2017-09-30 13:14:46 +02:00
}
2022-06-03 11:58:26 +02:00
// Originall copied over from showLeagueRanking
public static function showLeagueSwapForm() {
$a=new TimeLogger("LeaguesController::showLeagueSwapForm()");
Log::info($a->getStartMessage());
$season = Season::getCurrent();
//$leagues = array_reverse(League::getRanking($rank));
$leagueNames = League::getNames();
$userNames = User::getNames();
$leagues = League::where('season_id','=',$season->id)->where('rank','<=', 4)->orderBy('id')->get();
if (!sizeof($leagues)) {
Log::info($a->getEndMessage());
return redirect("/");
}
$totalRanking = League::getTotalRankingOverall(); // Nur für Auf/Abstiegsinfo benötigt.
$rankInfo=array();
foreach($totalRanking as $index => $entry) {
$userId=$entry[0];
$rankInfo[$userId]=$totalRanking[$index][4];
}
$cols=array();
$cols[1]="greenyellow";
$cols[2]="orange";
$cols[3]="lightgrey";
$cols[4]="";
$leagueScores=array();
foreach([1,2,3,4] as $rank) {
$leagueScores[$rank] = League::getRanking($rank);
}
Log::info($a->getEndMessage());
//error_log($a->getEndMessage());
return view ('leagues.chart_swapform',compact('season', 'leagueNames', 'userNames','leagueScores','rankInfo','rank','cols'));
}
public static function swapLeagueMembers(Request $request) {
Log::info("Start: LeaguesController::swapLeagueMembers");
Log::info("Method triggered successfully.");
Log::info($request->method()); // Klappt
$input=$request->input();
Log::info("Params:".sizeof($input));
foreach ($input as $key => $value) {
2022-06-12 00:22:19 +02:00
Log::info( $key."=>".$value );
}
2022-06-12 00:22:19 +02:00
$leagueIds = League::getIds();
$strLeagueIds = join(",", $leagueIds);
Log::info("League Ids: ".$strLeagueIds);
$user1_id=$request->input('user1_id');
$user2_id=$request->input('user2_id');
Log::info($user1_id);
Log::info($user2_id);
$sql=str_replace("#USER1_ID",$user1_id, "update leagues_matches set user1_id=999 where user1_id=#USER1_ID and league_id in (#LEAGUES);");
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER1_ID",$user1_id, "update leagues_matches set user1_id=#USER1_ID where user1_id=#USER2_ID and league_id in (#LEAGUES);");
$sql=str_replace("#USER2_ID", $user2_id,$sql);
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER2_ID",$user2_id, "update leagues_matches set user1_id=#USER2_ID where user1_id=999 and league_id in (#LEAGUES);");
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER1_ID",$user1_id, "update leagues_matches set user2_id=999 where user2_id=#USER1_ID and league_id in (#LEAGUES);");
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER1_ID",$user1_id, "update leagues_matches set user2_id=#USER1_ID where user2_id=#USER2_ID and league_id in (#LEAGUES);");
$sql=str_replace("#USER2_ID", $user2_id,$sql);
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER2_ID",$user2_id, "update leagues_matches set user2_id=#USER2_ID where user2_id=999 and league_id in (#LEAGUES);");
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER1_ID",$user1_id, "update leagues_members set user_id=999 where user_id=#USER1_ID and league_id in (#LEAGUES);");
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER1_ID",$user1_id, "update leagues_members set user_id=#USER1_ID where user_id=#USER2_ID and league_id in (#LEAGUES);");
$sql=str_replace("#USER2_ID", $user2_id,$sql);
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
$sql=str_replace("#USER2_ID",$user2_id, "update leagues_members set user_id=#USER2_ID where user_id=999 and league_id in (#LEAGUES);");
$sql=str_replace("#LEAGUES", $strLeagueIds,$sql);
Log::info($sql);
DB::update($sql);
}
2022-06-03 11:58:26 +02:00
2017-09-30 13:14:46 +02:00
public static function showSingleUserMatches($userId) {
$season = Season::getCurrent();
$leagueNames = League::getNames();
$roundNames = Round::getNames();
$matches = Match::getMatchResultsForUser($userId);
$user = User::where('id','=', $userId)->first();
$userName = $user->name;
return view ('leagues.playermatches',compact('season','leagueNames','roundNames','matches','userName'));
}
2017-09-22 17:40:48 +02:00
2017-09-11 10:38:57 +02:00
}