public void PopulateLeagueTable()
        {
            PopulatePlayerPicks();

            using (var db = new leaguepredictorEntities())
            {
                CurrentLeaguePositions = db.team_rankings.Select(tr => new LeaguePosition {
                    Team = tr.team.team_name, Position = tr.position
                })
                                         .ToList();

                //calculate each player scores
                PlayerPicks.ForEach(pp =>
                {
                    var ltm = GenerateLeagueTableModel(pp);
                    ltm.ID  = pp.ID;
                    LeagueTable.Add(ltm);
                });

                LeagueTable = LeagueTable.OrderBy(ltm => ltm.Points).ToList();
                foreach (var ltm in LeagueTable)
                {
                    ltm.Position = LeagueTable.IndexOf(ltm) + 1;
                }
            }
        }
        public PlayerScoresViewModel GetPlayerScoresViewModel(int id)
        {
            using (var db = new leaguepredictorEntities())
            {
                var psvm = db.players.Where(p => p.id == id).Select(p =>
                                                                    new
                {
                    ID          = p.id,
                    PlayerPicks = new
                    {
                        Player     = p.name,
                        Selections = p.players_selections.Where(ps => ps.player_id == p.id).Select(ps =>
                                                                                                   new
                        {
                            Team     = ps.team.team_name,
                            Position = ps.position,
                            League   = ps.team.league.league_name
                        }
                                                                                                   ).ToList(),
                    }
                }).SingleOrDefault();

                if (psvm == null)
                {
                    return(null);
                }

                var ret = new PlayerScoresViewModel
                {
                    ID          = psvm.ID,
                    PlayerPicks = new PlayerPicks
                    {
                        Player     = psvm.PlayerPicks.Player,
                        Selections = psvm.PlayerPicks.Selections.Select(a => new LeaguePosition
                        {
                            League   = a.League,
                            Position = a.Position,
                            Team     = a.Team
                        }).ToList()
                    },
                    CurrentLeaguePosition = CurrentLeaguePositions
                };
                ret.Points = new Dictionary <LeaguePosition, int>();
                ret.PlayerPicks.Selections.ForEach(sel =>
                {
                    ret.Points.Add(sel, GetPositionDifference(sel));
                });

                return(ret);
            }
        }
 public TeamSelectionsViewModel()
 {
     using (var db = new leaguepredictorEntities())
     {
         PremTeams = new List <SelectListItem>();
         PremTeams = db.teams.Where(t => t.league.league_name.Equals("Premier League")).Select(t =>
                                                                                               new SelectListItem {
             Text = t.team_name, Value = t.team_name
         }
                                                                                               ).OrderBy(t => t.Text).ToList();
         ChampTeams = new List <SelectListItem>();
         ChampTeams = db.teams.Where(t => t.league.league_name.Equals("Championship")).Select(t =>
                                                                                              new SelectListItem {
             Text = t.team_name, Value = t.team_name
         }
                                                                                              ).OrderBy(t => t.Text).ToList();
     }
 }
        public void PopulatePlayerPicks()
        {
            using (var db = new leaguepredictorEntities())
            {
                var playerSelections = db.players_selections.Select(sel =>
                                                                    new { Player = sel.player.name, Team = sel.team.team_name, Position = sel.position, Id = sel.player.id }).ToList();

                foreach (var p in playerSelections.Select(p => p.Player).Distinct())
                {
                    var pp = new PlayerPicks();
                    pp.Player     = p;
                    pp.ID         = db.players.Where(pl => pl.name.Equals(p)).Single().id;
                    pp.Selections = playerSelections.Where(ps => ps.Player.Equals(pp.Player))
                                    .Select(ps => new LeaguePosition {
                        Team = ps.Team, Position = ps.Position
                    })
                                    .ToList();
                    PlayerPicks.Add(pp);
                }
            }
        }
Example #5
0
        public static void Initialize()
        {
            using (var context = new leaguepredictorEntities())
            {
                context.players.AddRange(new player[]
                {
                    new player {
                        name = "chris"
                    },
                    new player {
                        name = "john"
                    }
                });

                context.leagues.AddRange(new league[]
                {
                    new league {
                        league_name = "Premier League"
                    },
                    new league {
                        league_name = "Championship"
                    }
                });

                context.SaveChanges();

                context.teams.AddRange(new team[]
                {
                    new team {
                        team_name = "Man Utd", league = context.leagues.Single(l => l.league_name.Equals("Premier League"))
                    },
                    new team {
                        team_name = "Arsenal", league = context.leagues.Single(l => l.league_name.Equals("Championship"))
                    },
                    new team {
                        team_name = "Chelsea", league = context.leagues.Single(l => l.league_name.Equals("Premier League"))
                    },
                    new team {
                        team_name = "Liverpool", league = context.leagues.Single(l => l.league_name.Equals("Premier League"))
                    }
                });

                context.SaveChanges();

                context.team_rankings.AddRange(new team_rankings[]
                {
                    new team_rankings {
                        team = context.teams.Single(t => t.team_name.Equals("Man Utd")), position = 1
                    },
                    new team_rankings {
                        team = context.teams.Single(t => t.team_name.Equals("Arsenal")), position = 15
                    },
                    new team_rankings {
                        team = context.teams.Single(t => t.team_name.Equals("Chelsea")), position = 8
                    },
                    new team_rankings {
                        team = context.teams.Single(t => t.team_name.Equals("Liverpool")), position = 4
                    }
                });

                context.SaveChanges();

                context.players_selections.AddRange(new players_selections[]
                {
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("chris")), team = context.teams.Single(t => t.team_name.Equals("Man Utd")), position = 4
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("chris")), team = context.teams.Single(t => t.team_name.Equals("Arsenal")), position = 1
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("chris")), team = context.teams.Single(t => t.team_name.Equals("Chelsea")), position = 2
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("chris")), team = context.teams.Single(t => t.team_name.Equals("Liverpool")), position = 3
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("john")), team = context.teams.Single(t => t.team_name.Equals("Man Utd")), position = 3
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("john")), team = context.teams.Single(t => t.team_name.Equals("Arsenal")), position = 4
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("john")), team = context.teams.Single(t => t.team_name.Equals("Chelsea")), position = 1
                    },
                    new players_selections {
                        player = context.players.Single(p => p.name.Equals("john")), team = context.teams.Single(t => t.team_name.Equals("Liverpool")), position = 2
                    }
                });

                context.SaveChanges();
            }
        }