You can now see up to 4 of the battles that are going on (You can even click on the drivers to see them on the bottom of the screen)

This commit is contained in:
2023-05-24 13:54:42 +02:00
parent e4edd71adc
commit f356d90c35
5 changed files with 189 additions and 53 deletions

View File

@@ -13,9 +13,9 @@ namespace Test_Merge
private Reader Reader;
private SqliteStorage Storage;
List<List<DriverData>> LiveDriverDataLogs = new List<List<DriverData>>();
public DataWrapper(string configFile,Bitmap screenshot)
public DataWrapper(string configFile, Bitmap screenshot)
{
Reader = new Reader(configFile,screenshot, true);
Reader = new Reader(configFile, screenshot, true);
Storage = Reader.Storage;
}
public int Refresh()
@@ -29,38 +29,144 @@ namespace Test_Merge
{
Reader.ChangeImage(image);
}
public DriverData GetFullDriverData(string driverName,Panel lastFiveLapsPanel)
public DriverData GetFullDriverData(string driverName, Panel lastFiveLapsPanel, Form1 form1)
{
DriverData result = new DriverData();
foreach (DriverData data in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1])
if (LiveDriverDataLogs.Count > 0)
{
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);
foreach ((int LapTime, int Lap) lapData in lapsInfos)
foreach (DriverData data in LiveDriverDataLogs[LiveDriverDataLogs.Count - 1])
{
Button newButton = new Button();
newButton.Name = driverName + "_" + lapData.Lap;
newButton.Text = Reader.ConvertMsToTime(lapData.LapTime);
if (data.Name == driverName)
result = data;
}
lastFiveLapsPanel.Controls.Add(newButton);
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);
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.Click += form1.btnLapTime_Click;
}
}
}
return result;
}
public void DisplayBattles(Panel pnlBattles,Form1 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 DisplayLapTimeInfos(string driverName, int Lap, string LapTime)
{
List<int> sectors = Storage.GetSectorsFromLapTime(driverName, Lap);
string message = "Lap time infos" + Environment.NewLine;
if (sectors.Count() == 3)
{
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, Form1 form1)
{
if(LiveDriverDataLogs.Count > 0)
if (LiveDriverDataLogs.Count > 0)
{
pnl.Controls.Clear();
//Gets the last item that should be the most recent data
@@ -68,14 +174,14 @@ namespace Test_Merge
Button[] buttons = new Button[liveData.Count];
Size buttonDimensions = new Size(pnl.Width,pnl.Height/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.Location = new Point(0, driverCount * buttonDimensions.Height);
DriverData driver = liveData[driverCount];
@@ -84,7 +190,7 @@ namespace Test_Merge
//Its a DNF
newButton.Enabled = false;
}
if(driver.Position > 1)
if (driver.Position > 1)
{
newButton.Text = driver.Name + " +" + Reader.ConvertMsToTime(driver.GapToLeader);
}
@@ -92,16 +198,18 @@ namespace Test_Merge
{
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)
foreach (Button button in buttons)
{
pnl.Controls.Add(button);
}

View File

@@ -39,7 +39,6 @@
this.label1 = new System.Windows.Forms.Label();
this.gpbxRanking = new System.Windows.Forms.GroupBox();
this.pnlLiveRanking = new System.Windows.Forms.Panel();
this.tbxResult = new System.Windows.Forms.TextBox();
this.gpbxOvertakes = new System.Windows.Forms.GroupBox();
this.pnlOvertakes = new System.Windows.Forms.Panel();
this.gpbxDriverInfos = new System.Windows.Forms.GroupBox();
@@ -66,7 +65,6 @@
((System.ComponentModel.ISupportInitialize)(this.pbxResult)).BeginInit();
this.gpbxControls.SuspendLayout();
this.gpbxRanking.SuspendLayout();
this.pnlLiveRanking.SuspendLayout();
this.gpbxOvertakes.SuspendLayout();
this.gpbxDriverInfos.SuspendLayout();
this.gpbxLapTimes.SuspendLayout();
@@ -180,20 +178,11 @@
//
// pnlLiveRanking
//
this.pnlLiveRanking.Controls.Add(this.tbxResult);
this.pnlLiveRanking.Location = new System.Drawing.Point(6, 21);
this.pnlLiveRanking.Name = "pnlLiveRanking";
this.pnlLiveRanking.Size = new System.Drawing.Size(341, 669);
this.pnlLiveRanking.TabIndex = 0;
//
// tbxResult
//
this.tbxResult.Location = new System.Drawing.Point(17, 12);
this.tbxResult.Multiline = true;
this.tbxResult.Name = "tbxResult";
this.tbxResult.Size = new System.Drawing.Size(307, 442);
this.tbxResult.TabIndex = 0;
//
// gpbxOvertakes
//
this.gpbxOvertakes.Controls.Add(this.pnlOvertakes);
@@ -441,8 +430,6 @@
this.gpbxControls.ResumeLayout(false);
this.gpbxControls.PerformLayout();
this.gpbxRanking.ResumeLayout(false);
this.pnlLiveRanking.ResumeLayout(false);
this.pnlLiveRanking.PerformLayout();
this.gpbxOvertakes.ResumeLayout(false);
this.gpbxDriverInfos.ResumeLayout(false);
this.gpbxDriverInfos.PerformLayout();
@@ -489,7 +476,6 @@
private System.Windows.Forms.Panel pnlGainingTime;
private System.Windows.Forms.GroupBox gpbxBattles;
private System.Windows.Forms.Panel pnlBattles;
private System.Windows.Forms.TextBox tbxResult;
}
}

View File

@@ -49,7 +49,7 @@ namespace Test_Merge
Settings settingsForm = new Settings();
settingsForm.ShowDialog();
//MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear);
if(settingsForm.GrandPrixUrl != "" && settingsForm.SelectedConfigFile != "")
if (settingsForm.GrandPrixUrl != "" && settingsForm.SelectedConfigFile != "")
{
GpUrl = settingsForm.GrandPrixUrl;
if (File.Exists(settingsForm.SelectedConfigFile))
@@ -112,10 +112,11 @@ namespace Test_Merge
int errorCode = Wrapper.Refresh();
sw.Stop();
// Task completed
// Task completed
Invoke((MethodInvoker)delegate
{
DisplayResults(errorCode,sw, screen);
DisplayResults(errorCode, sw, screen);
DisplayBattles();
});
});
}
@@ -132,6 +133,10 @@ namespace Test_Merge
btnSettings.Enabled = true;
}
}
private void DisplayBattles()
{
Wrapper.DisplayBattles(pnlBattles,this);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Emulator != null)
@@ -139,7 +144,7 @@ namespace Test_Merge
Emulator.Stop();
}
}
private void DisplayResults(int errorCode,Stopwatch sw, Bitmap screen)
private void DisplayResults(int errorCode, Stopwatch sw, Bitmap screen)
{
if (errorCode != 0)
{
@@ -148,7 +153,7 @@ namespace Test_Merge
}
else
{
Wrapper.DisplayLiveRanking(pnlLiveRanking,this);
Wrapper.DisplayLiveRanking(pnlLiveRanking, this);
}
}
@@ -206,14 +211,16 @@ namespace Test_Merge
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (tbxResult.Text != "")
if (textBox1.Text != "")
GpUrl = textBox1.Text;
}
public void btnDriver_Click(object sender, EventArgs e)
{
//Happens when a driver button has been clicked
//MessageBox.Show((sender as Button).Name + " has been selected");
DriverData driver = Wrapper.GetFullDriverData((sender as Button).Name,pnlCurrentDriverLapsHistory);
Button btn = (sender as Button);
string[] parts = btn.Name.Split('_');
DriverData driver = Wrapper.GetFullDriverData(parts[0], pnlCurrentDriverLapsHistory,this);
lblCurrentDriverName.Text = driver.Name;
lblCurrentDriverPosition.Text = driver.Position.ToString();
lblCurrentDriverGapToLeader.Text = Reader.ConvertMsToTime(driver.GapToLeader);
@@ -234,10 +241,10 @@ namespace Test_Merge
case Tyre.Type.Undefined:
lblCurrentDriverTyreType.Text = "uuuuh...";
lblCurrentDriverTyreType.ForeColor = Color.Violet;
break;
break;
case Tyre.Type.Hard:
lblCurrentDriverTyreType.Text = "Hard";
lblCurrentDriverTyreType.ForeColor = Color.FromArgb(164,165,168);
lblCurrentDriverTyreType.ForeColor = Color.FromArgb(164, 165, 168);
break;
case Tyre.Type.Medium:
lblCurrentDriverTyreType.Text = "Medium";
@@ -257,5 +264,12 @@ namespace Test_Merge
break;
}
}
public void btnLapTime_Click(object sender, EventArgs e)
{
//Happens when a lapTime has been clicked
Button btn = sender as Button;
string[] parts = btn.Name.Split('_');
Wrapper.DisplayLapTimeInfos(parts[0], Convert.ToInt32(parts[1]),btn.Text);
}
}
}

