Ejemplo n.º 1
0
 /**
  * Show the form for creating a new resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function create()
 {
     $leagues = League::all();
     $stadiums = Stadium::all();
     $teams = Team::all();
     return view('admin.fixtures.create', compact('leagues', $leagues, 'teams', $teams, 'stadiums', $stadiums));
 }
Ejemplo n.º 2
0
 public function fixtureLeague(Request $request)
 {
     $this->validate($request, ['league_title' => 'required', 'file' => 'required|image|mimes:jpeg,jpg,png,bmp,gif,svg']);
     //dd($request->all());
     if ($request->file('file')) {
         //$this->validate($request,['file'=>'required|image|mimes:jpeg,jpg,png,bmp,gif,svg']);
         $file = $request->file('file');
         $path = $this->_league_logo_path;
         $name = uniqid() . $file->getClientOriginalName();
         $file->move($path, $name);
     }
     $league = new League(array('title' => $request->get('league_title'), 'logo_name' => $name, 'logo_path' => $path));
     $league->save();
     $leagues = League::all();
     return response()->json($leagues);
 }
Ejemplo n.º 3
0
 /**
  * Show the form for editing the specified tournament.
  *
  * @param  int  $id Id of edited tournament
  * @return Response View with editing form for tournament
  */
 public function edit($id)
 {
     $leagues = League::all();
     $tournament = Tournament::findOrFail($id);
     return view('/tournaments/tournament-create', ['leagues' => $leagues, 'edited' => $tournament]);
 }
Ejemplo n.º 4
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $leagues = League::all();
     return view('league.index', ['leagues' => $leagues]);
 }
Ejemplo n.º 5
0
 public function listLeagues()
 {
     $leagues = League::all();
     return view('admin/listLeagues')->with(['leagues' => $leagues]);
 }
