Beispiel #1
0
        public async System.Threading.Tasks.Task <bool> login()
        {
            try
            {
                string user = MainApp.g_setting.dyn_odd_credential.user;
                string pwd  = MainApp.g_setting.dyn_odd_credential.pwd;
                string res  = await WRequest.get_response($"{ep}Login.asp?UserName={user}&Password={pwd}");

                XDocument doc  = XDocument.Parse(res);
                var       _sid = doc.Element("Login").Element("SessionID");
                if (_sid == null)
                {
                    return(false);
                }
                session_id        = _sid.Value;
                MainApp.g_working = true;
                return(true);
            }
            catch (Exception ex)
            {
                MainApp.g_working = false;
                MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                return(false);
            }
        }
Beispiel #2
0
        // get tracking notificatoins from the api, this notification will be further supervised by tracking app
        // return: List of notification on success, null on failure
        public async Task <List <Notification> > get_tracking_notifications()
        {
            try
            {
                string endp   = $"{api_endp}/trackingnotifications";
                var    header = new Dictionary <string, string>()
                {
                    { "Authorization", $"Token {token}" }
                };

                List <Notification> ret = new List <Notification>();

                string resp = await WRequest.get_response(endp, header, "");

                var jarray = JArray.Parse(resp);
                foreach (var jobj in jarray)
                {
                    var note = new JavaScriptSerializer().Deserialize <Notification>(jobj.ToString());
                    ret.Add(note);
                }

                return(ret);
            }
            catch (Exception ex)
            {
                MainApp.log_error("Loading notifications from API failed. " + ex.Message);
            }
            return(null);
        }
Beispiel #3
0
        public async Task <int> get_event_result(string event_id)
        {
            XDocument doc;
            string    res;

            try
            {
                string endpoint = $"{ep}GetData.asp?SessionID={session_id}&Method=GetEventResults&EventID={event_id}";
                res = await WRequest.get_response(endpoint);

                doc = XDocument.Parse(res);
                if (check_session(doc) == false)
                {
                    if (res.Contains("Request Limit Exceeded"))
                    {
                        await Task.Delay(1000);

                        return(await get_event_result(event_id));
                    }
                    MainApp.log_error("Error getting event results");
                    MainApp.log_error(res);
                    MainApp.g_working = false;
                    return(-1);
                }

                IEnumerable <XElement> elems;
                try
                {
                    elems = doc.Element("Data").Element("EventResults").Elements("Placings").Elements("Result").Elements("No");
                }
                catch (Exception ex)
                {
                    MainApp.log_error("Error parsing event results");
                    MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                    MainApp.g_working = false;
                    return(-1);
                }
                var elem = elems.FirstOrDefault();
                if (elem == null)
                {
                    return(-1);
                }
                MainApp.g_working = true;
                return(int.Parse(elem.Value.ToString()));
            }
            catch (Exception ex)
            {
                MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                MainApp.g_working = false;
                return(-1);
            }
        }
Beispiel #4
0
        public async Task <bool> update_notification(int id, Notification note)
        {
            try
            {
                string endp   = $"{api_endp}/notifications/{id}";
                var    header = new Dictionary <string, string>()
                {
                    { "Authorization", $"Token {token}" }
                };
                var    json = new JavaScriptSerializer().Serialize(note);
                string resp = await WRequest.put_response(endp, json, header);

                return(true);
            }
            catch (Exception ex)
            {
                MainApp.log_error("Updating notification failed. " + ex.Message);
            }
            return(false);
        }
Beispiel #5
0
        // login using the credential given by the setting file
        // return: true on success false on failure
        public async Task <bool> login()
        {
            try
            {
                string endp = $"{api_endp}/auth";
                string data = "{" +
                              $"\"username\":\"{MainApp.g_setting.api_credential.user}\"," +
                              $"\"password\":\"{MainApp.g_setting.api_credential.pwd}\"" +
                              "}";
                string resp = await WRequest.post_response(endp, data);

                var json = JObject.Parse(resp);
                token = json["token"].ToString();
                return(true);
            }
            catch (Exception ex)
            {
                MainApp.log_error("Login failed." + ex.Message);
            }
            return(false);
        }
