Completed Dayscore Ranking

This commit is contained in:
Torsten 2017-08-29 15:52:12 +02:00
parent 1f5e056b7f
commit 76947b1df9
7 changed files with 294 additions and 155 deletions

View file

@ -179,15 +179,9 @@ class UsersController extends Controller
}
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();
@ -222,11 +216,8 @@ class UsersController extends Controller
$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;
@ -241,125 +232,245 @@ class UsersController extends Controller
$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]['dayscore']=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
// 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);
// 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;
}
}
Log::info("max_round_id=".$max_round_id);
////////////////////////////////////////////////////////
// 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
foreach ($round_ids as $round_id) {
$highestDayScore = $highestDayscores[$round_id];
$usersWithHighestDayScore = array();
foreach ($user_ids as $user_id) {
$key = $round_id . "_" . $user_id;
if ($roundtipps[$key]['dayscore'] == $highestDayScore && $roundtipps[$key]['round_completed']) {
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)
$payouts = Quota::getPayouts($currentSeason->id);
$single_payout = 0;
$winnerCount = sizeof($dayScoreWinnersIds);
if ($winnerCount > 0) {
$single_payout = $payouts["Tagessieg"] / $winnerCount;
$single_payout = number_format((float)$single_payout, 2, '.', '');
$single_payout = str_replace(".", ",", $single_payout);
}
foreach ($round_ids as $round_id) {
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 ///////////////////
///////////////////////////// DISPLAY AUFBAU /////////////////////////////
///
// AUFBAU RANKINGLISTE A.K.A. RANKINGDISPLAY AB HIER NUR NOCH ANZEIGE-SPEZIFIKA !
// Aufbau der Rankingliste in rankingDisplay, sowie aufaddieren der jeweiligen Punkte.
// Loop through games and users (again)
$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']);}
$round_id = $max_round_id;
if (!array_key_exists($user_id, $rankingDisplay)) {
$rankingDisplay[$user_id] = array();
$key = $round_id . "_" . $user_id;
$rankingDisplay[$user_id]["user_id"] = $user_id;
if (!array_key_exists($user_id, $rankingDisplay)) {
$rankingDisplay[$user_id] = array();
$rankingDisplay[$user_id]["user_name"] = $roundtipps[$key]["user_name"];
$rankingDisplay[$user_id]["is_dayscorewinner"] = 0;
$rankingDisplay[$user_id]["user_id"] = $user_id;
$rankingDisplay[$user_id]["round_id"] = $round_id;
$rankingDisplay[$user_id]["rank"] = $roundtipps[$key]["rank"];
$rankingDisplay[$user_id]["payout"] = $roundtipps[$key]["payout"];
$rankingDisplay[$user_id]["user_name"] = $roundtipps[$key]["user_name"];
$rankingDisplay[$user_id]["is_dayscorewinner"] = $roundtipps[$key]["is_dayscorewinner"];
if ($user_id==1) { Log::info("rankscore after user init for display round_id ".$round_id." = ".$roundtipps[$key]["rankscore"]);}
if ($round_id==4 && $roundtipps[$key]["is_dayscorewinner"]) {
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]["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]);
$rankingDisplay[$user_id]["dayScoreWithRankScore"] = $roundtipps[$key]["dayScoreWithRankScore"]; // Dayscore der letzten durchlaufenen Runde, NICHT in init stellen !!
}
$rankingDisplay[$user_id]["totalRankScore"] = $roundtipps[$key]['totalRankScore'];
$rankingDisplay[$user_id]["tipp_score1"] = $roundtipps[$key]["tipps"][0];
$rankingDisplay[$user_id]["tipp_score2"] = $roundtipps[$key]["tipps"][1];
$rankingDisplay[$user_id]["joker1"] = $roundtipps[$key]["joker"][0];
$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]["joker"][0]);
$rankingDisplay[$user_id]["tipp_score3"] = $roundtipps[$key]["tipps"][2];
$rankingDisplay[$user_id]["tipp_score4"] = $roundtipps[$key]["tipps"][3];
$rankingDisplay[$user_id]["joker2"] = $roundtipps[$key]["joker"][1];
$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"][2],$roundtipps[$key]["tipps"][3],
$roundtipps[$key]["joker"][1]);
$rankingDisplay[$user_id]["tipp_score5"] = $roundtipps[$key]["tipps"][4];
$rankingDisplay[$user_id]["tipp_score6"] = $roundtipps[$key]["tipps"][5];
$rankingDisplay[$user_id]["joker3"] = $roundtipps[$key]["joker"][2];
$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"][4],$roundtipps[$key]["tipps"][5],
$roundtipps[$key]["joker"][2]);
$rankingDisplay[$user_id]["tipp_score7"] = $roundtipps[$key]["tipps"][6];
$rankingDisplay[$user_id]["tipp_score8"] = $roundtipps[$key]["tipps"][7];
$rankingDisplay[$user_id]["joker4"] = $roundtipps[$key]["joker"][3];
$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"][6],$roundtipps[$key]["tipps"][7],
$roundtipps[$key]["joker"][3]);
}
$userids = array_keys($rankingDisplay);
@ -376,71 +487,29 @@ class UsersController extends Controller
}
$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"]);
$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']);
return ($user1['totalRankScore'] < $user2['totalRankScore']);
}
public function cmpDayWin($user1, $user2) {
return ($user1[1] < $user2[1]); // Feld0: user_id, Feld1: dayScoreWithRankScore
}
public function cmpRank($rank1, $rank2) {
return ($rank1[1] < $rank2[1]);
}
public function show_payouts() {
$currentSeason = Season::getCurrent();