예제 #1
0
        public IEnumerable <DataContainers.PlayerDetails> GetPlayers(uint teamId)
        {
            string url = new StringBuilder("file=players&version=2.0&teamID=")
                         .Append(teamId).ToString();

            XDocument doc = XDocument.Load(ChppAccessor.GetDataReader(url, DataFlags.Dynamic));

            XElement elTeam = doc.Root.AssertElement("Team");

            Team compTeam = MatchParserHelper.GetTeam(elTeam, string.Empty);

            if (teamId != compTeam.ID)
            {
                throw new Exception("received wrong team info");
            }

            IList <PlayerDetails> players = new List <PlayerDetails>();

            foreach (XElement el in elTeam.AssertElement("PlayerList").Elements("Player"))
            {
                int    id        = int.Parse(el.AssertElement("PlayerID").Value);
                string givenName = el.AssertElement("FirstName").Value;
                string nickName  = el.AssertElement("NickName").Value;
                string surName   = el.AssertElement("LastName").Value;

                players.Add(new PlayerDetails(id, givenName, nickName, surName));
                //yield return new PlayerDetails(id, givenName, nickName, surName); // this was very slow, why?
            }

            return(players);
        }
예제 #2
0
        public TeamDetails GetTeamDetails(uint teamId)
        {
            string url = new StringBuilder("file=teamdetails&version=2.6&teamID=")
                         .Append(teamId).ToString();

            XDocument doc = XDocument.Load(ChppAccessor.GetDataReader(url, DataFlags.Dynamic));

            XElement elUser = doc.Root.AssertElement("User");

            int      userId       = int.Parse(elUser.AssertElement("UserID").Value);
            string   userName     = elUser.AssertElement("Loginname").Value;
            DateTime userJoinDate = DateTime.Parse(elUser.AssertElement("ActivationDate").Value); // not <SignupDate> here

            User user = new User(userId, userName)
            {
                JoinDate = userJoinDate
            };

            Team team = MatchParserHelper.GetTeam(doc.Root.AssertElement("Team"), string.Empty);

            return(new TeamDetails(team.ID, team.Name)
            {
                Owner = user
            });
        }
        public TransferHistory GetTransfers(uint teamId)
        {
            Team            team      = null;
            DateTime        from      = DateTime.MaxValue;
            DateTime        to        = DateTime.MinValue;
            List <Transfer> transfers = new List <Transfer>();

            uint numberOfPages = 0;
            uint curPage       = 1;

            do
            {
                string url = new StringBuilder("file=transfersteam&version=1.2")
                             .Append("&teamID=").Append(teamId)
                             .Append("&pageIndex=")
                             .Append(curPage).ToString();

                XDocument doc = XDocument.Load(ChppAccessor.GetDataReader(url, DataFlags.Dynamic));

                team = MatchParserHelper.GetTeam(doc.Root.AssertElement("Team"), string.Empty);

                XElement elTransfers = doc.Root.AssertElement("Transfers");
                numberOfPages = uint.Parse(elTransfers.AssertElement("Pages").Value);
                DateTime startDate = DateTime.Parse(elTransfers.AssertElement("StartDate").Value);
                DateTime endDate   = DateTime.Parse(elTransfers.AssertElement("EndDate").Value);
                if (startDate < from && startDate != DateTime.MinValue)
                {
                    from = startDate;
                }
                // ORIGINAL IMPLEMENTATION OF NEXT LINE:
                //if (endDate > to && endDate != DateTime.MaxValue) to = endDate;
                // REASON FOR CHANGE:
                // DateTime.MaxValue contains additional nanosecond ticks, so it does not equal DateTime(9999,12,31,23,59,59)
                // For simplicity, we deduct one day from this max value and assert that the returned value is smaller than this
                if (endDate > to && endDate < DateTime.MaxValue.AddDays(-1))
                {
                    to = endDate;
                }

                foreach (XElement elTransfer in elTransfers.Elements("Transfer"))
                {
                    int      id     = int.Parse(elTransfer.AssertElement("TransferID").Value);
                    DateTime date   = DateTime.Parse(elTransfer.AssertElement("Deadline").Value);
                    Player   player = MatchParserHelper.GetPlayer(elTransfer.AssertElement("Player"));
                    Team     buyer  = MatchParserHelper.GetTeam(elTransfer.AssertElement("Buyer"), "Buyer");
                    Team     seller = MatchParserHelper.GetTeam(elTransfer.AssertElement("Seller"), "Seller");
                    Money    price  = new Money(double.Parse(elTransfer.AssertElement("Price").Value), Currency.SEK);

                    transfers.Add(new Transfer(id, date, player, buyer, seller, price));
                }
            } while (curPage++ < numberOfPages);

            return(new TransferHistory(team, from, to)
            {
                Transfers = transfers
            });
        }
        public MatchArchive GetMatches(uint teamId, DateTime startDate, DateTime endDate)
        {
            List <Match> matches       = new List <Match>();
            DateTime     curMonthStart = startDate;
            Team         team          = null;

            // 15 minute grid
            endDate = endDate.AddSeconds(-endDate.Second);
            endDate = endDate.AddMinutes(-(endDate.Minute % 15));

            while (curMonthStart < endDate)
            {
                DateTime curMonthEnd = new DateTime(curMonthStart.Year, curMonthStart.Month, 1).AddMonths(1).AddSeconds(-1);
                if (curMonthEnd > endDate)
                {
                    curMonthEnd = endDate;
                }

                string url = new StringBuilder("file=matchesarchive&version=1.1")
                             .Append("&teamID=").Append(teamId)
                             .Append("&FirstMatchDate=").Append(curMonthStart.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture))
                             .Append("&LastMatchDate=").Append(curMonthEnd.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)).ToString();

                XDocument doc = XDocument.Load(ChppAccessor.GetDataReader(url, DataFlags.Static));

                XElement elTeam = doc.Root.AssertElement("Team");

                Team compTeam = MatchParserHelper.GetTeam(elTeam, string.Empty);
                if (teamId != compTeam.ID)
                {
                    throw new Exception("received wrong team info");
                }
                team = compTeam;
                // assert dates

                foreach (XElement el in doc.Descendants("Match"))
                {
                    matches.Add(MatchParserHelper.GetMatch(el));
                }

                curMonthStart = curMonthEnd.AddSeconds(1);
            }

            // TODO: team may be null here (if endDate < startDate)
            MatchArchive ar = new MatchArchive(team, startDate, endDate);

            ar.Matches = matches;

            return(ar);
        }
