using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; using System.Diagnostics; using System.Windows.Threading; using System.Net; using System.ComponentModel; using Finisar.SQLite; namespace Live_Scoring_V1 { class Webservices { public int USERID; SqlDatabase SQL = new SqlDatabase(); public ProgressBar DownloadingProgress; public TextBlock DownloadingProgressText; public Grid ProgressPopup; public Frame MainFrame; public int Reload; public void GetNewData() { try { DownloadingProgress.Maximum = 100; DownloadingProgress.Value = 0; DownloadingProgress.Visibility = Visibility.Visible; WebClient wb = new WebClient(); wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wb_DownloadProgressChanged); wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted); //wb.DownloadStringAsync(new Uri("https://footprintapp.net/api/fixtures/getdetails/UserID/" + USERID)); wb.DownloadStringAsync(new Uri("https://qa.footprintapp.net/api/fixtures/getdetails/UserID/" + USERID)); //QA } catch { } } void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { string htmldoc = e.Result; string Web_Data = Convert.ToString(htmldoc); Web_Data = Web_Data.Replace("'", "''"); var JsonObjects = Newtonsoft.Json.JsonConvert.DeserializeObject(Web_Data); int i = 0; foreach (var Fixture in JsonObjects) { //var Json = Newtonsoft.Json.JsonConvert.DeserializeObject(line); //string str = Int32.Parse(line.FID); SetFixture(Fixture); int FID = Int32.Parse(Convert.ToString(Fixture.FID)); setTeam(Fixture.TeamH, Fixture.HOME_LINEUP, Convert.ToInt32(Fixture.HomeTeam), FID); setTeam(Fixture.TeamA, Fixture.AWAY_LINEUP, Convert.ToInt32(Fixture.AwayTeam), FID); if (i == 0) { Events(Fixture.EVENTS); Events(Fixture.SESSIONS); } i++; } ProgressPopup.Visibility = Visibility.Hidden; if (Reload == 1) { Listfixturs frame = new Listfixturs(); frame.MainFrame = MainFrame; MainFrame.Content = frame; } } void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { DownloadingProgress.Minimum = 0; double receive = double.Parse(e.BytesReceived.ToString()); double total = double.Parse(e.TotalBytesToReceive.ToString()); double percentage = receive / total * 100; DownloadingProgressText.Text = "Downloaded " + string.Format("{0:0.##}", percentage); DownloadingProgress.Value = e.ProgressPercentage; //DownloadingProgressText.Text = "Downloaded " + string.Format("{0:0.##}", e.ProgressPercentage); } public static bool CheckForInternetConnection() { try { using (var client = new WebClient()) { using (client.OpenRead("http://clients3.google.com/generate_204")) { return true; } } } catch { return false; } } public static bool PostComment(int _FID, string Comment) { try { //string URI = "https://footprintapp.net/api/fixtures/uploadcommentary/fid/" + _FID; string URI = "https://qa.footprintapp.net/api/fixtures/uploadcommentary/fid/" + _FID; //QA string myParameters = "comment=" + Comment; using (WebClient wc = new WebClient()) { wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; string HtmlResult = wc.UploadString(URI, myParameters); } return true; } catch { return false; } } private void SetFixture(dynamic Fixture) { SQLiteConnection sqlite_conn; string dbPath = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\Sideline Manager\\"; sqlite_conn = new SQLiteConnection("Data Source="+ dbPath +"Footprint.db;Version=3;"); sqlite_conn.Open(); string sql; try { sql = "UPDATE Fixture SET Current = 0 WHERE Current = 1; "; var command = new SQLiteCommand(sql, sqlite_conn); command.ExecuteNonQuery(); string StageName = Fixture.StageName; StageName = StageName.Replace("'", ""); dynamic MatchOfficials = Fixture.MATCHOFFICIALS; string GameReferee = ""; string AssistantReferee = ""; string TelevisionReferee = ""; foreach (var official in MatchOfficials) { foreach (var MatchOfficial in official) { if (MatchOfficial.PosDescription == "Referee") { GameReferee = MatchOfficial.FullName; GameReferee = GameReferee.Replace("'", "''"); } else if (MatchOfficial.PosDescription == "Assistant Referee 1") { AssistantReferee = MatchOfficial.FullName; AssistantReferee = AssistantReferee.Replace("'", "''"); } else if (MatchOfficial.PosDescription == "TMO") { TelevisionReferee = MatchOfficial.FullName; TelevisionReferee = TelevisionReferee.Replace("'", "''"); } } } /* sql = "insert into Fixture (FID, FixtureDate,StartTime,Location,HomeTeam,AwayTeam," + "FixtureNumber,StageName,RoundNumber,HomeName,AwayName,HomeAbbr,AwayAbbr," + "Allow22MeterTry,Tries,Conversion,Penalties,Dropgoal,PenaltyTry,Portry,Code,TID,TournName,Current,HalfTime)" + " values ('" + Fixture.FID + "', '" + Fixture.FixtureDate + "','" + Fixture.StartTime + "','" + Fixture.Location + "'," + "'" + Fixture.HomeTeam + "','" + Fixture.AwayTeam + "','" + Fixture.FixtureNumber + "','" + StageName + "','" + Fixture.RoundNumber + "'," + "'" + Fixture.HomeName + "','" + Fixture.AwayName + "','" + Fixture.HomeAbbr + "','" + Fixture.AwayAbbr + "'," + "'" + Fixture.Allow22MeterTry + "','" + Fixture.Tries_Points + "','" + Fixture.Conversion_Points + "','" + Fixture.Penalties_Points + "'," + "'" + Fixture.Dropgoal_Points + "','" + Fixture.PenaltyTry_Points + "','" + Fixture.Portry_Points + "','" + Fixture.Code + "'," + "'" + Fixture.TID + "','" + Fixture.TournName + "','0','"+Fixture.YELLOWCARDCOUNTER + "','" + Fixture.HtimeMin + "')"; */ sql = "insert into Fixture (FID, FixtureDate,StartTime,Location,HomeTeam,AwayTeam," + "FixtureNumber,StageName,GameReferee,AssistantReferee,TelevisionReferee,RoundNumber,HomeName,AwayName,HomeAbbr,AwayAbbr," + "Allow22MeterTry,Tries,Conversion,Penalties,Dropgoal,PenaltyTry,Portry,Code,TID,YellowCardCounter,YellowCardTimer,BloodTimer,HIATimer,HalfTime,TournName)" + " values ('" + Fixture.FID + "', '" + Fixture.FixtureDate + "','" + Fixture.StartTime + "','" + Fixture.Location + "'," + "'" + Fixture.HomeTeam + "','" + Fixture.AwayTeam + "','" + Fixture.FixtureNumber + "','" + StageName + "','" + GameReferee + "','" + AssistantReferee + "','" + TelevisionReferee + "','" + Fixture.RoundNumber + "'," + "'" + Fixture.HomeName + "','" + Fixture.AwayName + "','" + Fixture.HomeAbbr + "','" + Fixture.AwayAbbr + "'," + "'" + Fixture.Allow22MeterTry + "','" + Fixture.Tries_Points + "','" + Fixture.Conversion_Points + "','" + Fixture.Penalties_Points + "'," + "'" + Fixture.Dropgoal_Points + "','" + Fixture.PenaltyTry_Points + "','" + Fixture.Portry_Points + "','" + Fixture.Code + "'," + "'" + Fixture.TID + "','" + Fixture.YELLOWCARDCOUNTER + "','" + Fixture.YELLOWCARDTIMER + "','" + Fixture.BLOODTIMER + "','" + Fixture.HIATIMER + "','" + Fixture.HtimeMin + "','" + Fixture.TournName + "')"; Console.WriteLine(sql); command = new SQLiteCommand(sql, sqlite_conn); command.ExecuteNonQuery(); } catch (Exception error) { sql = "UPDATE Fixture SET Current = 0 WHERE Current = 1; "; var command = new SQLiteCommand(sql, sqlite_conn); command.ExecuteNonQuery(); string StageName = Fixture.StageName; StageName = StageName.Replace("'", ""); dynamic MatchOfficials = Fixture.MATCHOFFICIALS; string GameReferee = ""; string AssistantReferee = ""; string TelevisionReferee = ""; foreach (var official in MatchOfficials) { foreach (var MatchOfficial in official) { if (MatchOfficial.PosDescription == "Referee") { GameReferee = MatchOfficial.FullName; GameReferee = GameReferee.Replace("'", "''"); } else if (MatchOfficial.PosDescription == "Assistant Referee 1") { AssistantReferee = MatchOfficial.FullName; AssistantReferee = AssistantReferee.Replace("'", "''"); } else if (MatchOfficial.PosDescription == "TMO") { TelevisionReferee = MatchOfficial.FullName; TelevisionReferee = TelevisionReferee.Replace("'", "''"); } } } sql = "UPDATE Fixture SET Current = 1," + " FID = '" + Fixture.FID + "'," + " FixtureDate = '" + Fixture.FixtureDate + "'," + " StartTime = '" + Fixture.StartTime + "'," + " Location = '" + Fixture.Location + "'," + " HomeTeam = '" + Fixture.HomeTeam + "'," + " AwayTeam = '" + Fixture.AwayTeam + "'," + " FixtureNumber = '" + Fixture.FixtureNumber + "'," + " StageName = '" + StageName + "'," + " GameReferee = '" + GameReferee + "'," + " AssistantReferee = '" + AssistantReferee + "'," + " TelevisionReferee = '" + TelevisionReferee + "'," + " RoundNumber = '" + Fixture.RoundNumber + "'," + " HomeName = '" + Fixture.HomeName + "', " + " AwayName = '" + Fixture.AwayName + "', " + " HomeAbbr = '" + Fixture.HomeAbbr + "', " + " AwayAbbr = '" + Fixture.AwayAbbr + "', " + " Allow22MeterTry = '" + Fixture.Allow22MeterTry + "', " + " Tries = '" + Fixture.Tries_Points + "', " + " Conversion = '" + Fixture.Conversion_Points + "', " + " Penalties = '" + Fixture.Penalties_Points + "', " + " Dropgoal = '" + Fixture.Dropgoal_Points + "', " + " PenaltyTry = '" + Fixture.PenaltyTry_Points + "', " + " Portry = '" + Fixture.Portry_Points + "', " + " Code = '" + Fixture.Code + "', " + " TID = '" + Fixture.TID + "' ," + " YellowCardCounter = '" + Fixture.YELLOWCARDCOUNTER + "' ," + " YellowCardTimer = '" + Fixture.YELLOWCARDTIMER + "' ," + " BloodTimer = '" + Fixture.BLOODTIMER + "' ," + " HIATimer = '" + Fixture.HIATIMER + "' ," + " HalfTime = '" + Fixture.HtimeMin + "' ," + " TournName = '" + Fixture.TournName + "' " + " WHERE FID = '" + Fixture.FID + "'; "; command = new SQLiteCommand(sql, sqlite_conn); command.ExecuteNonQuery(); } sqlite_conn.Close(); } private void Events(dynamic EVENTS) { SQLiteConnection sqlite_conn; string dbPath = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\Sideline Manager\\"; sqlite_conn = new SQLiteConnection("Data Source="+ dbPath +"Footprint.db;Version=3;"); sqlite_conn.Open(); string sql; foreach (var Obj in EVENTS) { try { sql = "insert into Events (CEID, CommName,position,type,Subs,SubsRequired,Card,Color,Groups,Session)" + " values ('" + Obj.CEID + "', '" + Obj.CommName + "','" + Obj.position + "','" + Obj.type + "','" + Obj.Subs + "','" + Obj.SubsRequired + "','" + Obj.Card + "','" + Obj.Color + "','" + Obj.Group + "','" + Obj.Session + "')"; var command = new SQLiteCommand(sql, sqlite_conn); command.ExecuteNonQuery(); } catch (Exception error) { sql = "UPDATE Events SET CEID = '" + Obj.CEID + "', " + "CommName = '" + Obj.CommName + "'," + "position = '" + Obj.position + "'," + "type = '" + Obj.type + "'," + "Subs = '" + Obj.Subs + "'," + "SubsRequired = '" + Obj.SubsRequired + "'," + "Card = '" + Obj.Card + "'," + "Color = '" + Obj.Color + "', " + "Session = '" + Obj.Session + "', " + "Groups = '" + Obj.Group + "' " + " WHERE CEID = " + Obj.CEID + "; "; var command = new SQLiteCommand(sql, sqlite_conn); command.ExecuteNonQuery(); } } sqlite_conn.Close(); } private void setTeam(dynamic TEAMSIDE, dynamic TEAM, int SID, int FID) { int i = 0; foreach (var Obj in TEAM) { string FirstName = Convert.ToString(Obj.FirstName); FirstName = FirstName.Replace("/", ""); FirstName = FirstName.Replace("\\", ""); //FirstName = FirstName.Replace("'", "''"); string LastName = Convert.ToString(Obj.LastName); LastName = LastName.Replace("/", ""); LastName = LastName.Replace("\\", ""); //LastName = LastName.Replace("'", "''"); string NickName = Convert.ToString(Obj.NickName); NickName = NickName.Replace("/", ""); NickName = NickName.Replace("\\", ""); //NickName = NickName.Replace("'", "''"); string FullName = Convert.ToString(Obj.FullName); FullName = FullName.Replace("/", ""); FullName = FullName.Replace("\\", ""); //FullName = FullName.Replace("'", "''"); string Front = ""; try { foreach (var item in TEAMSIDE) { if (Front == "") Front = Obj.UserID == item.ID ? item.Front : ""; } } catch (Exception) { } string sql = "UPDATE Teams SET " + " SID = '" + SID + "', " + " FID = '" + FID + "', " + " UserID = '" + Obj.UserID + "', " + " FirstName = '" + FirstName + "'," + " LastName = '" + LastName + "'," + " NickName = '" + NickName + "'," + " FullName = '" + FullName + "'," + " Pos = '" + Obj.Pos + "'," + " CurrentPos = '" + Obj.Pos + "'," + " Vice = '" + Obj.Vice + "'," + " Cap = '" + Obj.Cap + "'," + " Location = '0'," + " Front = '" + Front + "'," + " Time = '0'," + " Jersey = '" + Obj.Jersey + "' " + " WHERE UserID = " + Obj.UserID + " AND SID = " + SID + " AND FID = " + FID + " ; "; bool update = SQL.Execute(sql); if (update == false) { sql = "insert into Teams (SID,FID,UserID,FirstName,LastName,NickName,FullName,Pos,Vice,Cap,Jersey,CurrentPos,Location,Front,Time)" + " values ( '" + SID + "','" + FID + "','" + Obj.UserID + "','" + FirstName + "','" + LastName + "','" + NickName + "','" + FullName + "','" + Obj.Pos + "','" + Obj.Vice + "','" + Obj.Cap + "','" + Obj.Jersey + "','" + Obj.Pos + "','0','" + Front + "','0')"; bool insert = SQL.Execute(sql); Console.WriteLine(i + " - " + FullName); } i++; } } } }