public ActionResult Create(Game game)
        {
            if (ModelState.IsValid)
            {
                db.Games.Add(game);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(game);
        }
Example #2
0
        public static void UpdateGames(string xmlLocation)
        {
            //The XML document is expected to be in the same format as http://www.nfl.com/liveupdate/scorestrip/ss.xml
            XDocument xml = XDocument.Load(xmlLocation);
            var games = xml.Descendants("g");

            using (PickemDBContext db = new PickemDBContext())
            {
                foreach (var g in games)
                {
                    Game game = new Game();
                    game.Eid = g.Attribute("eid").Value.ToString();
                    game.Gsis = g.Attribute("gsis").Value.ToString();
                    game.Week = Convert.ToInt32(g.Parent.Attribute("w").Value.ToString());
                    game.Year = Convert.ToInt32(g.Parent.Attribute("y").Value.ToString());
                    game.Day = g.Attribute("d").Value.ToString();
                    game.Time = g.Attribute("t").Value.ToString();
                    game.Quarter = g.Attribute("q").Value.ToString();
                    game.TimeRemaining = (g.Attribute("k") != null) ? g.Attribute("k").Value.ToString() : null;
                    game.HomeTeam = g.Attribute("h").Value.ToString().Replace("JAX", "JAC");
                    game.HomeTeamScore = Convert.ToInt32(g.Attribute("hs").Value.ToString());
                    game.VisitorTeam = g.Attribute("v").Value.ToString().Replace("JAX", "JAC");
                    game.VisitorTeamScore = Convert.ToInt32(g.Attribute("vs").Value.ToString());
                    game.GameType = g.Attribute("gt").Value.ToString();

                    if (game.HomeTeamScore == game.VisitorTeamScore)
                        game.WinningTeam = null;
                    else if (game.HomeTeamScore > game.VisitorTeamScore)
                        game.WinningTeam = game.HomeTeam;
                    else if (game.VisitorTeamScore > game.HomeTeamScore)
                        game.WinningTeam = game.VisitorTeam;

                    int gameId = (from t in db.Games
                                  where t.Eid == game.Eid
                                  select t.Id).SingleOrDefault();

                    if (gameId != 0)
                    {
                        game.Id = Convert.ToInt32(gameId);

                        db.Entry(game).State = System.Data.Entity.EntityState.Modified;
                    }
                    else
                    {
                        db.Games.Add(game);
                    }
                }

                db.SaveChanges();

                var week = xml.Descendants("gms").FirstOrDefault();
                if (week != null)
                {
                    //Update pick results
                    UpdatePicks(Convert.ToInt32(week.Attribute("w").Value), Convert.ToInt32(week.Attribute("y").Value));

                    //Save XML to file if all games are completed
                    xml.Save(HttpContext.Current.Server.MapPath(VirtualPathUtility.ToAbsolute(string.Format("~/Content/datafiles/{0}Week{1}.xml", week.Attribute("y").Value, week.Attribute("w").Value))));
                }

            }
        }
 public ActionResult Edit(Game game)
 {
     if (ModelState.IsValid)
     {
         db.Entry(game).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(game);
 }