public void AddRound(BORound round, Dictionary<long, BOTeam> teamList)
        {
            int result = round.Winner ? 1 : 0;

            double expectedOutcomeA = CalculateExpectedOutcomeFast(round);
            double expectedOutcomeB = 1 - expectedOutcomeA;

            BOTeam team1 = round.RNDTeam1Team;
            BOTeam team2 = round.RNDTeam2Team;

            team1.ELOValue = (long)Math.Round(team1.ELOValue + _K_VALUE * (result - expectedOutcomeA));
            team2.ELOValue = (long)Math.Round(team2.ELOValue + _K_VALUE * ((1 - result) - expectedOutcomeB));
        }
        public ActionResult Create(RoundViewModel model)
        {
            if (ModelState.IsValid)
            {
                var businessObject = new BORound();
                Mapper.Map<RoundViewModel, BORound>(model, businessObject);

                businessObject.RNDTeam1Team = new BOTeam(model.Team1ListID);
                businessObject.RNDTeam2Team = new BOTeam(model.Team2ListID);
                businessObject.Match = new BOMatch(model.MatchListID);

                _roundService.Save(businessObject);

                return RedirectToAction("Index");
            }
            model.Team1List = new SelectList (_teamService.GetAll(), SelectLists.DataValueField, SelectLists.DataTextField, SelectLists.UnitializedSelectValue);
            model.Team2List = new SelectList (_teamService.GetAll(), SelectLists.DataValueField, SelectLists.DataTextField, SelectLists.UnitializedSelectValue);
            model.MatchList = new SelectList (_matchService.GetAll(), SelectLists.DataValueField, SelectLists.DataTextField, SelectLists.UnitializedSelectValue);

            return View(model);
        }
        public static List<BOMatch> GetAllMatchesOrderedByDate()
        {
            BORoundCollection a = new BORoundCollection();
            SqlConnection con = new SqlConnection(GetConnectionString());

            con.Open();

            try
            {
                SqlCommand cmd = new SqlCommand("P_Match_GetAllSortedByDate", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult);

                try
                {
                    while (rdr.Read())
                    {
                        BORound businessObject = new BORound(rdr);
                        a.Add(businessObject);
                    }
                }
                finally
                {
                    rdr.Close();
                }
            }
            finally
            {
                con.Close();
            }
            ArrayList arrayList = a;

            // Create intermediary array
            BOMatch[] array = (BOMatch[])arrayList.ToArray(typeof(BOMatch));

            // Create the List<T> using the constructor that takes an IEnumerable<T>
            List<BOMatch> list = new List<BOMatch>(array);

            return list;
        }
        public double CalculateExpectedOutcomeOneRound(BORound round)
        {
            BOTeam team1 = round.RNDTeam1Team;
            BOTeam team2 = round.RNDTeam2Team;

            long diff = team2.ELOValue - team1.ELOValue;

            return EloFormula(team1.ELOValue, team2.ELOValue);
        }
 public double CalculateExpectedOutcomeFast(BORound round)
 {
     return CalculateExpectedOutcomeFast(round.RNDTeam1Team, round.RNDTeam1Team, round.Match.Rounds.Count);
 }
        /// <summary>
        /// Internal 0:Needs to be processed, 1:processing, 2:live event, 3:done, 4:elo done
        /// </summary>
        public void Process()
        {
            if (InternalStatus == 3 || InternalStatus == 1)
                return;
            if (InternalStatus == 0)
                InternalStatus = 1;
            Save();
            // This code needs to be moved
            WebClient Client = new WebClient();
            HTML = Client.DownloadString(this.URL);

            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(HTML);
            HtmlNodeCollection matches = document.DocumentNode.Descendants().Where(d => d.Name.Contains("tbody")).ToArray()[1].SelectNodes("tr");

            if (matches != null)
            {
                long roundNumberPrevious = 0;
                BOMatch match = new BOMatch();

                foreach (HtmlNode row in matches)
                {
                    var columns = row.SelectNodes("td");
                    string link = columns[0].FirstChild.Attributes["href"].Value;
                    string externalID = columns[0].InnerText;
                    DateTime date = DateTime.Parse(columns[1].InnerText);
                    string Team1Short = columns[2].InnerText;
                    string Team1URL = columns[2].FirstChild.Attributes["href"].Value;
                    string Team2Short = columns[3].InnerText;
                    string Team2URL = columns[3].FirstChild.Attributes["href"].Value;
                    string winningSide = columns[4].InnerText;
                    long roundNubmer;
                    if (!string.IsNullOrEmpty(columns[6].InnerText))
                        roundNubmer = long.Parse(columns[6].InnerText);
                    else roundNubmer = 1;
                    float gameTime = float.Parse(columns[7].InnerText);
                    int scoreTeam1 = int.Parse(columns[8].InnerText.Split('-')[0].Trim());
                    int scoreTeam2 = int.Parse(columns[8].InnerText.Split('-')[1].Trim());

                    if (BOMatch.CheckExistingExternalID(long.Parse(externalID)))
                    {
                        BORound round = new BORound();

                        round.Winner = winningSide.ToUpper().Contains("RADIANT");
                        round.ScoreTeam1 = scoreTeam1;
                        round.ScoreTeam2 = scoreTeam2;
                        round.GameLengthMinutes = (int)gameTime;

                        if (roundNumberPrevious <= roundNubmer)
                        {// create a new match
                            match = new BOMatch();
                            match.Status = 1;
                            match.Event = this;
                        }
                        round.ExternalID = long.Parse(externalID);
                        round.Match = match;

                        BOTeam t1 = BOTeam.GetByName(Team1Short);
                        if(t1 == null)
                        {
                            t1 = new BOTeam();
                            t1.Name = Team1Short;
                            t1.Game = this.Game;
                            t1.ELOValue = BOTeam.DefaultELOValue;

                            t1.Save();
                        }
                        BOTeam t2 = BOTeam.GetByName(Team2Short);
                        if (t2 == null)
                        {
                            t2 = new BOTeam();
                            t2.Name = Team2Short;
                            t2.Game = this.Game;
                            t2.ELOValue = BOTeam.DefaultELOValue;

                            t2.Save();
                        }

                        round.RNDTeam1Team = t1;
                        round.RNDTeam2Team = t2;
                        round.OddsforTeam1 = 0;
                        round.BetsforTeam1 = 1;
                        match.Format = new BOFormat(1);
                        match.InternalStatus = 1;

                        match.Date = date;
                        round.Date = date;

                        round.ELO1 = 0;
                        round.ELO2 = 0;

                        match.Save();

                        round.Save();

                        roundNumberPrevious = roundNubmer;
                    }
                }
            }
        }
 public BORoundComparer(BORound.Columns column, BORound.SortDirections direction)
 {
     _column = column;
     _direction = direction;
 }