예제 #5
0
        private Lineup GetLineup(int matchId, int teamId)
        {
            string url = new StringBuilder("file=matchlineup&version=1.6&matchID=").Append(matchId)
                         .Append("&teamID=").Append(teamId).ToString();

            XDocument doc = XDocument.Load(ChppAccessor.GetDataReader(url, DataFlags.Static));

            XElement elTeam = doc.Root.AssertElement("Team");

            Team compTeam = MatchParserHelper.GetTeam(elTeam, string.Empty);

            if (teamId != compTeam.ID)
            {
                throw new Exception("received wrong team info");
            }

            XElement elLineup = elTeam.AssertElement("Lineup");

            Lineup lineup = new Lineup();

            foreach (XElement elPlayer in elLineup.Elements("Player"))
            {
                Lineup.LineupRole role;
                XElement          elRoleId = elPlayer.Element("RoleID");
                if (elRoleId == null || string.IsNullOrEmpty(elRoleId.Value))
                {
                    role = Lineup.LineupRole.ReplacedPlayerN;
                }
                else
                {
                    role = (Lineup.LineupRole) int.Parse(elPlayer.AssertElement("RoleID").Value);
                }

                double?  stars         = null;
                XElement elRatingStars = elPlayer.Element("RatingStars");
                if (elRatingStars != null && !string.IsNullOrEmpty(elRatingStars.Value))
                {
                    stars = double.Parse(elRatingStars.Value, CultureInfo.InvariantCulture);
                }

                Player player = MatchParserHelper.GetPlayer(elPlayer);
                lineup.LineupItems.Add(new Lineup.LineupItem()
                {
                    Player = player, Role = role, RatingStars = stars
                });
            }

            return(lineup);
        }
