private void AnswerLeagueCompareRequest(CompareLeague l, bool answer) { using (VilkaEntities context = new VilkaEntities()) { if (answer == true) { League league = context.Leagues.Where((i) => i.Name == l.First || i.Name == l.Second).FirstOrDefault(); if (league == null) { League newLeague = new League(); newLeague.Name = l.First; newLeague.SportID = l.SportID; newLeague.RegionID = l.RegionID; league = context.Leagues.Add(newLeague); context.SaveChanges(); } Tuple <string, Region, Sport> keyFirst = new Tuple <string, Region, Sport>(l.First, l.Region, l.Sport); Tuple <string, Region, Sport> keySecond = new Tuple <string, Region, Sport>(l.Second, l.Region, l.Sport); if (!Dicts.LeagueDict.ContainsKey(keyFirst)) { Dicts.LeagueDict.Add(keyFirst, league); } if (!Dicts.LeagueDict.ContainsKey(keySecond)) { Dicts.LeagueDict.Add(keySecond, league); } } context.CompareLeagues.Remove(context.CompareLeagues.Where((i) => i.ID == l.ID).FirstOrDefault()); context.SaveChanges(); } }
public void FillCompareTable() { Data = null; socket = new WebSocket(wsURL); socket.Connect(); socket.OnMessage += Socket_OnMessage; socket.Send(sessionReq); while (!dataArrived) { Task.Delay(1000).ConfigureAwait(false); } foreach (var sport in Data["sport"].First) { if (!mappings.SportMappings.ContainsKey(sport["alias"].ToObject <string>())) { continue; } foreach (var region in sport["region"]) { JObject regionObj = (JObject)region.First; string regionName = regionObj["alias"].ToObject <string>(); foreach (var competition in regionObj["competition"]) { JObject league = (JObject)competition.First; string leagueName = league["name"].ToObject <string>(); foreach (var game in league["game"]) { try { Compare_Events ev = new Compare_Events(); JObject gameObj = (JObject)game.First; ev.Home = gameObj["team1_name"].ToObject <string>(); ev.Away = gameObj["team2_name"].ToObject <string>(); ev.Region = regionName; ev.League = leagueName; ev.Start = DateHelpers.TimeStampToDateTime(gameObj["start_ts"].ToObject <double>()); var a = gameObj["start_ts"]; ev.PrematchEnd = ev.Start; ev.SportID = mappings.SportMappings[sport["alias"].ToObject <string>()].ID; ev.SiteID = mappings.ID; context.Compare_Events.Add(ev); } catch { continue; } } } } } int count = context.SaveChanges(); }
public void Run() { using (VilkaEntities context = new VilkaEntities()) { IQueryable <EventSiteData> liveEventSiteDatas = context.EventSiteDatas.Where((d) => d.Event.PrematchEnd <= DateTime.Now); Console.WriteLine(liveEventSiteDatas.Count() + "Event Datas found."); context.EventSiteDatas.RemoveRange(liveEventSiteDatas); IQueryable <Outcome> liveOutcomes = context.Outcomes.Where((d) => d.BetOffer.Event.PrematchEnd <= DateTime.Now); Console.WriteLine(liveOutcomes.Count() + "Outcomes found."); context.Outcomes.RemoveRange(liveOutcomes); IQueryable <BetOffer> liveBetOffers = context.BetOffers.Where((d) => d.Event.PrematchEnd <= DateTime.Now); Console.WriteLine(liveBetOffers.Count() + "Bet Offers found."); context.BetOffers.RemoveRange(liveBetOffers); IQueryable <Event> liveEvents = context.Events.Where((d) => d.PrematchEnd <= DateTime.Now); Console.WriteLine(liveEvents.Count() + "Events found."); context.Events.RemoveRange(liveEvents); Console.WriteLine("Removing live Events..."); Console.WriteLine(context.SaveChanges() + " rows affected."); Console.WriteLine(); } }
private static void BuildLeagueDictionary() { ObjectResult <CEventsGetLeagues_Result> leagues = context.CEventsGetLeagues(); foreach (CEventsGetLeagues_Result element in leagues.ToList()) { if (!Dicts.RegionDict.ContainsKey(element.Region)) { EnsureRegionExists(element.Region); } Sport sport = context.Sports.Where((s) => s.ID == element.SportID).FirstOrDefault(); if (sport == null || Dicts.LeagueDict.ContainsKey(new Tuple <string, Region, Sport>(element.League, Dicts.RegionDict[element.Region], sport))) { continue; } bool found = false; foreach (League item in context.Leagues.ToList()) { //item.Region = context.Regions.Where(r => r.ID == item.RegionID).FirstOrDefault(); if (item.RegionID == Dicts.RegionDict[element.Region].ID && item.SportID == sport.ID) { LeagueComparisonQueue.Enqueue(new CompareLeague() { RegionID = Dicts.RegionDict[element.Region].ID, First = element.League, Second = item.Name, SportID = sport.ID }); found = true; break; } } if (!found) { League newLeague = new League(); newLeague.Name = element.League; newLeague.SportID = sport.ID; newLeague.RegionID = Dicts.RegionDict[element.Region].ID; context.Leagues.Add(newLeague); context.SaveChanges(); Dicts.LeagueDict.Add(new Tuple <string, Region, Sport>(element.League, Dicts.RegionDict[element.Region], sport), newLeague); } } }
public void EnsureID() { using (VilkaEntities context = new VilkaEntities()) { Site better = context.Sites.Where((site) => site.Name == Name).FirstOrDefault(); if (better == null) { better = new Site(); better.Name = Name; context.Sites.Add(better); context.SaveChanges(); } ID = better.ID; } }
public void FillDB() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_allEventsApiUrl); request.Method = "GET"; string json; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); json = reader.ReadToEnd(); reader.Close(); dataStream.Close(); } int added = 0; JObject obj; try { obj = JObject.Parse(json); foreach (var item in obj["events"]) { JObject itemObj = ((JObject)item); JObject eventObj = (JObject)itemObj["event"]; Event ev = new Event(); string eventData = eventObj["id"].ToObject <string>(); ev.Home = eventObj["homeName"].ToObject <string>(); ev.Away = eventObj["awayName"].ToObject <string>(); ev.Start = DateHelpers.TimeStampToDateTime(eventObj["start"].ToObject <double>()); ev.PrematchEnd = DateHelpers.TimeStampToDateTime(eventObj["prematchEnd"].ToObject <double>()); if (ev.IsLive()) { continue; } JArray paths = ((JArray)eventObj["path"]); if (!mappings.SportMappings.ContainsKey(paths[0]["name"].ToObject <string>())) { continue; } Sport sport = mappings.SportMappings[paths[0]["name"].ToObject <string>()].DBCopy(context); if (sport != null) { ev.Sport = sport; } else { break; } if (paths.Count == 2) { ev.League = paths[1]["name"].ToObject <string>(); } else if (paths.Count == 3) { ev.Region = paths[1]["name"].ToObject <string>(); ev.League = paths[2]["name"].ToObject <string>(); } Event dbEvent = DBFinder.FindEvent(ev).DBCopy(context); int eventID; if (dbEvent != null) { continue; } else { context.Events.Add(ev); context.SaveChanges(); EventSiteData data = new EventSiteData(); dbEvent = ev; data.EventID = ev.ID; data.SiteID = mappings.ID; data.Data = eventData; context.EventSiteDatas.Add(data); Int32.TryParse(eventData, out eventID); } if (eventID == 0) { continue; } string offersUrl = GetEventOffersApiUrl(eventID); request = (HttpWebRequest)WebRequest.Create(offersUrl); try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); json = reader.ReadToEnd(); reader.Close(); dataStream.Close(); } } catch (WebException ex) { continue; } obj = JObject.Parse(json); foreach (JObject offer in obj["betoffers"]) { string betOfferType = ((JObject)offer["betOfferType"])["name"].ToObject <string>(); string betTarget = ((JObject)offer["criterion"])["label"].ToObject <string>(); if (!mappings.BetTypeMappings.ContainsKey(betOfferType)) { continue; } if (!mappings.BetTargetMappings.ContainsKey(betTarget)) { continue; } BetType type = mappings.BetTypeMappings[betOfferType].DBCopy(context); BetTarget target = mappings.BetTargetMappings[betTarget].DBCopy(context); BetOffer betOffer = new BetOffer(); betOffer.BetTypeID = type.ID; if (target != null) { betOffer.BetTargetID = target.ID; } dbEvent.BetOffers.Add(betOffer); context.SaveChanges(); foreach (JObject outcome in offer["outcomes"]) { Outcome o = new Outcome(); if (!mappings.OutcomeTypeMappings.ContainsKey(outcome["label"].ToObject <string>())) { continue; } o.OutcomeType = mappings.OutcomeTypeMappings[outcome["label"].ToObject <string>()].DBCopy(context); o.Odds = outcome["oddsAmerican"].ToObject <string>(); o.BetOfferID = betOffer.ID; if (outcome.ContainsKey("line")) { o.Line = outcome["line"].ToObject <int>(); } context.Outcomes.Add(o); } } Console.WriteLine("Event added: " + dbEvent.Home + " - " + dbEvent.Away); added++; } context.SaveChanges(); Console.WriteLine("Total: " + added); } catch (Exception ex) { Console.WriteLine("888 invalid json, or changed json formatting"); } }