public ActionResult DraftPlayer(DraftRoomViewModel draftRoomViewModel) { Player draftedPlayer = draftRoomViewModel.Players.Where(p => p.PlayerId == draftRoomViewModel.SelectedPlayerId).FirstOrDefault(); if (draftedPlayer == null) { draftedPlayer = draftRoomViewModel.Players.Where(p => p.Name == draftRoomViewModel.SelectedPlayerName).FirstOrDefault(); } if (draftedPlayer == null && draftRoomViewModel.SelectedPlayerName == null) { draftRoomViewModel.Message = string.Format("No available player, or player already drafted, with id of {0}.", draftRoomViewModel.SelectedPlayerId); draftRoomViewModel.IsErrorMessage = true; draftRoomViewModel.SelectedPlayerId = 0; draftRoomViewModel.SelectedPlayerName = null; ModelState.Clear(); return(PartialView("_DraftBoard", draftRoomViewModel)); } DraftPickManager draftPickManager = new DraftPickManager(_context); DraftPick draftPick; if (draftedPlayer != null) { draftPick = draftPickManager.Add( draftRoomViewModel.Draft.DraftId, draftedPlayer.PlayerId, draftRoomViewModel.CurrentRound, draftRoomViewModel.CurrentPick); draftPick.Player = draftedPlayer; string draftPickText = (draftRoomViewModel.DraftPicks == null) ? "1" : (draftRoomViewModel.DraftPicks.Count + 1).ToString(); if (draftPickText[draftPickText.Length - 1] == '1') { draftPickText += "st"; } else if (draftPickText[draftPickText.Length - 1] == '2') { draftPickText += "nd"; } else if (draftPickText[draftPickText.Length - 1] == '3') { draftPickText += "rd"; } else { draftPickText += "th"; } string draftedPlayerMessage = string.Format("With the {0} pick in the draft, {1} has drafted {2}.", draftPickText, draftRoomViewModel.Draft.DraftParticipants[draftRoomViewModel.CurrentPick - 1].Name, draftedPlayer.Name); draftRoomViewModel.Message = draftedPlayerMessage; draftRoomViewModel.IsErrorMessage = false; } else { draftPick = draftPickManager.Add( draftRoomViewModel.Draft.DraftId, draftRoomViewModel.SelectedPlayerName, draftRoomViewModel.CurrentRound, draftRoomViewModel.CurrentPick); draftPick.Player = draftedPlayer; draftRoomViewModel.Message = string.Format("{0} has successfully drafted {1}!", draftRoomViewModel.Draft.DraftParticipants[draftRoomViewModel.CurrentPick - 1].Name, draftRoomViewModel.SelectedPlayerName); draftRoomViewModel.IsErrorMessage = false; } draftRoomViewModel.Players.Remove(draftedPlayer); if (draftRoomViewModel.DraftPicks == null) { draftRoomViewModel.DraftPicks = new List <DraftPick>(); } draftRoomViewModel.DraftPicks.Add(draftPick); //clear the selected player draftRoomViewModel.SelectedPlayerId = 0; draftRoomViewModel.SelectedPlayerName = null; //draft picks are ascending (from 1 => 12) if (draftRoomViewModel.DraftPickAscending) { //standard draft pick, stay in the same round and go to the next pick (+1) if (draftRoomViewModel.CurrentPick < draftRoomViewModel.Draft.DraftParticipants.Count) { draftRoomViewModel.CurrentPick += 1; } //made a draft pick at the end of the round (pick 12) else { //increase the round, keep the pick the same, reverse the draft direction draftRoomViewModel.CurrentRound += 1; draftRoomViewModel.DraftPickAscending = false; } } //draft picks are descending (from 12 => 1) else { //standard draft pick, stay in the same round and go to the next pick (-1) if (draftRoomViewModel.CurrentPick > 1) { draftRoomViewModel.CurrentPick -= 1; } //made a draft pick at the end of the round (pick 1) else { draftRoomViewModel.CurrentRound += 1; draftRoomViewModel.DraftPickAscending = true; } } ModelState.Clear(); return(PartialView("_DraftBoard", draftRoomViewModel)); }
public ActionResult Room(int id) { DraftManager draftManager = new DraftManager(_context); DraftPickManager draftPickManager = new DraftPickManager(_context); PlayerManager playerManager = new PlayerManager(_context); TradeManager tradeManager = new TradeManager(_context); Draft draft = draftManager.Get(id); List <DraftPick> draftPicks = draftPickManager.Get(draft); List <Player> players = playerManager.Get(draft.League); foreach (var draftPick in draftPicks) { players.Remove(draftPick.Player); } //int currentPick = (draftPicks != null && draftPicks.Count > 0) ? // draftPicks.Count + 1 : // 1; int currentPick = 1; int currentRound = 1; bool draftPicksAscending = true; if (draftPicks != null) { for (int i = 0; i < draftPicks.Count; i++) { if (draftPicksAscending && currentPick < draft.Teams) { currentPick = currentPick + 1; } else if (draftPicksAscending && currentPick == draft.Teams) { currentRound = currentRound + 1; draftPicksAscending = false; } else if (!draftPicksAscending && currentPick > 1) { currentPick = currentPick - 1; } else if (!draftPicksAscending && currentPick == 1) { currentRound = currentRound + 1; draftPicksAscending = true; } } } return(View(new DraftRoomViewModel() { Draft = draft, DraftPicks = draftPicks, Players = players, CurrentRound = currentRound, CurrentPick = currentPick, DraftPickAscending = draftPicksAscending, SelectedPlayerId = 0 })); }