Auslagerung roundTipps nach Tipp, um Wiederverwendung und Caching zu ermöglichen

This commit is contained in:
Torsten 2017-08-31 07:36:27 +02:00
parent b9e4fcca7f
commit 054a8d6f63
8 changed files with 5871 additions and 213 deletions

View file

@ -180,6 +180,8 @@ class UsersController extends Controller
public function ranking($max_round_id) {
$startTime = microtime(true);
$currentSeason = Season::getCurrent();
// Check ob mindestens eine Runde der Saison gelockt ist angezeigt werden kann
@ -204,201 +206,11 @@ class UsersController extends Controller
return view('users.no_payment');
}
// Hier werden _alle_ Tipps der aktuellen Saison ausgerechnet, die $round_id Begrenzung wird erst später ausgewertet.
// TODO: Nur die Runden durchrechnen, die aktuell sind, oder round_incomplete, alle anderen Runden (bis zur ersten unvollständigen) sollen in eine Cachetabelle gespeichert werden.
$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 . "and users.deleted is null ";
$sql = $sql . "order by rounds.id, games.id, users.id;";
$result=DB::select($sql);
$roundtipps = array();
$user_ids=array();
$round_ids=array();
// Write sql result to roundtipps (score per round & user)
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]['joker']=array();
$roundtipps[$key]['round_completed']=true; // Um ggfs.
$roundtipps[$key]['user_name'] = $entry->user_name;
$roundtipps[$key]['user_id'] = $entry->user_id;
$roundtipps[$key]['rankscore']=0;
$roundtipps[$key]['payout']=0;
$roundtipps[$key]['earliest_kickoff']=$entry->game_kickoff;
$roundtipps[$key]['hits']=0; // Wenigstens Tendenz getroffen. Wenn 4, dann 2 Punkte Bonus auf rankscore
$roundtipps[$key]['is_dayscorewinner']=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
if ($entry->game_score1==-1 || $entry->game_score2==-1) { $roundtipps[$key]['round_completed'] = false; }
array_push($roundtipps[$key]['tipps'],$entry->tipp_score1);
array_push($roundtipps[$key]['tipps'],$entry->tipp_score2);
array_push($roundtipps[$key]['results'],$entry->game_score1);
array_push($roundtipps[$key]['results'],$entry->game_score2);
array_push($roundtipps[$key]['joker'],$entry->joker);
if ( $entry->game_kickoff < $roundtipps[$key]['earliest_kickoff'] ) { $roundtipps[$key]['earliest_kickoff'] = $entry->game_kickoff; }
}
// NOW ALL KEYS FOR ROUNDTIPPS (ROUND & USER) EXIST
// CALCULATION OF SCORES, RANKS, ETC.
// FIRST LOOP: COMPUTE AGGREGATED VALUES
$highestDayscores=array();
foreach($user_ids as $user_id) {
$totalRankScore=0;
foreach($round_ids as $round_id) {
$key=$round_id."_".$user_id;
// Compute Dayscore values
$dayScore1WithRankScore = Tipp::getDayScoreLocal($roundtipps[$key]['results'][0],$roundtipps[$key]['results'][1],$roundtipps[$key]['tipps'][0],$roundtipps[$key]['tipps'][1],$roundtipps[$key]['joker'][0],$roundtipps[$key]['rankscore']);
$dayScore2WithRankScore = Tipp::getDayScoreLocal($roundtipps[$key]['results'][2],$roundtipps[$key]['results'][3],$roundtipps[$key]['tipps'][2],$roundtipps[$key]['tipps'][3],$roundtipps[$key]['joker'][1],$roundtipps[$key]['rankscore']);
$dayScore3WithRankScore = Tipp::getDayScoreLocal($roundtipps[$key]['results'][4],$roundtipps[$key]['results'][5],$roundtipps[$key]['tipps'][4],$roundtipps[$key]['tipps'][5],$roundtipps[$key]['joker'][2],$roundtipps[$key]['rankscore']);
$dayScore4WithRankScore = Tipp::getDayScoreLocal($roundtipps[$key]['results'][6],$roundtipps[$key]['results'][7],$roundtipps[$key]['tipps'][6],$roundtipps[$key]['tipps'][7],$roundtipps[$key]['joker'][3],$roundtipps[$key]['rankscore']);
$totalDayScoreWithRankScore = Tipp::addDayScoreValues($dayScore1WithRankScore,$dayScore2WithRankScore);
$totalDayScoreWithRankScore = Tipp::addDayScoreValues($totalDayScoreWithRankScore,$dayScore3WithRankScore);
$totalDayScoreWithRankScore = Tipp::addDayScoreValues($totalDayScoreWithRankScore,$dayScore4WithRankScore);
$roundtipps[$key]['dayScoreWithRankScore'] = $totalDayScoreWithRankScore;
$roundtipps[$key]['dayscore'] = Tipp::getDayScoreWithoutRankScore($totalDayScoreWithRankScore);
if (!array_key_exists($round_id,$highestDayscores)) { $highestDayscores[$round_id] = 0;}
if ($highestDayscores[$round_id] < $roundtipps[$key]['dayscore'] ) { $highestDayscores[$round_id]=$roundtipps[$key]['dayscore'];}
$totalRankScore += $roundtipps[$key]["rankscore"];
$roundtipps[$key]['totalRankScore'] = $totalRankScore;
}
}
////////////////////////////////////////////////////////
// Sortier-Array nach Rang (nur für max_round_id)
$userRankings = array();
foreach ($user_ids as $user_id) {
$key = $max_round_id."_".$user_id;
array_push($userRankings, [$user_id , $roundtipps[$key]['totalRankScore']]);
}
usort($userRankings,array($this, 'cmpRank'));
// Ranknr identifizieren, inkl, geteilter Rankplatzierungen nur für max_round_id notwendig, da der Rest ja gar nicht angezeigt wird
$lastScore=-1;
$lastrank=0;
$current_list_position=0;
foreach($userRankings as $userRank) {
$user_id=$userRank[0];
$totalRankScore=$userRank[1];
$key=$max_round_id."_".$user_id;
$current_list_position+=1;
if ($roundtipps[$key]["totalRankScore"]==$lastScore) {
$roundtipps[$key]["rank"] = $lastrank;
} else {
$lastrank = $current_list_position;
$roundtipps[$key]["rank"] = $current_list_position;
$lastScore = $totalRankScore;
}
}
//////////////////////////////
// START OF HURTFUL 6 PLACE RESTRICTION IF DAYSCORE WIN (exception: completely even scores)
// If more than 6 DayScoreWinners: Restrict by dayscore (rank)
// Compute highest dayscore
// Addition: no dayscorewinner && round_completed: Payout goes to next round
$payouts = Quota::getPayouts($currentSeason->id);
$totalDayWinPayout = 0;
foreach ($round_ids as $round_id) {
$totalDayWinPayout += $payouts["tagessieg"];
$highestDayScore = $highestDayscores[$round_id];
$usersWithHighestDayScore = array();
$round_completed=false;
foreach ($user_ids as $user_id) {
$round_completed = $roundtipps[$key]['round_completed']; // TODO: Separaten info array roundData erzeugen, der Rundenspezifische, SPielerübergreifende Infos enthält, so wie round_completed.
$key = $round_id . "_" . $user_id;
if ($roundtipps[$key]['dayscore'] == $highestDayScore && $roundtipps[$key]['round_completed'] && $highestDayScore>0 ) {
array_push($usersWithHighestDayScore, [$user_id, $roundtipps[$key]['dayScoreWithRankScore']]);
}
}
if (sizeof($usersWithHighestDayScore) > 6) {
usort($usersWithHighestDayScore, array($this, 'cmpDayWin'));
}
$count = 0;
$cutOffScore = 0;
$dayScoreWinnersIds = array();
foreach ($usersWithHighestDayScore as $user) { // Feld0: user_id, dayScoreWithRankScore
$count += 1;
if ($count > 6) {
if ($user[1] < $cutOffScore) {
break;
}
}
array_push($dayScoreWinnersIds, $user[0]);
$key = $round_id . "_" . $user[0];
$roundtipps[$key]['is_dayscorewinner'] = true;
Log::info("Dayscorewinner Round " . $round_id . " = " . $roundtipps[$key]["user_name"]);
$cutOffScore = $user[1];
}
// Compute Dayscorewin payout (Tagessieg)
$winnerCount = sizeof($dayScoreWinnersIds);
$single_payout=0;
if ($winnerCount > 0) {
$single_payout = $totalDayWinPayout / $winnerCount;
$single_payout = number_format((float)$single_payout, 2, '.', '');
$single_payout = str_replace(".", ",", $single_payout);
$totalDayWinPayout=0;
}
foreach ($user_ids as $user_id) {
$key = $round_id . "_" . $user_id;
if ($roundtipps[$key]['is_dayscorewinner']) {
$roundtipps[$key]['payout'] = $single_payout;
} else {
$roundtipps[$key]['payout'] = '';
}
}
}
//////////////////// END OF ROUNDTIPPS CREATION ///////////////////
$response = Tipp::getRoundTipps($max_round_id);
$roundtipps = $response['roundtipps'];
$roundData = $response['roundData'];
$user_ids = $response['user_ids'];
$round_ids = $response['round_ids'];
@ -429,10 +241,9 @@ class UsersController extends Controller
$rankingDisplay[$user_id]["is_dayscorewinner"] = $roundtipps[$key]["is_dayscorewinner"];
if ($round_id==4 && $roundtipps[$key]["is_dayscorewinner"]) {
Log::info("Dayscorewinner: ".$roundtipps[$key]["user_name"]);
//Log::info("Dayscorewinner: ".$roundtipps[$key]["user_name"]);
}
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]["dayScoreWithRankScore"] = $roundtipps[$key]["dayScoreWithRankScore"]; // Dayscore der letzten durchlaufenen Runde, NICHT in init stellen !!
@ -504,10 +315,11 @@ class UsersController extends Controller
// 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
$a=$rankingDisplay[0];
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"]);
$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"]);
Log::info("UserController::ranking: ExecutionTime: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n");
return view('users.ranking', compact('all_rounds','rankingDisplay','userids','round','games','game_results'));
}