using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; namespace Test_Merge { internal class DataWrapper { private Reader Reader; private SqliteStorage Storage; List> LiveDriverDataLogs = new List>(); public DataWrapper(string configFile, Bitmap screenshot) { Reader = new Reader(configFile, screenshot, true); Storage = Reader.Storage; } public int Refresh() { LiveDriverDataLogs.Add(Reader.Decode(Reader.MainZones, Reader.Drivers)); if (LiveDriverDataLogs.Count > 0) return 0; return 1; } public void ChangeImage(Bitmap image) { Reader.ChangeImage(image); } public DriverData GetFullDriverData(string driverName, Panel lastFiveLapsPanel, Main form1) { DriverData result = new DriverData(); if (LiveDriverDataLogs.Count > 0) { foreach (DriverData data in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1]) { if (data.Name == driverName) result = data; } if (result.Name != "") { lastFiveLapsPanel.Controls.Clear(); Size labelDimensions = new Size(lastFiveLapsPanel.Width, lastFiveLapsPanel.Height / 5); List<(int LapTime, int Lap)> lapsInfos = Storage.GetDriverLaptimes(driverName, 5); int id = 0; foreach ((int LapTime, int Lap) lapData in lapsInfos) { Button newButton = new Button(); lastFiveLapsPanel.Controls.Add(newButton); newButton.Name = driverName + "_" + lapData.Lap; newButton.Text = Reader.ConvertMsToTime(lapData.LapTime); newButton.Size = labelDimensions; newButton.FlatStyle = FlatStyle.Popup; newButton.Click += form1.btnLapTime_Click; newButton.Location = new Point(0, id * newButton.Height); id++; } } } return result; } public void DisplayBattles(Panel pnlBattles,Main form1) { DriverData oldDriver = null; List<(DriverData d1, DriverData d2, int gap)> battles = new List<(DriverData d1, DriverData d2, int gap)>(); foreach (DriverData driver in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1]) { if (oldDriver != null && driver.Position != -1 && oldDriver.Position != -1) { if (driver.GapToLeader < oldDriver.GapToLeader) { //There is a problem with the drivers gaps } else { int gap = driver.GapToLeader - oldDriver.GapToLeader; //3000ms is 3s. If drivers are that close then they are definitely in battle. If they are farther then maybe not if (gap <= 3000) { battles.Add((oldDriver, driver, gap)); } } oldDriver = driver; } else { oldDriver = driver; } } //We will only display 4 battles max int maxBattles = 4; if (battles.Count > 0) { pnlBattles.Controls.Clear(); int maxUiHeight = Math.Max(pnlBattles.Height / maxBattles, pnlBattles.Height / battles.Count); int id = 0; foreach ((DriverData d1, DriverData d2, int gap) battle in battles) { if(id < maxBattles) { Button btnFirstDriver = new Button(); Button btnSecondDriver = new Button(); Label lblGap = new Label(); pnlBattles.Controls.Add(btnFirstDriver); pnlBattles.Controls.Add(lblGap); pnlBattles.Controls.Add(btnSecondDriver); btnFirstDriver.Anchor = AnchorStyles.Left | AnchorStyles.Top; btnSecondDriver.Anchor = AnchorStyles.Right | AnchorStyles.Top; lblGap.Anchor = AnchorStyles.Left | AnchorStyles.Right; lblGap.Dock = DockStyle.None; lblGap.TextAlign = ContentAlignment.MiddleCenter; btnFirstDriver.Click += form1.btnDriver_Click; btnSecondDriver.Click += form1.btnDriver_Click; btnFirstDriver.FlatStyle = FlatStyle.Popup; btnSecondDriver.FlatStyle = FlatStyle.Popup; btnFirstDriver.Size = new Size(pnlBattles.Width / 3, maxUiHeight); btnSecondDriver.Size = new Size(pnlBattles.Width / 3, maxUiHeight); btnFirstDriver.Location = new Point(0, id * maxUiHeight); lblGap.Location = new Point(btnFirstDriver.Width, id * maxUiHeight); btnSecondDriver.Location = new Point(pnlBattles.Width / 3 * 2, id * maxUiHeight); btnFirstDriver.Text = battle.d1.Name; lblGap.Text = "+ " + Reader.ConvertMsToTime(battle.gap); if (battle.gap <= 2000) lblGap.ForeColor = Color.Yellow; if (battle.gap <= 1000) lblGap.ForeColor = Color.Green; btnSecondDriver.Text = battle.d2.Name; btnFirstDriver.Name = battle.d1.Name + "_" + id; lblGap.Name = "lbl_Gap_" + id; btnSecondDriver.Name = battle.d2.Name + "_" + id; } else { break; } id++; } } } public void DisplayTimesDeltas(Panel pnlFastest,Panel pnlSlowest, Main form1) { List<(int avg, string driverName)> averages = new List<(int avg, string driverName)>(); foreach (DriverData driver in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1]) { //We want to recover the last 5 lap times List<(int lapTime,int lap)> laps = Storage.GetDriverLaptimes(driver.Name,5); if(laps.Count > 0) { int avg = 0; foreach ((int lapTime, int lap) lap in laps) { avg += lap.lapTime; } avg = avg / laps.Count; averages.Add((avg, driver.Name)); } } int numberOfDriversToShow = 5; if (averages.Count > 0 && averages.Count > numberOfDriversToShow) { averages = averages.OrderBy(item => item.avg).ToList(); pnlFastest.Controls.Clear(); pnlSlowest.Controls.Clear(); int maxUiSize = pnlFastest.Height / numberOfDriversToShow; for (int i = 0; i < numberOfDriversToShow; i++) { Button newButton = new Button(); (int avg, string driver) data = averages[i]; pnlFastest.Controls.Add(newButton); newButton.Size = new Size(pnlFastest.Width, maxUiSize); newButton.Location = new Point(0, i * maxUiSize); newButton.Text = data.driver; newButton.FlatStyle = FlatStyle.Popup; newButton.Name = data.driver + "_fastest_" + i; newButton.Click += form1.btnDriver_Click; //We take the average time lost per lap if (i != 0) newButton.Text += " + " + Reader.ConvertMsToTime(Convert.ToInt32(((float)data.avg - (float)averages[0].avg) / 5.0f)); } int badId = 0; for (int i = averages.Count -1; i >= averages.Count - numberOfDriversToShow; i--) { Button newButton = new Button(); (int avg, string driver) data = averages[i]; pnlSlowest.Controls.Add(newButton); newButton.Size = new Size(pnlFastest.Width, maxUiSize); newButton.Location = new Point(0, badId * maxUiSize); newButton.Text = data.driver; newButton.FlatStyle = FlatStyle.Popup; newButton.Name = data.driver + "_slowest_" + i; newButton.Click += form1.btnDriver_Click; //We take the average time lost per lap newButton.Text += " + " + Reader.ConvertMsToTime(Convert.ToInt32(((float)data.avg) - (float)averages[0].avg / 5.0f)); badId++; } } } public void DisplayOvertakes(ListBox lsbResult) { if (LiveDriverDataLogs.Count > 1) { List oldList = LiveDriverDataLogs[LiveDriverDataLogs.Count - 2]; List newList = LiveDriverDataLogs[LiveDriverDataLogs.Count - 1]; for (int i = 0; i < LiveDriverDataLogs[LiveDriverDataLogs.Count - 1].Count;i++) { if (oldList[i].Name != newList[i].Name) { //There has been a change in the standings for(int y = 0; y < oldList.Count;y++) { if (newList[y].Name == oldList[i].Name) { //We found its new location if (y > i) { //The driver overtook someone lsbResult.Items.Add(newList[y].Name + " climbed to " + y); } else { //The driver got overtook by someone lsbResult.Items.Add(newList[y].Name + " fell to " + y); } } } } } } } public void DisplayLapTimeInfos(string driverName, int Lap, string LapTime) { List sectors = Storage.GetSectorsFromLapTime(driverName, Lap); string message = "Lap time infos" + Environment.NewLine; message += LapTime + Environment.NewLine; if (sectors.Count > 0) message += "Sector 1 : " + Reader.ConvertMsToTime(sectors[0]) + Environment.NewLine; if (sectors.Count > 1) message += "Sector 2 : " + Reader.ConvertMsToTime(sectors[1]) + Environment.NewLine; if (sectors.Count > 2) message += "Sector 3 : " + Reader.ConvertMsToTime(sectors[2]) + Environment.NewLine; MessageBox.Show(message); } public void DisplayLiveRanking(Panel pnl, Main form1) { if (LiveDriverDataLogs.Count > 0) { pnl.Controls.Clear(); //Gets the last item that should be the most recent data List liveData = LiveDriverDataLogs[LiveDriverDataLogs.Count - 1]; Button[] buttons = new Button[liveData.Count]; Size buttonDimensions = new Size(pnl.Width, pnl.Height / liveData.Count); for (int driverCount = 0; driverCount < liveData.Count; driverCount++) { Button newButton = new Button(); newButton.Size = buttonDimensions; newButton.Location = new Point(0, driverCount * buttonDimensions.Height); newButton.FlatStyle = FlatStyle.Popup; DriverData driver = liveData[driverCount]; if (driver.Position == -1) { //Its a DNF newButton.Enabled = false; } if (driver.Position > 1) { newButton.Text = driver.Name + " +" + Reader.ConvertMsToTime(driver.GapToLeader); } else { newButton.Text = driver.Name; } newButton.Name = liveData[driverCount].Name; newButton.TextAlign = ContentAlignment.MiddleLeft; newButton.FlatStyle = FlatStyle.Popup; newButton.Click += form1.btnDriver_Click; buttons[driverCount] = newButton; } foreach (Button button in buttons) { pnl.Controls.Add(button); } } } } }