Skip to content

Commit

Permalink
adding database and log feature
Browse files Browse the repository at this point in the history
  • Loading branch information
my-th-os committed Oct 17, 2024
1 parent 2407162 commit 42bad69
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 10 deletions.
62 changes: 61 additions & 1 deletion MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 131 additions & 9 deletions MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace SEGPS
Expand All @@ -10,6 +11,10 @@ namespace SEGPS
public partial class MainWindow : Form
{
private List<Planet> planets = new List<Planet>();
private List<Gps> database = new List<Gps>();
private string databasePath = Path.ChangeExtension(Application.ExecutablePath, ".txt");
private List<String> log = new List<String>();
private string logPath = Path.ChangeExtension(Application.ExecutablePath, ".log");
private bool textIsChanging = false;

private string clipBoardContent;
Expand Down Expand Up @@ -37,6 +42,9 @@ Name Distance Diameter G-radius GPS
planets.Add(new Planet(new Gps("GPS:Alien:131072.50:131072.50:5731072.50:#FF75C9F1:"), 105000));
planets.Add(new Planet(new Gps("GPS:Titan:36384.50:226384.50:5796384.50:#FF75C9F1:"), 13000));

DatabaseLoad();
LogLoad();

timerClipboard.Enabled = true;
}

Expand All @@ -62,7 +70,10 @@ private void Clipboard_Changed(object sender, string content)
{
Gps gps = new Gps(content);
if (gps.IsValid)
{
LogAppend(gps);
Gps_Changed(sender, gps);
}
}

private void SetTextBox(object sender, TextBox tb, string text)
Expand Down Expand Up @@ -94,16 +105,16 @@ private void Gps_Changed(object sender, Gps gps)

tbLength.Text = (gps.Length() / 1000).ToString("0.00", Gps.NumberFormat); // [km]

Planet closest = GetClosest(gps);
if (closest != null)
Planet closestPlanet = GetClosestPlanet(gps);
if (closestPlanet != null)
{
Gps center = closest.Item1;
Gps center = closestPlanet.Item1;
tbAbove.Text = center.Name;

float height = center.Distance(gps);
tbHeight.Text = (height / 1000).ToString("0.00", Gps.NumberFormat); // [km]

float gravity = closest.Item2;
float gravity = closestPlanet.Item2;
tbGravity.Text = (gravity / 1000).ToString("0.00", Gps.NumberFormat); // [km]
if (height > 1 && gravity - height > 1)
{
Expand All @@ -123,10 +134,83 @@ private void Gps_Changed(object sender, Gps gps)
tbRefDist.Text = (distance / 1000).ToString("0.00", Gps.NumberFormat); // [km]
}

Gps closestDatabase = GetClosestDatabase(gps);
if (closestDatabase != null)
{
float distance = closestDatabase.Distance(gps);
tbDb.Text = closestDatabase.ToString();
tbDbDist.Text = (distance / 1000).ToString("0.00", Gps.NumberFormat); // [km]
}

textIsChanging = false;
}

private Planet GetClosest(Gps gps)
private void DatabaseLoad()
{
database.Clear();
if (File.Exists(databasePath))
{
foreach (var line in File.ReadAllLines(databasePath))
{
database.Add(new Gps(line));
}
}
}

private void DatabaseWrite()
{
File.WriteAllLines(databasePath, database.ConvertAll(gps => gps.ToString()));
}

private void DatabaseUpdate(Gps gps)
{
bool found = false;
for (int i = 0; i < database.Count; i++)
{
if (gps.Distance(database[i]) < 10) // [m]
{
database[i] = gps;
found = true;
break;
}
}
if (!found)
{
database.Add(gps);
}
DatabaseWrite();
}

private void LogLoad()
{
log.Clear();
if (File.Exists(logPath))
{
foreach (var line in File.ReadAllLines(logPath))
{
log.Add(line);
}
}
}

private void LogAppend(Gps gps)
{
var line = gps.ToString();
// only log unseen coords
foreach (var entry in log)
{
if (entry == line)
{
return;
}
}
using (var writer = File.AppendText(logPath))
{
writer.WriteLine(line);
}
}

private Planet GetClosestPlanet(Gps gps)
{
Planet closest = null;
float min = float.PositiveInfinity;
Expand All @@ -143,6 +227,23 @@ private Planet GetClosest(Gps gps)
return closest;
}

private Gps GetClosestDatabase(Gps gps)
{
Gps closest = null;
float min = float.PositiveInfinity;

foreach (var entry in database)
{
float d = gps.Distance(entry);
if (d < min)
{
closest = entry;
min = d;
}
}
return closest;
}

private void tbGPS_TextChanged(object sender, EventArgs e)
{
Gps gps = new Gps(tbGPS.Text);
Expand All @@ -166,16 +267,37 @@ private void tbGPS_DoubleClick(object sender, EventArgs e)

private void tbJumpGPS_DoubleClick(object sender, EventArgs e)
{
tbJumpGPS.Select(0, tbJumpGPS.Text.Length);
// the JumpGPS coord will become new GPS coord, when clipboard change is detected
// could be prevented by: clipBoardContent = tbJumpGPS.Text;
Clipboard.SetText(tbJumpGPS.Text, TextDataFormat.UnicodeText);
if (tbJumpGPS.Text.Length > 0)
{
tbJumpGPS.Select(0, tbJumpGPS.Text.Length);
// the JumpGPS coord will become new GPS coord, when clipboard change is detected
// could be prevented by: clipBoardContent = tbJumpGPS.Text;
Clipboard.SetText(tbJumpGPS.Text, TextDataFormat.UnicodeText);
}
}

private void btRef_Click(object sender, EventArgs e)
{
tbRef.Text = tbGPS.Text;
tbRefDist.Text = "0.00";
}

private void btDb_Click(object sender, EventArgs e)
{
tbDb.Text = tbGPS.Text;
tbDbDist.Text = "0.00";
DatabaseUpdate(new Gps(tbGPS.Text));
}

private void tbDb_DoubleClick(object sender, EventArgs e)
{
if (tbDb.Text.Length > 0)
{
tbDb.Select(0, tbDb.Text.Length);
// the database coord will become new GPS coord, when clipboard change is detected
// could be prevented by: clipBoardContent = tbDb.Text;
Clipboard.SetText(tbDb.Text, TextDataFormat.UnicodeText);
}
}
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ Besides the individual components SEGPS will also show you:
If the coords are within gravity, SEGPS will calculate a "Jump GPS" approach vector just outside the gravity but straight above your target. Double click here to load this into clipboard. In-game use "new from clipboard" and search for the prefix "->".

Once a coord is loaded, you can use that as a reference point by pressing the "<" button next to the reference field. For any future coords, SEGPS will calculate the distance from there to the reference.

Coords can be added to the database with the "<" button next to the database field. This will show the nearest database entry and distance to there. Adding the same x, y, z will update text and color of the existing entry instead of adding a new one.

The database is located in SEGPS.txt next to the executable. There is also a log SEGPS.log that contains any seen coords so far, just in case you lost some.
1 change: 1 addition & 0 deletions SEGPS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Version>1.1.0</Version>
</PropertyGroup>

</Project>
Binary file modified doc/screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 42bad69

Please sign in to comment.