// GET: Players/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var viewModel = new PlayerDetailsViewModel()
            {
                Player = await _playersService.GetPlayerAsync(id.Value)
            };

            if (viewModel.Player == null)
            {
                return(NotFound());
            }

            viewModel.Games = await _gamesService.GetFinalGamesByTeamAsync(viewModel.Player.TeamID);

            viewModel.GameLogs = new List <PlayerGameStats>();

            foreach (Game g in viewModel.Games)
            {
                var log = _playerGameStatsService.GetPlayerGameStatsByGameAsync(viewModel.Player.PlayerID, g.GameID);;
                viewModel.GameLogs.Add(await log);
            }
            return(View(viewModel));
        }
Esempio n. 2
0
        public async Task <bool> Create(PlayerMyTeam playerMyTeam)
        {
            Player player = null;
            IEnumerable <PlayerMyTeam> roster = null;
            var playerTask = _playersService.GetPlayerAsync(playerMyTeam.PlayerID);
            var rosterTask = GetRosterAsync(playerMyTeam.MyTeamID);
            var tasks      = new List <Task> {
                playerTask, rosterTask
            };

            while (tasks.Any())
            {
                Task finshed = await Task.WhenAny(tasks);

                if (finshed == playerTask)
                {
                    tasks.Remove(playerTask);
                    player = await playerTask;
                }
                else if (finshed == rosterTask)
                {
                    tasks.Remove(rosterTask);
                    roster = await rosterTask;
                }
                else
                {
                    tasks.Remove(finshed);
                }
            }

            int spots = GetRosterSpots(roster, player.Position);

            if (player.Position == "C" && spots > 0 || player.Position != "C" && spots > 1 || await IsPlayerOnRoster(playerMyTeam.MyTeamID, playerMyTeam.PlayerID))
            {
                return(false);
            }
            try
            {
                await _context.AddAsync(playerMyTeam);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Create([Bind("PlayerID,MyTeamID")] PlayerMyTeam playerMyTeam)
        {
            string pos        = "";
            MyTeam myTeam     = null;
            var    playerTask = (_playersService.GetPlayerAsync(playerMyTeam.PlayerID));
            var    myTeamTask = _myTeamsService.GetMyTeamByIDAsync(playerMyTeam.MyTeamID);
            var    tasks      = new List <Task> {
                playerTask, myTeamTask
            };

            while (tasks.Any())
            {
                Task finished = await Task.WhenAny(tasks);

                if (finished == playerTask)
                {
                    tasks.Remove(playerTask);
                    pos = (await playerTask).Position;
                }
                else if (finished == myTeamTask)
                {
                    tasks.Remove(myTeamTask);
                    myTeam = await myTeamTask;
                }
            }
            var isAuthorized = await _auth.AuthorizeAsync(User, myTeam, Operations.Create);

            if (!isAuthorized.Succeeded)
            {
                return(new ChallengeResult());
            }

            if (ModelState.IsValid)
            {
                if (await _playerMyTeamService.Create(playerMyTeam))
                {
                    return(RedirectToAction("Details", "MyTeams", new { id = playerMyTeam.MyTeamID }));
                }
                else
                {
                    TempData["PosMessage"] = "You cannot have that many players at that position!";
                    return(RedirectToAction("Create", new { teamID = playerMyTeam.MyTeamID, posFilter = pos }));
                }
            }
            TempData["PosMessage"] = "An error occured!";
            return(RedirectToAction("Create", new { teamID = playerMyTeam.MyTeamID, posFilter = pos }));
        }
        public async Task <IActionResult> GetPlayerAsync(long teamId, long playerId)
        {
            try
            {
                var player = await _playerService.GetPlayerAsync(teamId, playerId);

                return(Ok(player));
            }
            catch (NotFoundItemException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Something unexpected happened."));
            }
        }