Ejemplo n.º 1
0
        int CellFromMousePos(Point mouse, out int xly, out int zly)
        {
            Rectangle imageviewport = GetImageViewPort();
            int       mx            = mouse.X - imageviewport.X;
            int       my            = mouse.Y - imageviewport.Y;

            xly = zly = 0;

            if (mx >= 0 && mx < imageviewport.Width && my >= 0 && my <= imageviewport.Height)
            {
                float fractx = (float)mx / imageviewport.Width;
                float fracty = (float)my / imageviewport.Height;

                Rectangle imagesourceregion = GetSourceImageRegion();                                                 // This is what we are displaying
                Point     lypostop          = galaxy.LYPos(new Point(imagesourceregion.Left, imagesourceregion.Top)); // convert the top of what displayed to lys.

                int lywidth  = imagesourceregion.Width * galaxy.LYWidth / galaxy.PixelWidth;                          // work out the width of the displayed in ly.
                int lyheight = imagesourceregion.Height * galaxy.LYHeight / galaxy.PixelHeight;

                xly = (int)(lypostop.X + fractx * lywidth);
                zly = (int)(lypostop.Y - fracty * lyheight);

                int id = GridId.Id(xly, zly);

                // System.Diagnostics.Debug.WriteLine("ID {0} {1} {2}" ,id, xly, zly);

                return(id);
            }
            else
            {
                return(-1);
            }
        }
Ejemplo n.º 2
0
        public void Initialise()
        {
            for (int z = 0; z < GridId.GridZRange; z++)
            {
                for (int x = 0; x < GridId.GridXRange; x++)
                {
                    int   id = GridId.IdFromComponents(x, z);
                    float xp = 0, zp = 0;
                    bool  ok = GridId.XZ(id, out xp, out zp);
                    Debug.Assert(ok);
                    StarGrid grd = new StarGrid(id, xp, zp, Color.Transparent, 1.0F); //A=0 means use default colour array
                    if (xp == 0 && zp == 0)                                           // sol grid, unpopulated stars please
                    {
                        grd.dBAsk = SystemsDB.SystemAskType.UnpopulatedStars;
                    }

                    grids.Add(grd);
                }
            }

            systemlistgrid = new StarGrid(-1, 0, 0, Color.Orange, 1.0F);    // grid ID -1 means it won't be filled by the Update task
            grids.Add(systemlistgrid);

            int solid = GridId.Id(0, 0);

            populatedgrid       = new StarGrid(solid, 0, 0, Color.Transparent, 1.0F); // Duplicate grid id but asking for populated stars
            populatedgrid.dBAsk = SystemsDB.SystemAskType.PopulatedStars;
            grids.Add(populatedgrid);                                                 // add last so shown last

            Console.WriteLine("Grids " + grids.Count + " mid " + midpercentage + " far " + farpercentage);
        }
Ejemplo n.º 3
0
        // used by Starnames in a thread..
        public void GetSystemsInView(ref SortedDictionary <float, StarGrid.InViewInfo> list, double gridlylimit, StarGrid.TransFormInfo ti)
        {
            int idpos = GridId.Id(ti.campos.X, ti.campos.Z);

            foreach (StarGrid grd in grids)                                                      // either we are inside the grid, or close to the centre of another grid..
            {
                if (grd.Id == idpos || grd.DistanceFrom(ti.campos.X, ti.campos.Z) < gridlylimit) // only consider grids which are nearer than this..
                {
                    grd.GetSystemsInView(ref list, ti, (ForceWhite) ? 0x00ffff : 0);
                    //Console.WriteLine("Check grid " + grd.X + "," + grd.Z);
                }
            }

            visitedsystemsgrid.GetSystemsInView(ref list, ti);          // this can be anywhere in space.. so must check
        }
