419 lines
18 KiB
PHP
Executable file
419 lines
18 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 App\Quota;
|
|
use App\Mail\Roundmail;
|
|
|
|
use DB;
|
|
use Auth;
|
|
use Log;
|
|
|
|
use Carbon;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
public function main() {
|
|
$roundname="";
|
|
$report="";
|
|
|
|
$currentSeason=Season::getCurrent();
|
|
|
|
$display_rounds=array();
|
|
if ($currentSeason) {
|
|
$round_id = Round::getLastCompleteRoundId();
|
|
|
|
if ($round_id > -1) {
|
|
$rounds = Round::where("id", "<=", $round_id)->get();
|
|
foreach($rounds as $round) {
|
|
$dpr =array();
|
|
$dpr["name"]=$round->name;
|
|
$dpr["report"]=$round->report;
|
|
array_push($display_rounds, $dpr);
|
|
}
|
|
$display_rounds = array_reverse($display_rounds);
|
|
Log::info("Loaded report:".$report." round-id=".$round_id);
|
|
$roundname = $round->name;
|
|
} else { Log::info("No round identified. Last Locked=".$round_id); $report="";}
|
|
} else {Log::info("No current season");}
|
|
|
|
return view('main',compact('roundname','display_rounds'));
|
|
}
|
|
|
|
public function index() {
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
if (!Auth::user()->admin) { return redirect("/"); }
|
|
|
|
$currentSeason=Season::getCurrent();
|
|
$active_user_count = sizeof( DB::table('payments')->where('season_id',$currentSeason->id)->whereNotNull('paid_date')->get() );
|
|
|
|
$users = DB::table('users')->where('deleted',null)->orderBy('lastname')->get();
|
|
|
|
|
|
foreach($users as $user) {
|
|
Log::info($user->id);
|
|
}
|
|
|
|
|
|
|
|
return view('users.index', compact('users','active_user_count'));
|
|
}
|
|
/*
|
|
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($max_round_id) {
|
|
$currentSeason = Season::getCurrent();
|
|
|
|
|
|
|
|
// Check ob mindestens eine Runde der Saison gelockt ist angezeigt werden kann
|
|
if($max_round_id==-1) {
|
|
$max_round_id = Round::getLastLockedRoundId();
|
|
if ($max_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("/");
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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, games.kickoff as game_kickoff, ";
|
|
$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();
|
|
|
|
|
|
|
|
|
|
if ($max_round_id==-1) { $max_round_id=Round::getLastLockedRoundId(); }
|
|
|
|
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']=0;
|
|
$roundtipps[$key]['dayscore']=0;
|
|
|
|
$roundtipps[$key]['earliest_kickoff']=$entry->game_kickoff;
|
|
$roundtipps[$key]['hits']=0; // Wenigstens Tendenz getroffen. Wenn 4, dann 2 Punkte Bonus auf rankscore
|
|
}
|
|
$game_value=Tipp::getScoreLocal($entry->game_score1,$entry->game_score2,$entry->tipp_score1,$entry->tipp_score2,$entry->joker); // Für alle 4 Spiele
|
|
$roundtipps[$key]['rankscore']+=$game_value;
|
|
if($game_value>0) { $roundtipps[$key]['hits']+=1;}
|
|
|
|
|
|
|
|
if ($roundtipps[$key]['hits']==4) {$roundtipps[$key]['rankscore']+=2;} // Wenn alle 4 Spiele mind. Tendenz, dann 2 Punkte Bonus
|
|
|
|
|
|
|
|
|
|
// START: Add Up Dayscore Win Values for each round
|
|
$tipp_hits = intdiv($roundtipps[$key]['dayscore'],100);
|
|
$joker_value = $roundtipps[$key]['dayscore'] - $tipp_hits*100;
|
|
|
|
$current_score=Tipp::getDayScoreLocal($entry->game_score1,$entry->game_score2,$entry->tipp_score1,$entry->tipp_score2,$entry->joker);
|
|
$current_tipp_hit = intdiv($current_score,100);
|
|
$current_joker_value = $current_score - ( $current_tipp_hit * 100 );
|
|
|
|
$hits_total = $tipp_hits + $current_tipp_hit;
|
|
$current_joker_value = max($joker_value,$current_joker_value);
|
|
$roundtipps[$key]["dayscore"] = $hits_total * 100 + $current_joker_value;
|
|
// END: Add Up Dayscore Win Values
|
|
|
|
|
|
|
|
if ($entry->game_kickoff < $roundtipps[$key]['earliest_kickoff']) { $roundtipps[$key]['earliest_kickoff']=$entry->game_kickoff; }
|
|
|
|
array_push($roundtipps[$key]['tipps'], $entry->tipp_score1, $entry->tipp_score2, $entry->joker);
|
|
array_push($roundtipps[$key]['results'], $entry->game_score1, $entry->game_score2);
|
|
|
|
}
|
|
|
|
Log::info("max_round_id=".$max_round_id);
|
|
|
|
// Aufbau der Rankingliste in rankingDisplay, sowie aufaddieren der jeweiligen Punkte.
|
|
$rankingDisplay=array();
|
|
|
|
|
|
foreach ($user_ids as $user_id) {
|
|
$rankscore_total= 0;
|
|
$payout_sum = 0;
|
|
|
|
foreach($round_ids as $round_id) {
|
|
if ($user_id==1) { Log::info("Accumulating display data for round_id ".$round_id); }
|
|
$key = $round_id . "_" . $user_id;
|
|
if ($user_id==1) { Log::info("Dayscore round: ".$round_id." = ".$roundtipps[$key]['dayscore']);}
|
|
if ($user_id==1) { Log::info("Rankscore round: ".$round_id." = ".$roundtipps[$key]['rankscore']);}
|
|
|
|
if (!array_key_exists($user_id, $rankingDisplay)) {
|
|
$rankingDisplay[$user_id] = array();
|
|
|
|
$rankingDisplay[$user_id]["user_id"] = $user_id;
|
|
|
|
$rankingDisplay[$user_id]["user_name"] = $roundtipps[$key]["user_name"];
|
|
$rankingDisplay[$user_id]["is_dayscorewinner"] = 0;
|
|
|
|
if ($user_id==1) { Log::info("rankscore after user init for display round_id ".$round_id." = ".$roundtipps[$key]["rankscore"]);}
|
|
}
|
|
|
|
$rankingDisplay[$user_id]["rankscore"] = $roundtipps[$key]["rankscore"]; // Summe Rangwertung alle 4 Spiele/Tipps der letzten durchlaufenen Runde, NICHT in init stellen !!
|
|
$rankingDisplay[$user_id]["dayscore"] = $roundtipps[$key]["dayscore"]; // Dayscore der letzten durchlaufenen Runde, NICHT in init stellen !!
|
|
|
|
$rankingDisplay[$user_id]["rankscore_total"] = $rankscore_total + $roundtipps[$key]["rankscore"];
|
|
$rankscore_total=$rankingDisplay[$user_id]["rankscore_total"];
|
|
|
|
$rankingDisplay[$user_id]["tipp_score1"] = $roundtipps[$key]["tipps"][0];
|
|
$rankingDisplay[$user_id]["tipp_score2"] = $roundtipps[$key]["tipps"][1];
|
|
$rankingDisplay[$user_id]["joker1"] = $roundtipps[$key]["tipps"][2];
|
|
$rankingDisplay[$user_id]["result_score1"] = $roundtipps[$key]["results"][0];
|
|
$rankingDisplay[$user_id]["result_score2"] = $roundtipps[$key]["results"][1];
|
|
|
|
// Determine coloration for Game1
|
|
$rankingDisplay[$user_id]["trendCol1"] = Tipp::trendCol($roundtipps[$key]["results"][0],$roundtipps[$key]["results"][1],
|
|
$roundtipps[$key]["tipps"][0],$roundtipps[$key]["tipps"][1],
|
|
$roundtipps[$key]["tipps"][2]);
|
|
|
|
$rankingDisplay[$user_id]["tipp_score3"] = $roundtipps[$key]["tipps"][3];
|
|
$rankingDisplay[$user_id]["tipp_score4"] = $roundtipps[$key]["tipps"][4];
|
|
$rankingDisplay[$user_id]["joker2"] = $roundtipps[$key]["tipps"][5];
|
|
$rankingDisplay[$user_id]["result_score3"] = $roundtipps[$key]["results"][2];
|
|
$rankingDisplay[$user_id]["result_score4"] = $roundtipps[$key]["results"][3];
|
|
|
|
// Determine coloration for Game2
|
|
$rankingDisplay[$user_id]["trendCol2"] = Tipp::trendCol($roundtipps[$key]["results"][2],$roundtipps[$key]["results"][3],
|
|
$roundtipps[$key]["tipps"][3],$roundtipps[$key]["tipps"][4],
|
|
$roundtipps[$key]["tipps"][5]);
|
|
|
|
$rankingDisplay[$user_id]["tipp_score5"] = $roundtipps[$key]["tipps"][6];
|
|
$rankingDisplay[$user_id]["tipp_score6"] = $roundtipps[$key]["tipps"][7];
|
|
$rankingDisplay[$user_id]["joker3"] = $roundtipps[$key]["tipps"][8];
|
|
$rankingDisplay[$user_id]["result_score5"] = $roundtipps[$key]["results"][4];
|
|
$rankingDisplay[$user_id]["result_score6"] = $roundtipps[$key]["results"][5];
|
|
// Determine coloration for Game3
|
|
$rankingDisplay[$user_id]["trendCol3"] = Tipp::trendCol($roundtipps[$key]["results"][4],$roundtipps[$key]["results"][5],
|
|
$roundtipps[$key]["tipps"][6],$roundtipps[$key]["tipps"][7],
|
|
$roundtipps[$key]["tipps"][8]);
|
|
|
|
$rankingDisplay[$user_id]["tipp_score7"] = $roundtipps[$key]["tipps"][9];
|
|
$rankingDisplay[$user_id]["tipp_score8"] = $roundtipps[$key]["tipps"][10];
|
|
$rankingDisplay[$user_id]["joker4"] = $roundtipps[$key]["tipps"][11];
|
|
$rankingDisplay[$user_id]["result_score7"] = $roundtipps[$key]["results"][6];
|
|
$rankingDisplay[$user_id]["result_score8"] = $roundtipps[$key]["results"][7];
|
|
// Determine coloration for Game4
|
|
$rankingDisplay[$user_id]["trendCol4"] = Tipp::trendCol($roundtipps[$key]["results"][6],$roundtipps[$key]["results"][7],
|
|
$roundtipps[$key]["tipps"][9],$roundtipps[$key]["tipps"][10],
|
|
$roundtipps[$key]["tipps"][11]);
|
|
}
|
|
|
|
}
|
|
|
|
$userids = array_keys($rankingDisplay);
|
|
$round=Round::where('id','=',$max_round_id)->first();
|
|
$games=Game::where('round_id','=',$max_round_id)->get();
|
|
$all_rounds=Round::where('season_id','=',$currentSeason->id)->get();
|
|
|
|
// Sortieren nach Rankscore
|
|
$entries = array();
|
|
usort($rankingDisplay, array($this,"cmp"));
|
|
|
|
foreach($rankingDisplay as $entry) {
|
|
array_push($entries, $entry);
|
|
}
|
|
$rankingDisplay = $entries;
|
|
|
|
// Ranknr identifizieren, inkl, geteilter Rankplatzierungen
|
|
$lastScore=-1;
|
|
$lastrank=0;
|
|
$current_list_position=0;
|
|
$highest_dayscore=0;
|
|
foreach($rankingDisplay as $key => $user) {
|
|
$current_list_position+=1;
|
|
if ($user["rankscore_total"]==$lastScore) {
|
|
$rankingDisplay[$key]["rank"] = $lastrank;
|
|
} else {
|
|
$lastrank = $current_list_position;
|
|
$rankingDisplay[$key]["rank"] = $current_list_position;
|
|
$lastScore = $user["rankscore_total"];
|
|
}
|
|
|
|
if ($user["dayscore"]>$highest_dayscore) { $highest_dayscore = $user["dayscore"]; }
|
|
}
|
|
|
|
|
|
// is_dayscorewinner setzen (Tagessieg)
|
|
// Nur wenn alle Spiele eingetragen=gespielt sind !
|
|
Log::info("Searching for dayscore winners. Highest dayscore=".$highest_dayscore);
|
|
$daywinner_count=0;
|
|
$round_completed = Round::isCompleted($max_round_id);
|
|
foreach($rankingDisplay as $key => $user) {
|
|
if ($user["dayscore"]>0 && $user["dayscore"]==$highest_dayscore && $round_completed) {
|
|
$rankingDisplay[$key]["is_dayscorewinner"]=1;
|
|
$daywinner_count+=1;
|
|
Log::info("Setting dayscorewin for player:".$user["user_name"]);
|
|
}
|
|
}
|
|
|
|
//TODO: if ($daywinner_count > 6) { ... }
|
|
|
|
// Compute Dayscorewin payout (Tagessieg)
|
|
$payouts = Quota::getPayouts($currentSeason->id);
|
|
$single_payout=0;
|
|
|
|
if ($daywinner_count>0) {
|
|
$single_payout = $payouts["Tagessieg"] / $daywinner_count ;
|
|
$single_payout = number_format((float)$single_payout, 2, '.', '');
|
|
$single_payout = str_replace(".",",",$single_payout);
|
|
}
|
|
|
|
foreach($rankingDisplay as $key => $user) {
|
|
if ($user["is_dayscorewinner"]==1) {
|
|
$rankingDisplay[$key]['payout']=$single_payout;
|
|
} else {
|
|
$rankingDisplay[$key]['payout']='';
|
|
}
|
|
}
|
|
|
|
|
|
// Store game results as separate array. Wir nutzen aus, dass die Ergebnisse in jeder Zeile
|
|
// von Rankingdisplay abgespeichert sind.
|
|
$game_results=array(); // 4 Spielergebnisse der angezeigten Runde
|
|
array_push($game_results,$rankingDisplay[0]["result_score1"],$rankingDisplay[0]["result_score2"],$rankingDisplay[0]["result_score3"],$rankingDisplay[0]["result_score4"],$rankingDisplay[0]["result_score5"],$rankingDisplay[0]["result_score6"],$rankingDisplay[0]["result_score7"],$rankingDisplay[0]["result_score8"]);
|
|
|
|
|
|
return view('users.ranking', compact('all_rounds','rankingDisplay','userids','round','games','game_results'));
|
|
}
|
|
|
|
public function cmp($user1, $user2) {
|
|
return ($user1['rankscore_total'] < $user2['rankscore_total']);
|
|
}
|
|
|
|
public function show_payouts() {
|
|
$currentSeason = Season::getCurrent();
|
|
|
|
$active_user_count = sizeof( DB::table('payments')->where('season_id',$currentSeason->id)->whereNotNull('paid_date')->get() );
|
|
$einsatz = Quota::getQuota($currentSeason->id,"Einsatz");
|
|
$payouts = Quota::getPayouts($currentSeason->id);
|
|
|
|
Log::info("EINSATZ=".$einsatz);
|
|
|
|
return view('users.show_payouts', compact('payouts','active_user_count','einsatz','currentSeason'));
|
|
}
|
|
|
|
public function mailtest() {
|
|
// Works:
|
|
// $user = User::where('id','=',1)->first();
|
|
// \Mail::to($user)->send(new Roundmail);
|
|
|
|
$users = User::whereIn('id',array(1,2,7,57))->get();
|
|
foreach($users as $user) {
|
|
Log::info("Sending Roundmail to: ".$user->name);
|
|
\Mail::to($user)->send(new Roundmail);
|
|
}
|
|
}
|
|
|
|
|
|
}
|