コード例 #1
0
 public ActionResult Edit([Bind(Include = "SeasonID,StartDate,EndDate,Champion")] Season season)
 {
     if (ModelState.IsValid)
     {
         db.Entry(season).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(season));
 }
コード例 #2
0
        public async Task <ActionResult> Edit([Bind(Include = "TeamId,Name,City")] Team team)
        {
            if (ModelState.IsValid)
            {
                db.Entry(team).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(team));
        }
コード例 #3
0
        public async Task <ActionResult> Edit([Bind(Include = "PlayerId,JerseyNumber,FirstName,LastName,Position,Country,TeamId")] Player player)
        {
            if (ModelState.IsValid)
            {
                db.Entry(player).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.TeamId = new SelectList(db.Teams, "TeamId", "Name", player.TeamId);
            return(View(player));
        }
コード例 #4
0
        public async Task UpdateOveralls(List <HMPlayer> players, bool isRookie)
        {
            bool isGoalie = false;

            if (players.All(x => x.Position == "G"))
            {
                isGoalie = true;
            }

            List <decimal> oldRanks = new List <decimal>();

            foreach (var player in players)
            {
                decimal gamesPlayed     = Convert.ToDecimal(player.GamesPlayed);
                decimal goals           = Convert.ToDecimal(player.Goals);
                decimal assists         = Convert.ToDecimal(player.Assists);
                decimal points          = Convert.ToDecimal(player.Points);
                decimal Toi             = Convert.ToDecimal(player.TimeOnIce);
                decimal hits            = Convert.ToDecimal(player.Hits);
                decimal plusMinus       = Convert.ToDecimal(player.PlusMinus);
                decimal powerPlayGoals  = Convert.ToDecimal(player.PowerPlayGoals);
                decimal penalityMinutes = Convert.ToDecimal(player.PenalityMinutes);
                decimal saves           = Convert.ToDecimal(player.Saves);
                decimal goalsAgainst    = Convert.ToDecimal(player.GoalsAgainst);
                decimal shutouts        = Convert.ToDecimal(player.Shutouts);

                if (player.Position == "C")
                {
                    decimal rank = (0.75m * (goals / gamesPlayed) + 0.70m * (assists / gamesPlayed) + 0.20m * (Toi / gamesPlayed) + 0.05m * (hits / gamesPlayed) + 0.10m * (powerPlayGoals / gamesPlayed) - 0.05m * (penalityMinutes / gamesPlayed) + 0.10m * (plusMinus / gamesPlayed));
                    oldRanks.Add(rank);
                }
                else if (player.Position == "LW" || player.Position == "RW")
                {
                    decimal rank = (0.75m * (goals / gamesPlayed) + 0.70m * (assists / gamesPlayed) + 0.20m * (Toi / gamesPlayed) + 0.05m * (hits / gamesPlayed) + 0.10m * (powerPlayGoals / gamesPlayed) - 0.05m * (penalityMinutes / gamesPlayed) + 0.05m * (plusMinus / gamesPlayed));
                    oldRanks.Add(rank);
                }
                else if (player.Position == "D")
                {
                    decimal rank = (0.75m * (goals / gamesPlayed) + 0.70m * (assists / gamesPlayed) + 0.20m * (Toi / gamesPlayed) + 0.10m * (hits / gamesPlayed) + 0.03m * (powerPlayGoals / gamesPlayed) - 0.03m * (penalityMinutes / gamesPlayed) + 0.05m * (plusMinus / gamesPlayed));
                    oldRanks.Add(rank);
                }
                if (player.Position == "G")
                {
                    decimal rank = (-0.75m * (goalsAgainst) + 0.10m * (saves) + 0.20m * (shutouts));
                    oldRanks.Add(rank);
                }
            }

            //Change overalls to match EA Sports algorithm

            decimal oldMin = oldRanks.Min();
            decimal oldMax = oldRanks.Max();
            decimal newMax;
            decimal newMin;

            if (isRookie && !isGoalie)
            {
                newMax = 64;
                newMin = 59;
            }
            else if (!isRookie && !isGoalie)
            {
                newMax = 95;
                newMin = 65;
            }
            else if (isRookie && isGoalie)
            {
                newMax = 74;
                newMin = 65;
            }
            else
            {
                newMax = 95;
                newMin = 75;
            }


            int indexCount = 0;

            foreach (var player in players)
            {
                decimal oldValue = oldRanks[indexCount];
                player.Overall = (int)Math.Round((((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin, 0);
                _context.Players.Attach(player);
                _context.Entry(player).Property(x => x.Overall).IsModified = true;
                indexCount++;
            }

            await _context.SaveChangesAsync();
        }
コード例 #5
0
        public async Task <string> AddOrUpdateTeam(int id, string name, string[] players)
        {
            var anyTeam = _context.Teams.Include(x => x.TeamInfo).Where(x => x.PoolId == id && x.UserId == _userManager.GetUserId(User));

            var enrolledPool = _context.PoolList.Where(x => x.PoolId == id && _userManager.GetUserId(User) == x.UserId).FirstOrDefault();

            if (enrolledPool == null)
            {
                return("You're not enrolled in this pool!!");
            }

            if (name == null)
            {
                return("Please enter a team name.");
            }

            var ruleId = _context.Pools.Where(x => x.Id == id).FirstOrDefault().RuleSetId;

            if (ruleId == null)
            {
                return("Something went wrong..");
            }

            var rule = _context.RuleSets.Where(x => x.Id == ruleId).FirstOrDefault();

            var hMPlayers = _context.Players.Where(x => players.Contains(x.PlayerInfoId.ToString()) && x.ApiId != 0).ToList();

            var forwards   = 0;
            var defencemen = 0;
            var goalies    = 0;

            foreach (var player in hMPlayers)
            {
                //Count every position
                if (player.Position == "C" || player.Position == "LW" || player.Position == "RW")
                {
                    forwards++;
                }
                else if (player.Position == "D")
                {
                    defencemen++;
                }
                else if (player.Position == "G")
                {
                    goalies++;
                }

                //Check to make sure the user hasn't exceeded the player position limit
                if (forwards > rule.maxForwards)
                {
                    return("Too many forwards!");
                }
                else if (defencemen > rule.maxDefensemen)
                {
                    return("Too many defencemen!");
                }
                else if (goalies > rule.maxGoalies)
                {
                    return("Too many goalies!");
                }
            }
            //Check if team is too big or too small
            if ((forwards + defencemen + goalies) > rule.maxPlayers)
            {
                return("Too many players!");
            }
            else if ((forwards + defencemen + goalies) < rule.maxPlayers)
            {
                return($"{rule.maxPlayers - (forwards + defencemen + goalies)} more players are required.");
            }

            //All validation succeeded

            HMTeam team = new HMTeam();

            if (anyTeam.Any())
            {
                //Update
                team = anyTeam.FirstOrDefault();

                if (team.TeamInfo.Name != name)
                {
                    team.TeamInfo.Name = name;
                    _context.TeamInfo.Attach(team.TeamInfo);
                    _context.Entry(team.TeamInfo).Property(x => x.Name).IsModified = true;
                    await _context.SaveChangesAsync();
                }

                players = players.Skip(1).ToArray();
                int[] newPlayers = Array.ConvertAll(players, s => int.Parse(s));
                var   oldPlayers = _context.Players.Include(x => x.Team).Include(x => x.PlayerInfo).Where(x => x.Team.PoolId == id && x.Team.UserId == _userManager.GetUserId(User)).Select(x => x.PlayerInfo.Id).ToArray();
                var   sameTeam   = Enumerable.SequenceEqual(oldPlayers, newPlayers);

                if (sameTeam)
                {
                    return("name updated");
                }

                var existingPlayers = _context.Players.Include(x => x.Team).Where(x => x.Team.Id == anyTeam.FirstOrDefault().Id).ToList();
                _context.Players.RemoveRange(existingPlayers);
                await _context.SaveChangesAsync();
            }
            else
            {
                //Add
                HMTeamInfo teamInfo = new HMTeamInfo();
                teamInfo.Name = name;

                await _context.TeamInfo.AddAsync(teamInfo);

                await _context.SaveChangesAsync();


                team.TeamInfoId = teamInfo.Id;
                team.PoolId     = id;
                team.UserId     = _userManager.GetUserId(User);

                await _context.Teams.AddAsync(team);

                await _context.SaveChangesAsync();
            }

            List <HMPlayer> newlyGeneratedPlayers = new List <HMPlayer>();

            foreach (var player in hMPlayers)
            {
                newlyGeneratedPlayers.Add(new HMPlayer
                {
                    Position     = player.Position,
                    Overall      = player.Overall,
                    TeamId       = team.Id,
                    PlayerInfoId = player.PlayerInfoId
                });
            }

            await _context.Players.AddRangeAsync(newlyGeneratedPlayers);

            await _context.SaveChangesAsync();

            return("success");
        }