Ejemplo n.º 4
0
        public bool IsDisplayed(double xp, double zp)
        {
            int gridid = GridId.Id(xp, zp);

            StarGrid grid = grids.Find(x => x.Id == gridid);

            if (grid != null)
            {
                return(grid.Displayed);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        public bool IsDisplayChanged(float xp, float zp)        // has this grid changed in display count since last time checked?
        {
            int gridid = GridId.Id(xp, zp);

            List <StarGrid> gridsatid = grids.Where(x => x.Id == gridid).ToList();           // SOL has two grids, populated and unpopulated, need to find both to check..

            foreach (StarGrid grd in gridsatid)
            {
                if (grd.DisplayCount != grd.DisplayChecked)       // if display changed count since last check
                {
                    grd.DisplayChecked = grd.DisplayCount;
                    //Console.WriteLine("Grid changed at {0} {1}, recheck" , xp ,zp );
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
        public void Initialise()
        {
            for (int z = 0; z < GridId.gridzrange; z++)
            {
                for (int x = 0; x < GridId.gridxrange; x++)
                {
                    int    id = GridId.IdFromComponents(x, z);
                    double xp = 0, zp = 0;
                    bool   ok = GridId.XZ(id, out xp, out zp);
                    Debug.Assert(ok);
                    StarGrid grd = new StarGrid(id, xp, zp, Color.Transparent, 1.0F); //A=0 means use default colour array
                    if (xp == 0 && zp == 0)                                           // sol grid, unpopulated stars please
                    {
                        grd.dBAsk = SystemClass.SystemAskType.UnPopulatedStars;
                    }

                    grids.Add(grd);
                }
            }

            visitedsystemsgrid = new StarGrid(-1, 0, 0, Color.Orange, 1.0F);    // grid ID -1 means it won't be filled by the Update task
            grids.Add(visitedsystemsgrid);

            int solid = GridId.Id(0, 0);

            populatedgrid       = new StarGrid(solid, 0, 0, Color.Transparent, 1.0F); // Duplicate grid id but asking for populated stars
            populatedgrid.dBAsk = SystemClass.SystemAskType.PopulatedStars;
            grids.Add(populatedgrid);                                                 // add last, so displayed last, so overwrites anything else

            long total = SystemClass.GetTotalSystems();

            total = Math.Min(total, 10000000);                  // scaling limit at 10mil
            long offset = (total - 1000000) / 100000;           // scale down slowly.. experimental!

            midpercentage -= (int)(offset / 2);
            farpercentage -= (int)(offset / 3);

            //midpercentage = 10;           // agressive debugging options
            //farpercentage = 1;

            Console.WriteLine("Grids " + grids.Count + "Database Stars " + total + " mid " + midpercentage + " far " + farpercentage);
        }
Ejemplo n.º 7
0
        // returns no of updates + inserts, not no of items processed.   Protect yourself against bad json

        private static long DoParseEDSMUpdateSystemsReader(JsonTextReader jr, bool[] grididallowed, ref DateTime maxdate, Func <bool> cancelRequested, Action <int, string> reportProgress, bool useCache = true, bool useTempSystems = false)
        {
            Dictionary <long, SystemClassBase> systemsByEdsmId = useCache ? GetEdsmSystemsLite() : new Dictionary <long, SystemClassBase>();
            int       count             = 0;
            int       updatecount       = 0;
            int       insertcount       = 0;
            Random    rnd               = new Random();
            string    sysnamesTableName = useTempSystems ? "SystemNames_temp" : "SystemNames";
            string    edsmsysTableName  = useTempSystems ? "EdsmSystems_temp" : "EdsmSystems";
            Stopwatch sw        = Stopwatch.StartNew();
            const int BlockSize = 10000;

            while (!cancelRequested())
            {
                bool jr_eof = false;
                List <EDSMDumpSystem> objs = new List <EDSMDumpSystem>(BlockSize);

                while (!cancelRequested())
                {
                    if (jr.Read())
                    {
                        if (jr.TokenType == JsonToken.StartObject)
                        {
                            objs.Add(EDSMDumpSystem.Deserialize(jr));

                            if (objs.Count >= BlockSize)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        jr_eof = true;
                        break;
                    }
                }

                IEnumerator <EDSMDumpSystem> jo_enum = objs.GetEnumerator();
                bool jo_enum_finished = false;

                while (!jo_enum_finished && !cancelRequested())
                {
                    int blkcount     = 0;
                    int oldinsertcnt = insertcount;
                    int oldupdatecnt = updatecount;
                    int oldcount     = count;

                    using (SQLiteTxnLockED <SQLiteConnectionSystem> tl = new SQLiteTxnLockED <SQLiteConnectionSystem>())
                    {
                        tl.OpenWriter();
                        using (SQLiteConnectionSystem cn = new SQLiteConnectionSystem(mode: EDDbAccessMode.Writer))
                        {
                            using (DbTransaction txn = cn.BeginTransaction())
                            {
                                DbCommand updateNameCmd = null;
                                DbCommand updateSysCmd  = null;
                                DbCommand insertNameCmd = null;
                                DbCommand insertSysCmd  = null;
                                DbCommand selectSysCmd  = null;
                                DbCommand selectNameCmd = null;

                                try
                                {
                                    updateNameCmd = cn.CreateCommand("UPDATE SystemNames SET Name=@Name WHERE EdsmId=@EdsmId", txn);
                                    updateNameCmd.AddParameter("@Name", DbType.String);
                                    updateNameCmd.AddParameter("@EdsmId", DbType.Int64);

                                    updateSysCmd = cn.CreateCommand("UPDATE EdsmSystems SET X=@X, Y=@Y, Z=@Z, UpdateTimestamp=@UpdateTimestamp, VersionTimestamp=@VersionTimestamp, GridId=@GridId, RandomId=@RandomId WHERE EdsmId=@EdsmId", txn);
                                    updateSysCmd.AddParameter("@X", DbType.Int64);
                                    updateSysCmd.AddParameter("@Y", DbType.Int64);
                                    updateSysCmd.AddParameter("@Z", DbType.Int64);
                                    updateSysCmd.AddParameter("@UpdateTimestamp", DbType.Int64);
                                    updateSysCmd.AddParameter("@VersionTimestamp", DbType.Int64);
                                    updateSysCmd.AddParameter("@GridId", DbType.Int64);
                                    updateSysCmd.AddParameter("@RandomId", DbType.Int64);
                                    updateSysCmd.AddParameter("@EdsmId", DbType.Int64);

                                    insertNameCmd = cn.CreateCommand("INSERT INTO " + sysnamesTableName + " (Name, EdsmId) VALUES (@Name, @EdsmId)", txn);
                                    insertNameCmd.AddParameter("@Name", DbType.String);
                                    insertNameCmd.AddParameter("@EdsmId", DbType.Int64);

                                    insertSysCmd = cn.CreateCommand("INSERT INTO " + edsmsysTableName + " (EdsmId, X, Y, Z, CreateTimestamp, UpdateTimestamp, VersionTimestamp, GridId, RandomId) VALUES (@EdsmId, @X, @Y, @Z, @CreateTimestamp, @UpdateTimestamp, @VersionTimestamp, @GridId, @RandomId)", txn);
                                    insertSysCmd.AddParameter("@EdsmId", DbType.Int64);
                                    insertSysCmd.AddParameter("@X", DbType.Int64);
                                    insertSysCmd.AddParameter("@Y", DbType.Int64);
                                    insertSysCmd.AddParameter("@Z", DbType.Int64);
                                    insertSysCmd.AddParameter("@CreateTimestamp", DbType.Int64);
                                    insertSysCmd.AddParameter("@UpdateTimestamp", DbType.Int64);
                                    insertSysCmd.AddParameter("@VersionTimestamp", DbType.Int64);
                                    insertSysCmd.AddParameter("@GridId", DbType.Int64);
                                    insertSysCmd.AddParameter("@RandomId", DbType.Int64);

                                    selectSysCmd = cn.CreateCommand("SELECT Id, X, Y, Z, GridId, RandomId FROM EdsmSystems WHERE EdsmId=@EdsmId");
                                    selectSysCmd.AddParameter("@EdsmId", DbType.Int64);

                                    selectNameCmd = cn.CreateCommand("SELECT Name FROM SystemNames WHERE EdsmId = @EdsmId");
                                    selectNameCmd.AddParameter("@EdsmId", DbType.Int64);

                                    while (!cancelRequested())
                                    {
                                        if (!jo_enum.MoveNext())
                                        {
                                            reportProgress(-1, $"Syncing EDSM systems: {count:N0} processed, {insertcount:N0} new systems, {updatecount:N0} updated systems");
                                            txn.Commit();

                                            if (jr_eof)
                                            {
                                                System.Diagnostics.Debug.WriteLine("Maximum date was " + maxdate.ToString());
                                                System.Diagnostics.Debug.WriteLine($"Import took {sw.ElapsedMilliseconds}ms");

                                                return(updatecount + insertcount);
                                            }

                                            jo_enum_finished = true;
                                            break;
                                        }
                                        else if (SQLiteConnectionSystem.IsReadWaiting)
                                        {
                                            if (blkcount < objs.Count * 3 / 4) // Let the reader barge in if we've processed less than 3/4 of the items
                                            {
                                                // Reset the counts, roll back the transaction, and let the reader through...
                                                insertcount = oldinsertcnt;
                                                updatecount = oldupdatecnt;
                                                count       = oldcount;
                                                jo_enum.Reset();
                                                txn.Rollback();
                                                break;
                                            }
                                        }

                                        EDSMDumpSystem       jo     = jo_enum.Current;
                                        EDSMDumpSystemCoords coords = jo.coords;

                                        if (coords != null)
                                        {
                                            DateTime updatedate = DateTime.SpecifyKind(jo.date, DateTimeKind.Utc);

                                            if (updatedate > maxdate)                                   // even if we reject it due to grid id, keep last date up to date
                                            {
                                                maxdate = updatedate;
                                            }

                                            double x      = coords.x;
                                            double z      = coords.z;
                                            int    gridid = GridId.Id(x, z);

                                            if (grididallowed[gridid])  // if grid allows it to be added..
                                            {
                                                //System.Diagnostics.Debug.WriteLine("Accept due to gridid " + gridid);
                                                double y      = coords.y;
                                                long   edsmid = jo.id;
                                                string name   = jo.name;

                                                int randomid = rnd.Next(0, 99);

                                                SystemClassBase dbsys = null;

                                                if (!useTempSystems)
                                                {
                                                    if (useCache && systemsByEdsmId.ContainsKey(edsmid))
                                                    {
                                                        dbsys = systemsByEdsmId[edsmid];
                                                    }

                                                    if (!useCache)
                                                    {
                                                        selectSysCmd.Parameters["@EdsmId"].Value = edsmid;
                                                        using (DbDataReader reader = selectSysCmd.ExecuteReader())
                                                        {
                                                            if (reader.Read())
                                                            {
                                                                dbsys = new SystemClassBase
                                                                {
                                                                    ID     = (long)reader["id"],
                                                                    EDSMID = edsmid
                                                                };

                                                                if (System.DBNull.Value == reader["x"])
                                                                {
                                                                    dbsys.X = double.NaN;
                                                                    dbsys.Y = double.NaN;
                                                                    dbsys.Z = double.NaN;
                                                                }
                                                                else
                                                                {
                                                                    dbsys.X = ((double)(long)reader["X"]) / SystemClassDB.XYZScalar;
                                                                    dbsys.Y = ((double)(long)reader["Y"]) / SystemClassDB.XYZScalar;
                                                                    dbsys.Z = ((double)(long)reader["Z"]) / SystemClassDB.XYZScalar;
                                                                }

                                                                dbsys.EDSMID   = edsmid;
                                                                dbsys.GridID   = reader["GridId"] == DBNull.Value ? 0 : (int)((long)reader["GridId"]);
                                                                dbsys.RandomID = reader["RandomId"] == DBNull.Value ? 0 : (int)((long)reader["RandomId"]);
                                                            }
                                                        }

                                                        if (dbsys != null)
                                                        {
                                                            selectNameCmd.Parameters["@EdsmId"].Value = edsmid;
                                                            using (DbDataReader reader = selectNameCmd.ExecuteReader())
                                                            {
                                                                if (reader.Read())
                                                                {
                                                                    dbsys.Name = (string)reader["Name"];
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                if (dbsys != null)
                                                {
                                                    // see if EDSM data changed..
                                                    if (!dbsys.Name.Equals(name))
                                                    {
                                                        updateNameCmd.Parameters["@Name"].Value   = name;
                                                        updateNameCmd.Parameters["@EdsmId"].Value = edsmid;
                                                        updateNameCmd.ExecuteNonQuery();
                                                    }

                                                    if (Math.Abs(dbsys.X - x) > 0.01 ||
                                                        Math.Abs(dbsys.Y - y) > 0.01 ||
                                                        Math.Abs(dbsys.Z - z) > 0.01 ||
                                                        dbsys.GridID != gridid)  // position changed
                                                    {
                                                        updateSysCmd.Parameters["@X"].Value = (long)(x * SystemClassDB.XYZScalar);
                                                        updateSysCmd.Parameters["@Y"].Value = (long)(y * SystemClassDB.XYZScalar);
                                                        updateSysCmd.Parameters["@Z"].Value = (long)(z * SystemClassDB.XYZScalar);
                                                        updateSysCmd.Parameters["@UpdateTimestamp"].Value  = updatedate.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                        updateSysCmd.Parameters["@VersionTimestamp"].Value = DateTime.UtcNow.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                        updateSysCmd.Parameters["@GridId"].Value           = gridid;
                                                        updateSysCmd.Parameters["@RandomId"].Value         = randomid;
                                                        updateSysCmd.Parameters["@EdsmId"].Value           = edsmid;
                                                        updateSysCmd.ExecuteNonQuery();
                                                        updatecount++;
                                                    }
                                                }
                                                else                                                                  // not in database..
                                                {
                                                    insertNameCmd.Parameters["@Name"].Value   = name;
                                                    insertNameCmd.Parameters["@EdsmId"].Value = edsmid;

                                                    insertNameCmd.ExecuteNonQuery();
                                                    insertSysCmd.Parameters["@EdsmId"].Value           = edsmid;
                                                    insertSysCmd.Parameters["@X"].Value                = (long)(x * SystemClassDB.XYZScalar);
                                                    insertSysCmd.Parameters["@Y"].Value                = (long)(y * SystemClassDB.XYZScalar);
                                                    insertSysCmd.Parameters["@Z"].Value                = (long)(z * SystemClassDB.XYZScalar);
                                                    insertSysCmd.Parameters["@CreateTimestamp"].Value  = updatedate.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                    insertSysCmd.Parameters["@UpdateTimestamp"].Value  = updatedate.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                    insertSysCmd.Parameters["@VersionTimestamp"].Value = DateTime.UtcNow.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                    insertSysCmd.Parameters["@GridId"].Value           = gridid;
                                                    insertSysCmd.Parameters["@RandomId"].Value         = randomid;
                                                    insertSysCmd.ExecuteNonQuery();
                                                    insertcount++;
                                                }
                                            }
                                            else
                                            {
                                                //System.Diagnostics.Debug.WriteLine("Reject due to gridid " + gridid);
                                            }
                                        }
                                        else
                                        {
                                            System.Diagnostics.Debug.WriteLine("Reject due to coords ");
                                        }

                                        count++;
                                        blkcount++;
                                    }   // WHILE END
                                }
                                finally
                                {
                                    if (updateNameCmd != null)
                                    {
                                        updateNameCmd.Dispose();
                                    }
                                    if (updateSysCmd != null)
                                    {
                                        updateSysCmd.Dispose();
                                    }
                                    if (insertNameCmd != null)
                                    {
                                        insertNameCmd.Dispose();
                                    }
                                    if (insertSysCmd != null)
                                    {
                                        insertSysCmd.Dispose();
                                    }
                                    if (selectSysCmd != null)
                                    {
                                        selectSysCmd.Dispose();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (cancelRequested())
            {
                throw new OperationCanceledException();
            }

            return(updatecount + insertcount);
        }
Ejemplo n.º 8
0
        private static long DoParseEDSMUpdateSystemsReader(JsonTextReader jr, ref string date, ref bool outoforder, Func <bool> cancelRequested, Action <int, string> reportProgress, bool useCache = true, bool useTempSystems = false)
        {
            DateTime maxdate;

            if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.None, out maxdate))
            {
                maxdate = new DateTime(2010, 1, 1);
            }

            Dictionary <long, SystemClassBase> systemsByEdsmId = useCache ? GetEdsmSystemsLite() : new Dictionary <long, SystemClassBase>();
            int    count       = 0;
            int    updatecount = 0;
            int    insertcount = 0;
            Random rnd         = new Random();

            int[]     histogramsystems  = new int[50000];
            string    sysnamesTableName = useTempSystems ? "SystemNames_temp" : "SystemNames";
            string    edsmsysTableName  = useTempSystems ? "EdsmSystems_temp" : "EdsmSystems";
            Stopwatch sw        = Stopwatch.StartNew();
            const int BlockSize = 10000;

            while (!cancelRequested())
            {
                bool jr_eof = false;
                List <EDSMDumpSystem> objs = new List <EDSMDumpSystem>(BlockSize);

                while (!cancelRequested())
                {
                    if (jr.Read())
                    {
                        if (jr.TokenType == JsonToken.StartObject)
                        {
                            objs.Add(EDSMDumpSystem.Deserialize(jr));

                            if (objs.Count >= BlockSize)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        jr_eof = true;
                        break;
                    }
                }

                IEnumerator <EDSMDumpSystem> jo_enum = objs.GetEnumerator();
                bool jo_enum_finished = false;

                while (!jo_enum_finished && !cancelRequested())
                {
                    using (SQLiteConnectionSystem cn = new SQLiteConnectionSystem(mode: EDDbAccessMode.Writer))
                    {
                        using (DbTransaction txn = cn.BeginTransaction())
                        {
                            DbCommand updateNameCmd = null;
                            DbCommand updateSysCmd  = null;
                            DbCommand insertNameCmd = null;
                            DbCommand insertSysCmd  = null;
                            DbCommand selectSysCmd  = null;
                            DbCommand selectNameCmd = null;

                            try
                            {
                                updateNameCmd = cn.CreateCommand("UPDATE SystemNames SET Name=@Name WHERE EdsmId=@EdsmId", txn);
                                updateNameCmd.AddParameter("@Name", DbType.String);
                                updateNameCmd.AddParameter("@EdsmId", DbType.Int64);

                                updateSysCmd = cn.CreateCommand("UPDATE EdsmSystems SET X=@X, Y=@Y, Z=@Z, UpdateTimestamp=@UpdateTimestamp, VersionTimestamp=@VersionTimestamp, GridId=@GridId, RandomId=@RandomId WHERE EdsmId=@EdsmId", txn);
                                updateSysCmd.AddParameter("@X", DbType.Int64);
                                updateSysCmd.AddParameter("@Y", DbType.Int64);
                                updateSysCmd.AddParameter("@Z", DbType.Int64);
                                updateSysCmd.AddParameter("@UpdateTimestamp", DbType.Int64);
                                updateSysCmd.AddParameter("@VersionTimestamp", DbType.Int64);
                                updateSysCmd.AddParameter("@GridId", DbType.Int64);
                                updateSysCmd.AddParameter("@RandomId", DbType.Int64);
                                updateSysCmd.AddParameter("@EdsmId", DbType.Int64);

                                insertNameCmd = cn.CreateCommand("INSERT INTO " + sysnamesTableName + " (Name, EdsmId) VALUES (@Name, @EdsmId)", txn);
                                insertNameCmd.AddParameter("@Name", DbType.String);
                                insertNameCmd.AddParameter("@EdsmId", DbType.Int64);

                                insertSysCmd = cn.CreateCommand("INSERT INTO " + edsmsysTableName + " (EdsmId, X, Y, Z, CreateTimestamp, UpdateTimestamp, VersionTimestamp, GridId, RandomId) VALUES (@EdsmId, @X, @Y, @Z, @CreateTimestamp, @UpdateTimestamp, @VersionTimestamp, @GridId, @RandomId)", txn);
                                insertSysCmd.AddParameter("@EdsmId", DbType.Int64);
                                insertSysCmd.AddParameter("@X", DbType.Int64);
                                insertSysCmd.AddParameter("@Y", DbType.Int64);
                                insertSysCmd.AddParameter("@Z", DbType.Int64);
                                insertSysCmd.AddParameter("@CreateTimestamp", DbType.Int64);
                                insertSysCmd.AddParameter("@UpdateTimestamp", DbType.Int64);
                                insertSysCmd.AddParameter("@VersionTimestamp", DbType.Int64);
                                insertSysCmd.AddParameter("@GridId", DbType.Int64);
                                insertSysCmd.AddParameter("@RandomId", DbType.Int64);

                                selectSysCmd = cn.CreateCommand("SELECT Id, X, Y, Z, GridId, RandomId FROM EdsmSystems WHERE EdsmId=@EdsmId");
                                selectSysCmd.AddParameter("@EdsmId", DbType.Int64);

                                selectNameCmd = cn.CreateCommand("SELECT Name FROM SystemNames WHERE EdsmId = @EdsmId");
                                selectNameCmd.AddParameter("@EdsmId", DbType.Int64);

                                while (!cancelRequested() && !SQLiteConnectionSystem.IsReadWaiting)
                                {
                                    if (!jo_enum.MoveNext())
                                    {
                                        reportProgress(-1, $"Syncing EDSM systems: {count:N0} processed, {insertcount:N0} new systems, {updatecount:N0} updated systems");
                                        txn.Commit();

                                        if (jr_eof)
                                        {
                                            date = maxdate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

                                            Console.WriteLine($"Import took {sw.ElapsedMilliseconds}ms");

                                            for (int id = 0; id < histogramsystems.Length; id++)
                                            {
                                                if (histogramsystems[id] != 0)
                                                {
                                                    Console.WriteLine("Id " + id + " count " + histogramsystems[id]);
                                                }
                                            }

                                            return(updatecount + insertcount);
                                        }

                                        jo_enum_finished = true;
                                        break;
                                    }

                                    EDSMDumpSystem       jo     = jo_enum.Current;
                                    EDSMDumpSystemCoords coords = jo.coords;

                                    if (coords != null)
                                    {
                                        double   x          = coords.x;
                                        double   y          = coords.y;
                                        double   z          = coords.z;
                                        long     edsmid     = jo.id;
                                        string   name       = jo.name;
                                        int      gridid     = GridId.Id(x, z);
                                        int      randomid   = rnd.Next(0, 99);
                                        DateTime updatedate = jo.date;
                                        histogramsystems[gridid]++;

                                        if (updatedate > maxdate)
                                        {
                                            maxdate = updatedate;
                                        }
                                        else if (updatedate < maxdate - TimeSpan.FromHours(1))
                                        {
                                            outoforder = true;
                                        }

                                        SystemClassBase dbsys = null;

                                        if (!useTempSystems)
                                        {
                                            if (useCache && systemsByEdsmId.ContainsKey(edsmid))
                                            {
                                                dbsys = systemsByEdsmId[edsmid];
                                            }

                                            if (!useCache)
                                            {
                                                selectSysCmd.Parameters["@EdsmId"].Value = edsmid;
                                                using (DbDataReader reader = selectSysCmd.ExecuteReader())
                                                {
                                                    if (reader.Read())
                                                    {
                                                        dbsys = new SystemClassBase
                                                        {
                                                            id      = (long)reader["id"],
                                                            id_edsm = edsmid
                                                        };

                                                        if (System.DBNull.Value == reader["x"])
                                                        {
                                                            dbsys.x = double.NaN;
                                                            dbsys.y = double.NaN;
                                                            dbsys.z = double.NaN;
                                                        }
                                                        else
                                                        {
                                                            dbsys.x = ((double)(long)reader["X"]) / SystemClassDB.XYZScalar;
                                                            dbsys.y = ((double)(long)reader["Y"]) / SystemClassDB.XYZScalar;
                                                            dbsys.z = ((double)(long)reader["Z"]) / SystemClassDB.XYZScalar;
                                                        }

                                                        dbsys.id_edsm  = edsmid;
                                                        dbsys.gridid   = reader["GridId"] == DBNull.Value ? 0 : (int)((long)reader["GridId"]);
                                                        dbsys.randomid = reader["RandomId"] == DBNull.Value ? 0 : (int)((long)reader["RandomId"]);
                                                    }
                                                }

                                                if (dbsys != null)
                                                {
                                                    selectNameCmd.Parameters["@EdsmId"].Value = edsmid;
                                                    using (DbDataReader reader = selectNameCmd.ExecuteReader())
                                                    {
                                                        if (reader.Read())
                                                        {
                                                            dbsys.name = (string)reader["Name"];
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        if (dbsys != null)
                                        {
                                            // see if EDSM data changed..
                                            if (!dbsys.name.Equals(name))
                                            {
                                                updateNameCmd.Parameters["@Name"].Value   = name;
                                                updateNameCmd.Parameters["@EdsmId"].Value = edsmid;
                                                updateNameCmd.ExecuteNonQuery();
                                            }

                                            if (Math.Abs(dbsys.x - x) > 0.01 ||
                                                Math.Abs(dbsys.y - y) > 0.01 ||
                                                Math.Abs(dbsys.z - z) > 0.01 ||
                                                dbsys.gridid != gridid)  // position changed
                                            {
                                                updateSysCmd.Parameters["@X"].Value = (long)(x * SystemClassDB.XYZScalar);
                                                updateSysCmd.Parameters["@Y"].Value = (long)(y * SystemClassDB.XYZScalar);
                                                updateSysCmd.Parameters["@Z"].Value = (long)(z * SystemClassDB.XYZScalar);
                                                updateSysCmd.Parameters["@UpdateTimestamp"].Value  = updatedate.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                updateSysCmd.Parameters["@VersionTimestamp"].Value = DateTime.UtcNow.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                                updateSysCmd.Parameters["@GridId"].Value           = gridid;
                                                updateSysCmd.Parameters["@RandomId"].Value         = randomid;
                                                updateSysCmd.Parameters["@EdsmId"].Value           = edsmid;
                                                updateSysCmd.ExecuteNonQuery();
                                                updatecount++;
                                            }
                                        }
                                        else                                                                  // not in database..
                                        {
                                            insertNameCmd.Parameters["@Name"].Value   = name;
                                            insertNameCmd.Parameters["@EdsmId"].Value = edsmid;
                                            insertNameCmd.ExecuteNonQuery();
                                            insertSysCmd.Parameters["@EdsmId"].Value           = edsmid;
                                            insertSysCmd.Parameters["@X"].Value                = (long)(x * SystemClassDB.XYZScalar);
                                            insertSysCmd.Parameters["@Y"].Value                = (long)(y * SystemClassDB.XYZScalar);
                                            insertSysCmd.Parameters["@Z"].Value                = (long)(z * SystemClassDB.XYZScalar);
                                            insertSysCmd.Parameters["@CreateTimestamp"].Value  = updatedate.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                            insertSysCmd.Parameters["@UpdateTimestamp"].Value  = updatedate.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                            insertSysCmd.Parameters["@VersionTimestamp"].Value = DateTime.UtcNow.Subtract(new DateTime(2015, 1, 1)).TotalSeconds;
                                            insertSysCmd.Parameters["@GridId"].Value           = gridid;
                                            insertSysCmd.Parameters["@RandomId"].Value         = randomid;
                                            insertSysCmd.ExecuteNonQuery();
                                            insertcount++;
                                        }
                                    }

                                    count++;
                                }
                            }
                            finally
                            {
                                if (updateNameCmd != null)
                                {
                                    updateNameCmd.Dispose();
                                }
                                if (updateSysCmd != null)
                                {
                                    updateSysCmd.Dispose();
                                }
                                if (insertNameCmd != null)
                                {
                                    insertNameCmd.Dispose();
                                }
                                if (insertSysCmd != null)
                                {
                                    insertSysCmd.Dispose();
                                }
                                if (selectSysCmd != null)
                                {
                                    selectSysCmd.Dispose();
                                }
                            }
                        }
                    }
                }
            }

            if (cancelRequested())
            {
                throw new OperationCanceledException();
            }

            return(updatecount + insertcount);
        }