741 lines
29 KiB
PHP
Executable file
741 lines
29 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use DB;
|
|
use Carbon;
|
|
use Log;
|
|
|
|
class Tipp extends Model
|
|
{
|
|
protected $fillable = [
|
|
'id', 'user_id', 'game_id','score1','score2','joker'
|
|
];
|
|
|
|
public static function trendCol($score1, $score2, $tipp1, $tipp2, $joker) {
|
|
$tippTendenz = $tipp1 - $tipp2;
|
|
|
|
if ($tippTendenz > 0) {$tippTendenz=1;}
|
|
if ($tippTendenz < 0) {$tippTendenz=-1;}
|
|
|
|
$gameTendenz = $score1 - $score2;
|
|
if ($gameTendenz<0) {$gameTendenz=-1;}
|
|
if ($gameTendenz>0) {$gameTendenz=1;}
|
|
|
|
if ($tippTendenz!=$gameTendenz || $score1 ==-1 || $score2==-1 || $tipp1 == -1 || $tipp2==-1) {
|
|
return "";
|
|
}
|
|
|
|
$styling='background-color:grey;';
|
|
|
|
if ( ($tipp1 == $score1) && ($tipp2 == $score2) ) {
|
|
$styling='background-color:orange;';
|
|
if ($joker) {
|
|
$styling='background-color:red';
|
|
}
|
|
}
|
|
return $styling;
|
|
}
|
|
|
|
|
|
// get tendency Color
|
|
public static function getCol($user_id,$game_id) {
|
|
$tipp = Tipp::getTipp($user_id,$game_id);
|
|
$game = Game::where('id',$tipp->game_id)->first();
|
|
|
|
$tippTendenz = $tipp->score1 - $tipp->score2;
|
|
if ($tippTendenz > 0) {$tippTendenz=1;}
|
|
if ($tippTendenz < 0) {$tippTendenz=-1;}
|
|
|
|
$gameTendenz = $game->score1 - $game->score2;
|
|
if ($gameTendenz<0) {$gameTendenz=-1;}
|
|
if ($gameTendenz>0) {$gameTendenz=1;}
|
|
|
|
if ($tippTendenz!=$gameTendenz || $game->score1 ==-1 || $game->score2==-1 || $tipp->score1 == -1 || $tipp->score2==-1) {
|
|
return "";
|
|
}
|
|
|
|
$styling='background-color:grey;';
|
|
|
|
if ( ($tipp->score1 == $game->score1) && ($tipp->score2 == $game->score2) ) {
|
|
$styling='background-color:orange;';
|
|
if ($tipp->joker==true) {
|
|
$styling='background-color:red';
|
|
}
|
|
}
|
|
return $styling;
|
|
|
|
}
|
|
|
|
public static function getTipp( $user_id,$game_id) {
|
|
//Log::info("getTipp for game_id, user_id = ".$game_id.",".$user_id);
|
|
return DB::table('tipps')->where([
|
|
['user_id','=',$user_id],
|
|
['game_id','=',$game_id]
|
|
])->first();
|
|
}
|
|
|
|
public static function getScoreLocal($goals1, $goals2, $tipp1, $tipp2, $joker) {
|
|
$score=0;
|
|
$tippTendenz = $tipp1 - $tipp2;
|
|
if ($tippTendenz > 0) {$tippTendenz=1;}
|
|
if ($tippTendenz < 0) {$tippTendenz=-1;}
|
|
|
|
$gameTendenz = $goals1 - $goals2;
|
|
if ($gameTendenz<0) {$gameTendenz=-1;}
|
|
if ($gameTendenz>0) {$gameTendenz=1;}
|
|
|
|
if ($tippTendenz==$gameTendenz) { $score=1; }
|
|
|
|
if ( ($tipp1 == $goals1) && ($tipp2 == $goals2) ) {
|
|
$bonus = max(0,$goals1 + $goals2 - 3);
|
|
$score= 2 + $bonus;
|
|
}
|
|
|
|
if ($joker==true) {
|
|
$score = $score * 2;
|
|
}
|
|
|
|
if ($tipp1==-1||$tipp2==-1||$goals1==-1||$goals2==-1) { $score=0; }
|
|
|
|
return $score;
|
|
}
|
|
|
|
// Bei Tagessieg gilt nur wer am meisten genau richtig getippt hat,
|
|
// Bei Gleichstand wer den höchsten Jokersieg hat ( Jokersieg mit mehr Toren zählt mehr)
|
|
// Bei Gleichstand gilt die Summe aller Rankscores, deshalb wird sie auch im Ergebnis gespeichert
|
|
// Fürs Dayscore-Bewerten ist rankscore allerdings irrelevant. Mit Rank nur fürs Sortieren, um die Auswahl einzugrenzen.
|
|
public static function getDayScoreLocal($goals1, $goals2, $tipp1, $tipp2, $joker, $rankScore=0) {
|
|
if ($tipp1==-1||$tipp2==-1||$goals1==-1||$goals2==-1) { return 0; } // Ungetippt oder Spiel hat noch kein Ergebnis
|
|
if ( ($tipp1 != $goals1) || ($tipp2 != $goals2) ) return 0;
|
|
|
|
$jokerValue=0;
|
|
if ($joker) {
|
|
$jokerValue = max(0, $goals1 + $goals2 - 3) + 1;
|
|
}
|
|
return 1000 + $jokerValue * 100 + $rankScore;
|
|
}
|
|
|
|
|
|
public static function getTippScore($user_id,$game_id, $team=0) {
|
|
//Log::info("Getting tippstring for game_id=". $game_id .", user_id=" . $user_id.", $team=".$team);
|
|
|
|
$tipp=Tipp::getTipp($user_id,$game_id);
|
|
if (!$tipp) {
|
|
//Log::info("not found !");
|
|
return "x";
|
|
}
|
|
|
|
$tippScore="";
|
|
if ($team==0) { $tippScore= $tipp->score1;}
|
|
else { $tippScore=$tipp->score2; }
|
|
if ($tippScore==-1) { /*Log::info("untipped!");*/$tippScore="x";}
|
|
|
|
//Log::info("Returning: ". $tippScore);
|
|
return $tippScore;
|
|
}
|
|
|
|
public static function getScore($tipp_id) {
|
|
$tipp = Tipp::where('id',$tipp_id)->first();
|
|
$game = Game::where('id',$tipp->game_id)->first();
|
|
|
|
$bonus = max(0,$game->score1 + $game->score2 - 3);
|
|
|
|
$tippTendenz = $tipp->score1 - $tipp->score2;
|
|
if ($tippTendenz > 0) {$tippTendenz=1;}
|
|
if ($tippTendenz < 0) {$tippTendenz=-1;}
|
|
|
|
$gameTendenz = $game->score1 - $game->score2;
|
|
if ($gameTendenz<0) {$gameTendenz=-1;}
|
|
if ($gameTendenz>0) {$gameTendenz=1;}
|
|
|
|
$score=0;
|
|
|
|
if ($tippTendenz == $gameTendenz) { $score=1; }
|
|
|
|
if ( ($tipp->score1 == $game->score1) && ($tipp->score2 == $game->score2) ) {
|
|
$score=2 + $bonus;
|
|
}
|
|
|
|
if ($tipp->joker==true) {
|
|
$score = $score *2;
|
|
}
|
|
|
|
if ($game->score1 == -1) { $score=0; }
|
|
if ($tipp->score1==-1 || $tipp->score2==-1) {$score=0;}
|
|
|
|
return $score;
|
|
}
|
|
|
|
public static function resultString($tipp_id) {
|
|
$tipp = Tipp::where('id',$tipp_id)->first();
|
|
$rStr="Ungetippt";
|
|
if ($tipp->score1 > -1) $rStr= $tipp->score1 . " : " . $tipp->score2;
|
|
|
|
return $rStr;
|
|
}
|
|
|
|
public static function createTipps($user_id) {
|
|
$activeSeason = Season::getCurrent();
|
|
|
|
$rounds = Round::all()->where('season_id', '=', $activeSeason->id);
|
|
foreach($rounds as $round) {
|
|
|
|
$games=Game::all()->where('round_id','=',$round->id);
|
|
foreach($games as $game) {
|
|
|
|
// Tipp erzeugen wenn noch nicht vorhanden
|
|
$tipp = DB::table('tipps')->where([
|
|
['game_id','=',$game->id],
|
|
['user_id','=',$user_id]]
|
|
)->first();
|
|
if (! $tipp) {
|
|
$tipp=Tipp::create([
|
|
'user_id' => $user_id,
|
|
'game_id'=> $game->id,
|
|
'score1'=> -1,
|
|
'score2'=> -1,
|
|
'joker'=> false
|
|
]);
|
|
$tipp->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function createAllTips() {
|
|
$users = DB::table('users')->where('deleted',null)->get();
|
|
foreach($users as $user) {
|
|
Log::info("Creating tipps for userid ".$user->id);
|
|
Tipp::createTipps($user->id);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Struktur: gameval * 1000 + jokerValue * 100 + rankscore
|
|
public static function addDayScoreValues($val1, $val2) {
|
|
$gameVal1 = intdiv($val1 , 1000);
|
|
$gameVal2 = intdiv( $val2, 1000);
|
|
|
|
$jokerVal1 = intdiv(($val1 - $gameVal1*1000),100);
|
|
$jokerVal2 = intdiv(($val2 - $gameVal2*1000),100);
|
|
|
|
if ( $gameVal1 ==0 ) { $jokerVal1=0; $rankVal1=0; }
|
|
if ( $gameVal2 ==0 ) { $jokerVal2=0; $rankVal2=0; }
|
|
|
|
$rankVal1 = $val1 - $gameVal1*1000 - $jokerVal1*100;
|
|
$rankVal2 = $val2 - $gameVal2*1000 - $jokerVal2*100;
|
|
|
|
// Minimaler Brainfuck, da $rankVal1 und $rankVal2 immer gleich sein sollten
|
|
$result = ($gameVal1 + $gameVal2) * 1000 + max($jokerVal1, $jokerVal2) * 100 + max ($rankVal1, $rankVal2);
|
|
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function getDayScoreWithoutRankScore($val) {
|
|
$gameVal = intdiv($val , 1000);
|
|
$jokerVal = intdiv(($val - $gameVal*1000),100);
|
|
if ($gameVal==0) return 0;
|
|
return $gameVal * 1000 + $jokerVal * 100;
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement partial caching
|
|
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();
|
|
}
|
|
|
|
$startTime = microtime(true);
|
|
|
|
$currentSeason = Season::getCurrent();
|
|
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 = $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();
|
|
$roundData = 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);
|
|
}
|
|
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);
|
|
}
|
|
if (!in_array($round_id, $roundData)) {
|
|
$roundData[$round_id] = array();
|
|
}
|
|
|
|
$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;
|
|
$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
|
|
|
|
}
|
|
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;
|
|
|
|
|
|
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; // TODO: Remove it, its succeeded by roundData['is_completed']
|
|
$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);
|
|
|
|
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;
|
|
// 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('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];
|
|
$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;
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////
|
|
// 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]["rankscore"] == $lastScore) {
|
|
$roundtipps[$key]["grank"] = $lastGrank;
|
|
} else {
|
|
$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
|
|
|
|
|
|
$userList=array(); // Only to detect ranks 1-5; score results will get written to roundtipps[$key]['...']
|
|
|
|
// (A) SET GSCORE EVERY ROUND
|
|
// 1.) Loop over roundtipps and : a.) add user + rankscore + foulcount to $userList
|
|
// b.) Sort $userList by rankscore
|
|
// c.) Determine ranks 1-5 and number of points to distribute
|
|
// d.) Mark Rank1 as is_gshirt_owner
|
|
// e.) Distribute points, set roundtipps[$key]['gshirt_score'], every other to zero
|
|
// (B) SET GSCORE_TOTAL EVERY ROUND
|
|
// 2.) Loop over roundtipps and: a.) add gshirt_score to gshirt_score_total --OR-- zero gshirt_score_total if foulCount==3
|
|
|
|
|
|
// A) SET GSCORE EVERY ROUND
|
|
foreach($round_ids as $round_id) {
|
|
// A-1 fill members with numbers of users per rank
|
|
$members=array();
|
|
foreach($user_ids as $user_id) {
|
|
$key=$round_id."_".$user_id;
|
|
|
|
$grank=$roundtipps[$key]["grank"];
|
|
if(!array_key_exists($grank, $members)) {
|
|
$members[$grank]=0;
|
|
}
|
|
$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 $grank) {
|
|
if (!array_key_exists($grank,$members)) {
|
|
$scoreGain[$grank-1]+= $scoreGain[$grank];
|
|
$scoreGain[$grank]=0; // Shouldnt be needed, but makes movement of gain explicit
|
|
} else {
|
|
|
|
}
|
|
}
|
|
|
|
|
|
//dd($log."Rank gain: ".$scoreGain[1].", ".$scoreGain[2].", ".$scoreGain[3].", ",$scoreGain[4].", ".$scoreGain[5]);
|
|
|
|
|
|
// Distribute gain
|
|
foreach($user_ids as $user_id) {
|
|
$key = $round_id . "_" . $user_id;
|
|
$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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// B) SET GSCORE TOTAL EVERY ROUND ( and take fouls into consideration )
|
|
|
|
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) {
|
|
$foulCount+=1;
|
|
if ($foulCount==3) {
|
|
$foulCount=0;
|
|
$totalScore=0;
|
|
}
|
|
}
|
|
$totalScore += $score;
|
|
$roundtipps[$key]['gshirt_totalscore']=$totalScore;
|
|
$roundtipps[$key]['gshirt_foulcount']=$foulCount;
|
|
|
|
|
|
//error_log("Test: key=".$key.", ".$roundtipps[$key]['gshirt_score']);
|
|
//Log::info("Test: key=".$key.", ".$roundtipps[$key]['gshirt_score']);
|
|
}
|
|
}
|
|
|
|
//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)
|
|
// 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();
|
|
$totalDayWinPayout = 0;
|
|
|
|
$round_count=0;
|
|
|
|
//error_log("Round ids: ".implode(",",$round_ids));
|
|
|
|
foreach ($round_ids as $round_id) {
|
|
$round_count += 1;
|
|
$totalDayWinPayout += $payouts["tagessieg"];
|
|
|
|
$highestDayScore = $highestDayscores[$round_id];
|
|
$usersWithHighestDayScore = array();
|
|
$round_completed = false;
|
|
|
|
foreach ($user_ids as $user_id) {
|
|
$key = $round_id . "_" . $user_id;
|
|
if ($roundtipps[$key]['dayscore'] == $highestDayScore && $roundtipps[$key]['round_completed'] && $highestDayScore > 0) {
|
|
array_push($usersWithHighestDayScore, [$user_id, $roundtipps[$key]['dayScoreWithRankScore']]);
|
|
}
|
|
}
|
|
|
|
$dayScoreWinnersIds = array();
|
|
// Bei >6 Tagessiegern über rankscore reduzieren
|
|
if (sizeof($usersWithHighestDayScore) > 6) {
|
|
|
|
usort($usersWithHighestDayScore, array('App\Tipp', 'cmpDayWin'));
|
|
$count = 0;
|
|
$cutOffScore = 0;
|
|
|
|
foreach ($usersWithHighestDayScore as $user) { // Feld0: user_id, dayScoreWithRankScore
|
|
$key = $max_round_id . "_" . $user[0]; // Hier: $user[0] = user_id, $user[1] = dayScore WithRankScore
|
|
$rankscore = $user[1];
|
|
$count += 1;
|
|
|
|
if ($count == 1) {
|
|
$cutOffScore = $rankscore;
|
|
}
|
|
|
|
if ($rankscore < $cutOffScore) {
|
|
break;
|
|
}
|
|
|
|
array_push($dayScoreWinnersIds, $user[0]);
|
|
$key = $round_id . "_" . $user[0];
|
|
$roundtipps[$key]['is_dayscorewinner'] = true;
|
|
}
|
|
}
|
|
// Bei bis zu 6 Gewinnern normal als Sieger klassifizieren
|
|
else {
|
|
foreach ($usersWithHighestDayScore as $user) {
|
|
array_push($dayScoreWinnersIds, $user[0]);
|
|
$key = $round_id . "_" . $user[0];
|
|
$roundtipps[$key]['is_dayscorewinner'] = true;
|
|
}
|
|
}
|
|
|
|
// 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 HURTFUL 6 PLACE RESTRICTION
|
|
|
|
$quotas=Quota::getQuotas();
|
|
|
|
error_log("Round-Count=".$round_count);
|
|
|
|
if ($round_count==Round::getCount()) {
|
|
// Ranking Payout ausschütten
|
|
$payout_groups =array();
|
|
$lastRoundId= array_values(array_slice($round_ids,-1))[0];
|
|
foreach($user_ids as $user_id) {
|
|
$key = $lastRoundId."_".$user_id;
|
|
$rank = $roundtipps[$key]["rank"];
|
|
//Log::info("Setting ranking_payout for user_id ".$user_id);
|
|
//TODO: Do it for any number of rank payouts
|
|
if ($rank<11) {
|
|
if (!array_key_exists($rank,$payout_groups)) {
|
|
$payout_groups[$rank]=array();
|
|
}
|
|
array_push($payout_groups[$rank],$user_id);
|
|
}
|
|
}
|
|
|
|
// platz0= unsused, just for getting place equal to rank position.
|
|
$rank_names=['platz0','platz1','platz2','platz3','platz4','platz5','platz6','platz7','platz8','platz9','platz10'];
|
|
|
|
// Ausschüttungsgruppen für Platzierung bilden
|
|
foreach([1,2,3,4,5,6,7,8,9,10] as $rank) { // Jedes Payout, das verteilt werden muss
|
|
$current_rank = $rank;
|
|
$rankPayout=$payouts[$rank_names[$rank]];
|
|
|
|
while($current_rank) {
|
|
while (!array_key_exists($current_rank, $payout_groups)) { $current_rank -=1; }
|
|
$groupsize = sizeof($payout_groups[$current_rank]);
|
|
|
|
$single_payout = $rankPayout / $groupsize;
|
|
foreach($payout_groups[$current_rank] as $user_id) {
|
|
$key = $lastRoundId."_".$user_id;
|
|
$roundtipps[$key]["ranking_payout"] += $single_payout;
|
|
}
|
|
$current_rank=0;
|
|
}
|
|
}
|
|
}
|
|
|
|
//////////////////// END OF ROUNDTIPPS CREATION ///////////////////
|
|
|
|
$response = array();
|
|
$response['roundtipps'] = $roundtipps;
|
|
$response['roundData'] = $roundData;
|
|
$response['user_ids'] = $user_ids;
|
|
$response['round_ids'] = $round_ids;
|
|
|
|
Log::info("Tipp::getRoundTipps: ExecutionTime: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n");
|
|
|
|
return $response;
|
|
}
|
|
|
|
// Used by getRoundTipps method
|
|
public static function cmpRank($rank1, $rank2) {
|
|
return ($rank1[1] < $rank2[1]); //Feld0: user_id, Feld1: rank
|
|
}
|
|
|
|
// Used by getRoundTipps method
|
|
public static function cmpDayWin($user1, $user2) {
|
|
return ($user1[1] < $user2[1]); // Feld0: user_id, Feld1: dayScoreWithRankScore
|
|
}
|
|
|
|
// Used by getRoundTipps
|
|
public static function cmpGShirt($rank1, $rank2) {
|
|
return ($rank1[1] < $rank2[1]); //Feld0: user_id, Feld1: rank
|
|
}
|
|
|
|
//Used by gshirt_table: sort by gshirt_totalscore
|
|
public static function cmpGShirtTotal($ranktippEntry1, $ranktippEntry2) {
|
|
return ($ranktippEntry1['gshirt_totalscore'] < $ranktippEntry2['gshirt_totalscore']); //Feld0: user_id, Feld1: rank
|
|
}
|
|
|
|
public static function gshirt_table() {
|
|
$rt = Tipp::getRoundTipps();
|
|
$roundtipps = $rt['roundtipps'];
|
|
$round_ids = $rt['round_ids'];
|
|
$last_round_id = end($round_ids);
|
|
$users=User::getActives();
|
|
|
|
$roundNames = Round::getNames();
|
|
|
|
$lastroundentries=array();
|
|
foreach ($users as $user) {
|
|
$key=$last_round_id."_".$user->id;
|
|
array_push($lastroundentries, $roundtipps[$key]);
|
|
|
|
}
|
|
usort($lastroundentries, array('App\Tipp', 'cmpGShirtTotal'));
|
|
|
|
$rankingDisplay=array();
|
|
foreach($lastroundentries as $entry) {
|
|
$user_id = $entry['user_id'];
|
|
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][$round_id]=array();
|
|
$roundtipps[$key]["round_name"]=$roundNames[$round_id];
|
|
array_push($rankingDisplay[$user_id][$round_id],$roundtipps[$key]);
|
|
}
|
|
}
|
|
|
|
//Rundenreihenfolge umkehren: Neuste zuerst
|
|
foreach($lastroundentries as $entry){
|
|
$user_id = $entry['user_id'];
|
|
$rankingDisplay[$user_id]=array_reverse($rankingDisplay[$user_id]);
|
|
}
|
|
|
|
return $rankingDisplay;
|
|
|
|
}
|
|
}
|