Beispiel #6
0
        public async System.Threading.Tasks.Task <Dictionary <string, string> > get_agencies()
        {
            try
            {
                Dictionary <string, string> agency = new Dictionary <string, string>();
                string endpoint = $"{ep}GetData.asp?SessionID={session_id}&Method=GetBettingAgencies";
                string res      = await WRequest.get_response(endpoint);

                XDocument doc = XDocument.Parse(res);
                if (check_session(doc) == false)
                {
                    if (res.Contains("Request Limit Exceeded"))
                    {
                        await Task.Delay(1000);

                        return(await get_agencies());
                    }
                    MainApp.log_error("Error getting agencies");
                    MainApp.log_error(res);
                    MainApp.g_working = false;
                    return(null);
                }

                foreach (var node in doc.Descendants("BA"))
                {
                    agency.Add(node.Attribute("ID").Value, node.Element("Name").Value);
                }
                MainApp.g_working = true;
                return(agency);
            }
            catch (Exception ex)
            {
                MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                MainApp.g_working = false;
                return(null);
            }
        }
Beispiel #7
0
        public async System.Threading.Tasks.Task <RaceEvent> get_event(RaceEvent ref_eve, string eve_id)
        {
            try
            {
                RaceEvent eve      = new RaceEvent();
                string    endpoint = $"{ep}GetData.asp?SessionID={session_id}&Method=GetEvent&EventID={eve_id}&Runners=true";
                string    res      = await WRequest.get_response(endpoint);

                XDocument doc = XDocument.Parse(res);
                if (check_session(doc) == false)
                {
                    if (res.Contains("Request Limit Exceeded"))
                    {
                        await Task.Delay(1000);

                        return(await get_event(ref_eve, eve_id));
                    }
                    MainApp.g_working = false;
                    return(null);
                }

                MainApp.g_working = true;
                XElement elem = null;
                try
                {
                    elem = doc.Element("Data").Element("GetEvent").Element("Event");
                }
                catch (Exception e)
                {
                    return(null);
                }
                if (elem == null)
                {
                    return(null);
                }
                eve.ID           = elem.Attribute("ID").Value;
                eve.EventNo      = elem.Element("EventNo").Value;
                eve.Name         = elem.Element("Name").Value;
                eve.Distance     = elem.Element("Distance").Value;
                eve.Track        = elem.Element("Track").Value;
                eve.TrackRtg     = elem.Element("TrackRtg").Value;
                eve.Starters     = elem.Element("Starters").Value;
                eve.StartTime_Au = elem.Element("StartTime").Value;
                eve.Class        = elem.Element("Class").Value;
                eve.Prizemoney   = elem.Element("Prizemoney").Value;
                eve.Status       = elem.Element("Status").Value;
                if (eve.Status.ToUpper() == "FINAL")
                {
                    eve.Status = "PAYING";
                }
                eve.WeatherTC = elem.Element("WeatherTC").Value;
                WeatherConditions con = new WeatherConditions();
                con.TempC      = elem.Element("WeatherCondtions").Element("TempC").Value;
                con.Humidity   = elem.Element("WeatherCondtions").Element("Humidity").Value;
                con.WindSpeed  = elem.Element("WeatherCondtions").Element("WindSpeed").Value;
                con.WindDir    = elem.Element("WeatherCondtions").Element("WindDir").Value;
                eve.WeatherCon = con;
                eve.Rail       = elem.Element("Rail").Value;
                eve.CodeAAP    = elem.Element("CodeAAP").Value;
                eve.CodeBF     = elem.Element("CodeBF").Value;
                eve.Runners    = new List <Runner>();
                foreach (var node in elem.Elements("Runner"))
                {
                    Runner runner = new Runner();
                    runner.Name      = node.Element("Name").Value;
                    runner.Jockey    = node.Element("Jockey").Value;
                    runner.Trainer   = node.Element("Trainer").Value;
                    runner.Bar       = node.Element("Bar").Value;
                    runner.Hcp       = node.Element("Hcp").Value;
                    runner.Scr       = node.Element("Scr").Value;
                    runner.Emergency = node.Element("Emergency").Value;
                    eve.Runners.Add(runner);
                }

                eve.Venue         = ref_eve.Venue;
                eve.EventCode     = ref_eve.EventCode;
                eve.Type          = ref_eve.Type;
                eve.State         = ref_eve.State;
                eve.StartTime     = ref_eve.StartTime;
                eve.Date          = ref_eve.Date;
                MainApp.g_working = true;
                return(eve);
            }
            catch (Exception ex)
            {
                MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                MainApp.g_working = false;
                return(null);
            }
        }