View File

@@ -255,9 +255,12 @@ namespace Test_Merge
//Lap detection can be f***ed if the OCR takes so much time that an entire sector can be raced without us knowing.
if (
DriverDataLogs[i][DriverDataLogs[i].Count - 1].Sector3 != 0
&& DriverDataLogs[i][DriverDataLogs[i].Count - 2].Sector3 == 0)
&& DriverDataLogs[i][DriverDataLogs[i].Count - 2].Sector3 == 0
&& DriverDataLogs[i][DriverDataLogs[i].Count - 2].Position != -1
&& DriverDataLogs[i][DriverDataLogs[i].Count - 1].Position != -1)
{
DriverData stats = DriverDataLogs[i][DriverDataLogs[i].Count - 2];
DriverData stats = new DriverData();
stats = DriverDataLogs[i][DriverDataLogs[i].Count - 1];
DriverLaps[i]++;
Storage.AddDriverStat(stats, DriverLaps[i]);
}

View File

@@ -117,11 +117,36 @@ namespace Test_Merge
}
return result;
}
public List<int> GetSectorsFromLapTime(string driverName,int lap)
{
int driverId = GetDriverID(driverName);
string selectQuery = "SELECT Sector1,Sector2,Sector3 FROM Stats WHERE DriverID = @driverID AND Lap = @Lap";
List<int> result = new List<int>();
using (var command = new SQLiteCommand(selectQuery, Connection))
{
command.Parameters.AddWithValue("@driverID", driverId);
command.Parameters.AddWithValue("@lap", lap);
try
{
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int sectorTime = reader.GetInt32(0);
result.Add(sectorTime);
}
}
catch
{
//MessageBox.Show("There has been an error while trying to retrieve the ID of a Driver from the database");
}
}
return result;
}
public List<(int LapTime, int Lap)> GetDriverLaptimes(string driverName,int numberOfLaptimes)
{
int driverId = GetDriverID(driverName);
List<(int LapTime, int Lap)> lapData = new List<(int LapTime, int Lap)>();
string selectQuery = "Select LapTime,Lap from Stats WHERE DriverID = @driverID LIMIT @limit";
string selectQuery = "Select LapTime,Lap from Stats WHERE DriverID = @driverID ORDER BY Lap DESC LIMIT @limit";
using (var command = new SQLiteCommand(selectQuery, Connection))
{
command.Parameters.AddWithValue("@driverID", driverId);