private void _onBodiesReceived(SpanshBodiesResult obj) { m_loading.isVisible = false; displayMessage("Search Succeeded." + obj.count + " bodies found", EDColors.GREEN); string material_name = ""; if (m_LastSearch.SearchBodies.filters.materials != null && m_LastSearch.SearchBodies.filters.materials.Length > 0) { material_name = m_LastSearch.SearchBodies.filters.materials[0].name; } for (int i = 0; i < m_results.Length; i++) { if (i < obj.results.Length) { m_results[i].isVisible = true; m_results[i].SetDatas(obj, material_name, i - 1); } else { m_results[i].isVisible = false; } } MoveCursorToFirst(); }
// ===> Moved to Search engine public static SpanshBodiesResult SpanshBodies(string _systemOrigin, int _maxDistance, string[] _materials, bool _isLandable = true) { _maxDistance = Math.Min(_maxDistance, 100); Dictionary <string, string> _sParams = new Dictionary <string, string>(); SpanshValue <double>[] _spanshMats = new SpanshValue <double> [_materials.Length]; for (int i = 0; i < _materials.Length; i++) { _spanshMats[i] = new SpanshValue <double>(_materials[i], ">", 0); } SpanshSortValue[] _spanshMatsSort = new SpanshSortValue[_materials.Length]; for (int i = 0; i < _materials.Length; i++) { _spanshMatsSort[i] = new SpanshSortValue(_materials[i].ToString(), false); } SpanshSearchBodies p = new SpanshSearchBodies() { size = 11, reference_system = _systemOrigin, filters = new SpanshFilterBodies() { distance_from_coords = new SpanshValue <int>() { max = _maxDistance }, materials = _spanshMats, is_landable = new SpanshValue <bool>(_isLandable) }, sort = new SpanshSort[] { new SpanshSort() { materials = _spanshMatsSort, }, new SpanshSort() { distance_from_coords = new SpanshSortValue(true) } } }; string _spanshJson = JsonConvert.SerializeObject(p, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore });; Console.WriteLine(_spanshJson); string json = requestPOSTFromURL(Url_SpanshBodies, _spanshJson); try { SpanshBodiesResult result = JsonConvert.DeserializeObject <SpanshBodiesResult>(json); Console.WriteLine("Result: {0} RealCount: {1}", result.count, result.results.Length); Console.WriteLine("Search ID: " + result.search_reference); return(result); } catch (Exception ex) { NexHudEngine.Log(ex.Message); NexHudEngine.Log(json); } return(null); }
private static void _onBodiesFounded(SpanshBodiesResult obj) { Console.WriteLine("-> _onBodiesFounded: {0}", obj?.search_reference); }
public void SetDatas(SpanshBodiesResult result, string material_name, int index = -1) { m_background.width = width; m_background.height = height; isSelectable = index != -1; isEnable = index != -1; Selected = false; LastSystem = null; LastBody = null; //datas //Visibilitys for (int i = 0; i < m_props.Length; i++) { if (i < 6) { m_props[i].isVisible = true; m_separators[i].isVisible = true; } else { m_props[i].isVisible = false; m_separators[i].isVisible = false; } } if (index == -1) { m_props[0].text = "Dist."; m_props[1].text = "System name"; m_props[2].text = "Body"; m_props[3].text = "Dist. to arrival"; m_props[4].text = "Gravity"; m_props[5].text = material_name; //??? Nom material } else if (index < result.count) { LastBody = result.results[index]; //distance from system to system m_props[0].text = Math.Round((double)result.results[index].distance, 1).ToString(); //System name m_props[1].text = result.results[index].system_name; //Body m_props[2].text = result.results[index].name; //Distance to arrival m_props[3].text = Math.Round((double)result.results[index].distance_to_arrival, 0).ToString(); //Gravity m_props[4].text = Math.Round((double)result.results[index].gravity, 2).ToString(); //Material foreach (SpanshMaterial mat in result.results[index].materials) { if (mat.name.ToLower() == material_name.ToLower()) { m_props[5].text = Math.Round((double)mat.share, 2).ToString() + " %"; } } } /// m_registerWidth = true; m_background.Color = isEnable ? Color.Orange : Color.White; }
private void _startResearch(uint _id) { Stopwatch _watch = new Stopwatch(); _watch.Start(); int _statAddedSystem = 0; int _statUpdatedSystem = 0; if (!m_lastUSRs.ContainsKey(_id)) { return; } //Check if search is still valid if (m_lastUSRs[_id].searchID != m_lastSearchId) { m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.Aborted; m_lastUSRs[_id].isDone = true; return; } try { if (!getCurrentSystem().receivedEdsmBasics) { m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.CurrentSystemNotCompleted; } else if (m_lastUSRs[_id].entry.searchType == NxSearchType.system) { //If names are specified we will ignore other parameters if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.name)) { string[] _names = m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.name]; string[] _namesNotes = new string[0]; if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.nameNotes)) { _namesNotes = m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.nameNotes]; } for (int i = 0; i < _names.Length; i++) { _names[i] = _names[i].Trim(); } for (int i = 0; i < _names.Length; i++) { //Can be optimized with https://www.edsm.net/api-v1/systems EDSMSystemDatas _edsmDatas = ApiConnection.EDSMSystemFullInfos(_names[i]); if (_edsmDatas != null) { EDSystem _system = new EDSystem(); _system.updateEDSM(_edsmDatas); NexHudEngine.Log("(EDSM) Found system: " + _system.name); m_lastUSRs[_id].addSystem(_system); if (i < _namesNotes.Length) { _system.Notes = _namesNotes[i]; } } } } else { //Get systems in radius int _radius = m_greatestSearchRadius; int _startRardius = 40; if (m_lastUSRs[_id].entry.searchMaxRadius > 0 && m_lastUSRs[_id].entry.searchMaxRadius < _startRardius) { _startRardius = m_lastUSRs[_id].entry.searchMaxRadius; } int _maxEdsmRadius = 100; if (m_lastUSRs[_id].entry.searchMaxRadius > _startRardius && m_lastUSRs[_id].entry.searchMaxRadius < _maxEdsmRadius) { _maxEdsmRadius = m_lastUSRs[_id].entry.searchMaxRadius; } int[] _increments = new int[] { //Threshold,increment 60, 20, 90, 10, 100, 5 }; int _pass = 1; if (_radius < _startRardius) { _radius = _startRardius; } int _radiusMinimum = 0; while (m_lastUSRs[_id].Count < UiSearch.MAX_LINE_RESULT && _radius <= _maxEdsmRadius) { m_lastUSRs[_id].ResearchTime = _watch.ElapsedMilliseconds; m_lastUSRs[_id].CurrentPass = _pass; m_lastUSRs[_id].CurrentRadius = _radius; //Building list if (_radius > m_greatestSearchRadius) { EDSMSystemDatas[] _systemsInSphereRadius = ApiConnection.EDSMSystemsInSphereRadius(getCurrentSystem().name, m_greatestSearchRadius, _radius); if (_systemsInSphereRadius != null) { m_greatestSearchRadius = _radius; for (int i = 0; i < _systemsInSphereRadius.Length; i++) { if (!m_systemAround.Contains(_systemsInSphereRadius[i].name)) { m_systemAround.Add(_systemsInSphereRadius[i].name); } if (!m_systems.ContainsKey(_systemsInSphereRadius[i].name)) { EDSystem _newSystem = new EDSystem(); _newSystem.updateEDSM(_systemsInSphereRadius[i]); _statAddedSystem++; m_systems.Add(_systemsInSphereRadius[i].name, _newSystem); } } } } List <EDSystem> _systemInRadius = new List <EDSystem>(); //Recalculate distance and updating basics... foreach (string _sys in m_systemAround) { if (!m_systems.ContainsKey(_sys)) { _statAddedSystem++; m_systems.Add(_sys, new EDSystem()); } if (!m_systems[_sys].receivedEdsmInfos) { NexHudEngine.Log("//WARNING// system {0} has not received informations datas. Retrieving...", _sys); m_systems[_sys].updateEDSM(ApiConnection.EDSMSystem(new ApiConnection.EDSMSystemParameters() { name = _sys, showInformation = true })); m_systems[_sys].calculDistanceFromCurrent(); } if (m_systems[_sys].distanceFromCurrentSystem < _radius && m_systems[_sys].distanceFromCurrentSystem >= _radiusMinimum) { _systemInRadius.Add(m_systems[_sys]); } } IEnumerable <EDSystem> _ordered = _systemInRadius.OrderBy(s => s.distanceFromCurrentSystem); //Allegiance if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.allegiance)) { _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.allegiance].Any(y => x.allegiance.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase))); } //Government if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.government)) { _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.government].Any(y => x.government.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase))); } //Primary economy if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.economy)) { _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.economy].Any(y => x.economy.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase))); } //Reserve if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.reserve)) { _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.reserve].Any(y => x.reserve.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase))); } //Security if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.security)) { _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.security].Any(y => x.security.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase))); } //State if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.state)) { _ordered = _ordered.Where(x => m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.state].Any(y => x.state.ToStringFormated().Equals(y, StringComparison.InvariantCultureIgnoreCase))); } NexHudEngine.Log("+ Search total result: {0} . Pass {1}", _ordered.Count(), _pass); m_lastUSRs[_id].ResearchTime = _watch.ElapsedMilliseconds; foreach (EDSystem x in _ordered) { m_lastUSRs[_id].addSystem(x); if (m_lastUSRs[_id].Count >= UiSearch.MAX_LINE_RESULT) { break; } } _radiusMinimum = _radius; for (int _inc = 0; _inc < _increments.Length; _inc += 2) { if (_radius < _increments[_inc]) { _radius += _increments[_inc + 1]; break; } } if (_radius >= _maxEdsmRadius) { break; } _pass++; } } } else if (m_lastUSRs[_id].entry.searchType == NxSearchType.body) { List <string> _listMaterials = new List <string>(); //Make sure the materials are valid if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.rawMaterial)) { foreach (string _material in m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.rawMaterial]) { if (EngineerHelper.isRawMaterial(_material)) { _listMaterials.Add(_material); } } } bool isLandable = true; if (m_lastUSRs[_id].entry.searchParamsFormated.ContainsKey(NxSearchParam.isLandable)) { isLandable = m_lastUSRs[_id].entry.searchParamsFormated[NxSearchParam.isLandable][0] == "true"; } if (_listMaterials.Count > 0) { int maxDistance = 20; if (m_lastUSRs[_id].entry.searchMaxRadius > 0) { maxDistance = Math.Min(m_lastUSRs[_id].entry.searchMaxRadius, 100); } SpanshBodiesResult _spanshResult = ApiConnection.SpanshBodies(getCurrentSystem().name, maxDistance, _listMaterials.ToArray(), isLandable); //Get infos about the targeted systems List <string> _systemWithBodies = new List <string>(); foreach (SpanshBody b in _spanshResult.results) { if (!_systemWithBodies.Contains(b.system_name)) { _systemWithBodies.Add(b.system_name); } } List <string> _systemToUpdate = new List <string>(); //Clean list for system we already received infos foreach (string s in _systemWithBodies) { if (m_systems.ContainsKey(s)) { if (!m_systems[s].receivedEdsmBasics && !m_systems[s].receivedEdsmInfos) { _systemToUpdate.Add(s); } } else { m_systemAround.Add(s); m_systems.Add(s, new EDSystem()); _systemToUpdate.Add(s); } } if (_systemToUpdate.Count > 0) { EDSMSystemDatas[] _edsmDatas = ApiConnection.EDSMSystemsList(_systemToUpdate.ToArray(), true); foreach (EDSMSystemDatas _data in _edsmDatas) { m_systems[_data.name].updateEDSM(_data); } } foreach (SpanshBody b in _spanshResult.results) { m_lastUSRs[_id].addBody(m_systems[b.system_name].addOrUpdateBody(b)); } } } } catch (Exception ex) { if (ex.InnerException is ThreadAbortException) { m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.Aborted; NexHudEngine.Log("Aborting last search thread {0}. Last Id {1}", Thread.CurrentThread.Name, _id); } else { m_lastUSRs[_id].Error = UserSearchResult.UserSearchError.UnknowError; NexHudEngine.Log("ERROR: Search {0} reported an error: {1}", _id, ex.Message); } } finally { _watch.Stop(); NexHudEngine.Log("Search was done in {0}ms. Error:{1} Added:{2} Updated:{3}", _watch.ElapsedMilliseconds, m_lastUSRs[_id].Error, _statAddedSystem, _statUpdatedSystem); m_lastUSRs[_id].isDone = true; m_lastUSRs[_id].ResearchTime = _watch.ElapsedMilliseconds; } }