tippspiel/pfosten/app/Round.php

221 lines
6.5 KiB
PHP
Executable file

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Game;
use App\Season;
use Log;
use DB;
use Carbon;
class Round extends Model
{
protected $fillable = [
'name', 'season_id'
];
public function games()
{
return $this->hasMany('App\Game');
}
// true, wenn irgend ein Game locked ist.
public static function hasLockedGame($round_id)
{
//Log::info("Check Round::HasLockedGame => ".$round_id);
$games = Game::where('round_id', $round_id)->get();
foreach ($games as $game) {
if (Game::locked($game->id)==true) {
return true;
}
}
return false;
}
public static function isCompleted($round_id) {
$round = Round::where('id','=',$round_id)->first();
$games = Game::where('round_id','=',$round->id)->get();
$completed = true;
foreach($games as $game) {
if ($game->score1==-1 || $game->score2==-1) {
$completed=false;
}
}
return $completed;
}
public static function getLastCompleteRoundId($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season = Season::getCurrent();
} else {
$season=Season::where('id','=',$seasonId)->first();
}
if (!$season) return -1;
$seasonId=$season->id;
$rounds=Round::where('season_id','=',$season->id)->orderBy('id')->get();
$lastCompleteRound=-1;
foreach ($rounds as $round) {
if (Round::isCompleted($round->id) == true) {
$lastCompleteRound = $round->id;
}
}
return $lastCompleteRound;
}
public static function getFirstIncompleteRound($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season = Season::getCurrent();
} else {
$season = Season::where('id','=',$seasonId)->first();
}
if (!$season) return -1;
$seasonId=$season->id;
$rounds = Round::where('season_id','=',$seasonId)->orderBy('id')->get();
foreach($rounds as $round) {
if (!Round::isCompleted($round->id)) return $round->id;
}
return -1;
}
public static function getNextRoundId($roundId, $seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$currentSeason=Season::getCurrent();
} else {
$currentSeason=Season::where('id','=', $seasonId)->first();
}
if (!$currentSeason) return -1;
$seasonId = $currentSeason->id;
$rounds = Round::where('season_id','=',$seasonId)->where('id','>=',$roundId)->get();
foreach($rounds as $round) {
if ($round->id == $roundId) { continue; }
return $round->id;
}
return -1;
}
public static function getLastLockedRoundId($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season=Season::getCurrent();
} else {
$season=Season::where('id','=', $seasonId)->first();
}
if (!$season) return -1;
$rounds=Round::where('season_id','=',$season->id)->orderBy('id')->get();
$lastLockedRoundId=-1;
foreach ($rounds as $round) {
if (Round::hasLockedGame($round->id) == true) {
$lastLockedRoundId = $round->id;
}
}
return $lastLockedRoundId;
}
public static function getFirstTippableRoundId($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season=Season::getCurrent();
} else {
$season=Season::where('id','=', $seasonId)->first();
}
if (!$season) return null;
$round = Round::getFirstTippableRound($season->id);
if (!$round) return -1;
return $round->id;
}
public static function getFirstTippableRound($seasonId=-1) {
$season=null;
if ($seasonId==-1) {
$season=Season::getCurrent();
} else {
$season=Season::where('id','=', $seasonId)->first();
}
if (!$season) return null;
$rounds=Round::where('season_id','=',$season->id)->orderBy('id')->get();
$locked = false;
foreach ($rounds as $round) {
if (!Round::hasLockedGame($round->id) ) {
return $round;
}
}
return null;
}
// TODO: CHeck : Unused ??
public static function getClosestRoundToNow() {
$sql="select round_id,DATEDIFF(NOW(),kickoff) from games order by 2 desc limit 1;";
$result=DB::select($sql);
if (count($result)==0) return 0;
return $result[0]->round_id;
}
public static function getTippSpielLink() // Mit Verweis auf den aktuellen Tipp
{
$firstTippableRoundId=Round::getFirstTippableRound()->id;
$tippSpielLink="http://hoschis-tippspiel.de/tipps#".$firstTippableRoundId;
return $tippSpielLink;
}
// Ab sofort Verzicht auf statische Methoden, soweit möglich.
public function getLockTime() {
$games = Game::where('round_id','=',$this->id)->get();
$earliestKickoff = Carbon\Carbon::now();
$earliestKickoff->addWeeks(100);
foreach($games as $game) {
$kickoff = Carbon\Carbon::parse($game->kickoff);
if ($kickoff < $earliestKickoff) { $earliestKickoff = $kickoff; }
}
$earliestKickoff->subHour();
return $earliestKickoff;
}
public function isInLockWarningTime($hours) {
$hourDiffToLock=$this->getLockTime()->diffInHours(Carbon\Carbon::now());
if ($hourDiffToLock<=$hours) return true;
return false;
}
public function getRemainingHoursBeforeLock() {
$hourDiffToLock=$this->getLockTime()->diffInHours(Carbon\Carbon::now());
return $hourDiffToLock;
}
public static function getNames($seasonId=-1) {
Log::info("Round::getNames for seasonId=".$seasonId);
$season= Season::getCurrent();
if ($seasonId!=-1) { $season = Season::where('id','=',$seasonId)->first(); }
$rounds = Round::where('season_id','=',$season->id)->get();
Log::info("Number of rounds:".sizeof($rounds));
Log::info("START");
$roundnames = array();
foreach($rounds as $round) {
$roundnames[$round->id] = $round->name;
}
return $roundnames;
}
public static function getLastRoundId($seasonId) {
$round = Round::where('season_id', '=', $seasonId)->orderBy('id','desc')->first(); // get last entry
return $round->id;
}
}