345 lines
13 KiB
PHP
Executable file
345 lines
13 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\User;
|
|
use App\Season;
|
|
use App\Payment;
|
|
use App\Round;
|
|
use App\Game;
|
|
use App\Tipp;
|
|
use DB;
|
|
use Auth;
|
|
use Log;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
public function main() {
|
|
$roundname="";
|
|
$report="";
|
|
|
|
$currentSeason=Season::getCurrent();
|
|
|
|
if ($currentSeason) {
|
|
$round_id = Round::getLastLockedRoundId();
|
|
if ($round_id > -1) {
|
|
$round = Round::where("id", "=", $round_id)->first();
|
|
$report = $round->report;
|
|
Log::info("Loaded report:".$report." round-id=".$round_id);
|
|
$roundname = $round->name;
|
|
} else { Log::info("No round identified. Last Locked=".$round_id); }
|
|
} else {Log::info("No current season");}
|
|
|
|
return view('main',compact('roundname','report'));
|
|
}
|
|
|
|
public function index() {
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
if (!Auth::user()->admin) { return redirect("/"); }
|
|
|
|
$users = DB::table('users')->where('deleted',null)->orderBy('lastname')->get();
|
|
return view('users.index', compact('users'));
|
|
}
|
|
/*
|
|
public function showUsers() {
|
|
$users = DB::table('users')->orderBy('lastname')->get();
|
|
return view('users.list', compact('users'));
|
|
}
|
|
*/
|
|
// Route-Model binding, because parameter in web.php route has same name as functionparameter
|
|
public function showUser(User $userObject) {
|
|
//return $userObject; // If you want it as a JSON object
|
|
//$user = DB::table('users')->find($id);
|
|
return view('users.show', compact('userObject'));
|
|
}
|
|
|
|
public function edit($user_id) {
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
if (!Auth::user()->admin) { return redirect("/"); }
|
|
|
|
$user = User::where('id',$user_id)->first();
|
|
$season_id = Season::getCurrent()->id;
|
|
|
|
$paid_date_str="";
|
|
$val=User::payment_date($user_id, $season_id);
|
|
Log::info("UserController:edit => payment_date=".$val);
|
|
|
|
if ($val) { $paid_date_str=User::toGermanDate($val);}
|
|
Log::info("German Date=".$paid_date_str);
|
|
|
|
return view ('users.edit', compact('user','paid_date_str'));
|
|
}
|
|
|
|
public function store() {
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
$season_id = Season::getCurrent()->id;
|
|
// Validate form
|
|
$this->validate(request(), [
|
|
'id' => 'required',
|
|
'firstname' => 'required',
|
|
'lastname' => 'required',
|
|
'email'=> 'required'
|
|
]);
|
|
|
|
|
|
$user=User::where('id', request('id'))->first();
|
|
|
|
$user->name=request('firstname')." ".request('lastname');
|
|
$user->firstname=request('firstname');
|
|
$user->lastname=request('lastname');
|
|
$user->email=request('email');
|
|
|
|
if (request()->has('admin')) {
|
|
$user->admin = true;
|
|
|
|
} else {
|
|
$user->admin=false;
|
|
}
|
|
$user->save();
|
|
|
|
Payment::set_payment_date(request('id'), $season_id, request('paid'));
|
|
|
|
return redirect('/users');
|
|
}
|
|
|
|
|
|
|
|
public function ranking($round_id) {
|
|
Log::info("Start ranking");
|
|
|
|
$currentSeason=Season::getCurrent();
|
|
if (!$currentSeason) { return redirect("/"); }
|
|
|
|
if($round_id==-1) {
|
|
$round_id = Round::getLastLockedRoundId();
|
|
if ($round_id==-1) {
|
|
|
|
$sql="select DATE_FORMAT(date_sub(min(games.kickoff), interval 1 hour),'%d.%m.%Y %H:%i') as start_date from rounds,games where rounds.season_id=".$currentSeason->id." and games.round_id=rounds.id;";
|
|
$date = DB::select($sql);
|
|
|
|
if (count($date)>0) {
|
|
$start_date = $date[0]->start_date;
|
|
return view('users.no_ranking', compact('start_date'));
|
|
} else {
|
|
return redirect("/");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
|
|
TippsController::createAllTips();
|
|
|
|
$users = DB::table('users')->where('deleted',null)->get();
|
|
|
|
$highestDayScore=0;
|
|
|
|
// Tagessieg
|
|
foreach($users as $user) {
|
|
//Log::info("User: ".$user->id.", ".$user->fullname);
|
|
$user->totalScore=0; // Summe Ranglisten-Punkte bis heute
|
|
$user->dayScore=0; // Heute gemachte Ranglisten-Punkte
|
|
|
|
$user->dayWinScore=0; // Tagessieg-Punkte, nur exakte Ergebnisse zählen
|
|
$user->dayWinHighestJoker=0; // Tagessieg-Punkte, nur exakte Ergebnisse zählen
|
|
|
|
$round=Round::where('id','=',$round_id)->first();
|
|
$games=Game::where('round_id','=',$round->id)->get();
|
|
//Log::info('Nr of games found: '.count($games)." game->id=".$round->id." user->id=".$user->id );
|
|
|
|
|
|
foreach($games as $game) {
|
|
$tipp = Tipp::where([['game_id','=',$game->id],['user_id','=',$user->id]])->first();
|
|
if ($tipp) {
|
|
$score = Tipp::getDayScoreLocal($game->score1,$game->score2,$tipp->score1,$tipp->score2,$tipp->joker);
|
|
|
|
if ($score > $user->dayWinScore) {
|
|
$user->dayWinScore = $score;
|
|
|
|
}
|
|
if ($score > $highestDayScore) { $highestDayScore = $score; }
|
|
|
|
} else {
|
|
Log::info("No tipp found for game_id=".$game->id.", user_id=".$user->id);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//Tendency Key=UserID_Runde
|
|
$tendencies=array('0_0'=>0);
|
|
|
|
|
|
// Punktrangliste
|
|
$rounds=Round::where([['season_id','=',$currentSeason->id],['id','<=',$round_id]])->orderBy('id')->get();
|
|
foreach($rounds as $round) {
|
|
|
|
$games=Game::where('round_id','=',$round->id)->orderBy('id')->get();
|
|
$index=0;
|
|
|
|
foreach($games as $game) {
|
|
foreach($users as $user) {
|
|
$key = $user->id."_".$round->id;
|
|
$user->tendencyCount=0;// 4 correct tendencies ==> +2 bonus score
|
|
$tipp = Tipp::where('game_id','=',$game->id)->where('user_id','=',$user->id)->first();
|
|
|
|
if ($tipp) {
|
|
// Ranglisten-Punkte berechen
|
|
$score = Tipp::getScoreLocal($game->score1,$game->score2,$tipp->score1,$tipp->score2,$tipp->joker);
|
|
|
|
if ($score>0) {
|
|
|
|
if (!array_key_exists($key,$tendencies)) { $tendencies[$key]=0; }
|
|
$tendencies[$key]+=1;
|
|
|
|
}
|
|
|
|
$user->totalScore += $score;
|
|
|
|
if ($game->round_id == $round_id) {
|
|
$user->dayScore += $score;
|
|
}
|
|
|
|
$tippDisplay[$user->id][$game->id][0] = $tipp->score1 ==-1 ? 'x' : $tipp->score1;
|
|
$tippDisplay[$user->id][$game->id][1] = $tipp->score2 ==-1 ? 'x' : $tipp->score2;
|
|
$tippDisplay[$user->id][$game->id][2] = $tipp->joker;
|
|
}
|
|
|
|
// Increase score here
|
|
if (array_key_exists($key,$tendencies)) {
|
|
if ($tendencies[$key] == 4) {
|
|
//Log::info("Increasing score ! userid=" . $user->id . ", gameid " . $game->id . ", roundid " . $round->id . ", tippid " . $tipp->id . ", tendency=" . $tendencies[$key]);
|
|
$user->totalScore += 2;
|
|
$user->dayScore+=2;
|
|
}
|
|
}
|
|
}
|
|
$index+=1;
|
|
}
|
|
}
|
|
|
|
$arr=[];
|
|
foreach($users as $user) {
|
|
array_push($arr, $user);
|
|
}
|
|
usort($arr, array($this,"cmp"));
|
|
$users=$arr;
|
|
|
|
$rank=1;
|
|
foreach($users as $user) {
|
|
$user->rank =$rank;
|
|
$rank +=1;
|
|
}
|
|
|
|
$round=Round::where('id','=',$round_id)->first();
|
|
$games=Game::where('round_id','=',$round->id)->get();
|
|
|
|
$all_rounds=Round::where('season_id','=',$currentSeason->id)->get();
|
|
return view('users.ranking', compact('users','round','all_rounds','games','tippDisplay','highestDayScore'));
|
|
|
|
}
|
|
|
|
public function cmp($user1, $user2) {
|
|
return ($user1->totalScore < $user2->totalScore);
|
|
}
|
|
|
|
|
|
|
|
|
|
public function ranking2($max_round_id) {
|
|
$currentSeason = Season::getCurrent();
|
|
|
|
// Hier werden _alle_ Tipps der aktuellen Saison ausgerechnet, die $round_id Begrenzung wird erst später ausgewertet.
|
|
$sql = "select users.id as user_id, users.name as user_name, ";
|
|
$sql = $sql . "rounds.id as round_id, rounds.name as round_name, ";
|
|
$sql = $sql . "games.id as game_id, games.team1, games.team2, games.score1 as game_score1, games.score2 as game_score2, ";
|
|
$sql = $sql . "tipps.id as tipp_id, tipps.score1 as tipp_score1, tipps.score2 as tipp_score2, tipps.joker ";
|
|
$sql = $sql . "from users,rounds,games,tipps,payments ";
|
|
$sql = $sql . "where rounds.season_id=".$currentSeason->id." ";
|
|
$sql = $sql . "and games.round_id=rounds.id and tipps.game_id=games.id and users.id = tipps.user_id ";
|
|
$sql = $sql . "and payments.season_id=".$currentSeason->id." and payments.user_id = users.id ";
|
|
$sql = $sql . "and payments.paid_date is not null ";
|
|
$sql = $sql . "order by rounds.id, games.id, users.id;";
|
|
|
|
$result=DB::select($sql);
|
|
|
|
$roundtipps = array();
|
|
$user_ids=array();
|
|
$round_ids=array();
|
|
|
|
foreach ($result as $entry) {
|
|
$round_id = $entry->round_id;
|
|
if ($max_round_id >-1 && $round_id>$max_round_id) break;
|
|
|
|
if (!in_array($entry->user_id,$user_ids)) { array_push($user_ids,$entry->user_id); }
|
|
if (!in_array($entry->round_id,$round_ids)) { array_push($round_ids,$entry->round_id); }
|
|
|
|
$user_id = $entry->user_id;
|
|
|
|
$key = $round_id."_".$user_id;
|
|
if (!array_key_exists($key,$roundtipps)) {
|
|
$roundtipps[$key]=array();
|
|
$roundtipps[$key]['tipps']=array();
|
|
$roundtipps[$key]['results']=array();
|
|
|
|
$roundtipps[$key]['user_name'] = $entry->user_name;
|
|
$roundtipps[$key]['user_id'] = $entry->user_id;
|
|
|
|
$roundtipps[$key]['rankscore']=Tipp::getScoreLocal($entry->game_score1,$entry->game_score2,$entry->tipp_score1,$entry->tipp_score2,$entry->joker);
|
|
$roundtipps[$key]['dayscore']=Tipp::getDayScoreLocal($entry->game_score1,$entry->game_score2,$entry->tipp_score1,$entry->tipp_score2,$entry->joker);
|
|
}
|
|
|
|
array_push($roundtipps[$key]['tipps'], $entry->tipp_score1, $entry->tipp_score2, $entry->joker);
|
|
array_push($roundtipps[$key]['results'], $entry->game_score1, $entry->game_score2);
|
|
}
|
|
|
|
//dd($roundtipps);
|
|
//dd($user_ids);
|
|
//dd($round_ids);
|
|
|
|
//Log::info($user_ids);
|
|
//Log::info($round_ids);
|
|
|
|
// Aufbau der Rankingliste in rankingDisplay, sowie aufaddieren der jeweiligen Punkte.
|
|
$rankingDisplay=array();
|
|
foreach ($user_ids as $user_id) {
|
|
$ranking_sum = 0;
|
|
$payout_sum = 0;
|
|
foreach($round_ids as $round_id) {
|
|
$key = $round_id . "_" . $user_id;
|
|
if (!array_key_exists($user_id, $rankingDisplay)) {
|
|
$rankingDisplay[$user_id] = array();
|
|
$rankingDisplay[$user_id]["rankscore"] = 0;
|
|
$rankingDisplay[$user_id]["dayscore"] = 0;
|
|
$rankingDisplay[$user_id]["user_name"] = $roundtipps[$key]["user_name"];
|
|
}
|
|
if (!array_key_exists("tipp_score1", $rankingDisplay[$user_id])) {
|
|
$rankingDisplay[$user_id]["tipp_score1"] = $roundtipps[$key]["tipps"][0];
|
|
$rankingDisplay[$user_id]["tipp_score2"] = $roundtipps[$key]["tipps"][1];
|
|
// Determine coloration for Game1
|
|
} elseif (!array_key_exists("tipp_score3", $rankingDisplay[$user_id])) {
|
|
$rankingDisplay[$user_id]["tipp_score3"] = $roundtipps[$key]["tipps"][2];
|
|
$rankingDisplay[$user_id]["tipp_score4"] = $roundtipps[$key]["tipps"][3];
|
|
// Determine coloration for Game2
|
|
} elseif (!array_key_exists("tipp_score5", $rankingDisplay[$user_id])) {
|
|
$rankingDisplay[$user_id]["tipp_score5"] = $roundtipps[$key]["tipps"][4];
|
|
$rankingDisplay[$user_id]["tipp_score6"] = $roundtipps[$key]["tipps"][5];
|
|
// Determine coloration for Game3
|
|
} elseif (!array_key_exists("tipp_score7", $rankingDisplay[$user_id])) {
|
|
$rankingDisplay[$user_id]["tipp_score7"] = $roundtipps[$key]["tipps"][6];
|
|
$rankingDisplay[$user_id]["tipp_score8"] = $roundtipps[$key]["tipps"][7];
|
|
// Determine coloration for Game4
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|