public IEnumerable<GameEvent> Get(long gameId)
 {
     using (var repository = new GameEventRepository())
     {
         return (from evnt in repository.GetAll()
                 where evnt.GameId == gameId
                 select evnt).OrderBy(evnt => evnt.Timestamp).ToList();
     }
 }
        public void Mine()
        {
            bool notFound = false;
            int dayId = StartMatchDayId;
            int oldDayId = 0;
            int matchId = StartMatchId;

            while (dayId <= EndMatchDayId)
            {
                if (oldDayId != dayId)
                {
                    Console.Out.WriteLine(String.Format("Mining game events from matchday {0}", dayId));
                    oldDayId = dayId;
                }
                try
                {
                    string data = GetUrlData(detailsUrl, dayId, matchId);
                    Game game = GetGameFromDetailsData(data);
                    bool valid = true;

                    // check for parse errors
                    if (game == null)
                    {
                        Console.Out.WriteLine(String.Format("Unable to match game id {0}", matchId));
                        valid = false;
                    }
                    else
                    {
                        // check to see if game exists
                        using (var repository = new GameRepository()) {

                            var existing = from g in repository.GetAll()
                                           where g.MatchDay == dayId
                                           && g.HomeTeamId == game.HomeTeamId
                                           && g.AwayTeamId == game.AwayTeamId
                                           && g.EventsRetrieved == RetrievalStatus.COMPLETED
                                           select g;
                            if (existing.Count() > 0)
                                valid = false;
                        }
                    }

                    if (valid)
                    {
                        // add more identity information to the game
                        game.LiveMatchId = matchId;
                        game.MatchDay = dayId;
                        game.EventsRetrieved = RetrievalStatus.IN_PROGRESS;
                        using (var repository = new GameRepository())
                        {
                            repository.Save(game);
                        }

                        Team home = null;
                        Team away = null;
                        using (var repository = new TeamRepository())
                        {
                            home = repository.GetAll().Where(t => t.Id == game.HomeTeamId).First();
                            away = repository.GetAll().Where(t => t.Id == game.AwayTeamId).First();
                        }

                        Console.Out.WriteLine(String.Format("Matched data from {0} vs {1}", home.Name, away.Name));

                        // get the events and save them to the db
                        List<GameEvent> gameEvents = new List<GameEvent>();
                        if (BSTDateTime.FromBSTDateTime(game.Start.AddHours(2)) <= DateTime.UtcNow)
                        {
                            gameEvents = GetGameEvents(game);
                        }
                        if (gameEvents.Count > 0)
                        {
                            using (var repository = new GameEventRepository())
                            {
                                repository.AddRange(gameEvents);
                            }
                            // update the game event retrieval status
                            game.EventsRetrieved = RetrievalStatus.COMPLETED;
                        }
                        else
                        {
                            // no events found
                            game.EventsRetrieved = RetrievalStatus.NONE;
                        }
                        using (var repository = new GameRepository())
                        {
                            repository.Save(game);
                        }

                        Console.Out.WriteLine(String.Format("Mined {0} events from game", gameEvents.Count));
                    }
                    else
                    {
                        Console.Out.WriteLine(String.Format("Unable to match game id {0}", matchId));
                    }
                    // update loop variants
                    notFound = false;
                    matchId++;
                }
                catch
                {
                    if (notFound)
                        break;

                    notFound = true;
                    dayId++;
                }
            }
        }
        public void Update()
        {
            // Delete everything
            using (var repository = new TopEventRepository())
            {
                var evnts = (from evnt in repository.GetAll() select evnt).ToList();
                foreach(var evnt in evnts)
                {
                    repository.Delete(evnt);
                }
            }

            // update top points by count
            List<ActivityPoint> topPoints = new List<ActivityPoint>();
            List<GameEvent> topEvents = new List<GameEvent>();
            int skipPoints = 0;

            while (topEvents.Count < 10)
            {
                ActivityPoint point = null;
                GameEvent gameEvent = null;
                using (var repository = new ActivityRepository())
                {
                    point = (from p in repository.DataSet.Include("Game")
                             where p.Game.TweetCount > 0 &&
                             (p.Time < p.Game.HalftimeStart || p.Game.HalftimeEnd < p.Time)
                             select p).OrderByDescending(p => p.TweetCount).Skip(skipPoints).Take(1).First();
                }
                using (var repository = new GameEventRepository())
                {
                    gameEvent = (from evnt in repository.GetAll()
                                 where evnt.GameId == point.GameId
                                 && evnt.Timestamp < point.Time
                                 select evnt).OrderByDescending(evnt => evnt.Timestamp).Take(1).SingleOrDefault();
                }

                if (gameEvent == null
                    || gameEvent.Type == GameEventType.FIRST_HALF_START
                    || gameEvent.Type == GameEventType.FIRST_HALF_END
                    || gameEvent.Type == GameEventType.SECOND_HALF_START
                    || gameEvent.Type == GameEventType.SECOND_HALF_END
                    || topEvents.Where(evnt => evnt.Id == gameEvent.Id).Count() > 0)
                {
                    // this event is already in the list
                    skipPoints++;
                }
                else
                {
                    topEvents.Add(gameEvent);
                    topPoints.Add(point);
                }
            }

            using (var repository = new TopEventRepository())
            {
                for (int i = 0; i < topPoints.Count; i++)
                {
                    TopEvent evnt = new TopEvent()
                    {
                        IsRelativeTop = false,
                        Rank = i + 1,
                        GameId = topPoints[i].GameId,
                        ActivityId = topPoints[i].Id,
                        EventId = topEvents[i].Id
                    };
                    repository.Save(evnt);
                }
            }

            // update top points by percentage
            topPoints = new List<ActivityPoint>();
            topEvents = new List<GameEvent>();
            skipPoints = 0;

            while (topEvents.Count < 10)
            {
                ActivityPoint point = null;
                GameEvent gameEvent = null;
                using (var repository = new ActivityRepository())
                {
                    point = (from p in repository.DataSet.Include("Game")
                             where p.Game.TweetCount > 0 &&
                             (p.Time < p.Game.HalftimeStart || p.Game.HalftimeEnd < p.Time)
                             select p).OrderByDescending(p => (double)p.TweetCount / (double)p.Game.TweetCount).Skip(skipPoints).Take(1).First();
                }
                using (var repository = new GameEventRepository())
                {
                    gameEvent = (from evnt in repository.GetAll()
                                 where evnt.GameId == point.GameId
                                 && evnt.Timestamp < point.Time
                                 select evnt).OrderByDescending(evnt => evnt.Timestamp).Take(1).SingleOrDefault();
                }

                if (gameEvent == null
                    || gameEvent.Type == GameEventType.FIRST_HALF_START
                    || gameEvent.Type == GameEventType.FIRST_HALF_END
                    || gameEvent.Type == GameEventType.SECOND_HALF_START
                    || gameEvent.Type == GameEventType.SECOND_HALF_END
                    || topEvents.Where(evnt => evnt.Id == gameEvent.Id).Count() > 0)
                {
                    // this event is already in the list
                    skipPoints++;
                }
                else
                {
                    topEvents.Add(gameEvent);
                    topPoints.Add(point);
                }
            }

            using (var repository = new TopEventRepository())
            {
                for (int i = 0; i < topPoints.Count; i++)
                {
                    TopEvent evnt = new TopEvent()
                    {
                        IsRelativeTop = true,
                        Rank = i + 1,
                        GameId = topPoints[i].GameId,
                        ActivityId = topPoints[i].Id,
                        EventId = topEvents[i].Id
                    };
                    repository.Save(evnt);
                }
            }
        }