Exemplo n.º 1
0
        protected async void OnReloadMats(object sender, EventArgs e)
        {
            LastMTime = logs.GetMostRecentLogWrite();

            // Indicate that we are loading something
            form.SetLoadingState();

            // Initialize empty InventorySet of unknown "materials"
            unknown = new InventorySet();

            // Initialize empty InventorySet of deltas from log files
            deltas = new InventorySet();

            // Get last run materials from Picard State
            last = state.CalculateCurrentInventory();

            // Apply and store any updates to the data format
            state.ApplyUpdates(last);

            // Get Inara corrections, if any
            inaraCorrection = await FigureOutInaraCorrection();

            var lastUpdate = state.GetLastUpdateTimestamp();

            // Parse logs and get the changes to material counts
            // The filtering function adds unrecognized materials to unknown list
            var MatHandler = new InventoryHandler(dm.EngineerCostLookup);

            MatHandler.InventoryChanged +=
                (object invSender, InventoryEventArgs ie) =>
            {
                if (ie.JournalEntry.Timestamp < lastUpdate)
                {
                    return;
                }

                // If it's not for the Picard commander, ignore
                if (MatHandler.CurrentCmdr !=
                    state.CurrentState.EliteCmdrName)
                {
                    return;
                }

                var mat = ie.Name.ToLower();
                if (dm.EliteMatsLookup.ContainsKey(mat))
                {
                    deltas.AddMat(dm.EliteMatsLookup[mat], ie.Delta);
                }
                else if (!dm.IgnoreCommodities.Contains(mat))
                {
                    unknown.AddMat(mat, ie.Delta);
                }
            };

            var ChHandler = new CharacterHandler();

            ChHandler.CharacterDied +=
                (object chSender, DeathEventArgs de) =>
            {
                if (de.JournalEntry.Timestamp < lastUpdate)
                {
                    return;
                }


                foreach (var comm in dm.MaterialTypeLookup)
                {
                    if (comm.Value != "Commodities")
                    {
                        continue;
                    }

                    var c = comm.Key;

                    if (last.ContainsKey(c))
                    {
                        deltas[c] = -last[c];
                    }
                    else if (deltas.ContainsKey(c))
                    {
                        deltas[c] = 0;
                    }
                }
            };

            foreach (var entry in logs.GetLogEntries())
            {
                entry.Accept(ChHandler);
                entry.Accept(MatHandler);
            }

            // Apply changes to material counts
            result = last + deltas;

            // If there is a correction, also add that to the mat counts
            if (inaraCorrection != null)
            {
                result = result + inaraCorrection;
            }

            // Add all of the data we found above to the form
            UpdateFormWithCurrentMats();

            // Hand control back to the user
            // If deltas are not empty or there is a corretion, we have stuff to save
            form.SetReadyState(
                !deltas.IsZero || inaraCorrection != null);

            // If there are unknown materials, tell the user the
            // bad news, and prevent the window close.
            if (unknown.Count > 0)
            {
                var ackForm = new UnrecognizedMaterials(unknown);
                ackForm.ShowDialog();

                dm.AddIgnore(unknown);

                OnReloadMats(sender, e);
            }
        }