Example #1
0
        private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker.ReportProgress(0, new BGReport("Connecting to SoccerBase ..."));
            mainForm.openExcel(backgroundWorker);
            using (SqlConnection connection = new SqlConnection(MainForm.STRCONN))
            {
                bool      b1, b2, bX;
                byte      aLevel, aScore, hLevel, hScore, round;
                double    aOdds, avgPercent = 0, dOdds, hOdds, sumProfit = 0;
                int       aStrength, diffLevel, i = 0, hStrength, scoreResult, strengthDiff, strengthResult;
                string    bgcolor, aTeam, hTeam, q, qWhere;
                string    rowFormat   = @"<tr style='background-color:{0};color:{1};'>
                    <td class='League'>{2}</td>
                    <td class='Season'>{3}</td>
                    <td class='Round'>{4}</td>
                    <td class='HomeTeam'>{5}</td>
                    <td class='AwayTeam'>{6}</td>
                    <td class='HomeOdds'>{7}</td>
                    <td class='DrawOdds'>{8}</td>
                    <td class='AwayOdds'>{9}</td>
                    <td class='HomeStrength'>{10}</td>
                    <td class='AwayStrength'>{11}</td>
                    <td class='HomeLevel'>{12}</td>
                    <td class='AwayLevel'>{13}</td>
                    <td class='Result'>{14}</td>
                </tr>";
                string    tableFormat = @"<p>{0}</p><p1>{1}</p1>
                    <table border='1' style='font-family:Verdana;font-size:12px;'>
                        <thead>
                            <tr>
                                <th>League</th> <th>Season</th> <th>Round</th><th>HomeTeam</th> <th>AwayTeam</th><th>HomeOdds</th> <th>DrawOdds</th> <th>AwayOdds</th>
                                <th>HomeStrength</th> <th>AwayStrength</th><th>HomeLevel</th> <th>AwayLevel</th> <th>Result</th>
                            </tr>
                        </thead>
                        <tbody>{2}</tbody>
                    </table><br/>";
                Betting   bet         = new Betting(true);
                DataPoint dataPoint;
                Dictionary <LeaguesKey, Betting[]> htmlDict = new Dictionary <LeaguesKey, Betting[]>();
                LeaguesKey    lKey      = new LeaguesKey();
                List <string> alLeagues = new List <string>();
                Dictionary <LRTKey, Power> dict;
                Series        series;
                StringBuilder html = new StringBuilder();

                b1 = bX = b2 = false;
                Invoke((MethodInvoker)(() =>
                {
                    foreach (LeaguesKey item in lbLeagues.SelectedItems)
                    {
                        alLeagues.Add("Country='" + item.Country + "' AND League='" + item.League + "' AND Season='" + item.Season + "'");
                    }
                    b1 = cb1.Checked;
                    bX = cbX.Checked;
                    b2 = cb2.Checked;
                }));
                qWhere = alLeagues.Count > 0 ? "(" + String.Join(" OR ", alLeagues) + ") AND " : "";

                backgroundWorker.ReportProgress(30, new BGReport("Processing dbo.history ..."));
                connection.Open();

                dict = MethodAForm.getHistoryDict(connection);

                backgroundWorker.ReportProgress(60, new BGReport("Processing dbo.archive ..."));
                q = @"SELECT * FROM dbo.archive WHERE " + qWhere + "Round>2 AND ScoreH IS NOT NULL ORDER BY Country,League,Season,Round";
                using (SqlDataReader reader = new SqlCommand(q, connection).ExecuteReader())
                {
                    while (reader.Read())
                    {
                        round        = (byte)reader["Round"];
                        hTeam        = reader["HomeTeam"].ToString();
                        aTeam        = reader["AwayTeam"].ToString();
                        hScore       = (byte)reader["ScoreH"];
                        aScore       = (byte)reader["ScoreA"];
                        lKey.Country = reader["Country"].ToString();
                        lKey.League  = reader["League"].ToString();
                        lKey.Season  = reader["Season"].ToString();
                        hLevel       = dict[new LRTKey(reader, hTeam, aTeam, (byte)(round - 1))].Level;
                        aLevel       = dict[new LRTKey(reader, aTeam, hTeam, (byte)(round - 1))].Level;
                        hStrength    = dict[new LRTKey(reader, hTeam, aTeam, round)].PrevAvgStrength;
                        aStrength    = dict[new LRTKey(reader, aTeam, hTeam, round)].PrevAvgStrength;
                        if (MethodAForm.debug)
                        {
                            Console.WriteLine("[{0}, {1}] {2} - {3} L({4}:{5}) R({6}:{7}) S({8}:{9})\n",
                                              lKey, round, hTeam, aTeam, hLevel, aLevel, hScore, aScore, hStrength, aStrength
                                              );
                        }
                        hOdds          = (double)reader["HomeOdds"];
                        dOdds          = (double)reader["DrawOdds"];
                        aOdds          = (double)reader["AwayOdds"];
                        scoreResult    = hScore - aScore > 0 ? 1 : hScore - aScore < 0 ? -1 : 0;
                        strengthDiff   = hStrength - aStrength;
                        strengthResult = hStrength - aStrength > 0 ? 1 : hStrength - aStrength < 0 ? -1 : 0;

                        if (!htmlDict.ContainsKey(lKey))
                        {
                            htmlDict[lKey] = new Betting[FORMGAP];
                            for (i = 0; i < FORMGAP; i++)
                            {
                                htmlDict[lKey][i] = new Betting(true);
                            }
                        }

                        for (i = 0; i < FORMGAP; i++)
                        {
                            bet       = htmlDict[lKey][i];
                            bgcolor   = "white";
                            diffLevel = hLevel - aLevel;
                            if (
                                bX && Math.Abs(strengthDiff) <= i
                                //&& Math.Abs(diffLevel) <= 1
                                //&& diffLevel == 0
                                //&& hOdds > 1.9 && aOdds > 1.9
                                //aStrength - hStrength >=0 &&
                                //aStrength - hStrength <=i
                                )
                            {
                                bgcolor = "silver";
                                bet.tipsPlayed++;
                                bet.pot += Betting.MATCHPOT;
                                if (scoreResult == 0)
                                {
                                    bet.tipsWon++;
                                    bet.won += (dOdds - 0) * Betting.MATCHPOT;
                                    bgcolor  = "chartreuse";
                                }
                            }
                            else if (b1 && strengthDiff > i)
                            {
                                bgcolor = "silver";
                                bet.tipsPlayed++;
                                bet.pot += Betting.MATCHPOT;
                                if (scoreResult == 1)
                                {
                                    bet.tipsWon++;
                                    bet.won += (hOdds - 0) * Betting.MATCHPOT;
                                    bgcolor  = "chartreuse";
                                }
                            }
                            else if (b2 && strengthDiff < i)
                            {
                                bgcolor = "silver";
                                bet.tipsPlayed++;
                                bet.pot += Betting.MATCHPOT;
                                if (scoreResult == -1)
                                {
                                    bet.tipsWon++;
                                    bet.won += (aOdds - 0) * Betting.MATCHPOT;
                                    bgcolor  = "chartreuse";
                                }
                            }

                            bet.rowsHtml.Append(String.Format(rowFormat, bgcolor, "black",
                                                              reader["League"], reader["Season"], reader["Round"], hTeam, aTeam,
                                                              hOdds, dOdds, aOdds, hStrength, aStrength, hLevel, aLevel,
                                                              hScore + ":" + aScore
                                                              ));
                            htmlDict[lKey][i] = bet;
                        }
                    }
                    //Console.WriteLine("{0:0.00}% {1}", bet.winningPot / bet.pot * 100, lKey.ToString());
                    //Console.WriteLine("Invested:{0}, Won:{1}, Profit:{2}", bet.pot, bet.won, bet.won - bet.pot);
                }

                backgroundWorker.ReportProgress(90, new BGReport("Displaying html ..."));
                backgroundWorker.ReportProgress(90, new BGReport(""));
                Invoke((MethodInvoker)(() => { chart1.Series.Clear(); }));
                foreach (var kvp in htmlDict)
                {
                    bet         = kvp.Value[3];
                    avgPercent += bet.successPercent;
                    sumProfit  += bet.profit;
                    html.Append(String.Format(tableFormat, kvp.Key, bet, bet.rowsHtml));
                    backgroundWorker.ReportProgress(90, new BGReport(kvp.Key.ToString()));
                    backgroundWorker.ReportProgress(90, new BGReport("  " + bet));
                    series             = new Series();
                    series.BorderWidth = 2;
                    series.ChartType   = SeriesChartType.Line;
                    series.Name        = kvp.Key.ToString();
                    series.ToolTip     = "X:#VALX, Y:#VAL\n#SERIESNAME";
                    for (i = 0; i < FORMGAP; i++)
                    {
                        dataPoint            = new DataPoint();
                        dataPoint.XValue     = i;
                        dataPoint.YValues[0] = kvp.Value[i].successPercent;
                        series.Points.Add(dataPoint);
                    }
                    Invoke((MethodInvoker)(() => { chart1.Series.Add(series); }));
                }
                avgPercent /= htmlDict.Count;
                backgroundWorker.ReportProgress(90, new BGReport(""));
                backgroundWorker.ReportProgress(90, new BGReport(new String('-', 84)));
                backgroundWorker.ReportProgress(90, new BGReport(String.Format("Average percent: {0:0.00}%, Sum: {1:0.00}", avgPercent, sumProfit)));
                browser.DocumentText = "<html><head></head><body style='font-family:Verdana;font-size:12px;'>" + html.ToString() + "</body></html>";
            }
            mainForm.closeExcel(backgroundWorker);
        }
        private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker.ReportProgress(0, new BGReport("Connecting to SoccerBase ..."));
            mainForm.openExcel(backgroundWorker);
            using (SqlConnection connection = new SqlConnection(MainForm.STRCONN))
            {
                bool b1, b2, bX;
                byte aLevel, aScore, hLevel, hScore, round;
                double aOdds, avgPercent = 0, dOdds, hOdds, sumProfit = 0;
                int aStrength, diffLevel, i = 0, hStrength, scoreResult, strengthDiff, strengthResult;
                string bgcolor, aTeam, hTeam, q, qWhere;
                string rowFormat = @"<tr style='background-color:{0};color:{1};'>
                    <td class='League'>{2}</td>
                    <td class='Season'>{3}</td>
                    <td class='Round'>{4}</td>
                    <td class='HomeTeam'>{5}</td>
                    <td class='AwayTeam'>{6}</td>
                    <td class='HomeOdds'>{7}</td>
                    <td class='DrawOdds'>{8}</td>
                    <td class='AwayOdds'>{9}</td>
                    <td class='HomeStrength'>{10}</td>
                    <td class='AwayStrength'>{11}</td>
                    <td class='HomeLevel'>{12}</td>
                    <td class='AwayLevel'>{13}</td>
                    <td class='Result'>{14}</td>
                </tr>";
                string tableFormat = @"<p>{0}</p><p1>{1}</p1>
                    <table border='1' style='font-family:Verdana;font-size:12px;'>
                        <thead>
                            <tr>
                                <th>League</th> <th>Season</th> <th>Round</th><th>HomeTeam</th> <th>AwayTeam</th><th>HomeOdds</th> <th>DrawOdds</th> <th>AwayOdds</th>
                                <th>HomeStrength</th> <th>AwayStrength</th><th>HomeLevel</th> <th>AwayLevel</th> <th>Result</th>
                            </tr>
                        </thead>
                        <tbody>{2}</tbody>
                    </table><br/>";
                Betting bet = new Betting(true);
                DataPoint dataPoint;
                Dictionary<LeaguesKey, Betting[]> htmlDict = new Dictionary<LeaguesKey, Betting[]>();
                LeaguesKey lKey = new LeaguesKey();
                List<string> alLeagues = new List<string>();
                Dictionary<LRTKey, Power> dict;
                Series series;
                StringBuilder html = new StringBuilder();

                b1 = bX = b2 = false;
                Invoke((MethodInvoker)(() =>
                {
                    foreach (LeaguesKey item in lbLeagues.SelectedItems)
                    {
                        alLeagues.Add("Country='" + item.Country + "' AND League='" + item.League + "' AND Season='" + item.Season + "'");
                    }
                    b1 = cb1.Checked;
                    bX = cbX.Checked;
                    b2 = cb2.Checked;
                }));
                qWhere = alLeagues.Count > 0 ? "(" + String.Join(" OR ", alLeagues) + ") AND " : "";

                backgroundWorker.ReportProgress(30, new BGReport("Processing dbo.history ..."));
                connection.Open();

                dict = MethodAForm.getHistoryDict(connection);

                backgroundWorker.ReportProgress(60, new BGReport("Processing dbo.archive ..."));
                q = @"SELECT * FROM dbo.archive WHERE " + qWhere + "Round>2 AND ScoreH IS NOT NULL ORDER BY Country,League,Season,Round";
                using (SqlDataReader reader = new SqlCommand(q, connection).ExecuteReader())
                {
                    while (reader.Read())
                    {
                        round = (byte)reader["Round"];
                        hTeam = reader["HomeTeam"].ToString();
                        aTeam = reader["AwayTeam"].ToString();
                        hScore = (byte)reader["ScoreH"];
                        aScore = (byte)reader["ScoreA"];
                        lKey.Country = reader["Country"].ToString();
                        lKey.League = reader["League"].ToString();
                        lKey.Season = reader["Season"].ToString();
                        hLevel = dict[new LRTKey(reader, hTeam, aTeam, (byte)(round - 1))].Level;
                        aLevel = dict[new LRTKey(reader, aTeam, hTeam, (byte)(round - 1))].Level;
                        hStrength = dict[new LRTKey(reader, hTeam, aTeam, round)].PrevAvgStrength;
                        aStrength = dict[new LRTKey(reader, aTeam, hTeam, round)].PrevAvgStrength;
                        if (MethodAForm.debug) Console.WriteLine("[{0}, {1}] {2} - {3} L({4}:{5}) R({6}:{7}) S({8}:{9})\n",
                            lKey, round, hTeam, aTeam, hLevel, aLevel, hScore, aScore, hStrength, aStrength
                        );
                        hOdds = (double)reader["HomeOdds"];
                        dOdds = (double)reader["DrawOdds"];
                        aOdds = (double)reader["AwayOdds"];
                        scoreResult = hScore - aScore > 0 ? 1 : hScore - aScore < 0 ? -1 : 0;
                        strengthDiff = hStrength - aStrength;
                        strengthResult = hStrength - aStrength > 0 ? 1 : hStrength - aStrength < 0 ? -1 : 0;

                        if (!htmlDict.ContainsKey(lKey))
                        {
                            htmlDict[lKey] = new Betting[FORMGAP];
                            for (i = 0; i < FORMGAP; i++) htmlDict[lKey][i] = new Betting(true);
                        }

                        for (i = 0; i < FORMGAP; i++)
                        {
                            bet = htmlDict[lKey][i];
                            bgcolor = "white";
                            diffLevel = hLevel - aLevel;
                            if (
                              bX && Math.Abs(strengthDiff) <= i
                                //&& Math.Abs(diffLevel) <= 1
                                //&& diffLevel == 0
                                //&& hOdds > 1.9 && aOdds > 1.9
                                //aStrength - hStrength >=0 &&
                                //aStrength - hStrength <=i
                            )
                            {
                                bgcolor = "silver";
                                bet.tipsPlayed++;
                                bet.pot += Betting.MATCHPOT;
                                if (scoreResult == 0)
                                {
                                    bet.tipsWon++;
                                    bet.won += (dOdds - 0) * Betting.MATCHPOT;
                                    bgcolor = "chartreuse";
                                }
                            }
                            else if (b1 && strengthDiff > i)
                            {
                                bgcolor = "silver";
                                bet.tipsPlayed++;
                                bet.pot += Betting.MATCHPOT;
                                if (scoreResult == 1)
                                {
                                    bet.tipsWon++;
                                    bet.won += (hOdds - 0) * Betting.MATCHPOT;
                                    bgcolor = "chartreuse";
                                }
                            }
                            else if (b2 && strengthDiff < i)
                            {
                                bgcolor = "silver";
                                bet.tipsPlayed++;
                                bet.pot += Betting.MATCHPOT;
                                if (scoreResult == -1)
                                {
                                    bet.tipsWon++;
                                    bet.won += (aOdds - 0) * Betting.MATCHPOT;
                                    bgcolor = "chartreuse";
                                }
                            }

                            bet.rowsHtml.Append(String.Format(rowFormat, bgcolor, "black",
                                 reader["League"], reader["Season"], reader["Round"], hTeam, aTeam,
                                 hOdds, dOdds, aOdds, hStrength, aStrength, hLevel, aLevel,
                                 hScore + ":" + aScore
                            ));
                            htmlDict[lKey][i] = bet;
                        }
                    }
                    //Console.WriteLine("{0:0.00}% {1}", bet.winningPot / bet.pot * 100, lKey.ToString());
                    //Console.WriteLine("Invested:{0}, Won:{1}, Profit:{2}", bet.pot, bet.won, bet.won - bet.pot);
                }

                backgroundWorker.ReportProgress(90, new BGReport("Displaying html ..."));
                backgroundWorker.ReportProgress(90, new BGReport(""));
                Invoke((MethodInvoker)(() => { chart1.Series.Clear(); }));
                foreach (var kvp in htmlDict)
                {
                    bet = kvp.Value[3];
                    avgPercent += bet.successPercent;
                    sumProfit += bet.profit;
                    html.Append(String.Format(tableFormat, kvp.Key, bet, bet.rowsHtml));
                    backgroundWorker.ReportProgress(90, new BGReport(kvp.Key.ToString()));
                    backgroundWorker.ReportProgress(90, new BGReport("  " + bet));
                    series = new Series();
                    series.BorderWidth = 2;
                    series.ChartType = SeriesChartType.Line;
                    series.Name = kvp.Key.ToString();
                    series.ToolTip = "X:#VALX, Y:#VAL\n#SERIESNAME";
                    for (i = 0; i < FORMGAP; i++)
                    {
                        dataPoint = new DataPoint();
                        dataPoint.XValue = i;
                        dataPoint.YValues[0] = kvp.Value[i].successPercent;
                        series.Points.Add(dataPoint);
                    }
                    Invoke((MethodInvoker)(() => { chart1.Series.Add(series); }));
                }
                avgPercent /= htmlDict.Count;
                backgroundWorker.ReportProgress(90, new BGReport(""));
                backgroundWorker.ReportProgress(90, new BGReport(new String('-', 84)));
                backgroundWorker.ReportProgress(90, new BGReport(String.Format("Average percent: {0:0.00}%, Sum: {1:0.00}", avgPercent, sumProfit)));
                browser.DocumentText = "<html><head></head><body style='font-family:Verdana;font-size:12px;'>" + html.ToString() + "</body></html>";
            }
            mainForm.closeExcel(backgroundWorker);
        }