예제 #6
0
        public MatchDetails GetMatchDetails(uint matchId)
        {
            string url = new StringBuilder("file=matchdetails&version=2.0&matchID=")
                         .Append(matchId)
                         .Append("&matchEvents=true")
                         .ToString();

            XDocument doc = XDocument.Load(ChppAccessor.GetDataReader(url, DataFlags.Static));

            XElement elMatch = doc.Root.AssertElement("Match");
            Match    match   = MatchParserHelper.GetMatch(elMatch);

            MatchDetails md = new MatchDetails(match.ID, match.Type, match.HomeTeam, match.AwayTeam);

            md.Date = match.Date;

            XElement elHomeTeam = elMatch.AssertElement("HomeTeam");
            XElement elAwayTeam = elMatch.AssertElement("AwayTeam");

            uint homeGoals = uint.Parse(elHomeTeam.AssertElement("HomeGoals").Value);
            uint awayGoals = uint.Parse(elAwayTeam.AssertElement("AwayGoals").Value);

            md.FinalScore = new Score(homeGoals, awayGoals);

            XElement elArena = elMatch.Element("Arena");

            if (elArena != null)
            {
                md.Visitors = new Crowd();
                XElement elVisitors = elArena.Element("SoldTotal");
                if (elVisitors != null)
                {
                    md.Visitors.Total = uint.Parse(elVisitors.Value);
                }
                elVisitors = elArena.Element("SoldTerraces");
                if (elVisitors != null)
                {
                    md.Visitors.Terraces = uint.Parse(elVisitors.Value);
                }
                elVisitors = elArena.Element("SoldBasic");
                if (elVisitors != null)
                {
                    md.Visitors.BasicSeats = uint.Parse(elVisitors.Value);
                }
                elVisitors = elArena.Element("SoldRoof");
                if (elVisitors != null)
                {
                    md.Visitors.SeatsUnderRoof = uint.Parse(elVisitors.Value);
                }
                elVisitors = elArena.Element("SoldVIP");
                if (elVisitors != null)
                {
                    md.Visitors.Vip = uint.Parse(elVisitors.Value);
                }
            }

            IList <Goal> goals = new List <Goal>();

            foreach (XElement elGoal in doc.Root.Descendants("Goal"))
            {
                goals.Add(new Goal(
                              md,
                              uint.Parse(elGoal.AssertElement("ScorerMinute").Value),
                              new Score(
                                  uint.Parse(elGoal.AssertElement("ScorerHomeGoals").Value),
                                  uint.Parse(elGoal.AssertElement("ScorerAwayGoals").Value)),
                              new Team(
                                  int.Parse(elGoal.AssertElement("ScorerTeamID").Value),
                                  "unnamed team"),
                              new Player(
                                  int.Parse(elGoal.AssertElement("ScorerPlayerID").Value),   // this handles negative dummy players
                                  elGoal.AssertElement("ScorerPlayerName").Value)));
            }
            md.Goals = goals;

            IList <MatchEvent> events = new List <MatchEvent>();

            foreach (XElement elEvent in doc.Root.Descendants("Event"))
            {
                events.Add(new MatchEvent(
                               match,
                               uint.Parse(elEvent.Attribute("Index").Value),
                               (MatchEvent.MatchEventType) uint.Parse(elEvent.AssertElement("EventKey").Value.Split('_')[0]),
                               uint.Parse(elEvent.AssertElement("Minute").Value),
                               elEvent.AssertElement("EventText").Value,
                               int.Parse(elEvent.AssertElement("SubjectTeamID").Value),
                               int.Parse(elEvent.AssertElement("SubjectPlayerID").Value),  // this handles negative dummy players
                               int.Parse(elEvent.AssertElement("ObjectPlayerID").Value))); // this handles negative dummy players
            }
            md.Events = events;


            md.HomeRatings = new Ratings(uint.Parse(elHomeTeam.AssertElement("RatingMidfield").Value),
                                         uint.Parse(elHomeTeam.AssertElement("RatingLeftDef").Value),
                                         uint.Parse(elHomeTeam.AssertElement("RatingRightDef").Value),
                                         uint.Parse(elHomeTeam.AssertElement("RatingMidDef").Value),
                                         uint.Parse(elHomeTeam.AssertElement("RatingLeftAtt").Value),
                                         uint.Parse(elHomeTeam.AssertElement("RatingRightAtt").Value),
                                         uint.Parse(elHomeTeam.AssertElement("RatingMidAtt").Value));

            md.AwayRatings = new Ratings(uint.Parse(elAwayTeam.AssertElement("RatingMidfield").Value),
                                         uint.Parse(elAwayTeam.AssertElement("RatingLeftDef").Value),
                                         uint.Parse(elAwayTeam.AssertElement("RatingRightDef").Value),
                                         uint.Parse(elAwayTeam.AssertElement("RatingMidDef").Value),
                                         uint.Parse(elAwayTeam.AssertElement("RatingLeftAtt").Value),
                                         uint.Parse(elAwayTeam.AssertElement("RatingRightAtt").Value),
                                         uint.Parse(elAwayTeam.AssertElement("RatingMidAtt").Value));

            md.HomeLineup = GetLineup(md.ID, md.HomeTeam.ID);
            md.AwayLineup = GetLineup(md.ID, md.AwayTeam.ID);
            return(md);
        }