public async Task <bool> MakeAutoPick(InitialDraftPicksDto draftPick) { var draftSelection = await _context.InitialDrafts.FirstOrDefaultAsync(x => x.Pick == draftPick.Pick && x.Round == draftPick.Round); var teamId = draftSelection.TeamId; // Need to check whether the team has set a draft board var draftboard = await _context.DraftRankings.Where(x => x.TeamId == teamId).OrderBy(x => x.Rank).ToListAsync(); if (draftboard.Count > 0) { foreach (var db in draftboard) { var playerTeamForPlayerId = await _context.PlayerTeams.FirstOrDefaultAsync(x => x.PlayerId == db.PlayerId); if (playerTeamForPlayerId.TeamId == 31) { // Then this is the player that we will draft draftPick.TeamId = teamId; draftPick.PlayerId = playerTeamForPlayerId.PlayerId; return(await this.MakeDraftPick(draftPick)); } } } else { // Now need to get the auto pick order var autopick = await _context.AutoPickOrders.OrderByDescending(x => x.Score).FirstOrDefaultAsync(); draftPick.TeamId = teamId; draftPick.PlayerId = autopick.PlayerId; return(await this.MakeDraftPick(draftPick)); } return(false); }
public async Task <IActionResult> MakeAutoPick(InitialDraftPicksDto draftPick) { var selectionMade = await _repo.MakeAutoPick(draftPick); return(Ok(selectionMade)); }
public async Task <bool> MakeDraftPick(InitialDraftPicksDto draftPick) { var draftSelection = await _context.InitialDrafts.FirstOrDefaultAsync(x => x.Pick == draftPick.Pick && x.Round == draftPick.Round); draftSelection.PlayerId = draftPick.PlayerId; _context.Update(draftSelection); var playerTeam = await _context.PlayerTeams.FirstOrDefaultAsync(x => x.PlayerId == draftPick.PlayerId); playerTeam.TeamId = draftPick.TeamId; _context.Update(playerTeam); var teamRoser = new Roster { PlayerId = draftPick.PlayerId, TeamId = draftPick.TeamId }; await _context.AddAsync(teamRoser); var tracker = await _context.DraftTrackers.FirstOrDefaultAsync(); // Now need to add the InitialDraftPick Contract var contractDetails = await _context.InitialDraftContracts.FirstOrDefaultAsync(x => x.Round == tracker.Round && x.Pick == tracker.Pick); int yearOne = 0; int yearTwo = 0; int yearThree = 0; int yearFour = 0; int yearFive = 0; int gOne = 0; int gTwo = 0; int gThree = 0; int gFour = 0; int gFive = 0; if (contractDetails.Years == 1) { yearOne = contractDetails.SalaryAmount; gOne = 1; } else if (contractDetails.Years == 2) { yearOne = contractDetails.SalaryAmount; gOne = 1; yearTwo = contractDetails.SalaryAmount; gTwo = 1; } else if (contractDetails.Years == 3) { yearOne = contractDetails.SalaryAmount; gOne = 1; yearTwo = contractDetails.SalaryAmount; gTwo = 1; yearThree = contractDetails.SalaryAmount; gThree = 1; } else if (contractDetails.Years == 4) { yearOne = contractDetails.SalaryAmount; gOne = 1; yearTwo = contractDetails.SalaryAmount; gTwo = 1; yearThree = contractDetails.SalaryAmount; gThree = 1; yearFour = contractDetails.SalaryAmount; gFour = 1; } else if (contractDetails.Years == 5) { yearOne = contractDetails.SalaryAmount; gOne = 1; yearTwo = contractDetails.SalaryAmount; gTwo = 1; yearThree = contractDetails.SalaryAmount; gThree = 1; yearFour = contractDetails.SalaryAmount; gFour = 1; yearFive = contractDetails.SalaryAmount; gFive = 1; } PlayerContract pc = new PlayerContract { PlayerId = draftPick.PlayerId, TeamId = draftPick.TeamId, YearOne = yearOne, GuranteedOne = gOne, YearTwo = yearTwo, GuranteedTwo = gTwo, YearThree = yearThree, GuranteedThree = gThree, YearFour = yearFour, GuranteedFour = gFour, YearFive = yearFive, GuranteedFive = gFive }; await _context.AddAsync(pc); if (tracker.Pick < 30) { tracker.Pick++; } else { if (tracker.Round < 13) { tracker.Pick = 1; tracker.Round++; } else if (tracker.Round == 13 && tracker.Pick == 30) { // Draft is finished var leagueState = await _context.Leagues.FirstOrDefaultAsync(); leagueState.StateId = 5; leagueState.Day = 5; _context.Update(leagueState); // Make all undrafted players a free agent var undraftedPlayers = await _context.PlayerTeams.Where(x => x.TeamId == 31).ToListAsync(); foreach (var up in undraftedPlayers) { up.TeamId = 0; _context.PlayerTeams.Update(up); } // Update all teams salary caps var teams = await _context.Teams.ToListAsync(); foreach (var team in teams) { int teamSalary = 0; var contracts = await _context.PlayerContracts.Where(x => x.TeamId == team.Id).ToListAsync(); foreach (var contract in contracts) { teamSalary = teamSalary + contract.YearOne; } // Now update the TeamSalary Record var salaryCap = await _context.TeamSalaryCaps.FirstOrDefaultAsync(x => x.TeamId == team.Id); salaryCap.CurrentCapAmount = teamSalary; _context.TeamSalaryCaps.Update(salaryCap); } } } DateTime dt = DateTime.UtcNow.ToUniversalTime(); dt = dt.AddMinutes(6); string dateAndTime = dt.ToString("MM/dd/yyyy HH:mm:ss"); tracker.DateTimeOfLastPick = dateAndTime; _context.Update(tracker); // Need to remove from draftboards var draftboards = await _context.DraftRankings.Where(x => x.PlayerId == draftPick.PlayerId).ToListAsync(); foreach (var db in draftboards) { // Need to remove the record and update all rankings var teamDB = await _context.DraftRankings.Where(x => x.TeamId == db.TeamId).ToListAsync(); var recordToRemove = teamDB.Find(x => x.PlayerId == db.PlayerId); var rank = recordToRemove.Rank; foreach (var record in teamDB) { if (record.Rank > rank) { record.Rank--; _context.Update(record); } } _context.Remove(recordToRemove); } // Need to move from the autopick board var autopickRecord = await _context.AutoPickOrders.FirstOrDefaultAsync(x => x.PlayerId == draftPick.PlayerId); _context.Remove(autopickRecord); return(await _context.SaveChangesAsync() > 0); }