tippspiel/pfosten/app/Round.php

102 lines
2.8 KiB
PHP
Executable file

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Game;
use Log;
use DB;
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() {
$season = Season::getCurrent();
$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() {
$season_id = Season::getCurrent()->id;
$rounds = Round::where('season_id','=',$season_id)->orderBy('id')->get();
foreach($rounds as $round) {
if (!Round::isCompleted($round->id)) return $round->id;
}
return -1;
}
public static function getLastLockedRoundId() {
$season = Season::getCurrent();
$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 getFirstTippableRound() {
$season = Season::getCurrent();
$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;
}
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;
}
}