Fixed the slowest drivers windows and cleaned some of the code

This commit is contained in:
2023-05-30 09:38:51 +02:00
parent 6f36c829c2
commit 067e7233a0
16 changed files with 221 additions and 70 deletions
+68 -4
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 30/05/2023
/// File : DataWrapper.cs
/// Brief : Class that is used to interface between the main Form (vue) and the Storage (wich is a class that wraps the sqlite database, so the model) its almost MVC :D
/// Version : Alpha 1.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -13,11 +19,22 @@ namespace Test_Merge
private Reader Reader;
private SqliteStorage Storage;
List<List<DriverData>> LiveDriverDataLogs = new List<List<DriverData>>();
//Note : It could be usefull to get the mainForm at the start of the programm and not have to take it in half of the methods.
/// <summary>
/// Constructs a new DataWrapper. It needs the config file so it can create a Reader, It also needs a first screenshot for the same reason
/// </summary>
/// <param name="configFile">The JSON config file that is created by the configuration tool</param>
/// <param name="screenshot">A screenshot of the </param>
public DataWrapper(string configFile, Bitmap screenshot)
{
Reader = new Reader(configFile, screenshot, true);
//The Storage is here and on the Reader. It seems bad but it is ok as we dont use it at all to insert data and are only using it here to read some. The reader takes care of the inserts (Note: We could technically do both here but I did not find it usefull to transfer everything here)
Storage = Reader.Storage;
}
/// <summary>
/// Refreshes the controller so it has the latest driver datas (Be sure to call it everytime you need to use any other method and expects the data to be up to date)
/// </summary>
/// <returns>Error code, 0 is success, 1 is not (Note: Maybe it could be interesting in the future to add some more error handling here)</returns>
public int Refresh()
{
LiveDriverDataLogs.Add(Reader.Decode(Reader.MainZones, Reader.Drivers));
@@ -25,15 +42,28 @@ namespace Test_Merge
return 0;
return 1;
}
/// <summary>
/// Changes the image to the newest screenshot in all of the zones and windows
/// </summary>
/// <param name="image">The new screenshot to put everywhere (Do not mix resolutions)</param>
public void ChangeImage(Bitmap image)
{
Reader.ChangeImage(image);
}
/// <summary>
/// Gets all the data from one driver and also displays into the given panel the last five laps (or less if its the sart of the race) Note: Its responsive :D
/// </summary>
/// <param name="driverName">The name of the driver (should not be case sensitive but it MUST already exist in the first list that has been inserted into the DB)</param>
/// <param name="lastFiveLapsPanel">The pannel where you want the five last laps to be displayed</param>
/// <param name="form1">The Main form.</param>
/// <returns></returns>
public DriverData GetFullDriverData(string driverName, Panel lastFiveLapsPanel, Main form1)
{
//Note : I know that its a bad idea to ask the Form in this method and some others because it means that it wont work with any main form. And to that Ill say that... your right !
DriverData result = new DriverData();
if (LiveDriverDataLogs.Count > 0)
{
//Searches the most recent live data from the given driverName
foreach (DriverData data in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1])
{
if (data.Name == driverName)
@@ -42,6 +72,7 @@ namespace Test_Merge
if (result.Name != "")
{
//Recovers and displays the last five laps from the driver
lastFiveLapsPanel.Controls.Clear();
Size labelDimensions = new Size(lastFiveLapsPanel.Width, lastFiveLapsPanel.Height / 5);
@@ -50,6 +81,8 @@ namespace Test_Merge
int id = 0;
foreach ((int LapTime, int Lap) lapData in lapsInfos)
{
//Hardcodes the new button.
//Note : It could be smart to have like a default button for all the methods to use without needing to rewrite everything.
Button newButton = new Button();
lastFiveLapsPanel.Controls.Add(newButton);
newButton.Name = driverName + "_" + lapData.Lap;
@@ -65,10 +98,16 @@ namespace Test_Merge
return result;
}
/// <summary>
/// Runs trough every drivers live data to recover the drivers that are close to each others
/// </summary>
/// <param name="pnlBattles">The control that will host the displayed battles</param>
/// <param name="form1">The main form. It needs to have a method called 'btnDriver_Click' so it can reads the buttons clicks</param>
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)>();
//Search trough all the drivers and finds the one battling
foreach (DriverData driver in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1])
{
if (oldDriver != null && driver.Position != -1 && oldDriver.Position != -1)
@@ -104,6 +143,8 @@ namespace Test_Merge
{
if(id < maxBattles)
{
//*hardcoding* the different controls that needs to be added to the panel.
//Note : this stuff could totally be handled by the Form with method returning a list of the drivers. It was just easier for me at the time to code it this way but its not the prettiest
Button btnFirstDriver = new Button();
Button btnSecondDriver = new Button();
Label lblGap = new Label();
@@ -154,6 +195,12 @@ namespace Test_Merge
}
}
}
/// <summary>
/// Searches the fastest and slowests drivers and displays them in the given panels
/// </summary>
/// <param name="pnlFastest">Panel that will contain the constructed controls</param>
/// <param name="pnlSlowest">Panel that will contain the constructed controls</param>
/// <param name="form1">The main form that needs to implement the method btnDriver_Click to allow it to recover custom buttons click</param>
public void DisplayTimesDeltas(Panel pnlFastest,Panel pnlSlowest, Main form1)
{
List<(int avg, string driverName)> averages = new List<(int avg, string driverName)>();
@@ -180,6 +227,7 @@ namespace Test_Merge
pnlSlowest.Controls.Clear();
int maxUiSize = pnlFastest.Height / numberOfDriversToShow;
//Displays the fastest drivers
for (int i = 0; i < numberOfDriversToShow; i++)
{
Button newButton = new Button();
@@ -195,7 +243,7 @@ namespace Test_Merge
if (i != 0)
newButton.Text += " + " + Reader.ConvertMsToTime(Convert.ToInt32(((float)data.avg - (float)averages[0].avg) / 5.0f));
}
//Displays the slowests drivers
int badId = 0;
for (int i = averages.Count -1; i >= averages.Count - numberOfDriversToShow; i--)
{
@@ -209,13 +257,18 @@ namespace Test_Merge
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));
newButton.Text += " + " + Reader.ConvertMsToTime(Convert.ToInt32(((float)data.avg - (float)averages[0].avg) / 5.0f));
badId++;
}
}
}
/// <summary>
/// Will add to the list of overtakes the different changes of position
/// </summary>
/// <param name="lsbResult">The listbox containing all the infos</param>
public void DisplayOvertakes(ListBox lsbResult)
{
//Note : This method SHOULD REALLY not do this but just return a string or a list of string with the new overtakes so the form can handle it as it wishes
if (LiveDriverDataLogs.Count > 1)
{
List<DriverData> oldList = LiveDriverDataLogs[LiveDriverDataLogs.Count - 2];
@@ -246,6 +299,12 @@ namespace Test_Merge
}
}
}
/// <summary>
/// Displays a messageBox containing the infos about a lap time
/// </summary>
/// <param name="driverName">The name of the driver that has done the lapTime</param>
/// <param name="Lap">The number of the lap on wich the lapTime has been set (CAUTION ITS NOT THE RACING LAP ITS FROM THE DB)</param>
/// <param name="LapTime">The time (in ms) of the lap</param>
public void DisplayLapTimeInfos(string driverName, int Lap, string LapTime)
{
List<int> sectors = Storage.GetSectorsFromLapTime(driverName, Lap);
@@ -259,6 +318,11 @@ namespace Test_Merge
message += "Sector 3 : " + Reader.ConvertMsToTime(sectors[2]) + Environment.NewLine;
MessageBox.Show(message);
}
/// <summary>
/// Displays the live ranking with the names of the drivers and their gap to the leader in the right order
/// </summary>
/// <param name="pnl">The control that will host all the new controls</param>
/// <param name="form1">The main form</param>
public void DisplayLiveRanking(Panel pnl, Main form1)
{
if (LiveDriverDataLogs.Count > 0)
@@ -304,7 +368,7 @@ namespace Test_Merge
buttons[driverCount] = newButton;
}
//Note : It could be better to have this directly in the same loop
foreach (Button button in buttons)
{
pnl.Controls.Add(button);