示例#1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param Request $request
  *
  * @return mixed
  */
 public function store(Request $request)
 {
     $this->validate($request, ['division_id' => 'required|' . 'exists:divisions,id|' . 'unique:fixtures,division_id,NULL,id' . ',home_team_id,' . $request->get('home_team_id') . ',away_team_id,' . $request->get('away_team_id'), 'match_number' => 'required|unique:fixtures,match_number,NULL,id,division_id,' . $request->get('division_id'), 'match_date' => 'required', 'warm_up_time' => 'required', 'start_time' => 'required', 'home_team_id' => 'required|exists:teams,id', 'away_team_id' => 'required|exists:teams,id|different:home_team_id', 'venue_id' => 'required|exists:venues,id'], ['away_team_id.different' => 'The away team cannot be the same as the home team.', 'division_id.unique' => 'The fixture for these two teams have already been added in this division.', 'match_number.unique' => 'There is already a match with the same number in this division.']);
     Fixture::create($request->all());
     \Flash::success('Fixture added!');
     return redirect('admin/data-management/fixtures');
 }
示例#2
0
 /**
  * Store a newly created season.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $season = Season::create(['country' => $request->input('country'), 'league' => $request->input('league'), 'year' => $request->input('year'), 'number_of_teams' => $request->input('number_of_teams'), 'number_of_teams_to_cl' => $request->input('number_of_teams_to_cl'), 'number_of_teams_to_el' => $request->input('number_of_teams_to_el'), 'number_of_teams_to_ld' => $request->input('number_of_teams_to_ld')]);
     if ($request->has('link')) {
         // Example from football-data.org
         // England - Barclays Premier League - 2015
         // http://api.football-data.org/alpha/soccerseasons/398
         $link = $request->input('link');
         // Read data from link
         // $tmpTeamsFile = json_decode(file_get_contents($link."/teams"));
         // $tmpFixturesFile = json_decode(file_get_contents($link."/fixtures"));
         $base = base_path();
         $tmpTeamsFile = json_decode(file_get_contents($base . "/public/files/teams.json"));
         $tmpFixturesFile = json_decode(file_get_contents($base . "/public/files/fixtures.json"));
         $tmpTeams = $tmpTeamsFile->teams;
         $tmpFixtures = $tmpFixturesFile->fixtures;
         // Create teams
         foreach ($tmpTeams as $tmpTeam) {
             $team = Team::create(['name' => $tmpTeam->name, 'short_name' => $tmpTeam->code, 'season_id' => $season->id]);
             // Array of all teams from season
             $teams[] = $team;
         }
         // Create fixtures
         foreach ($tmpFixtures as $tmpFixture) {
             $home_team_name = $tmpFixture->homeTeamName;
             $away_team_name = $tmpFixture->awayTeamName;
             $home_team_index = $this->searchTeamIdByName($home_team_name, $teams);
             $away_team_index = $this->searchTeamIdByName($away_team_name, $teams);
             Fixture::create(['season_id' => $season->id, 'matchday' => $tmpFixture->matchday, 'home_team_id' => $teams[$home_team_index]->id, 'away_team_id' => $teams[$away_team_index]->id]);
         }
     } else {
         // Create teams
         for ($i = 1; $i <= $season->number_of_teams; $i++) {
             $team = Team::create(['name' => 'Team ' . sprintf("%02d", $i), 'short_name' => 'TN' . sprintf("%02d", $i), 'season_id' => $season->id]);
             $teams[] = $team;
         }
         // Create matchdays
         $tour = 1;
         $fixtures_in_tour = $season->number_of_teams / 2;
         for ($i = 1; $i <= $season->number_of_teams * ($season->number_of_teams - 1); $i++) {
             Fixture::create(['season_id' => $season->id, 'matchday' => $tour]);
             $tour = $i % $fixtures_in_tour == 0 ? ++$tour : $tour;
         }
     }
     // Create base league tables (tour #0)
     foreach ($teams as $team) {
         LeagueTable::create(['season_id' => $season->id, 'team_id' => $team->id]);
     }
     return response()->json(['success' => true]);
 }