Rangliste almost done

This commit is contained in:
Torsten 2017-06-26 17:21:30 +02:00
parent 12651fc2e8
commit 43fd8c8cbf
579 changed files with 172489 additions and 127 deletions

View file

@ -3,10 +3,44 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use Log;
use Carbon;
class Game extends Model
{
protected $fillable = [
'id','round_id','team1','team2','score1','score2','kickoff'
];
// true, wenn Anstoss - 1hr > now()
public static function locked($game_id)
{
$game = Game::find($game_id);
$now = Carbon\Carbon::now();
$tipp_input_end = Carbon\Carbon::parse($game->kickoff)->addHour(-1);
return ($tipp_input_end <= $now);
}
public static function has_result($game_id) {
$game = Game::where('id',$game_id)->first();
$hasIt = $game->score1 > -1;
if ($hasIt==false) {
return false;
}
return true;
}
public static function resultString($game_id) {
$game = Game::where('id',$game_id)->first();
if (!Game::has_result($game_id)) { return "Anstoss: " . $game->kickoff;}
return $game->score1. " : " . $game->score2;
}
}

View file

@ -5,18 +5,21 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Game;
use Carbon\Carbon;
use Auth;
use App\User;
class GamesController extends Controller
{
public function edit($game_id) {
if (Auth::guest()) { return redirect("/login"); }
$game=Game::where('id',$game_id)->first();
//$testdate=str_replace(' UTC ','T',date('Y-m-d h:m'));
return view('games.edit',compact('game'));
$kickoff_german = User::toGermanDate($game->kickoff);
return view('games.edit',compact('game','kickoff_german'));
}
public function store() {
if (Auth::guest()) { return redirect("/login"); }
// Validate form
$this->validate(request(), [
'id' => 'required',
@ -33,7 +36,7 @@ class GamesController extends Controller
$game->team2 = request('team2');
$game->score1= request('score1');
$game->score2= request('score2');
$game->kickoff=request('kickoff');
$game->kickoff=User::toDBDate(request('kickoff'));
$game->save();
// Redirect to the Rounds Index

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Log;
class RegistrationController extends Controller
{
@ -18,6 +19,7 @@ class RegistrationController extends Controller
// Validate form
$this->validate(request(), [
'name' => 'required',
'fullname' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed'
@ -27,9 +29,18 @@ class RegistrationController extends Controller
//$user = User::create(request(['name','email','password']));
$user = User::create([
'name' => request('name'),
'fullname' => request('fullname'),
'email'=> request('email'),
'password' => bcrypt(request('password'))]);
// Sign them in
// First user is made admin automatically
Log::info("User count:" . User::all()->count());
if (User::all()->count()==1) {
$user->admin = true;
}
$user->save();
auth()->login($user);
// Redirect to the home page

View file

@ -6,11 +6,16 @@ use Illuminate\Http\Request;
use App\Season;
use App\Round;
use App\Game;
use App\User;
use DB;
use Auth;
class RoundsController extends Controller
{
public function index() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$activeSeason=Season::getCurrent();
$rounds =null;
@ -28,6 +33,9 @@ class RoundsController extends Controller
public function create() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$currentSeason = Season::getCurrent();
if ($currentSeason) {
return view('rounds.create', compact('currentSeason'));
@ -37,6 +45,8 @@ class RoundsController extends Controller
public function store() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
// Validate form
$this->validate(request(), [
@ -68,28 +78,28 @@ class RoundsController extends Controller
'round_id' => $round_id,
'team1' => request('game1_team1'),
'team2' => request('game1_team2'),
'kickoff' => request('game1_kickoff')
'kickoff' => User::toDBDate(request('game1_kickoff'))
]);
$game2 = Game::create([
'round_id' => $round_id,
'team1' => request('game2_team1'),
'team2' => request('game2_team2'),
'kickoff' => request('game2_kickoff')
'kickoff' => User::toDBDate(request('game2_kickoff'))
]);
$game3 = Game::create([
'round_id' => $round_id,
'team1' => request('game3_team1'),
'team2' => request('game3_team2'),
'kickoff' => request('game3_kickoff')
'kickoff' => User::toDBDate(request('game3_kickoff'))
]);
$game4 = Game::create([
'round_id' => $round_id,
'team1' => request('game4_team1'),
'team2' => request('game4_team2'),
'kickoff' => request('game4_kickoff')
'kickoff' => User::toDBDate(request('game4_kickoff'))
]);
// Sign them in
@ -105,6 +115,9 @@ class RoundsController extends Controller
public function storename()
{
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
// Validate form
$this->validate(request(), [
'round_id' => 'required',

View file

@ -5,10 +5,15 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Season;
use DB;
use Auth;
use Log;
class SeasonsController extends Controller
{
public function index() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$seasons = DB::table('seasons')->get();
$activeSeason=Season::getCurrent();
return view('seasons.index', compact('seasons','activeSeason'));
@ -16,11 +21,15 @@ class SeasonsController extends Controller
public function create() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
return view('seasons.create');
}
public function store() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
// Validate form
$this->validate(request(), [
@ -33,9 +42,23 @@ class SeasonsController extends Controller
'name' => request('name')
]);
if (Season::all()->count()==1) {
Season::setCurrent($season->id);
Log::info('Setting season id=' .$season->id);
}
// Redirect to the home page
return redirect('/seasons');
}
public function setcurrent($new_season_id) {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
Season::setCurrent($new_season_id);
return redirect("/seasons");
}
}

View file

@ -18,10 +18,6 @@ class SessionsController extends Controller
public function store() {
// Attempt to authenticate the user
if (!auth()->attempt(request(['name','password']))) {
return back()->withErrors(['message'=>'Bitte Logindaten prüfen und nochmal versuchen.']);
}

View file

@ -7,6 +7,7 @@ use App\Season;
use App\Round;
use App\Game;
use App\Tipp;
use App\User;
use Auth;
use Log;
@ -55,10 +56,24 @@ class TippsController extends Controller
public function index()
{
if (Auth::guest()) { return redirect("/"); }
$user = Auth::user();
$activeSeason = Season::getCurrent();
if (!$activeSeason) {
return redirect('/');
}
if (! User::payment_date($user->id, $activeSeason->id)) {
return view('users.no_payment');
}
$rounds = null;
$user = Auth::user();
TippsController::createTipps($user->id); // ggfs. fehlender Tipp Eintraege einfügen
@ -93,33 +108,39 @@ class TippsController extends Controller
$user=Auth::user();
// Fetch, modify, save Tipp
$tipp = Tipp::where('id', request('id'))->first();
$tipp->score1= request('score1');
$tipp->score2= request('score2');
if (request()->has('joker')) {
$tipp->joker=true;
} else {
$tipp->joker=false;
if (Round::hasLockedGame(request('round_id'))==false) { // Jokermod nur wenn Runde noch nicht begonnen hat
$tipp->joker=true;
} else {
$tipp->joker = false;
}
}
$tipp->save();
// Joker gesetzt ? Dann Jokerflag für alle Tipps der selben Runde entfernen
if ($tipp->joker){
$games = DB::table('games')->where('round_id','=',request('round_id'))->get();
foreach($games as $game) {
Log::info("Checking for joker strip");
$game=Game::where('id','=',$tipp->game_id)->first();
$round_id=$game->round_id;
$tipp = Tipp::where('user_id',$user->id)->where('game_id',$game->id)->first();
$games = DB::table('games')->where('round_id', '=', request('round_id'))->get();
foreach ($games as $game) {
$tipp = Tipp::where('user_id', $user->id)->where('game_id', $game->id)->first();
Log::info("Testing game_id " . $game->id);
if ($tipp->id != request('id')) {
$tipp->joker=false;
Log::info("Resetting game_id " . $game->id);
$tipp->joker = false;
$tipp->save();
}
}
}
// Redirect to the Rounds Index

View file

@ -6,15 +6,25 @@ use Illuminate\Http\Request;
use App\User;
use App\Season;
use App\Payment;
use App\Round;
use App\Game;
use App\Tipp;
use DB;
use Auth;
use Log;
class UsersController extends Controller
{
public function main() {
return view('main');
}
public function index() {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$users = DB::table('users')->where('deleted',null)->get();
return view('users.index', compact('users'));
}
@ -36,17 +46,25 @@ class UsersController extends Controller
public function edit($user_id) {
if (Auth::guest()) { return redirect("/"); }
if (!Auth::user()->admin) { return redirect("/"); }
$user = User::where('id',$user_id)->first();
$season_id = Season::getCurrent()->id;
$paid_date_str="";
$val=User::payment_date($user_id, $season_id);
if ($val) { $paid_date_str=$val;}
Log::info("UserController:edit => payment_date=".$val);
if ($val) { $paid_date_str=User::toGermanDate($val);}
Log::info("German Date=".$paid_date_str);
return view ('users.edit', compact('user','paid_date_str'));
}
public function store() {
if (Auth::guest()) { return redirect("/"); }
$season_id = Season::getCurrent()->id;
@ -55,14 +73,16 @@ class UsersController extends Controller
$this->validate(request(), [
'id' => 'required',
'name' => 'required',
'fullname' => 'required',
'email'=> 'required'
]);
// Create and save the season
$user=User::where('id', request('id'))->first();
$user->name=request('name');
$user->fullname=request('fullname');
$user->email=request('email');
if (request()->has('admin')) {
@ -72,6 +92,8 @@ class UsersController extends Controller
$user->admin=false;
}
//
$user->save();
@ -84,10 +106,77 @@ class UsersController extends Controller
return redirect('/users');
}
public function ranking() {
public function ranking($round_id) {
$currentSeason=Season::getCurrent();
if (!$currentSeason) { return redirect("/"); }
if($round_id==-1) {
$round_id = Round::getLastCompleteRoundId();
//Log::info("Last complete round id=" . $round_id);
if ($round_id==-1) {return redirect("/");}
}
if (Auth::guest()) { return redirect("/"); }
TippsController::createAllTips();
$users = DB::table('users')->where('deleted',null)->get();
return view('users.ranking', compact('users'));
foreach($users as $user) {
$user->totalScore=0;
$user->dayScore=0;
}
$rounds=Round::where([['season_id','=',$currentSeason->id],['id','<=',$round_id]])->orderBy('id')->get();
foreach($rounds as $round) {
$games=Game::where('round_id','=',$round->id)->orderBy('id')->get();
$index=0;
foreach($games as $game) {
foreach($users as $user) {
$tipp = Tipp::where('game_id','=',$game->id)->where('user_id','=',$user->id)->first();
if ($tipp) {
$score = Tipp::getScoreLocal($game->score1,$game->score2,$tipp->score1,$tipp->score2,$tipp->joker);
$user->totalScore += $score;
if ($game->round_id == $round_id) {
$user->dayScore += $score;
}
//if ($user->id==3 && $score>0) { Log::info("Adding score=" . $score. "from game_id=".$game->id."(".$game->team1." - ".$game->team2.", ".$round->name.") totalScore=".$user->totalScore.", dayScore=".$user->dayScore); }
$tippDisplay[$user->id][$game->id][0] = $tipp->score1 ==-1 ? 'x' : $tipp->score1;
$tippDisplay[$user->id][$game->id][1] = $tipp->score2 ==-1 ? 'x' : $tipp->score2;
$tippDisplay[$user->id][$game->id][2] = $tipp->joker;
}
}
$index+=1;
}
}
$arr=[];
foreach($users as $user) {
array_push($arr, $user);
}
usort($arr, array($this,"cmp"));
$users=$arr;
$round=Round::where('id','=',$round_id)->first();
$games=Game::where('round_id','=',$round->id)->get();
return view('users.ranking', compact('users','round','games','tippDisplay'));
}
public function cmp($user1, $user2) {
return ($user1->totalScore < $user2->totalScore);
}
}

View file

@ -3,6 +3,7 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Payment extends Model
{
@ -21,13 +22,13 @@ class Payment extends Model
$payment= Payment::create([
'user_id' => $user_id,
'season_id'=> $season_id,
'paid_date'=> $paid_date
'paid_date'=> $paid_date ? User::toDBDate($paid_date):null
]);
} else {
$payment->user_id = $user_id;
$payment->season_id = $season_id;
$payment->paid_date = $paid_date;
$payment->paid_date = $paid_date ? User::toDBDate($paid_date):null;
}
$payment->save();

View file

@ -3,14 +3,52 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Game;
class Round extends Model
{
protected $fillable = [
'name','season_id'
'name', 'season_id'
];
public function games() {
public function games()
{
return $this->hasMany('App\Game');
}
// true, wenn irgend ein Game locked ist.
public static function 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;
}
}

View file

@ -4,6 +4,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
use \App\Setting;
use Log;
class Season extends Model
{
@ -17,6 +18,28 @@ class Season extends Model
*/
public static function getCurrent() {
return \App\Season::orderBy('id','desc')->where('active','=','1')->first();
$setting = Setting::all()->first();
if (!$setting) {
//Log::info('Season.getCurrent(): No current season set');
return null;
}
//Log::info("current_season_id=" . $setting->current_season_id);
$currentSeason = Season::where('id',$setting->current_season_id)->first();
return $currentSeason;
}
public static function setCurrent($season_id) {
$setting = Setting::all()->first();
if (!$setting) {
$setting = Setting::create([
'current_season_id' => $season_id
]);
}
$setting->current_season_id=$season_id;
$setting->save();
}
}

View file

@ -6,5 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = [
'current_season_id'
];
}

View file

@ -4,6 +4,8 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
use Carbon;
use Log;
class Tipp extends Model
{
@ -11,17 +13,98 @@ class Tipp extends Model
'id', 'user_id', 'game_id','score1','score2','joker'
];
public static function getTipp($game_id, $user_id) {
// 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;
}
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;}
@ -35,7 +118,7 @@ class Tipp extends Model
if ($tippTendenz == $gameTendenz) { $score=1; }
if ( ($tipp->score1 == $game->score1) && ($tipp->score2 == $game->score2) ) {
$score=2;
$score=2 + $bonus;
}
if ($tipp->joker==true) {
@ -43,7 +126,16 @@ class Tipp extends Model
}
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;
}
}

View file

@ -20,7 +20,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'fullname', 'email', 'password',
];
/**
@ -64,32 +64,69 @@ class User extends Authenticatable
return null;
}
public static function getScore($user_id) {
Log::info("User::getScore for userid=" . $user_id);
// Aufgerechnete Summe. wird $maxround_id mit übergeben, dann wird nur
// bis zur entsprechenden Runde aufsummiert. Das setzt voraus, dass die
// Runden in Reihenfolge eingegeben wurden...
public static function getTotalScore($user_id,$maxround_id=1000000) {
$season = Season::getCurrent();
$rounds = Round::where('season_id','=',$season->id)->get();
$totalScore=0;
foreach($rounds as $round) {
$games = Game::where('round_id','=',$round->id)->get();
Log::info(' round_id=' . $round->id);
foreach ($games as $game) {
Log::info(' game_id=' . $game->id);
$tipp = Tipp::getTipp($game->id,$user_id);
$score = Tipp::getScore($tipp->id);
Log::info(" Game id=" . $game->id. ", Tipp id=". $tipp->id.", score=" . $score);
$totalScore+=$score;
if ($round->id <= $maxround_id) {
$totalScore += User::getRoundScore($user_id, $round->id);
}
}
Log::info("Total score=" . $totalScore);
return $totalScore;
}
public static function getRoundScore($user_id, $round_id) {
$games = Game::where('round_id','=',$round_id)->get();
$totalScore=0;
foreach ($games as $game) {
$tipp = Tipp::getTipp($user_id,$game->id);
$score = Tipp::getScore($tipp->id);
$totalScore+=$score;
}
return $totalScore;
}
// This totally does not belong here. But i am too afraid to put it into
// a separate php file that gets smashed by autoregenerate.
public static function toGermanDate($engDate)
{
$values = explode("-", $engDate);
$year = $values[0];
$month = $values[1];
$day = explode(" ", $values[2])[0];
$timeval = explode(" ", $engDate)[1];
$hour = explode(":", $timeval)[0];
$minute = explode(":", $timeval)[1];
return $day.".".$month.".".$year." ".$hour.":".$minute;
}
public static function toDBDate($deDate)
{
$values = explode(" ", $deDate);
$year = explode(".",$values[0])[2];
$month = explode(".",$values[0])[1];
$day = explode(".", $values[0])[0];
Log::info("values[0]=".$values[0]." day=".$day."count=". count($values));
$hour="00";
$minute="00";
if (count($values)>1) {
$hour = explode(":", $values[1])[0];
$minute = explode(":", $values[1])[1];
}
$dbDate=$year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute . ":00";
Log::info("deDate=".$deDate." converted to:".$dbDate);
return $dbDate;
}
}