public async Task<ClientDistance> GetDistanceToClient(string target) { int logdepth = 0; EdsmCoords targetcoords =new EdsmCoords(); ClientDistance cd = new ClientDistance(); EdsmCoords sourcecoords = FuelumCoords; EdsmSystem sourceSystem = new EdsmSystem(); sourceSystem.Name = "Fuelum"; sourceSystem.Coords = FuelumCoords; cd.SourceCertainty = "Fuelum"; if (_myTravelLog!=null) { foreach (TravelLog mysource in _myTravelLog.Reverse()) { if (mysource.System.Coords == null) { logdepth++; } else { Logger.Debug("Found TL system to use: " + mysource.System.Name); sourcecoords = mysource.System.Coords; if (logdepth == 0) { cd.SourceCertainty = "Exact"; sourceSystem = mysource.System; break; } else { cd.SourceCertainty = logdepth.ToString(); sourceSystem = mysource.System; break; } } } } IEnumerable<EdsmSystem> candidates = await QueryEdsmSystem(target); cd.TargetCertainty = "Exact"; if (!candidates.Any()) { Logger.Debug("EDSM does not know system '" + target + "'. Widening search..."); candidates = await GetCandidateSystems(target); cd.TargetCertainty = "Nearby"; } EdsmSystem firstOrDefault = candidates.FirstOrDefault(); if (firstOrDefault != null && firstOrDefault.Coords == null) { Logger.Debug("Known system '" + target + "', but no coords. Widening search..."); candidates = await GetCandidateSystems(target); cd.TargetCertainty = "Region"; } if (candidates == null || !candidates.Any()) { //Still couldn't find something, abort. AppendStatus("Couldn't find a candidate system, aborting..."); return new ClientDistance(); } Logger.Debug("We have two sets of coords that we can use to find a distance."); EdsmSystem edsmSystem = candidates.FirstOrDefault(); if (edsmSystem != null) targetcoords = edsmSystem.Coords; if(sourceSystem == null || sourceSystem.Name==null) { Logger.Debug("Err... Source system (or its name) is null, that shouldn't happen at this point. Bailing!"); return new ClientDistance(); } Logger.Debug("Finding from coords: " + sourcecoords.X + " " + sourcecoords.Y + " " + sourcecoords.Z + " ("+sourceSystem.Name+") to " + targetcoords.X + " " + targetcoords.Y + " " + targetcoords.Z+" ("+edsmSystem.Name+")"); double deltaX = sourcecoords.X - targetcoords.X; double deltaY = sourcecoords.Y - targetcoords.Y; double deltaZ = sourcecoords.Z - targetcoords.Z; double distance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ); Logger.Debug("Distance should be " + distance); cd.Distance = distance; return cd; }
public List<EdsmSystem> GetSystemAsEdsm(string systemname) { List<EdsmSystem> systemResult = new List<EdsmSystem>(); Status = "Working..."; using (FbCommand getSystem = con.CreateCommand()) { getSystem.CommandText = "SELECT FIRST 10 name,id,x,y,z FROM eddb_systems WHERE lowercase_name LIKE '%" + systemname.ToLower() + "%'"; using (FbDataReader r = getSystem.ExecuteReader()) { while (r.Read()) { EdsmSystem tmpsys = new EdsmSystem(); tmpsys.Coords = new EdsmCoords(); tmpsys.Name = r.GetString(0); tmpsys.Coords.X = r.GetDouble(r.GetOrdinal("X")); tmpsys.Coords.Y = r.GetDouble(r.GetOrdinal("Y")); tmpsys.Coords.Z = r.GetDouble(r.GetOrdinal("Z")); systemResult.Add(tmpsys); Logger.Debug("GetSystemEDSM added: " + r.GetString(0) + ": " + r.GetString(1) +" X: "+r.GetString(2)+" Y: "+ r.GetString(3)+" Z: "+r.GetString(4)); } } } Status = "Ready!"; return systemResult; }