diff --git a/pfosten/app/Tipp.php b/pfosten/app/Tipp.php index e21d7472..9bb7f696 100755 --- a/pfosten/app/Tipp.php +++ b/pfosten/app/Tipp.php @@ -245,121 +245,139 @@ class Tipp extends Model // TODO: Implement partial caching - public static function getRoundTipps($max_round_id=-1, $seasonId=-1) { - $currentSeason=null; - if ($seasonId==null) { + public static function getRoundTipps($max_round_id=-1, $seasonId=-1) + { + $currentSeason = null; + if ($seasonId == null) { $currentSeason = Season::getCurrent(); } else { - $currentSeason = Season::where('id','=', $seasonId)->first(); + $currentSeason = Season::where('id', '=', $seasonId)->first(); } $startTime = microtime(true); $currentSeason = Season::getCurrent(); - if ($seasonId!=-1) { $currentSeason=Season::where('id','=',$seasonId)->first(); }; + if ($seasonId != -1) { + $currentSeason = Season::where('id', '=', $seasonId)->first(); + }; // 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 = "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 . "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.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); + $result = DB::select($sql); $roundtipps = array(); $roundData = array(); - $user_ids=array(); - $round_ids=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::getLastCompleteRoundId($currentSeason->id); } + if ($max_round_id == -1) { + $max_round_id = Round::getLastCompleteRoundId($currentSeason->id); + } foreach ($result as $entry) { $round_id = $entry->round_id; - if ($max_round_id >-1 && $round_id>$max_round_id) break; + 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); } - if (!in_array($round_id,$roundData)) { $roundData[$round_id]=array(); } + 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); + } + if (!in_array($round_id, $roundData)) { + $roundData[$round_id] = array(); + } - $user_id = $entry->user_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; + $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; $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; - $roundtipps[$key]["ranking_payout"]=0; // Existiert nur in der letzten Runde + $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; + $roundtipps[$key]["ranking_payout"] = 0; // Existiert nur in der letzten Runde } - if (!array_key_exists('is_completed',$roundData[$round_id])) { + if (!array_key_exists('is_completed', $roundData[$round_id])) { $roundData[$round_id]['is_completed'] = true; } - $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; + $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 ($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) { + if ($entry->game_score1 == -1 || $entry->game_score2 == -1) { $roundtipps[$key]['round_completed'] = false; // TODO: Remove it, its succeeded by roundData['is_completed'] - $rounds[$round_id]['is_completed'] = false; + $rounds[$round_id]['is_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); + 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; } + 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. (COMPUTE AGGREGATED VALUES) EXCEPT GREENSHIRT, ARGH !! ///////////////////////////////////////////////////////////////////// - $highestDayscores=array(); - foreach($user_ids as $user_id) { - $totalRankScore=0; - $foulCount=0; // Green Shirt Foul Counter - foreach($round_ids as $round_id) { - $key=$round_id."_".$user_id; + $highestDayscores = array(); + foreach ($user_ids as $user_id) { + $totalRankScore = 0; + $foulCount = 0; // Green Shirt Foul Counter + 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']); + $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); + $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'];} + $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; @@ -371,33 +389,65 @@ class Tipp extends Model // Sortier-Array nach Rang (nur für max_round_id) $userRankings = array(); foreach ($user_ids as $user_id) { - $key = $max_round_id."_".$user_id; - //error_log("Pushing:". $user_id." => ".$key); - array_push($userRankings, [$user_id , $roundtipps[$key]['totalRankScore']]); + $key = $max_round_id . "_" . $user_id; + array_push($userRankings, [$user_id, $roundtipps[$key]['totalRankScore']]); } - usort($userRankings, array('App\Tipp','cmpRank')); + usort($userRankings, array('App\Tipp', 'cmpRank')); // Ranknr identifizieren, inkl, geteilter Rankplatzierungen - $lastScore=-1; - $lastrank=0; - $current_list_position=0; - foreach($userRankings as $userRank) { - $user_id=$userRank[0]; - $totalRankScore=$userRank[1]; + $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; + } + } - foreach($round_ids as $round_id) { + //////////////////////////////////////////////////////// + // Sortier-Array für GShirt nach rankscore des Tages (für jede round_id) + + foreach ($round_ids as $round_id) { + $userRankings = array(); + foreach ($user_ids as $user_id) { + $key = $round_id . "_" . $user_id; + array_push($userRankings, [$user_id, $roundtipps[$key]['rankscore']]); + } + usort($userRankings, array('App\Tipp', 'cmpRank')); + + // Ranknr identifizieren, inkl, geteilter Rankplatzierungen + $lastScore = -1; + $lastGrank = 0; + $current_list_position = 0; + foreach ($userRankings as $userRank) { + $user_id = $userRank[0]; + $totalRankScore = $userRank[1]; $key = $round_id . "_" . $user_id; $current_list_position += 1; - if ($roundtipps[$key]["totalRankScore"] == $lastScore) { - $roundtipps[$key]["rank"] = $lastrank; + if ($roundtipps[$key]["rankscore"] == $lastScore) { + $roundtipps[$key]["grank"] = $lastGrank; } else { - $lastrank = $current_list_position; - $roundtipps[$key]["rank"] = $current_list_position; + $lastGrank = $current_list_position; + $roundtipps[$key]["grank"] = $current_list_position; $lastScore = $totalRankScore; } } } + + + + + + //////////////////////////////////////////////////////// // Greenshirt Score Determination // Loop again over roundtipps, sort each round, give/write forth greenshirt score @@ -421,32 +471,38 @@ class Tipp extends Model foreach($user_ids as $user_id) { $key=$round_id."_".$user_id; - $rank=$roundtipps[$key]["rank"]; - if(!array_key_exists($rank, $members)) { - $members[$rank]=0; + $grank=$roundtipps[$key]["grank"]; + if(!array_key_exists($grank, $members)) { + $members[$grank]=0; } - $members[$rank]+=1; + $members[$grank]+=1; } // A-2 compute gain $scoreGain=[0,10,5,3,2,1]; // Rank 0 existiert nicht // Move gain out of empty ranks into the upper ones. - foreach ([5,4,3,2,1] as $rank) { - if (!array_key_exists($rank,$members)) { - $scoreGain[$rank-1]+= $scoreGain[$rank]; - $scoreGain[$rank]=0; // Shouldnt be needed, but makes movement of gain explicit + foreach ([5,4,3,2,1] as $grank) { + if (!array_key_exists($grank,$members)) { + $scoreGain[$grank-1]+= $scoreGain[$grank]; + $scoreGain[$grank]=0; // Shouldnt be needed, but makes movement of gain explicit } } // Distribute gain foreach($user_ids as $user_id) { $key = $round_id . "_" . $user_id; - $rank= $roundtipps[$key]["rank"]; - if ($rank<=5) { - $roundtipps[$key]["gshirt_score"] = $scoreGain[$rank] / $members[$rank]; + $grank= $roundtipps[$key]["grank"]; + if ($grank<=5) { + $gain = round($scoreGain[$grank] / $members[$grank], 2); + $roundtipps[$key]["gshirt_score"] = $gain; } else { $roundtipps[$key]["gshirt_score"]=0; } + + if ($user_id==11) { + Log::info("round_id ".$round_id." grank=".$grank.", gshirt_score= ".$roundtipps[$key]["gshirt_score"]); + } + } } @@ -455,7 +511,7 @@ class Tipp extends Model - +/* foreach($user_ids as $user_id) { foreach($round_ids as $round_id) { $key=$round_id."_".$user_id; @@ -463,14 +519,16 @@ foreach($user_ids as $user_id) { $roundtipps[$key]['gshirt_foulcount']=-1; } } - +*/ /* FIND BUG ! +*/ foreach($user_ids as $user_id) { $foulCount=0; $totalScore=0; foreach ($round_ids as $round_id) { $key = $round_id . "_" . $user_id; + $score = $roundtipps[$key]['gshirt_score']; $isFoul = ( $roundtipps[$key]["rankscore"] == 0 ); if ($isFoul) { @@ -480,20 +538,22 @@ foreach($user_ids as $user_id) { $totalScore=0; } } - $totalScore+=$score; + $totalScore += $score; $roundtipps[$key]['gshirt_totalscore']=$totalScore; $roundtipps[$key]['gshirt_foulcount']=$foulCount; - if ($user_id==14) { - error_log("round_id=" . $round_id .",gshirt_score=".$roundtipps[$key]['gshirt_score'].", score=" . $score.", totalScore=".$totalScore); + if ($user_id==11) { + //error_log("round_id=" . $round_id .",gshirt_score=".$roundtipps[$key]['gshirt_score'].", score=" . $score.", totalScore=".$totalScore); + Log::info("round_id=" . $round_id .",gshirt_score=".$roundtipps[$key]['gshirt_score'].", score=" . $score.", totalScore=".$totalScore); } - error_log("Test: key=".$key.", ".$roundtipps[$key]['gshirt_score']); + //error_log("Test: key=".$key.", ".$roundtipps[$key]['gshirt_score']); + //Log::info("Test: key=".$key.", ".$roundtipps[$key]['gshirt_score']); } } - //dd("stop"); -*/ + //dd("User 11 Round 1 gshirt_score =".$roundtipps['1_11']['gshirt_score'].", totalscore=".$roundtipps['1_11']['gshirt_totalscore']); + ////////////////////////////// // START OF HURTFUL 6 PLACE RESTRICTION IF DAYSCORE WIN (exception: completely even scores)