tippspiel/pfosten/app/Tipp.php

151 lines
4.5 KiB
PHP
Raw Normal View History

2017-06-20 08:43:36 +02:00
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
2017-06-22 17:00:09 +02:00
use DB;
2017-06-26 17:21:30 +02:00
use Carbon;
use Log;
2017-06-20 08:43:36 +02:00
class Tipp extends Model
{
2017-06-22 17:00:09 +02:00
protected $fillable = [
'id', 'user_id', 'game_id','score1','score2','joker'
];
2017-06-26 17:21:30 +02:00
// get tendency Color
public static function getCol($user_id,$game_id) {
$tipp = Tipp::getTipp($user_id,$game_id);
2017-06-26 17:21:30 +02:00
$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);
2017-06-22 17:00:09 +02:00
return DB::table('tipps')->where([
['user_id','=',$user_id],
['game_id','=',$game_id]
])->first();
}
2017-06-26 17:21:30 +02:00
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)
public static function getDayScoreLocal($goals1, $goals2, $tipp1, $tipp2, $joker) {
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 100 + $jokerValue;
}
2017-06-26 17:21:30 +02:00
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;
}
2017-06-22 17:00:09 +02:00
public static function getScore($tipp_id) {
$tipp = Tipp::where('id',$tipp_id)->first();
$game = Game::where('id',$tipp->game_id)->first();
2017-06-26 17:21:30 +02:00
$bonus = max(0,$game->score1 + $game->score2 - 3);
2017-06-22 17:00:09 +02:00
$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) ) {
2017-06-26 17:21:30 +02:00
$score=2 + $bonus;
2017-06-22 17:00:09 +02:00
}
if ($tipp->joker==true) {
$score = $score *2;
}
if ($game->score1 == -1) { $score=0; }
2017-06-26 17:21:30 +02:00
if ($tipp->score1==-1 || $tipp->score2==-1) {$score=0;}
2017-06-22 17:00:09 +02:00
return $score;
}
2017-06-26 17:21:30 +02:00
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;
}
2017-06-20 08:43:36 +02:00
}