Esempio n. 1
0
        static private bool SendToEDSM(List <HistoryEntry> hl, EDCommander cmdr, out string errmsg)
        {
            EDSMClass edsm = new EDSMClass(cmdr);       // Ensure we use the commanders EDSM credentials.

            errmsg = null;

            List <JObject> entries = new List <JObject>();

            foreach (HistoryEntry he in hl)
            {
                JournalEntry je = he.journalEntry;

                if (je == null)
                {
                    je = JournalEntry.Get(he.Journalid);
                }

                JObject json = je.GetJson();
                RemoveCommonKeys(json);
                if (je.EventTypeID == JournalTypeEnum.FSDJump && json["FuelUsed"].Empty())
                {
                    json["_convertedNetlog"] = true;
                }
                if (json["StarPosFromEDSM"].Bool(false)) // Remove star pos from EDSM
                {
                    json.Remove("StarPos");
                }
                json.Remove("StarPosFromEDSM");
                json["_systemName"]        = he.System.Name;
                json["_systemCoordinates"] = new JArray(he.System.X, he.System.Y, he.System.Z);
                if (he.System.SystemAddress != null)
                {
                    json["_systemAddress"] = he.System.SystemAddress;
                }
                if (he.IsDocked)
                {
                    json["_stationName"] = he.StationName;
                    if (he.MarketID != null)
                    {
                        json["_stationMarketId"] = he.MarketID;
                    }
                }
                json["_shipId"] = he.ShipId;
                entries.Add(json);
            }

            List <JObject> results = edsm.SendJournalEvents(entries, out errmsg);

            if (results == null)
            {
                return(false);
            }
            else
            {
                using (var cn = new SQLiteConnectionUser(utc: true))
                {
                    using (var txn = cn.BeginTransaction())
                    {
                        for (int i = 0; i < hl.Count && i < results.Count; i++)
                        {
                            HistoryEntry he            = hl[i];
                            JObject      result        = results[i];
                            int          msgnr         = result["msgnum"].Int();
                            int          systemId      = result["systemId"].Int();
                            bool         systemCreated = result["systemCreated"].Bool();

                            if ((msgnr >= 100 && msgnr < 200) || msgnr == 500)
                            {
                                if (he.EntryType == JournalTypeEnum.FSDJump || he.EntryType == JournalTypeEnum.Location)
                                {
                                    if (systemId != 0)
                                    {
                                        he.System.EDSMID = systemId;
                                        JournalEntry.UpdateEDSMIDPosJump(he.Journalid, he.System, false, 0, cn, txn);
                                    }

                                    if (systemCreated)
                                    {
                                        he.SetFirstDiscover(true);
                                    }
                                }

                                he.SetEdsmSync(cn, txn);

                                if (msgnr == 500)
                                {
                                    System.Diagnostics.Trace.WriteLine($"Warning submitting event {he.Journalid} \"{he.EventSummary}\": {msgnr} {result["msg"].Str()}");
                                }
                            }
                            else
                            {
                                System.Diagnostics.Trace.WriteLine($"Error submitting event {he.Journalid} \"{he.EventSummary}\": {msgnr} {result["msg"].Str()}");
                            }
                        }

                        txn.Commit();
                    }
                }

                return(true);
            }
        }
Esempio n. 2
0
        static private bool SendToEDSM(List <HistoryEntry> hl, EDCommander cmdr, out string errmsg, out string firstdiscovers)
        {
            EDSMClass edsm = new EDSMClass(cmdr);       // Ensure we use the commanders EDSM credentials.

            errmsg         = null;
            firstdiscovers = "";

            List <JObject> entries = new List <JObject>();

            foreach (HistoryEntry he in hl)
            {
                JournalEntry je = he.journalEntry;

                if (je == null)
                {
                    je = JournalEntry.Get(he.Journalid);
                }

                JObject json = je.GetJson();

                if (json == null)
                {
                    continue;
                }

                RemoveCommonKeys(json);
                if (je.EventTypeID == JournalTypeEnum.FSDJump && json["FuelUsed"].Empty())
                {
                    json["_convertedNetlog"] = true;
                }
                if (json["StarPosFromEDSM"].Bool(false)) // Remove star pos from EDSM
                {
                    json.Remove("StarPos");
                }
                json.Remove("StarPosFromEDSM");
                json["_systemName"]        = he.System.Name;
                json["_systemCoordinates"] = new JArray(he.System.X, he.System.Y, he.System.Z);
                if (he.System.SystemAddress != null)
                {
                    json["_systemAddress"] = he.System.SystemAddress;
                }
                if (he.IsDocked)
                {
                    json["_stationName"] = he.WhereAmI;
                    if (he.MarketID != null)
                    {
                        json["_stationMarketId"] = he.MarketID;
                    }
                }
                json["_shipId"] = he.ShipId;
                entries.Add(json);
            }

            List <JObject> results = edsm.SendJournalEvents(entries, out errmsg);

            if (results == null)
            {
                return(false);
            }
            else
            {
                firstdiscovers = UserDatabase.Instance.ExecuteWithDatabase <string>(cn =>
                {
                    string firsts = "";
                    using (var txn = cn.Connection.BeginTransaction())
                    {
                        for (int i = 0; i < hl.Count && i < results.Count; i++)
                        {
                            HistoryEntry he = hl[i];
                            JObject result  = results[i];
                            int msgnr       = result["msgnum"].Int();
                            int systemId    = result["systemId"].Int();

                            if ((msgnr >= 100 && msgnr < 200) || msgnr == 500)
                            {
                                if (he.EntryType == JournalTypeEnum.FSDJump || he.EntryType == JournalTypeEnum.Location)
                                {
                                    if (systemId != 0)
                                    {
                                        he.System.EDSMID = systemId;
                                        JournalEntry.UpdateEDSMIDPosJump(he.Journalid, he.System, false, 0, cn.Connection, txn);
                                    }
                                }

                                if (he.EntryType == JournalTypeEnum.FSDJump)       // only on FSD, confirmed with Anthor.  25/4/2018
                                {
                                    bool systemCreated = result["systemCreated"].Bool();

                                    if (systemCreated)
                                    {
                                        System.Diagnostics.Debug.WriteLine("** EDSM indicates first entry for " + he.System.Name);
                                        (he.journalEntry as JournalFSDJump).UpdateFirstDiscover(true, cn.Connection, txn);
                                        firsts = firsts.AppendPrePad(he.System.Name, ";");
                                    }
                                }

                                he.journalEntry.SetEdsmSync(cn.Connection, txn);

                                if (msgnr == 500)
                                {
                                    System.Diagnostics.Trace.WriteLine($"Warning submitting event {he.Journalid} \"{he.EventSummary}\": {msgnr} {result["msg"].Str()}");
                                }
                            }
                            else
                            {
                                System.Diagnostics.Trace.WriteLine($"Error submitting event {he.Journalid} \"{he.EventSummary}\": {msgnr} {result["msg"].Str()}");
                            }
                        }

                        txn.Commit();
                        return(firsts);
                    }
                });

                return(true);
            }
        }