public IActionResult OnPost() { if (!ModelState.IsValid) { return(Page()); } try { _context.League.Add(League); _context.SaveChanges(); //Get new league Id int currentLeagueId = League.LeagueId; //Create 8 new teams string section = "A"; for (int i = 1; i < 9; i++) { //If short league last 4 teams are in section b if (League.LeagueType == LeagueType.Short && i > 4) { section = "B"; } Team team = new Team { TeamNo = i, TeamName = "Team No " + i, Section = section, LeagueId = League.LeagueId }; _context.Team.Add(team); } //save new teams _context.SaveChanges(); int playersRequired = League.NoOfLevels * 8; //new array to store the IDs of the new teams var newTeams = _context.Team.Where(l => l.LeagueId == League.LeagueId).ToArray(); //Get an array list of all players active and playing league var LeaguePlayers = _context.Player.Where(p => p.PlayerStatus == PlayerStatusEnum.Active && p.PlayingLeague == true) .OrderBy(l => l.Rank) .ToArray(); //check if enough players for league format if (LeaguePlayers.Length < playersRequired) { ErrorString.ErrorNo = 1001; ErrorString.ErrorMessage = "Not enough players to fill all levels"; return(Page()); } else { //Fill a temporary 2D array with team and players int[,] TeamArray = new int[League.NoOfLevels + 1, 8]; int counter = 0; for (int level = 1; level <= League.NoOfLevels; level++) { for (int teamNo = 0; teamNo < 8; teamNo++) { TeamArray[level, teamNo] = LeaguePlayers[counter].PlayerId; counter++; } } //Randomly shuffle the players on each level using Random randomFactory = new Random(); int randomSpot; int tempId; for (int level = 1; level <= League.NoOfLevels; level++) { for (int teamNo = 0; teamNo < 8; teamNo++) { randomSpot = randomFactory.Next(0, 8); tempId = TeamArray[level, teamNo]; TeamArray[level, teamNo] = TeamArray[level, randomSpot]; TeamArray[level, randomSpot] = tempId; } } //Create new teamplayers from temporary array for (int level = 1; level <= League.NoOfLevels; level++) { for (int teamNo = 0; teamNo < 8; teamNo++) { TeamPlayer newTeamPlayer = new TeamPlayer { PlayerId = TeamArray[level, teamNo], TeamId = newTeams[teamNo].TeamId, Level = level, MatchesPlayed = 0, MatchesWon = 0, GamesWon = 0, GamesLost = 0 }; _context.TeamPlayer.Add(newTeamPlayer); } } _context.SaveChanges(); return(RedirectToPage("./Index")); } } catch { ErrorString.ErrorMessage = "Something went wrong!"; ErrorString.ErrorNo = 1000; return(Page()); } }
//public Fixture Fixture { get; set; } public IActionResult OnPost() { if (!ModelState.IsValid) { return(Page()); } //Get template of fixtures for league type var fixtureList = new List <FixtureDate>(); //get current league using session data try { //string tempLeagueString = HttpContext.Session.GetString("SelectedLeague"); //int CurrentLeagueId = Convert.ToInt32(tempLeagueString); League League = _context.League.FirstOrDefault(c => c.LeagueId == SelectedLeagueId); if (League.FixturesMade) { return(RedirectToPage("./Index")); } if (League.LeagueType == LeagueType.Long) { fixtureList = _context.FixtureDate.Where(fd => fd.LeagueType == LeagueType.Long) .OrderBy(fd => fd.StartDaysPlus) .ToList(); } else { fixtureList = _context.FixtureDate.Where(fd => fd.LeagueType == LeagueType.Short) .OrderBy(fd => fd.StartDaysPlus) .ToList(); } //Get an array of the teams in this league var teamList = _context.Team.Where(t => t.LeagueId == League.LeagueId) .OrderBy(t => t.TeamNo) .ToArray(); //Loop through and make new fixtures foreach (FixtureDate fd in fixtureList) { Fixture newFixture = new Fixture { League = League, TeamAId = teamList[fd.TeamANo - 1].TeamId, TeamBId = teamList[fd.TeamBNo - 1].TeamId, PlayDate = League.StartDate.AddDays(fd.StartDaysPlus) }; _context.Fixture.Add(newFixture); _context.SaveChanges(); //An array of team A players var teamA = _context.TeamPlayer.Where(tp => tp.TeamId == newFixture.TeamAId) .OrderBy(tp => tp.Level) .ToArray(); //An array of team B players var teamB = _context.TeamPlayer.Where(tp => tp.TeamId == newFixture.TeamBId) .OrderBy(tp => tp.Level) .ToArray(); //Loop through how many levels in this league for (int level = 0; level < League.NoOfLevels; level++) { //get the playing time leveltime for this level LevelTime thisLevelTime = _context.LevelTime.FirstOrDefault(lt => lt.Level == level + 1); //Create a new match between opposing players at this level Match newMatch = new Match { FixtureId = newFixture.FixtureId, PlayerAId = teamA[level].PlayerId, PlayerBId = teamB[level].PlayerId, Level = level + 1, Played = false, }; _context.Match.Add(newMatch); _context.SaveChanges(); //Create new MatchSlot and assign this match to it for this scheduled date and time MatchSlot newMatchSlot = new MatchSlot { MatchId = newMatch.MatchId, BookingSlot = newFixture.PlayDate + thisLevelTime.StartTime }; _context.MatchSlot.Add(newMatchSlot); _context.SaveChanges(); } } //Change fistures made to true League.FixturesMade = true; _context.SaveChanges(); return(RedirectToPage("./Index")); } catch { return(RedirectToPage("./Index")); } }