Beispiel #8
0
        public async System.Threading.Tasks.Task <List <RaceEvent> > get_event_schedule(DateTime date)
        {
            try
            {
                DateTime         au_now   = date.Subtract(MainApp.g_time_diff);
                List <RaceEvent> schedule = new List <RaceEvent>();
                string           date_str = date.ToString("yyyy-MM-dd");
                string           endpoint = $"{ep}GetData.asp?SessionID={session_id}&Method=GetEventSchedule&Date={date_str}&Types=R,H,G&Limit=999";
                string           res      = await WRequest.get_response(endpoint);

                XDocument doc = XDocument.Parse(res);
                if (check_session(doc) == false)
                {
                    if (res.Contains("Request Limit Exceeded"))
                    {
                        await Task.Delay(1000);

                        return(await get_event_schedule(date));
                    }
                    MainApp.log_error("Error getting event schedule");
                    MainApp.log_error(res);
                    MainApp.g_working = false;
                    return(null);
                }

                var all_events = doc.Descendants("Event");
                foreach (var node in all_events)
                {
                    RaceEvent eve = new RaceEvent();
                    eve.ID           = node.Attribute("ID").Value;
                    eve.Date         = date;
                    eve.EventCode    = node.Attribute("EventCode").Value;
                    eve.StartTime_Au = node.Attribute("StartTime").Value;
                    eve.Type         = node.Attribute("Type").Value;
                    if (eve.Type == "T")
                    {
                        eve.Type = "H";
                    }
                    eve.Venue   = node.Attribute("Venue").Value;
                    eve.EventNo = node.Attribute("EventNo").Value;
                    eve.Status  = node.Attribute("EventStatus").Value;
                    if (eve.Status.ToUpper() == "FINAL")
                    {
                        eve.Status = "PAYING";
                    }
                    eve.State = node.Attribute("State").Value;

                    if (au_states.Contains(eve.State) == false ||
                        eve.Status.ToUpper() != "OPEN" ||
                        eve.Type != "R")
                    {
                        continue;
                    }

                    if (!MainApp.DEBUG && eve.StartTime_Au.CompareTo(au_now.ToString("HH:mm:ss")) < 0)
                    {
                        continue;
                    }

                    string[] fields = eve.StartTime_Au.Split(':');
                    eve.StartTime = new DateTime(au_now.Year, au_now.Month, au_now.Day, int.Parse(fields[0]), int.Parse(fields[1]), int.Parse(fields[2]));

                    //MainApp.log_info(eve.ToString());
                    schedule.Add(eve);
                }
                MainApp.g_working = true;
                return(schedule);
            }
            catch (Exception ex)
            {
                MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                MainApp.g_working = false;
                return(null);
            }
        }
Beispiel #9
0
        public async Task <OddOverview> get_runner_odds(string event_id)
        {
            XDocument doc;
            string    res;

            try
            {
                OddOverview overview = new OddOverview();
                overview.EveID = event_id;

                string endpoint = $"{ep}GetData.asp?SessionID={session_id}&Method=GetRunnerOdds&EventID={event_id}";
                res = await WRequest.get_response(endpoint);

                doc = XDocument.Parse(res);
                if (check_session(doc) == false)
                {
                    if (res.Contains("Request Limit Exceeded"))
                    {
                        await Task.Delay(1000);

                        return(await get_runner_odds(event_id));
                    }
                    MainApp.log_error("Error getting running odds");
                    MainApp.log_error(res);
                    MainApp.g_working = false;
                    return(null);
                }

                IEnumerable <XElement> elems;
                try
                {
                    elems = doc.Element("Data").Element("RunnerOdds").Elements();
                }
                catch (Exception ee)
                {
                    MainApp.log_error("DynHelperError: " + ee.Message + "\n" + ee.StackTrace);
                    MainApp.g_working = false;
                    return(null);
                }
                overview.Odds = new List <OddOneBookie>();
                foreach (var elem in elems)
                {
                    string name = elem.Name.ToString();
                    string val  = elem.Value.ToString();
                    if (name == "RNo")
                    {
                        overview.RNo = new List <string>(val.Split(','));
                    }
                    else if (name == "RName")
                    {
                        overview.RName = new List <string>(val.Split(','));
                    }
                    else
                    {
                        string bookie;
                        if (OddBookieMap.map.TryGetValue(name, out bookie))
                        {
                            OddOneBookie odd_one = new OddOneBookie();
                            odd_one.OddName = name;
                            odd_one.Bookie  = bookie;
                            odd_one.Odds    = new List <decimal>(val.Split(',').Select(x => x.ToString() == "" ? 0 : decimal.Parse(x)));
                            overview.Odds.Add(odd_one);
                        }
                    }
                }
                MainApp.g_working = true;
                return(overview);
            }
            catch (Exception ex)
            {
                MainApp.log_error("DynHelperError: " + ex.Message + "\n" + ex.StackTrace);
                MainApp.g_working = false;
                return(null);
            }
        }