using System; using System.Collections.Generic; using System.Security.Permissions; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Linq; //using System.Windows.Forms; using System.Windows.Forms; using System.Windows.Markup; using System.Windows.Media; using Application = System.Windows.Application; using Brush = System.Windows.Media.Brush; using ProgressBar = System.Windows.Controls.ProgressBar; using HoopTrackerAI.Account; using System.IO; using System.Text; using Newtonsoft.Json; using System.Net; using System.ComponentModel; namespace HoopTrackerAI.HoopTrackerPages { public class Params { [JsonProperty(PropertyName = "startdate")] public int startdate { get; set; } [JsonProperty(PropertyName = "enddate")] public int enddate { get; set; } [JsonProperty(PropertyName = "startdate_web")] public DateTime startdate_web { get; set; } [JsonProperty(PropertyName = "enddate_web")] public DateTime enddate_web { get; set; } [JsonProperty(PropertyName = "data")] public string[] data { get; set; } } public class DashboardFiltrDate { [JsonProperty(PropertyName = "error_id")] public int error_id { get; set; } [JsonProperty(PropertyName = "error_str")] public string error_str { get; set; } [JsonProperty(PropertyName = "params")] public Params[] mas { get; set; } } public class GameData { [JsonProperty(PropertyName = "ratio_ok")] public int ratio_ok { get; set; } [JsonProperty(PropertyName = "ratio_all")] public int ratio_all { get; set; } [JsonProperty(PropertyName = "procent")] public int procent { get; set; } [JsonProperty(PropertyName = "point")] public int point { get; set; } [JsonProperty(PropertyName = "resulttype")] public int resulttype { get; set; } } public class Games { [JsonProperty(PropertyName = "1")] public GameData data1 { get; set; } [JsonProperty(PropertyName = "2")] public GameData data2 { get; set; } [JsonProperty(PropertyName = "3")] public GameData data3 { get; set; } [JsonProperty(PropertyName = "4")] public GameData data4 { get; set; } [JsonProperty(PropertyName = "5")] public GameData data5 { get; set; } [JsonProperty(PropertyName = "6")] public GameData data6 { get; set; } [JsonProperty(PropertyName = "7")] public GameData data7 { get; set; } [JsonProperty(PropertyName = "8")] public GameData data8 { get; set; } [JsonProperty(PropertyName = "9")] public GameData data9 { get; set; } [JsonProperty(PropertyName = "10")] public GameData data10 { get; set; } [JsonProperty(PropertyName = "11")] public GameData data11 { get; set; } [JsonProperty(PropertyName = "12")] public GameData data12 { get; set; } [JsonProperty(PropertyName = "13")] public GameData data13 { get; set; } [JsonProperty(PropertyName = "14")] public GameData data14 { get; set; } [JsonProperty(PropertyName = "15")] public GameData data15 { get; set; } } public partial class DashboardView { enum Units { Percent, Ratio, Points }; int sessionsCount = 1; Units currentUnits = Units.Percent; const int MAXSESSIONS = 20; string host = "http://hoop.motorrad.com.ua/"; public Games gd; public Dictionary point = new Dictionary(); public DashboardFiltrDate dfd; public DashboardView() { InitializeComponent(); if(!DesignerProperties.GetIsInDesignMode(this)) { dt_from.SelectedDate = DateTime.Now.AddDays(-7); dt_to.SelectedDate = DateTime.Now; string request = "token=" + Properties.Settings.Default.Token; string url = host + "data/dashboardmainparam/"; var response_data = RegisterView.PostMethod(request, url); if(response_data != null) { var strreader = new StreamReader(response_data.GetResponseStream(), Encoding.UTF8); var responseToString = strreader.ReadToEnd(); dfd = new DashboardFiltrDate(); dfd = JsonConvert.DeserializeObject(responseToString); } string url2 = host + "data/dashboard/"; string token = Properties.Settings.Default.Token; DateTime dt_start = dfd.mas[0].startdate_web; DateTime dt_end = dfd.mas[0].enddate_web.Date; string sdat = dt_start.ToString("u").Substring(0, 10); string edat = dt_end.ToString("u").Substring(0, 10); string s = tb_sessioncount.Text; string postedData = "token=" + token + "&date_beg=" + sdat + "&date_end=" + edat + "&session_id=" + s; var response = PostMethod(postedData, url2); if(response != null) { var strreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var responseToString = strreader.ReadToEnd(); gd = new Games(); gd = JsonConvert.DeserializeObject(responseToString); } // Populate(); PopulatePoint(); } } private void btn_sessionCountDown_Click(object sender, System.Windows.RoutedEventArgs e) { if(sessionsCount > 1) sessionsCount--; UpdateSessionCounter(); } private void btn_sessionCountUp_Click(object sender, System.Windows.RoutedEventArgs e) { if(sessionsCount < MAXSESSIONS) sessionsCount++; UpdateSessionCounter(); } void UpdateSessionCounter() { tb_sessioncount.Text = sessionsCount.ToString(); // Populate(); PopulatePoint(); } void Populate() { //will need to be done with mvvm logic later DateTime from = dt_from.SelectedDate.Value; DateTime to = dt_to.SelectedDate.Value.Add(new TimeSpan(23, 59, 59)); List dataSessions = Helper.data.Where(s => s.TimeStamp >= from && s.TimeStamp <= to).Take(sessionsCount).ToList(); List allDataPoints = new List(); foreach(Session session in dataSessions) { allDataPoints.AddRange(session.DataPoints); } //total shots tb_totalShots.Text = allDataPoints.Sum(dp => dp.succesful) + "/" + allDataPoints.Sum(dp => dp.attempted); //percent bars //This will need to compare to other users int fg_total = 0; int fg_succeed = 0; int pt3_total = 0; int pt3_succeed = 0; int ft_total = 0; int ft_succeed = 0; foreach(DataPoint dataPoint in allDataPoints) { if(Helper.GetPointsScoreBlock(dataPoint.position) == scoreBlock.FT) { ft_total += dataPoint.attempted; ft_succeed += dataPoint.succesful; } if(Helper.GetPointsScoreBlock(dataPoint.position) == scoreBlock.PT3) { pt3_total += dataPoint.attempted; pt3_succeed += dataPoint.succesful; } else { fg_total += dataPoint.attempted; fg_succeed += dataPoint.succesful; } } progress_3PT.Value = (double)pt3_succeed / pt3_total; progress_FG.Value = (double)fg_succeed / fg_total; progress_FT.Value = (double)ft_succeed / ft_total; SetBarColor(progress_3PT); SetBarColor(progress_FG); SetBarColor(progress_FT); tb_3PT.Text = progress_3PT.Value.ToString("P0"); tb_FG.Text = progress_FG.Value.ToString("P0"); tb_FT.Text = progress_FT.Value.ToString("P0"); //point dots/ Dictionary> pointsDictionary = new Dictionary>(); pointsDictionary.Add(pnt_01, allDataPoints.Where(dp => dp.position == 1).ToList()); pointsDictionary.Add(pnt_02, allDataPoints.Where(dp => dp.position == 2).ToList()); pointsDictionary.Add(pnt_03, allDataPoints.Where(dp => dp.position == 3).ToList()); pointsDictionary.Add(pnt_04, allDataPoints.Where(dp => dp.position == 4).ToList()); pointsDictionary.Add(pnt_05, allDataPoints.Where(dp => dp.position == 5).ToList()); pointsDictionary.Add(pnt_06, allDataPoints.Where(dp => dp.position == 6).ToList()); pointsDictionary.Add(pnt_07, allDataPoints.Where(dp => dp.position == 7).ToList()); pointsDictionary.Add(pnt_08, allDataPoints.Where(dp => dp.position == 8).ToList()); pointsDictionary.Add(pnt_09, allDataPoints.Where(dp => dp.position == 9).ToList()); pointsDictionary.Add(pnt_10, allDataPoints.Where(dp => dp.position == 10).ToList()); pointsDictionary.Add(pnt_11, allDataPoints.Where(dp => dp.position == 11).ToList()); pointsDictionary.Add(pnt_12, allDataPoints.Where(dp => dp.position == 12).ToList()); pointsDictionary.Add(pnt_13, allDataPoints.Where(dp => dp.position == 13).ToList()); pointsDictionary.Add(pnt_14, allDataPoints.Where(dp => dp.position == 14).ToList()); pointsDictionary.Add(pnt_15, allDataPoints.Where(dp => dp.position == 15).ToList()); foreach(KeyValuePair> kvp in pointsDictionary) { if(!kvp.Value.Any()) { kvp.Key.Text = "--"; kvp.Key.DotColor = DotColor.Green; continue; } kvp.Key.DotColor = Helper.getDotColor(kvp.Value[0].position, kvp.Value.Sum(d => d.succesful), kvp.Value.Sum(d => d.attempted)); if(currentUnits == Units.Percent) { double percent = (double)kvp.Value.Sum(d => d.succesful) / kvp.Value.Sum(d => d.attempted); kvp.Key.Text = percent.ToString("P0"); } else if(currentUnits == Units.Points) { kvp.Key.Text = (Helper.GetPointsMultiplier(kvp.Value[0].position) * kvp.Value.Sum(d => d.succesful)).ToString(); } else if(currentUnits == Units.Ratio) { kvp.Key.Text = kvp.Value.Sum(d => d.succesful) + " / " + kvp.Value.Sum(d => d.attempted); } } //Correct button visual setButtonVisual(btn_Percentage, false); setButtonVisual(btn_Point, false); setButtonVisual(btn_Ratio, false); if(currentUnits == Units.Percent) setButtonVisual(btn_Percentage, true); else if(currentUnits == Units.Points) setButtonVisual(btn_Point, true); else if(currentUnits == Units.Ratio) setButtonVisual(btn_Ratio, true); } void SetBarColor(ProgressBar bar) { if(bar.Value < .4) bar.Foreground = (Brush)this.FindResource("GradientBrushRed"); else if(bar.Value < .6) bar.Foreground = (Brush)this.FindResource("GradientBrushOrange"); else if(bar.Value < .8) bar.Foreground = (Brush)this.FindResource("GradientBrushYellow"); else bar.Foreground = (Brush)this.FindResource("GradientBrushGreen"); } private void btn_Percentage_Click(object sender, System.Windows.Input.MouseButtonEventArgs e) { currentUnits = Units.Percent; // Populate(); PopulatePoint(); } private void btn_Point_Click(object sender, System.Windows.Input.MouseButtonEventArgs e) { currentUnits = Units.Points; // Populate(); PopulatePoint(); } private void btn_Ratio_Click(object sender, System.Windows.Input.MouseButtonEventArgs e) { currentUnits = Units.Ratio; //Populate(); PopulatePoint(); } void setButtonVisual(Grid button, bool on) { (button.Children[0] as System.Windows.Shapes.Rectangle).Visibility = on ? Visibility.Visible : Visibility.Collapsed; (button.Children[1] as TextBlock).Foreground = on ? Helper.BrushGrey : Brushes.Black; } public static HttpWebResponse PostMethod(string postedData, string postUrl) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl); request.Method = "POST"; request.Credentials = CredentialCache.DefaultCredentials; UTF8Encoding encoding = new UTF8Encoding(); var bytes = encoding.GetBytes(postedData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bytes.Length; using(var newStream = request.GetRequestStream()) { newStream.Write(bytes, 0, bytes.Length); newStream.Close(); } return (HttpWebResponse)request.GetResponse(); } void PopulatePoint() { string url = host + "data/dashboard/"; string token = Properties.Settings.Default.Token; DateTime dt_start = dfd.mas[0].startdate_web; DateTime dt_end = dfd.mas[0].enddate_web.Date; string sdat = dt_start.ToString("u").Substring(0, 10); string edat = dt_end.ToString("u").Substring(0, 10); string s = tb_sessioncount.Text; int point_all = 0; int point_ok = 0; string postedData = "token=" + token + "&date_beg=" + sdat + "&date_end=" + edat + "&session_id=" + s; var response = PostMethod(postedData, url); if(response != null) { var strreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var responseToString = strreader.ReadToEnd(); gd = new Games(); gd = JsonConvert.DeserializeObject(responseToString); } List list = new List(); list.Add(gd.data1); list.Add(gd.data2); list.Add(gd.data3); list.Add(gd.data4); list.Add(gd.data5); list.Add(gd.data6); list.Add(gd.data7); list.Add(gd.data8); list.Add(gd.data9); list.Add(gd.data10); list.Add(gd.data11); list.Add(gd.data12); list.Add(gd.data13); list.Add(gd.data14); list.Add(gd.data15); for (int i = 0; i < list.Count; i++ ) { point_all += list[i].ratio_all; point_ok += list[i].ratio_ok; } tb_totalShots.Text = point_ok.ToString() + " / " + point_all.ToString(); int fg_total = 0; int fg_succeed = 0; int pt3_total = 0; int pt3_succeed = 0; int ft_total = 0; int ft_succeed = 0; for (int i = 0; i < list.Count; i++) { if (Helper.GetPointsScoreBlock(i+1) == scoreBlock.FT) { ft_total += list[i].ratio_all; ft_succeed += list[i].ratio_ok; } if (Helper.GetPointsScoreBlock(i+1) == scoreBlock.PT3) { pt3_total += list[i].ratio_all; pt3_succeed += list[i].ratio_ok; } if (Helper.GetPointsScoreBlock(i + 1) == scoreBlock.FG) { fg_total += list[i].ratio_all; fg_succeed += list[i].ratio_ok; } } if ((pt3_succeed == 0) || (pt3_total == 0)) { progress_3PT.Value = 0; } else { progress_3PT.Value = (double)pt3_succeed / pt3_total; }; if((fg_succeed == 0)||(fg_total == 0)) { progress_FG.Value = 0; } else { progress_FG.Value = (double)fg_succeed / fg_total; }; if((ft_succeed==0)||(ft_total==0)) { progress_FT.Value = 0; } else { progress_FT.Value = (double)ft_succeed / ft_total; } SetBarColor(progress_3PT); SetBarColor(progress_FG); SetBarColor(progress_FT); tb_3PT.Text = progress_3PT.Value.ToString("P0"); tb_FG.Text = progress_FG.Value.ToString("P0"); tb_FT.Text = progress_FT.Value.ToString("P0"); //Correct button visual setButtonVisual(btn_Percentage, false); setButtonVisual(btn_Point, false); setButtonVisual(btn_Ratio, false); if (currentUnits == Units.Percent) { setButtonVisual(btn_Percentage, true); SetProcent(); } else if (currentUnits == Units.Points) { setButtonVisual(btn_Point, true); SetPoints(); } else if (currentUnits == Units.Ratio) { setButtonVisual(btn_Ratio, true); SetRatio(); } SetDotColor(); } void SetProcent() { pnt_01.Text = gd.data1.procent.ToString()+"%"; pnt_02.Text = gd.data2.procent.ToString() + "%"; pnt_03.Text = gd.data3.procent.ToString() + "%"; pnt_04.Text = gd.data4.procent.ToString() + "%"; pnt_05.Text = gd.data5.procent.ToString() + "%"; pnt_06.Text = gd.data6.procent.ToString() + "%"; pnt_07.Text = gd.data7.procent.ToString() + "%"; pnt_08.Text = gd.data8.procent.ToString() + "%"; pnt_09.Text = gd.data9.procent.ToString() + "%"; pnt_10.Text = gd.data10.procent.ToString() + "%"; pnt_11.Text = gd.data11.procent.ToString() + "%"; pnt_12.Text = gd.data12.procent.ToString() + "%"; pnt_13.Text = gd.data13.procent.ToString() + "%"; pnt_14.Text = gd.data14.procent.ToString() + "%"; pnt_15.Text = gd.data15.procent.ToString() + "%"; } void SetPoints() { pnt_01.Text = gd.data1.point.ToString(); pnt_02.Text = gd.data2.point.ToString(); pnt_03.Text = gd.data3.point.ToString(); pnt_04.Text = gd.data4.point.ToString(); pnt_05.Text = gd.data5.point.ToString(); pnt_06.Text = gd.data6.point.ToString(); pnt_07.Text = gd.data7.point.ToString(); pnt_08.Text = gd.data8.point.ToString(); pnt_09.Text = gd.data9.point.ToString(); pnt_10.Text = gd.data10.point.ToString(); pnt_11.Text = gd.data11.point.ToString(); pnt_12.Text = gd.data12.point.ToString(); pnt_13.Text = gd.data13.point.ToString(); pnt_14.Text = gd.data14.point.ToString(); pnt_15.Text = gd.data15.point.ToString(); } void SetRatio() { pnt_01.Text =gd.data1.ratio_ok.ToString() + "/" + gd.data1.ratio_all.ToString(); pnt_02.Text = gd.data2.ratio_ok.ToString() + "/" + gd.data2.ratio_all.ToString(); pnt_03.Text = gd.data3.ratio_ok.ToString() + "/" + gd.data3.ratio_all.ToString(); pnt_04.Text = gd.data4.ratio_ok.ToString() + "/" + gd.data4.ratio_all.ToString(); pnt_05.Text = gd.data5.ratio_ok.ToString() + "/" + gd.data5.ratio_all.ToString(); pnt_06.Text = gd.data6.ratio_ok.ToString() + "/" + gd.data6.ratio_all.ToString(); pnt_07.Text = gd.data7.ratio_ok.ToString() + "/" + gd.data7.ratio_all.ToString(); pnt_08.Text = gd.data8.ratio_ok.ToString() + "/" + gd.data8.ratio_all.ToString(); pnt_09.Text = gd.data9.ratio_ok.ToString() + "/" + gd.data9.ratio_all.ToString(); pnt_10.Text = gd.data10.ratio_ok.ToString() + "/" + gd.data10.ratio_all.ToString(); pnt_11.Text = gd.data11.ratio_ok.ToString() + "/" + gd.data11.ratio_all.ToString(); pnt_12.Text = gd.data12.ratio_ok.ToString() + "/" + gd.data12.ratio_all.ToString(); pnt_13.Text = gd.data13.ratio_ok.ToString() + "/" + gd.data13.ratio_all.ToString(); pnt_14.Text = gd.data14.ratio_ok.ToString() + "/" + gd.data14.ratio_all.ToString(); pnt_15.Text = gd.data15.ratio_ok.ToString() + "/" + gd.data15.ratio_all.ToString(); } void SetDotColor() { //point1 if(gd.data1.resulttype == 1) { pnt_01.DotColor = DotColor.Green; } else if(gd.data1.resulttype == 2) { pnt_01.DotColor = DotColor.Yellow; } else if(gd.data1.resulttype == 3) { pnt_01.DotColor = DotColor.Orange; } else if(gd.data1.resulttype == 4) { pnt_01.DotColor = DotColor.Red; } //point2 if(gd.data2.resulttype == 1) { pnt_02.DotColor = DotColor.Green; } else if(gd.data2.resulttype == 2) { pnt_02.DotColor = DotColor.Yellow; } else if(gd.data2.resulttype == 3) { pnt_02.DotColor = DotColor.Orange; } else if(gd.data2.resulttype == 4) { pnt_02.DotColor = DotColor.Red; } //point3 if(gd.data3.resulttype == 1) { pnt_03.DotColor = DotColor.Green; } else if(gd.data3.resulttype == 2) { pnt_03.DotColor = DotColor.Yellow; } else if(gd.data3.resulttype == 3) { pnt_03.DotColor = DotColor.Orange; } else if(gd.data3.resulttype == 4) { pnt_03.DotColor = DotColor.Red; } //point4 if(gd.data4.resulttype == 1) { pnt_04.DotColor = DotColor.Green; } else if(gd.data4.resulttype == 2) { pnt_04.DotColor = DotColor.Yellow; } else if(gd.data4.resulttype == 3) { pnt_04.DotColor = DotColor.Orange; } else if(gd.data4.resulttype == 4) { pnt_04.DotColor = DotColor.Red; } //point5 if(gd.data5.resulttype == 1) { pnt_05.DotColor = DotColor.Green; } else if(gd.data5.resulttype == 2) { pnt_05.DotColor = DotColor.Yellow; } else if(gd.data5.resulttype == 3) { pnt_05.DotColor = DotColor.Orange; } else if(gd.data5.resulttype == 4) { pnt_05.DotColor = DotColor.Red; } //point6 if(gd.data6.resulttype == 1) { pnt_06.DotColor = DotColor.Green; } else if(gd.data6.resulttype == 2) { pnt_06.DotColor = DotColor.Yellow; } else if(gd.data6.resulttype == 3) { pnt_06.DotColor = DotColor.Orange; } else if(gd.data6.resulttype == 4) { pnt_06.DotColor = DotColor.Red; } //point7 if(gd.data7.resulttype == 1) { pnt_07.DotColor = DotColor.Green; } else if(gd.data7.resulttype == 2) { pnt_07.DotColor = DotColor.Yellow; } else if(gd.data7.resulttype == 3) { pnt_07.DotColor = DotColor.Orange; } else if(gd.data7.resulttype == 4) { pnt_07.DotColor = DotColor.Red; } //point8 if(gd.data8.resulttype == 1) { pnt_08.DotColor = DotColor.Green; } else if(gd.data8.resulttype == 2) { pnt_08.DotColor = DotColor.Yellow; } else if(gd.data8.resulttype == 3) { pnt_08.DotColor = DotColor.Orange; } else if(gd.data8.resulttype == 4) { pnt_08.DotColor = DotColor.Red; } //point9 if(gd.data9.resulttype == 1) { pnt_09.DotColor = DotColor.Green; } else if(gd.data9.resulttype == 2) { pnt_09.DotColor = DotColor.Yellow; } else if(gd.data9.resulttype == 3) { pnt_09.DotColor = DotColor.Orange; } else if(gd.data9.resulttype == 4) { pnt_09.DotColor = DotColor.Red; } //point10 if(gd.data10.resulttype == 1) { pnt_10.DotColor = DotColor.Green; } else if(gd.data10.resulttype == 2) { pnt_10.DotColor = DotColor.Yellow; } else if(gd.data10.resulttype == 3) { pnt_10.DotColor = DotColor.Orange; } else if(gd.data10.resulttype == 4) { pnt_10.DotColor = DotColor.Red; } //point11 if(gd.data11.resulttype == 1) { pnt_11.DotColor = DotColor.Green; } else if(gd.data11.resulttype == 2) { pnt_11.DotColor = DotColor.Yellow; } else if(gd.data11.resulttype == 3) { pnt_11.DotColor = DotColor.Orange; } else if(gd.data11.resulttype == 4) { pnt_11.DotColor = DotColor.Red; } //point12 if(gd.data12.resulttype == 1) { pnt_12.DotColor = DotColor.Green; } else if(gd.data12.resulttype == 2) { pnt_12.DotColor = DotColor.Yellow; } else if(gd.data12.resulttype == 3) { pnt_12.DotColor = DotColor.Orange; } else if(gd.data12.resulttype == 4) { pnt_12.DotColor = DotColor.Red; } //point13 if(gd.data13.resulttype == 1) { pnt_13.DotColor = DotColor.Green; } else if(gd.data13.resulttype == 2) { pnt_13.DotColor = DotColor.Yellow; } else if(gd.data13.resulttype == 3) { pnt_13.DotColor = DotColor.Orange; } else if(gd.data13.resulttype == 4) { pnt_13.DotColor = DotColor.Red; } //point14 if(gd.data14.resulttype == 1) { pnt_14.DotColor = DotColor.Green; } else if(gd.data14.resulttype == 2) { pnt_14.DotColor = DotColor.Yellow; } else if(gd.data14.resulttype == 3) { pnt_14.DotColor = DotColor.Orange; } else if(gd.data14.resulttype == 4) { pnt_14.DotColor = DotColor.Red; } //point15 if(gd.data15.resulttype == 1) { pnt_15.DotColor = DotColor.Green; } else if(gd.data15.resulttype == 2) { pnt_15.DotColor = DotColor.Yellow; } else if(gd.data15.resulttype == 3) { pnt_15.DotColor = DotColor.Orange; } else if(gd.data15.resulttype == 4) { pnt_15.DotColor = DotColor.Red; } } } public class Session { //temporary for test data will come from db in the future public DateTime TimeStamp { get; set; } public List DataPoints { get; set; } public Session(DateTime _timeStamp, List datapoints) { TimeStamp = _timeStamp; DataPoints = datapoints; } } public class DataPoint { //temporary for test data will come from db in the future public DataPoint(int _position, int _succssful, int _attempted) { position = _position; succesful = _succssful; attempted = _attempted; } public int position { get; set; } public int succesful { get; set; } public int attempted { get; set; } } }