public void updateEDSM(EDSMSystemDatas _datas) { if (_datas == null) { return; } name = _datas.name; if (_datas.coords != null) { x = _datas.coords.x; y = _datas.coords.y; z = _datas.coords.z; } if (_datas.distance != null) { m_distanceFromCurrentSystem = (double)_datas.distance; } m_receivedEdsmBasics = true; if (_datas.requirePermit != null) { needsPermit = (bool)_datas.requirePermit; m_receivedEdsmPermit = true; } if (_datas.information != null) { if (_datas.information.allegiance != null) { Enum.TryParse(_datas.information.allegiance.Replace(" ", string.Empty), true, out allegiance); } if (_datas.information.government != null) { Enum.TryParse(_datas.information.government.Replace(" ", string.Empty), true, out government); } controlling_minor_faction = _datas.information.faction; if (_datas.information.factionState != null) { Enum.TryParse(_datas.information.factionState.Replace(" ", string.Empty), true, out state); } if (_datas.information.population != null) { population = (long)_datas.information.population; } if (_datas.information.security != null) { Enum.TryParse(_datas.information.security.Replace(" ", string.Empty), out security); } if (_datas.information.economy != null) { Enum.TryParse(_datas.information.economy.Replace(" ", string.Empty), true, out economy); } if (_datas.information.secondEconomy != null) { Enum.TryParse(_datas.information.secondEconomy.Replace(" ", string.Empty), true, out secondEconomy); } if (_datas.information.reserve != null) { Enum.TryParse(_datas.information.reserve.Replace(" ", string.Empty), true, out reserve); } m_receivedEdsmInfos = true; } if (_datas.primaryStar != null) { if (_datas.primaryStar.name.Length > 0) { star_name = _datas.primaryStar.name; } if (_datas.primaryStar.type.Length > 0) { star_type = _datas.primaryStar.type; } if (_datas.primaryStar.isScoopable != null) { star_isScoopable = (bool)_datas.primaryStar.isScoopable; } m_receivedEdsmPrimaryStar = true; } if (_datas.valuableBodies != null) { valuableBodies = _datas.valuableBodies; if (_datas.valuableBodies != null) { value = (uint)_datas.estimatedValue; } if (_datas.estimatedValueMapped != null) { valueMapped = (uint)_datas.estimatedValueMapped; } m_receivedEdsmEstValue = true; } }
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; } }