Ejemplo n.º 6
0
 public function viewRanking($year = null, $month = null, $leaguename_id = null)
 {
     setlocale(LC_TIME, 'pt_PT.UTF-8');
     $leagues = League::all()->sortByDesc(function ($league) {
         return sprintf('%04d%02d%02d', $league->year, $league->month, 1 / $league->leaguename_id);
     });
     if (!count($leagues)) {
         return redirect()->route('home');
     }
     if ($leaguename_id) {
         $leagueYear = $year;
         $leagueMonth = $month;
         $leagues = $leagues->whereLoose('year', $leagueYear)->whereLoose('month', $leagueMonth)->sortBy('leaguename_id');
         $league = League::where('year', $year)->where('month', $month)->where('leaguename_id', $leaguename_id)->first();
         if (!count($league)) {
             return redirect()->route('home');
         }
     } else {
         $leagueYear = $leagues->first()->year;
         $leagueMonth = $leagues->first()->month;
         $leagues = $leagues->whereLoose('year', $leagueYear)->whereLoose('month', $leagueMonth)->sortBy('leaguename_id');
         $user = Auth::id();
         foreach ($leagues as $key => $value) {
             $players = json_decode($value->users);
             if (in_array($user, $players)) {
                 $league = $value;
             }
         }
     }
     if (!isset($league)) {
         $league = $leagues->first();
     }
     $pastGameDays = array();
     $futureGameDays = array();
     $dayOffset = 0;
     for ($i = 0; $i < 20; $i++) {
         if (date("U", strtotime(date('d-m-Y'))) <= date("U", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset)) {
             $futureGameDays[$i]['day'] = date("j", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset);
             $futureGameDays[$i]['month'] = date("n", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset);
             $futureGameDays[$i]['year'] = date("Y", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset);
             $futureGameDays[$i]['date'] = date("d-m-Y", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * ($dayOffset + 1));
         } else {
             $pastGameDays[$i]['day'] = date("j", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset);
             $pastGameDays[$i]['month'] = date("n", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset);
             $pastGameDays[$i]['year'] = date("Y", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * $dayOffset);
             $pastGameDays[$i]['date'] = date("d-m-Y", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth) + 86400 * ($dayOffset + 1));
         }
         if (($i + 1) % 5 == 0) {
             $dayOffset = $dayOffset + 3;
         } else {
             $dayOffset++;
         }
     }
     $users = User::all();
     $roundQuestions = [];
     $roundAnswers = [];
     $leagueData = [];
     $leagueRounds = [];
     $leagueFutureRounds = [];
     $leagueRanking = [];
     $leagueAnwers = collect();
     $playerAnswers = [];
     $start = new Carbon(date("d-m-Y", strtotime('first monday of ' . $leagueYear . '-' . $leagueMonth)));
     if (count($pastGameDays)) {
         $end = new Carbon($pastGameDays[count($pastGameDays) - 1]['date']);
     } else {
         $end = $start;
     }
     foreach (json_decode($league->users) as $player) {
         $leagueRanking[$player]['leaguepoints'] = 0;
         $leagueRanking[$player]['roundpointsfor'] = 0;
         $leagueRanking[$player]['roundpointsagainst'] = 0;
         $leagueRanking[$player]['wins'] = 0;
         $leagueRanking[$player]['ties'] = 0;
         $leagueRanking[$player]['losses'] = 0;
         $leagueRanking[$player]['forfeits'] = 0;
         $leagueRanking[$player]['answers'] = 0;
         $playerAnswers[$player] = Answer::where('user_id', $player)->where('submitted', 1)->whereBetween('created_at', [$start, $end])->get();
         $leagueAnwers = $leagueAnwers->merge($playerAnswers[$player]);
     }
     $leagueAnwers = collect($leagueAnwers);
     $leagueAnwersByQuestion = [];
     foreach ($leagueAnwers as $key => $answer) {
         if (!isset($leagueAnwersByQuestion[$answer->question_id])) {
             $leagueAnwersByQuestion[$answer->question_id] = [];
         }
         array_push($leagueAnwersByQuestion[$answer->question_id], $answer);
     }
     foreach ($pastGameDays as $key => $pastGameDay) {
         $round = $key + 1;
         if (!isset($roundQuestions[$round])) {
             $roundQuestions[$round] = Question::where('year', intval($pastGameDay['year']))->where('month', intval($pastGameDay['month']))->where('day', intval($pastGameDay['day']))->get();
             foreach ($roundQuestions[$round] as $roundQuestion) {
                 if (isset($leagueAnwersByQuestion[$roundQuestion->id])) {
                     $roundAnswers[$roundQuestion->id] = collect($leagueAnwersByQuestion[$roundQuestion->id]);
                 } else {
                     $roundAnswers[$roundQuestion->id] = collect([]);
                 }
             }
         }
         if ($round % 10 == 0) {
             // SOLO ROUND
             $leagueRounds[$round]['day'] = $pastGameDay['day'] . '-' . $pastGameDay['month'] . '-' . $pastGameDay['year'];
             $leagueRounds[$round]['date'] = $pastGameDay;
             $leagueRounds[$round]['sologame'] = array();
             $roundGamePlayers = [];
             foreach ($roundQuestions[$round] as $roundQuestion) {
                 foreach ($roundAnswers[$roundQuestion->id] as $answer) {
                     if (!isset($leagueRanking[$answer->user_id])) {
                         continue;
                     } else {
                         $roundGamePlayers[$answer->user_id] = true;
                     }
                     if (!isset($leagueRounds[$round]['sologame'][$answer->user_id])) {
                         $leagueRounds[$round]['sologame'][$answer->user_id] = [];
                         $leagueRounds[$round]['sologame'][$answer->user_id]['points'] = 0;
                         $leagueRounds[$round]['sologame'][$answer->user_id]['corrects'] = 0;
                     }
                     if ($answer->corrected && $answer->correct) {
                         $leagueRanking[$answer->user_id]['leaguepoints'] += 0.5;
                         $leagueRanking[$answer->user_id]['roundpointsfor'] += 0.5;
                         $leagueRanking[$answer->user_id]['answers']++;
                         $leagueRounds[$round]['sologame'][$answer->user_id]['points'] += 0.5;
                         $leagueRounds[$round]['sologame'][$answer->user_id]['corrects']++;
                     }
                 }
             }
             foreach (json_decode($league->users) as $player) {
                 if (!isset($roundGamePlayers[$player])) {
                     $leagueRanking[$player]['forfeits']++;
                     $leagueRounds[$round]['sologame'][$player] = [];
                     $leagueRounds[$round]['sologame'][$player]['points'] = 0;
                     $leagueRounds[$round]['sologame'][$player]['corrects'] = 'F';
                 } else {
                     $leagueRanking[$player]['leaguepoints']++;
                     $leagueRounds[$round]['sologame'][$player]['points']++;
                 }
             }
             // TODO: PENDING SOLO GAMES
         } else {
             // HEAD-TO-HEAD ROUND
             $roundGames = Game::where('league_id', $league->id)->where('round', $round)->get();
             $leagueRounds[$round]['day'] = $pastGameDay['day'] . '-' . $pastGameDay['month'] . '-' . $pastGameDay['year'];
             $leagueRounds[$round]['games'] = array();
             foreach ($roundGames as $roundGame) {
                 $roundGamePlayers = array(array());
                 $pendingResult = false;
                 for ($i = 0; $i < 2; $i++) {
                     $roundGamePlayers[$i]['id'] = $roundGame['user_' . ($i + 1) . '_id'];
                     $roundGamePlayers[$i]['fullName'] = $users->find($roundGame['user_' . ($i + 1) . '_id'])->fullName();
                     $roundGamePlayers[$i]['points'] = 0;
                     $roundGamePlayers[$i]['corrects'] = 0;
                     $roundGamePlayers[$i]['forfeit'] = true;
                 }
                 foreach ($roundQuestions[$round] as $roundQuestion) {
                     $answerPlayers = [];
                     $answerPlayer[0] = $roundAnswers[$roundQuestion->id]->whereLoose('user_id', $roundGame['user_1_id'])->first();
                     $answerPlayer[1] = $roundAnswers[$roundQuestion->id]->whereLoose('user_id', $roundGame['user_2_id'])->first();
                     for ($i = 0; $i < 2; $i++) {
                         if (count($answerPlayer[$i])) {
                             if ($answerPlayer[$i]->corrected != $answerPlayer[$i]->submitted) {
                                 $pendingResult = true;
                             }
                             $roundGamePlayers[$i]['forfeit'] = false;
                             if ($answerPlayer[$i]->correct) {
                                 $roundGamePlayers[$i]['corrects']++;
                             }
                             if (count($answerPlayer[intval(!$i)])) {
                                 $roundGamePlayers[$i]['points'] += $answerPlayer[$i]->correct * $answerPlayer[intval(!$i)]->points;
                             }
                         }
                     }
                 }
                 if ($pendingResult) {
                     for ($i = 0; $i < 2; $i++) {
                         $roundGamePlayers[$i]['points'] = 'P';
                         $roundGamePlayers[$i]['leaguepoints'] = 'P';
                         $roundGamePlayers[$i]['corrects'] = 'P';
                     }
                 } else {
                     if (!($roundGamePlayers[0]['forfeit'] && $roundGamePlayers[1]['forfeit'])) {
                         for ($i = 0; $i < 2; $i++) {
                             if ($roundGamePlayers[intval(!$i)]['forfeit']) {
                                 switch ($roundGamePlayers[$i]['corrects']) {
                                     case 0:
                                         $roundGamePlayers[$i]['points'] = 0;
                                         break;
                                     case 1:
                                         $roundGamePlayers[$i]['points'] = 2;
                                         break;
                                     case 2:
                                         $roundGamePlayers[$i]['points'] = 3;
                                         break;
                                     case 3:
                                         $roundGamePlayers[$i]['points'] = 5;
                                         break;
                                     case 4:
                                         $roundGamePlayers[$i]['points'] = 6;
                                         break;
                                     case 5:
                                         $roundGamePlayers[$i]['points'] = 8;
                                         break;
                                     case 6:
                                         $roundGamePlayers[$i]['points'] = 9;
                                         break;
                                     case 7:
                                         $roundGamePlayers[$i]['points'] = 10;
                                         break;
                                     case 8:
                                         $roundGamePlayers[$i]['points'] = 12;
                                         break;
                                 }
                             }
                         }
                     }
                     for ($i = 0; $i < 2; $i++) {
                         if ($roundGamePlayers[$i]['forfeit']) {
                             $roundGamePlayers[$i]['leaguepoints'] = 0;
                             $leagueRanking[$roundGamePlayers[$i]['id']]['forfeits']++;
                             $roundGamePlayers[$i]['points'] = 'F';
                         } elseif ($roundGamePlayers[intval(!$i)]['forfeit']) {
                             $roundGamePlayers[$i]['leaguepoints'] = 3;
                             $leagueRanking[$roundGamePlayers[$i]['id']]['wins']++;
                         } elseif ($roundGamePlayers[$i]['points'] > $roundGamePlayers[intval(!$i)]['points']) {
                             $roundGamePlayers[$i]['leaguepoints'] = 3;
                             $leagueRanking[$roundGamePlayers[$i]['id']]['wins']++;
                         } elseif ($roundGamePlayers[$i]['points'] == $roundGamePlayers[intval(!$i)]['points']) {
                             $roundGamePlayers[$i]['leaguepoints'] = 2;
                             $leagueRanking[$roundGamePlayers[$i]['id']]['ties']++;
                         } elseif ($roundGamePlayers[$i]['points'] < $roundGamePlayers[intval(!$i)]['points']) {
                             $roundGamePlayers[$i]['leaguepoints'] = 1;
                             $leagueRanking[$roundGamePlayers[$i]['id']]['losses']++;
                         }
                         $leagueRanking[$roundGamePlayers[$i]['id']]['leaguepoints'] += $roundGamePlayers[$i]['leaguepoints'];
                         $leagueRanking[$roundGamePlayers[$i]['id']]['roundpointsfor'] += $roundGamePlayers[$i]['points'];
                         $leagueRanking[$roundGamePlayers[$i]['id']]['roundpointsagainst'] += $roundGamePlayers[intval(!$i)]['points'];
                         $leagueRanking[$roundGamePlayers[$i]['id']]['answers'] += $roundGamePlayers[$i]['corrects'];
                     }
                 }
                 $roundGame['player1'] = $roundGamePlayers[0]['fullName'];
                 $roundGame['player2'] = $roundGamePlayers[1]['fullName'];
                 $roundGame['points1'] = $roundGamePlayers[0]['points'];
                 $roundGame['points2'] = $roundGamePlayers[1]['points'];
                 $roundGame['corrects1'] = $roundGamePlayers[0]['corrects'];
                 $roundGame['corrects2'] = $roundGamePlayers[1]['corrects'];
                 array_push($leagueRounds[$round]['games'], $roundGame);
             }
         }
     }
     foreach ($leagueRanking as $key => $player) {
         $leagueRanking[$key]['user_id'] = $key;
         $leagueRanking[$key]['roundpointsdifference'] = $leagueRanking[$key]['roundpointsfor'] - $leagueRanking[$key]['roundpointsagainst'];
     }
     $leagueRanking = array_values(array_sort($leagueRanking, function ($value) {
         $leaguepoints = sprintf('%02d', $value['leaguepoints'] * 10);
         $difference = sprintf('%03d', ($value['roundpointsdifference'] + 500) * 10);
         $points = sprintf('%04d', $value['roundpointsfor'] * 10);
         $wins = sprintf('%02d', $value['wins']);
         $corrects = sprintf('%03d', $value['answers']);
         $tiebreak = $leaguepoints . $difference . $points . $wins . $corrects;
         return 1 / (1 + $tiebreak);
     }));
     foreach ($futureGameDays as $key => $futureGameDay) {
         $round = $key + 1;
         if ($round % 10 == 0) {
             // SOLO ROUND
             $leagueFutureRounds[$round]['day'] = $futureGameDay['day'] . '-' . $futureGameDay['month'] . '-' . $futureGameDay['year'];
             $leagueFutureRounds[$round]['sologame'] = array();
             foreach (json_decode($league->users) as $player) {
                 $leagueFutureRounds[$round]['sologame'][$player] = [];
             }
         } else {
             // HEAD-TO-HEAD ROUND
             $roundGames = Game::where('league_id', $league->id)->where('round', $round)->get();
             $leagueFutureRounds[$round]['day'] = $futureGameDay['day'] . '-' . $futureGameDay['month'] . '-' . $futureGameDay['year'];
             $leagueFutureRounds[$round]['games'] = array();
             foreach ($roundGames as $roundGame) {
                 $roundGamePlayers = array(array());
                 $pendingResult = false;
                 for ($i = 0; $i < 2; $i++) {
                     $roundGamePlayers[$i]['id'] = $roundGame['user_' . ($i + 1) . '_id'];
                     $roundGamePlayers[$i]['fullName'] = $users->find($roundGame['user_' . ($i + 1) . '_id'])->fullName();
                 }
                 $roundGame = [];
                 $roundGame['player1'] = $roundGamePlayers[0]['fullName'];
                 $roundGame['player2'] = $roundGamePlayers[1]['fullName'];
                 array_push($leagueFutureRounds[$round]['games'], $roundGame);
             }
         }
     }
     $leagueData['id'] = $league->id;
     $leagueData['tier'] = $league->leaguename_id;
     $leagueData['ranking'] = $leagueRanking;
     $leagueData['rounds'] = $leagueRounds;
     $leagueData['futurerounds'] = $leagueFutureRounds;
     $leagueData['name'] = $league->name();
     $usersCollection = [];
     foreach ($users as $key => $user) {
         $usersCollection[$user->id] = $user;
     }
     $archive = League::where('leaguename_id', 1)->get()->sortByDesc(function ($league) {
         return sprintf('%04d%02d', $league->year, $league->month);
     });
     return view('ranking')->with(['league' => $leagueData, 'leagues' => $leagues, 'archive' => $archive, 'users' => $usersCollection]);
 }