private void RunTrilaterationWorker() { var systemsEntries = new Dictionary<SystemClass, Trilateration.Entry>(); for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var distanceCell = dataGridViewDistances[1, i]; if (systemCell.Tag == null || distanceCell.Value == null) { continue; } var system = (SystemClass)systemCell.Tag; var culture = new CultureInfo("en-US"); var distance = double.Parse(distanceCell.Value.ToString().Replace(",", "."), culture); var entry = new Trilateration.Entry(system.x, system.y, system.z, distance); systemsEntries.Add(system, entry); } if (systemsEntries.Count < 3) { return; } Invoke((MethodInvoker) delegate { LogText("Starting trilateration..." + Environment.NewLine); labelStatus.Text = "Calculating…"; labelStatus.BackColor = Color.Gold; }); var trilateration = new Trilateration {Logger = Console.WriteLine}; foreach (var item in systemsEntries) { trilateration.AddEntry(item.Value); } //var trilaterationResultCS = trilateration.RunCSharp(); var trilaterationAlgorithm = radioButtonAlgorithmJs.Checked ? Trilateration.Algorithm.RedWizzard_Emulated : Trilateration.Algorithm.RedWizzard_Native; var stopwatch = new Stopwatch(); stopwatch.Start(); var trilaterationResult = trilateration.Run(trilaterationAlgorithm); stopwatch.Stop(); var spentTimeString = (stopwatch.ElapsedMilliseconds / 1000.0).ToString("0.0000") + "ms"; lastTrilatelationResult = trilaterationResult; lastTrilatelationEntries = systemsEntries; if (trilaterationResult.State == Trilateration.ResultState.Exact) { Invoke((MethodInvoker) delegate { LogText("Trilateration successful (" + spentTimeString + "), exact coordinates found." + Environment.NewLine); //LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + Environment.NewLine); labelStatus.Text = "Success, coordinates found!"; labelStatus.BackColor = Color.LawnGreen; }); } else if (trilaterationResult.State == Trilateration.ResultState.NotExact || trilaterationResult.State == Trilateration.ResultState.MultipleSolutions) { Invoke((MethodInvoker) delegate { LogText("Trilateration not successful (" + spentTimeString + "), only approximate coordinates found." + Environment.NewLine); //LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + Environment.NewLine); LogText("Enter more distances." + Environment.NewLine); labelStatus.Text = "Enter More Distances"; labelStatus.BackColor = Color.Orange; }); } else if (trilaterationResult.State == Trilateration.ResultState.NeedMoreDistances) { Invoke((MethodInvoker) delegate { LogText("Trilateration not successful (" + spentTimeString + "), coordinates not found." + Environment.NewLine); LogText("Enter more distances." + Environment.NewLine); labelStatus.Text = "Enter More Distances"; labelStatus.BackColor = Color.Red; ClearCalculatedDataGridViewDistancesRows(); }); } // update trilaterated coordinates if (trilaterationResult.Coordinate != null) { Invoke((MethodInvoker) delegate { textBoxCoordinateX.Text = trilaterationResult.Coordinate.X.ToString(); textBoxCoordinateY.Text = trilaterationResult.Coordinate.Y.ToString(); textBoxCoordinateZ.Text = trilaterationResult.Coordinate.Z.ToString(); if (TargetSystem != null) { TargetSystem.x = trilaterationResult.Coordinate.X; TargetSystem.y = trilaterationResult.Coordinate.Y; TargetSystem.z = trilaterationResult.Coordinate.Z; } toolStripButtonMap.Enabled = (TargetSystem != null); }); var suggestedSystems = GetListOfSuggestedSystems(trilaterationResult.Coordinate.X, trilaterationResult.Coordinate.Y, trilaterationResult.Coordinate.Z, 10); Invoke((MethodInvoker) (() => PopulateSuggestedSystems(suggestedSystems))); } else { Invoke((MethodInvoker) delegate { textBoxCoordinateX.Text = "?"; textBoxCoordinateY.Text = "?"; textBoxCoordinateZ.Text = "?"; }); } var hasInvalidDistances = false; // update dataGrid with calculated distances and status var entriesDistances = trilaterationResult.EntriesDistances; for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var calculatedDistanceCell = dataGridViewDistances[2, i]; var statusCell = dataGridViewDistances[3, i]; calculatedDistanceCell.Value = null; statusCell.Value = null; if (entriesDistances == null || systemCell.Value == null || systemCell.Tag == null) { continue; } var system = (SystemClass) systemCell.Tag; if (!systemsEntries.ContainsKey(system)) // calculated without this system, so skip the row { continue; } var systemEntry = systemsEntries[system]; var calculatedDistance = entriesDistances[systemEntry]; calculatedDistanceCell.Value = calculatedDistance.ToString(); if (systemEntry.Distance == calculatedDistance) { calculatedDistanceCell.Style.ForeColor = Color.Green; statusCell.Value = "OK"; statusCell.Style.ForeColor = Color.Green; } else { hasInvalidDistances = true; calculatedDistanceCell.Style.ForeColor = Color.Salmon; statusCell.Value = "Wrong distance?"; statusCell.Style.ForeColor = Color.Salmon; } } //// Always enable submiot so we can submit a partial result. //Invoke((MethodInvoker) delegate //{ // //buttonSubmitToEDSC.Enabled = true; //trilaterationResult.State == Trilateration.ResultState.Exact && !hasInvalidDistances; //}); }
private void SubmitToEDSC() { var travelHistoryControl = _discoveryForm.TravelControl; string commanderName = travelHistoryControl.GetCommanderName(); if (string.IsNullOrEmpty(commanderName)) { MessageBox.Show("Please enter commander name before submitting the system!"); UnfreezeTrilaterationUI(); return; } var distances = new Dictionary<string, double>(); foreach (var item in lastTrilatelationEntries) { var system = item.Key; var entry = item.Value; distances.Add(system.name, entry.Distance); } var edsc = new EDSCClass(); var edsm = new EDSMClass(); //if (!EDSCClass.UseTest) //{ // Invoke((MethodInvoker) delegate // { // // TODO temporarily mess with EDSC in test mode only // LogText("Forcibly switching to EDSC UseTest mode." + Environment.NewLine, Color.OrangeRed); // }); // EDSCClass.UseTest = true; //} var responseC = edsc.SubmitDistances(commanderName, TargetSystem.name, distances); var responseM = edsm.SubmitDistances(commanderName, TargetSystem.name, distances); Console.WriteLine(responseC); Console.WriteLine(responseM); string infoC, infoM; bool trilaterationOkC; bool trilaterationOkM; var responseOkC = edsc.ShowDistanceResponse(responseC, out infoC); var responseOkM = edsm.ShowDistanceResponse(responseM, out infoM, out trilaterationOkM); trilaterationOkC = infoC.IndexOf("Trilateration succesful") != -1; // FIXME this is ugly Console.WriteLine(infoC); Invoke((MethodInvoker) delegate { if (responseOkC && trilaterationOkC) { LogText("EDSC submission succeeded, trilateration successful." + Environment.NewLine, Color.Green); } else if (responseOkC) { LogText("EDSC submission succeeded, but trilateration failed. Try adding more distances." + Environment.NewLine, Color.Orange); } else { LogText("EDSC submission failed." + Environment.NewLine, Color.Red); } if (responseOkM && trilaterationOkM) { LogText("EDSM submission succeeded, trilateration successful." + Environment.NewLine, Color.Green); } else if (responseOkM) { LogText("EDSM submission succeeded, but trilateration failed. Try adding more distances." + Environment.NewLine, Color.Orange); } else { LogText("EDSM submission failed." + Environment.NewLine, Color.Red); } }); if (responseOkC && trilaterationOkC) { Invoke((MethodInvoker) delegate { //Visible = false; travelHistoryControl.TriggerEDSCRefresh(); // TODO we might eventually avoid this by further parsing EDSC response travelHistoryControl.RefreshHistory(); }); } else { Invoke((MethodInvoker) UnfreezeTrilaterationUI); lastTrilatelationResult = null; lastTrilatelationEntries = null; } }
private void TrilaterationControl_VisibleChanged(object sender, EventArgs e) { if (Visible == true && TargetSystem != null) { textBoxSystemName.Text = TargetSystem.name; labelStatus.Text = "Enter Distances"; labelStatus.BackColor = Color.LightBlue; UnfreezeTrilaterationUI(); dataGridViewDistances.Focus(); PopulateSuggestedSystems(); PopulateClosestSystems(); } if (Visible == false) { if (trilaterationThread != null) { trilaterationThread.Abort(); trilaterationThread = null; } textBoxSystemName.Text = null; textBoxCoordinateX.Text = "?"; textBoxCoordinateY.Text = "?"; textBoxCoordinateZ.Text = "?"; labelLastKnownSystem.Text = "Unknown"; ClearDataGridViewDistancesRows(); ClearDataGridViewClosestSystemsRows(); ClearDataGridViewSuggestedSystemsRows(); lastTrilatelationResult = null; lastTrilatelationEntries = null; } }
private void SubmitToEDSC() { var travelHistoryControl = _discoveryForm.TravelControl; string commanderName = travelHistoryControl.GetCommanderName(); if (string.IsNullOrEmpty(commanderName)) { MessageBox.Show("Please enter commander name before submitting the system!"); UnfreezeTrilaterationUI(); return; } var distances = new Dictionary <string, double>(); foreach (var item in lastTrilatelationEntries) { var system = item.Key; var entry = item.Value; distances.Add(system.name, entry.Distance); } var edsc = new EDSCClass(); var edsm = new EDSMClass(); //if (!EDSCClass.UseTest) //{ // Invoke((MethodInvoker) delegate // { // // TODO temporarily mess with EDSC in test mode only // LogText("Forcibly switching to EDSC UseTest mode." + Environment.NewLine, Color.OrangeRed); // }); // EDSCClass.UseTest = true; //} var responseC = edsc.SubmitDistances(commanderName, TargetSystem.name, distances); var responseM = edsm.SubmitDistances(commanderName, TargetSystem.name, distances); Console.WriteLine(responseC); Console.WriteLine(responseM); string infoC, infoM; bool trilaterationOkC; bool trilaterationOkM; var responseOkC = edsc.ShowDistanceResponse(responseC, out infoC); var responseOkM = edsm.ShowDistanceResponse(responseM, out infoM, out trilaterationOkM); trilaterationOkC = infoC.IndexOf("Trilateration succesful") != -1; // FIXME this is ugly Console.WriteLine(infoC); Invoke((MethodInvoker) delegate { if (responseOkC && trilaterationOkC) { LogText("EDSC submission succeeded, trilateration successful." + Environment.NewLine, Color.Green); } else if (responseOkC) { LogText("EDSC submission succeeded, but trilateration failed. Try adding more distances." + Environment.NewLine, Color.Orange); } else { LogText("EDSC submission failed." + Environment.NewLine, Color.Red); } if (responseOkM && trilaterationOkM) { LogText("EDSM submission succeeded, trilateration successful." + Environment.NewLine, Color.Green); } else if (responseOkM) { LogText("EDSM submission succeeded, but trilateration failed. Try adding more distances." + Environment.NewLine, Color.Orange); } else { LogText("EDSM submission failed." + Environment.NewLine, Color.Red); } }); if (responseOkC && trilaterationOkC) { Invoke((MethodInvoker) delegate { //Visible = false; travelHistoryControl.TriggerEDSCRefresh(); // TODO we might eventually avoid this by further parsing EDSC response travelHistoryControl.RefreshHistory(); }); } else { Invoke((MethodInvoker)UnfreezeTrilaterationUI); lastTrilatelationResult = null; lastTrilatelationEntries = null; } }
private void RunTrilaterationWorker() { var systemsEntries = new Dictionary <SystemClass, Trilateration.Entry>(); for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var distanceCell = dataGridViewDistances[1, i]; if (systemCell.Tag == null || distanceCell.Value == null) { continue; } var system = (SystemClass)systemCell.Tag; var culture = new CultureInfo("en-US"); var distance = double.Parse(distanceCell.Value.ToString().Replace(",", "."), culture); var entry = new Trilateration.Entry(system.x, system.y, system.z, distance); systemsEntries.Add(system, entry); } if (systemsEntries.Count < 3) { return; } Invoke((MethodInvoker) delegate { LogText("Starting trilateration..." + Environment.NewLine); labelStatus.Text = "Calculating…"; labelStatus.BackColor = Color.Gold; }); var trilateration = new Trilateration { Logger = Console.WriteLine }; foreach (var item in systemsEntries) { trilateration.AddEntry(item.Value); } //var trilaterationResultCS = trilateration.RunCSharp(); var trilaterationAlgorithm = radioButtonAlgorithmJs.Checked ? Trilateration.Algorithm.RedWizzard_Emulated : Trilateration.Algorithm.RedWizzard_Native; var stopwatch = new Stopwatch(); stopwatch.Start(); var trilaterationResult = trilateration.Run(trilaterationAlgorithm); stopwatch.Stop(); var spentTimeString = (stopwatch.ElapsedMilliseconds / 1000.0).ToString("0.0000") + "ms"; lastTrilatelationResult = trilaterationResult; lastTrilatelationEntries = systemsEntries; if (trilaterationResult.State == Trilateration.ResultState.Exact) { Invoke((MethodInvoker) delegate { LogText("Trilateration successful (" + spentTimeString + "), exact coordinates found." + Environment.NewLine); //LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + Environment.NewLine); labelStatus.Text = "Success, coordinates found!"; labelStatus.BackColor = Color.LawnGreen; }); } else if (trilaterationResult.State == Trilateration.ResultState.NotExact || trilaterationResult.State == Trilateration.ResultState.MultipleSolutions) { Invoke((MethodInvoker) delegate { LogText("Trilateration not successful (" + spentTimeString + "), only approximate coordinates found." + Environment.NewLine); //LogText("x=" + trilaterationResult.Coordinate.X + ", y=" + trilaterationResult.Coordinate.Y + ", z=" + trilaterationResult.Coordinate.Z + Environment.NewLine); LogText("Enter more distances." + Environment.NewLine); labelStatus.Text = "Enter More Distances"; labelStatus.BackColor = Color.Orange; }); } else if (trilaterationResult.State == Trilateration.ResultState.NeedMoreDistances) { Invoke((MethodInvoker) delegate { LogText("Trilateration not successful (" + spentTimeString + "), coordinates not found." + Environment.NewLine); LogText("Enter more distances." + Environment.NewLine); labelStatus.Text = "Enter More Distances"; labelStatus.BackColor = Color.Red; ClearCalculatedDataGridViewDistancesRows(); }); } // update trilaterated coordinates if (trilaterationResult.Coordinate != null) { Invoke((MethodInvoker) delegate { textBoxCoordinateX.Text = trilaterationResult.Coordinate.X.ToString(); textBoxCoordinateY.Text = trilaterationResult.Coordinate.Y.ToString(); textBoxCoordinateZ.Text = trilaterationResult.Coordinate.Z.ToString(); if (TargetSystem != null) { TargetSystem.x = trilaterationResult.Coordinate.X; TargetSystem.y = trilaterationResult.Coordinate.Y; TargetSystem.z = trilaterationResult.Coordinate.Z; } toolStripButtonMap.Enabled = (TargetSystem != null); }); var suggestedSystems = GetListOfSuggestedSystems(trilaterationResult.Coordinate.X, trilaterationResult.Coordinate.Y, trilaterationResult.Coordinate.Z, 10); Invoke((MethodInvoker)(() => PopulateSuggestedSystems(suggestedSystems))); } else { Invoke((MethodInvoker) delegate { textBoxCoordinateX.Text = "?"; textBoxCoordinateY.Text = "?"; textBoxCoordinateZ.Text = "?"; }); } var hasInvalidDistances = false; // update dataGrid with calculated distances and status var entriesDistances = trilaterationResult.EntriesDistances; for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var calculatedDistanceCell = dataGridViewDistances[2, i]; var statusCell = dataGridViewDistances[3, i]; calculatedDistanceCell.Value = null; statusCell.Value = null; if (entriesDistances == null || systemCell.Value == null || systemCell.Tag == null) { continue; } var system = (SystemClass)systemCell.Tag; if (!systemsEntries.ContainsKey(system)) // calculated without this system, so skip the row { continue; } var systemEntry = systemsEntries[system]; var calculatedDistance = entriesDistances[systemEntry]; calculatedDistanceCell.Value = calculatedDistance.ToString(); if (systemEntry.Distance == calculatedDistance) { calculatedDistanceCell.Style.ForeColor = Color.Green; statusCell.Value = "OK"; statusCell.Style.ForeColor = Color.Green; } else { hasInvalidDistances = true; calculatedDistanceCell.Style.ForeColor = Color.Salmon; statusCell.Value = "Wrong distance?"; statusCell.Style.ForeColor = Color.Salmon; } } //// Always enable submiot so we can submit a partial result. //Invoke((MethodInvoker) delegate //{ // //buttonSubmitToEDSC.Enabled = true; //trilaterationResult.State == Trilateration.ResultState.Exact && !hasInvalidDistances; //}); }
private void SubmitToEDSM() { edsm.apiKey = EDDiscoveryForm.EDDConfig.CurrentCommander.APIKey; edsm.commanderName = EDDiscoveryForm.EDDConfig.CurrentCommander.Name; var travelHistoryControl = _discoveryForm.TravelControl; if (string.IsNullOrEmpty(edsm.commanderName)) { string commanderName = travelHistoryControl.GetCommanderName(); if (string.IsNullOrEmpty(commanderName)) { MessageBox.Show("Please enter commander name before submitting the system!"); UnfreezeTrilaterationUI(); return; } edsm.commanderName = commanderName; } var distances = new Dictionary <string, double>(); var culture = new CultureInfo("en-US"); for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var distanceCell = dataGridViewDistances[1, i]; if (systemCell.Value != null && distanceCell.Value != null) { var system = systemCell.Value.ToString(); var distance = double.Parse(distanceCell.Value.ToString().Replace(",", "."), culture); // can over-ride drop down now if it's a real system so you could add duplicates if you wanted (even once I've figured out issue #81 which makes it easy if not likely...) if (!distances.Keys.Contains(system)) { distances.Add(system, distance); } } } var responseM = edsm.SubmitDistances(edsm.commanderName, TargetSystem.name, distances); Console.WriteLine(responseM); string infoM; bool trilaterationOkM; var responseOkM = edsm.ShowDistanceResponse(responseM, out infoM, out trilaterationOkM); Console.WriteLine(infoM); Invoke((MethodInvoker) delegate { if (responseOkM && trilaterationOkM) { LogText("EDSM submission succeeded, trilateration successful." + Environment.NewLine, Color.Green); } else if (responseOkM) { LogText("EDSM submission succeeded, but trilateration failed. Try adding more distances." + Environment.NewLine, Color.Orange); } else { LogText("EDSM submission failed." + Environment.NewLine, Color.Red); } }); if (responseOkM && trilaterationOkM) { Invoke((MethodInvoker) delegate { //Visible = false; UnfreezeTrilaterationUI(); travelHistoryControl.TriggerEDSMRefresh(); // TODO we might eventually avoid this by further parsing EDSC response travelHistoryControl.RefreshHistory(); checkForUnknownSystemsNowKnown(); }); } else { Invoke((MethodInvoker)UnfreezeTrilaterationUI); lastTrilatelationResult = null; lastTrilatelationEntries = null; } }
private void SubmitToEDSM() { var travelHistoryControl = _discoveryForm.TravelControl; if (string.IsNullOrEmpty(edsm.commanderName)) { string commanderName = travelHistoryControl.GetCommanderName(); if (string.IsNullOrEmpty(commanderName)) { MessageBox.Show("Please enter commander name before submitting the system!"); UnfreezeTrilaterationUI(); return; } edsm.commanderName = commanderName; } var distances = new Dictionary<string, double>(); var culture = new CultureInfo("en-US"); for (int i = 0, count = dataGridViewDistances.Rows.Count - 1; i < count; i++) { var systemCell = dataGridViewDistances[0, i]; var distanceCell = dataGridViewDistances[1, i]; if (systemCell.Value != null && distanceCell.Value != null) { var system = systemCell.Value.ToString(); var distance = double.Parse(distanceCell.Value.ToString().Replace(",", "."), culture); // can over-ride drop down now if it's a real system so you could add duplicates if you wanted (even once I've figured out issue #81 which makes it easy if not likely...) if (!distances.Keys.Contains(system)) { distances.Add(system, distance); } } } var responseM = edsm.SubmitDistances(edsm.commanderName, TargetSystem.name, distances); Console.WriteLine(responseM); string infoM; bool trilaterationOkM; var responseOkM = edsm.ShowDistanceResponse(responseM, out infoM, out trilaterationOkM); Console.WriteLine(infoM); Invoke((MethodInvoker) delegate { if (responseOkM && trilaterationOkM) { LogText("EDSM submission succeeded, trilateration successful." + Environment.NewLine, Color.Green); } else if (responseOkM) { LogText("EDSM submission succeeded, but trilateration failed. Try adding more distances." + Environment.NewLine, Color.Orange); } else { LogText("EDSM submission failed." + Environment.NewLine, Color.Red); } }); if (responseOkM && trilaterationOkM) { Invoke((MethodInvoker) delegate { //Visible = false; UnfreezeTrilaterationUI(); travelHistoryControl.TriggerEDSMRefresh(); // TODO we might eventually avoid this by further parsing EDSC response travelHistoryControl.RefreshHistory(); checkForUnknownSystemsNowKnown(); }); } else { Invoke((MethodInvoker) UnfreezeTrilaterationUI); lastTrilatelationResult = null; lastTrilatelationEntries = null; } }