Exemplo n.º 1
0
        public void _handleMissionFailedEvent(MissionFailedEvent @event)
        {
            foreach (Cargo inventoryCargo in inventory.ToList())
            {
                HaulageAmount haulageAmount = inventoryCargo.haulageamounts.FirstOrDefault(ha => ha.id == @event.missionid);
                if (haulageAmount != null)
                {
                    string type = @event.name.Split('_').ElementAtOrDefault(1).ToLowerInvariant();
                    switch (type)
                    {
                    case "delivery":
                    case "deliverywing":
                    case "rescue":
                    case "salvage":
                    case "smuggle":
                    {
                        // Calculate the amount of mission-related cargo still in inventory
                        int obtained = haulageAmount.amount;
                        // If not expired, then failure may have been due to jettisoning cargo
                        if (haulageAmount.expiry < DateTime.UtcNow)
                        {
                            obtained -= inventoryCargo.ejected;
                            inventoryCargo.ejected = 0;
                        }
                        obtained = Math.Min(inventoryCargo.haulage, obtained);

                        // Convert that amount of cargo from `haulage` to `stolen`
                        inventoryCargo.haulage -= obtained;
                        inventoryCargo.stolen  += obtained;

                        // Reduce our `need` counter by the amount of mission related cargo which had not yet been obtained.
                        inventoryCargo.need -= (haulageAmount.amount - obtained);
                    }
                    break;
                    }
                    inventoryCargo.haulageamounts.Remove(haulageAmount);
                    RemoveCargo(inventoryCargo);
                    break;
                }
            }
        }
Exemplo n.º 2
0
        public void _handleMissionAcceptedEvent(MissionAcceptedEvent @event)
        {
            string type = @event.name.Split('_').ElementAtOrDefault(1).ToLowerInvariant();

            switch (type)
            {
            case "altruism":
            case "collect":
            case "collectwing":
            case "delivery":
            case "deliverywing":
            case "mining":
            case "piracy":
            case "rescue":
            case "salvage":
            case "smuggle":
            {
                int           amount        = (type == "delivery" || type == "smuggle") ? @event.amount ?? 0 : 0;
                HaulageAmount haulageAmount = new HaulageAmount(@event.missionid ?? 0, @event.name, @event.amount ?? 0, (DateTime)@event.expiry);
                Cargo         cargo         = GetCargoWithEDName(@event.commodityDefinition?.edname);
                if (cargo != null)
                {
                    cargo.haulage += amount;
                    cargo.haulageamounts.Add(haulageAmount);
                    cargo.CalculateNeed();
                }
                else
                {
                    Cargo newCargo = new Cargo(@event.commodityDefinition?.edname, amount);
                    newCargo.haulage = amount;
                    newCargo.stolen  = 0;
                    newCargo.owned   = 0;
                    newCargo.haulageamounts.Add(haulageAmount);
                    newCargo.CalculateNeed();
                    AddCargo(newCargo);
                }
            }
            break;
            }
        }
Exemplo n.º 3
0
        public void _handleMissionAbandonedEvent(MissionAbandonedEvent @event)
        {
            foreach (Cargo inventoryCargo in inventory)
            {
                HaulageAmount haulageAmount = inventoryCargo.haulageamounts.FirstOrDefault(ha => ha.id == @event.missionid);
                if (haulageAmount != null)
                {
                    string type = @event.name.Split('_').ElementAtOrDefault(1).ToLowerInvariant();
                    switch (type)
                    {
                    case "delivery":
                    case "deliverywing":
                    case "rescue":
                    case "salvage":
                    case "smuggle":
                    {
                        // Calculate the amount of mission-related cargo still in inventory
                        int obtained = haulageAmount.amount - inventoryCargo.ejected;
                        obtained = Math.Min(inventoryCargo.haulage, obtained);

                        // Convert that amount of cargo from `haulage` to `stolen`
                        inventoryCargo.haulage -= obtained;
                        inventoryCargo.stolen  += obtained;

                        // Reduce our `need` counter by the amount of mission related cargo which had not yet been obtained.
                        inventoryCargo.need -= (haulageAmount.amount - obtained);

                        // We didn't fail for ejecting cargo so we set this counter to zero
                        inventoryCargo.ejected = 0;
                    }
                    break;
                    }
                    inventoryCargo.haulageamounts.Remove(haulageAmount);

                    RemoveCargo(inventoryCargo);
                    break;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Build a store from a list of variables
        /// </summary>
        private BuiltinStore buildStore(Dictionary <string, Cottle.Value> vars)
        {
            BuiltinStore store = new BuiltinStore();

            // TODO fetch this from configuration
            bool useICAO = SpeechServiceConfiguration.FromFile().EnableIcao;

            // Function to call another script
            store["F"] = new NativeFunction((values) =>
            {
                return(new ScriptResolver(scripts).resolve(values[0].AsString, store, false));
            }, 1);

            // Translation functions
            store["P"] = new NativeFunction((values) =>
            {
                string val         = values[0].AsString;
                string translation = val;
                if (translation == val)
                {
                    translation = Translations.Body(val, useICAO);
                }
                if (translation == val)
                {
                    translation = Translations.StarSystem(val, useICAO);
                }
                if (translation == val)
                {
                    translation = Translations.Faction(val);
                }
                if (translation == val)
                {
                    translation = Translations.Power(val);
                }
                if (translation == val)
                {
                    Ship ship = ShipDefinitions.FromModel(val);
                    if (ship != null && ship.EDID > 0)
                    {
                        translation = ship.SpokenModel();
                    }
                }
                if (translation == val)
                {
                    Ship ship = ShipDefinitions.FromEDModel(val);
                    if (ship != null && ship.EDID > 0)
                    {
                        translation = ship.SpokenModel();
                    }
                }
                return(translation);
            }, 1);

            // Boolean constants
            store["true"]  = true;
            store["false"] = false;

            // Helper functions
            store["OneOf"] = new NativeFunction((values) =>
            {
                return(new ScriptResolver(scripts).resolveScript(values[random.Next(values.Count)].AsString, store, false));
            });

            store["Occasionally"] = new NativeFunction((values) =>
            {
                if (random.Next((int)values[0].AsNumber) == 0)
                {
                    return(new ScriptResolver(scripts).resolveScript(values[1].AsString, store, false));
                }
                else
                {
                    return("");
                }
            }, 2);

            store["Humanise"] = new NativeFunction((values) =>
            {
                return(Translations.Humanize(values[0].AsNumber));
            }, 1);

            store["List"] = new NativeFunction((values) =>
            {
                string output             = String.Empty;
                const string localisedAnd = "and";
                if (values.Count == 1)
                {
                    foreach (KeyValuePair <Cottle.Value, Cottle.Value> value in values[0].Fields)
                    {
                        string valueString = value.Value.ToString();
                        if (value.Key == 0)
                        {
                            output = valueString;
                        }
                        else if (value.Key < (values[0].Fields.Count - 1))
                        {
                            output = $"{output}, {valueString}";
                        }
                        else
                        {
                            output = $"{output}, {localisedAnd} {valueString}";
                        }
                    }
                }
                return(output);
            }, 1);

            store["Pause"] = new NativeFunction((values) =>
            {
                return(@"<break time=""" + values[0].AsNumber + @"ms"" />");
            }, 1);

            store["Play"] = new NativeFunction((values) =>
            {
                return(@"<audio src=""" + values[0].AsString + @""" />");
            }, 1);

            store["Spacialise"] = new NativeFunction((values) =>
            {
                string Entree = values[0].AsString;
                if (Entree == "")
                {
                    return("");
                }
                string Sortie      = "";
                string UpperSortie = "";
                foreach (char c in Entree)
                {
                    Sortie = Sortie + c + " ";
                }
                UpperSortie = Sortie.ToUpper();
                return(UpperSortie);
            }, 1);

            store["Emphasize"] = new NativeFunction((values) =>
            {
                if (values.Count == 1)
                {
                    return(@"<emphasis level =""strong"">" + values[0].AsString + @"</emphasis>");
                }
                else if (values.Count == 2)
                {
                    return(@"<emphasis level =""" + values[1].AsString + @""">" + values[0].AsString + @"</emphasis>");
                }
                else
                {
                    return("The Emphasize function is used improperly. Please review the documentation for correct usage.");
                }
            }, 1, 2);

            store["SpeechPitch"] = new NativeFunction((values) =>
            {
                string text  = values[0].AsString;
                string pitch = "default";
                if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString))
                {
                    return(text);
                }
                else if (values.Count == 2)
                {
                    pitch = values[1].AsString;
                    return(@"<prosody pitch=""" + pitch + @""">" + text + "</prosody>");
                }
                else
                {
                    return("The SpeechPitch function is used improperly. Please review the documentation for correct usage.");
                }
            }, 1, 2);

            store["SpeechRate"] = new NativeFunction((values) =>
            {
                string text = values[0].AsString;
                string rate = "default";
                if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString))
                {
                    return(text);
                }
                else if (values.Count == 2)
                {
                    rate = values[1].AsString;
                    return(@"<prosody rate=""" + rate + @""">" + text + "</prosody>");
                }
                else
                {
                    return("The SpeechRate function is used improperly. Please review the documentation for correct usage.");
                }
            }, 1, 2);

            store["SpeechVolume"] = new NativeFunction((values) =>
            {
                string text   = values[0].AsString;
                string volume = "default";
                if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString))
                {
                    return(text);
                }
                else if (values.Count == 2)
                {
                    volume = values[1].AsString;
                    return(@"<prosody volume=""" + volume + @""">" + text + "</prosody>");
                }
                else
                {
                    return("The SpeechVolume function is used improperly. Please review the documentation for correct usage.");
                }
            }, 1, 2);

            store["Transmit"] = new NativeFunction((values) =>
            {
                string text = values[0].AsString;
                if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString))
                {
                    return(@"<transmit>" + values[0].AsString + "</transmit>"); // This is a synthetic tag used to signal to the speech service that radio effects should be enabled.
                }
                else
                {
                    return("The Transmit function is used improperly. Please review the documentation for correct usage.");
                }
            }, 1);

            store["StartsWithVowel"] = new NativeFunction((values) =>
            {
                string Entree = values[0].AsString;
                if (Entree == "")
                {
                    return("");
                }

                char[] vowels       = { 'a', 'à', 'â', 'ä', 'e', 'ê', 'é', 'è', 'ë', 'i', 'î', 'ï', 'o', 'ô', 'ö', 'u', 'ù', 'û', 'ü', 'œ', 'y' };
                char firstCharacter = Entree.ToLower().ToCharArray().ElementAt(0);
                Boolean result      = vowels.Contains(firstCharacter);

                return(result);
            }, 1);

            store["Voice"] = new NativeFunction((values) =>
            {
                string text  = values[0].AsString;
                string voice = values[1].AsString;
                if (values.Count == 2)
                {
                    return(@"<voice name=""" + voice + @""">" + text + "</voice>");
                }
                else
                {
                    return("The Voice function is used improperly. Please review the documentation for correct usage.");
                }
            }, 1, 2);

            //
            // Commander-specific functions
            //
            store["ShipName"] = new NativeFunction((values) =>
            {
                int?localId   = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                string model  = (values.Count == 2 ? values[1].AsString : null);
                Ship ship     = findShip(localId, model);
                string result = (ship == null ? "your ship" : ship.SpokenName());
                return(result);
            }, 0, 2);

            store["ShipCallsign"] = new NativeFunction((values) =>
            {
                int?localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                Ship ship   = findShip(localId, null);

                string result;
                if (ship != null)
                {
                    if (EDDI.Instance.Cmdr != null && EDDI.Instance.Cmdr.name != null)
                    {
                        // Obtain the first three characters
                        string chars = new Regex("[^a-zA-Z0-9]").Replace(EDDI.Instance.Cmdr.name, "").ToUpperInvariant().Substring(0, 3);
                        result       = ship.SpokenManufacturer() + " " + Translations.ICAO(chars);
                    }
                    else
                    {
                        if (ship.SpokenManufacturer() == null)
                        {
                            result = "unidentified ship";
                        }
                        else
                        {
                            result = "unidentified " + ship.SpokenManufacturer() + " " + ship.SpokenModel();
                        }
                    }
                }
                else
                {
                    result = "unidentified ship";
                }
                return(result);
            }, 0, 1);

            //
            // Obtain definition objects for various items
            //

            store["SecondsSince"] = new NativeFunction((values) =>
            {
                long?date = (long?)values[0].AsNumber;
                if (date == null)
                {
                    return(null);
                }
                long?now = (long?)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))).TotalSeconds;

                return(now - date);
            }, 1);

            store["ICAO"] = new NativeFunction((values) =>
            {
                // Turn a string in to an ICAO definition
                string value = values[0].AsString;
                if (value == null || value == "")
                {
                    return("");
                }

                // Remove anything that isn't alphanumeric
                Logging.Warn("value is " + value);
                value = value.ToUpperInvariant().Replace("[^A-Z0-9]", "");
                Logging.Warn("value is " + value);

                // Translate to ICAO
                return(Translations.ICAO(value));
            }, 1);

            store["ShipDetails"] = new NativeFunction((values) =>
            {
                Ship result = ShipDefinitions.FromModel(values[0].AsString);
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["CombatRatingDetails"] = new NativeFunction((values) =>
            {
                CombatRating result = CombatRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = CombatRating.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["TradeRatingDetails"] = new NativeFunction((values) =>
            {
                TradeRating result = TradeRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = TradeRating.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["ExplorationRatingDetails"] = new NativeFunction((values) =>
            {
                ExplorationRating result = ExplorationRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = ExplorationRating.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["EmpireRatingDetails"] = new NativeFunction((values) =>
            {
                EmpireRating result = EmpireRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = EmpireRating.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["FederationRatingDetails"] = new NativeFunction((values) =>
            {
                FederationRating result = FederationRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = FederationRating.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["SystemDetails"] = new NativeFunction((values) =>
            {
                StarSystem result = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[0].AsString, true);
                setSystemDistanceFromHome(result);
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["BodyDetails"] = new NativeFunction((values) =>
            {
                StarSystem system;
                if (values.Count == 1 || string.IsNullOrEmpty(values[1].AsString))
                {
                    // Current system
                    system = EDDI.Instance.CurrentStarSystem;
                }
                else
                {
                    // Named system
                    system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true);
                }
                Body result = system != null && system.bodies != null ? system.bodies.FirstOrDefault(v => v.name == values[0].AsString) : null;
                if (result != null && result.type == "Star" && result.chromaticity == null)
                {
                    // Need to set our internal extras for the star
                    result.setStellarExtras();
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1, 2);

            store["StationDetails"] = new NativeFunction((values) =>
            {
                if (values.Count == 0)
                {
                    return(null);
                }
                StarSystem system;
                if (values.Count == 1)
                {
                    // Current system
                    system = EDDI.Instance.CurrentStarSystem;
                }
                else
                {
                    // Named system
                    system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true);
                }
                Station result = system != null && system.stations != null ? system.stations.FirstOrDefault(v => v.name == values[0].AsString) : null;
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1, 2);

            store["SuperpowerDetails"] = new NativeFunction((values) =>
            {
                Superpower result = Superpower.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Superpower.From(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["StateDetails"] = new NativeFunction((values) =>
            {
                SystemState result = SystemState.FromName(values[0].AsString);
                if (result == null)
                {
                    result = SystemState.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["EconomyDetails"] = new NativeFunction((values) =>
            {
                Economy result = Economy.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Economy.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["GovernmentDetails"] = new NativeFunction((values) =>
            {
                Government result = Government.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Government.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["SecurityLevelDetails"] = new NativeFunction((values) =>
            {
                SecurityLevel result = SecurityLevel.FromName(values[0].AsString);
                if (result == null)
                {
                    result = SecurityLevel.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["MaterialDetails"] = new NativeFunction((values) =>
            {
                if (string.IsNullOrEmpty(values[0].AsString))
                {
                    return(new ReflectionValue(new object()));
                }

                Material result = Material.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Material.FromEDName(values[0].AsString);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["CommodityMarketDetails"] = new NativeFunction((values) =>
            {
                CommodityMarketQuote result = null;
                CommodityMarketQuote CommodityDetails(string commodityLocalizedName, Station station)
                {
                    return(station?.commodities?.FirstOrDefault(c => c.localizedName == commodityLocalizedName));
                }

                if (values.Count == 1)
                {
                    // Named commodity, current station
                    Station station = EDDI.Instance.CurrentStation;
                    result          = CommodityDetails(values[0].AsString, station);
                }
                else if (values.Count == 2)
                {
                    // Named commodity, named station, current system
                    StarSystem system  = EDDI.Instance.CurrentStarSystem;
                    string stationName = values[1].AsString;
                    Station station    = system?.stations?.FirstOrDefault(v => v.name == stationName);
                    result             = CommodityDetails(values[0].AsString, station);
                }
                else if (values.Count == 3)
                {
                    // Named commodity, named station, named system
                    StarSystem system  = DataProviderService.GetSystemData(values[2].AsString, null, null, null);
                    string stationName = values[1].AsString;
                    Station station    = system?.stations?.FirstOrDefault(v => v.name == stationName);
                    result             = CommodityDetails(values[0].AsString, station);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 0, 3);

            store["CargoDetails"] = new NativeFunction((values) =>
            {
                Cottle.Value value = values[0];
                Cargo result       = null;
                string edname      = string.Empty;

                if (value.Type == Cottle.ValueContent.String)
                {
                    edname = CommodityDefinition.FromName(value.AsString).edname;
                    result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetCargoWithEDName(edname);
                }
                else if (value.Type == Cottle.ValueContent.Number)
                {
                    result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetCargoWithMissionId((long)value.AsNumber);
                }
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["HaulageDetails"] = new NativeFunction((values) =>
            {
                HaulageAmount result = null;
                result = ((CargoMonitor)EDDI.Instance.ObtainMonitor("Cargo monitor")).GetHaulageWithMissionId((long)values[0].AsNumber);
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["BlueprintDetails"] = new NativeFunction((values) =>
            {
                BlueprintMaterials result = BlueprintMaterials.FromName(values[0].AsString);
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["GalnetNewsArticle"] = new NativeFunction((values) =>
            {
                News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString);
                return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["GalnetNewsArticles"] = new NativeFunction((values) =>
            {
                List <News> results = null;
                if (values.Count == 0)
                {
                    // Obtain all unread articles
                    results = GalnetSqLiteRepository.Instance.GetArticles();
                }
                else if (values.Count == 1)
                {
                    // Obtain all unread news of a given category
                    results = GalnetSqLiteRepository.Instance.GetArticles(values[0].AsString);
                }
                else if (values.Count == 2)
                {
                    // Obtain all news of a given category
                    results = GalnetSqLiteRepository.Instance.GetArticles(values[0].AsString, values[1].AsBoolean);
                }
                return(results == null ? new ReflectionValue(new List <News>()) : new ReflectionValue(results));
            }, 0, 2);

            store["GalnetNewsMarkRead"] = new NativeFunction((values) =>
            {
                News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString);
                if (result != null)
                {
                    GalnetSqLiteRepository.Instance.MarkRead(result);
                }
                return("");
            }, 1);

            store["GalnetNewsMarkUnread"] = new NativeFunction((values) =>
            {
                News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString);
                if (result != null)
                {
                    GalnetSqLiteRepository.Instance.MarkUnread(result);
                }
                return("");
            }, 1);

            store["GalnetNewsDelete"] = new NativeFunction((values) =>
            {
                News result = GalnetSqLiteRepository.Instance.GetArticle(values[0].AsString);
                if (result != null)
                {
                    GalnetSqLiteRepository.Instance.DeleteNews(result);
                }
                return("");
            }, 1);

            store["Distance"] = new NativeFunction((values) =>
            {
                return((decimal)Math.Round(Math.Sqrt(Math.Pow((double)(values[0].AsNumber - values[3].AsNumber), 2)
                                                     + Math.Pow((double)(values[1].AsNumber - values[4].AsNumber), 2)
                                                     + Math.Pow((double)(values[2].AsNumber - values[5].AsNumber), 2)), 2));
            }, 6);

            store["Log"] = new NativeFunction((values) =>
            {
                Logging.Info(values[0].AsString);
                return("");
            }, 1);

            store["SetState"] = new NativeFunction((values) =>
            {
                string name        = values[0].AsString.ToLowerInvariant().Replace(" ", "_");
                Cottle.Value value = values[1];
                if (value.Type == Cottle.ValueContent.Boolean)
                {
                    EDDI.Instance.State[name] = value.AsBoolean;
                    store["state"]            = buildState();
                }
                else if (value.Type == Cottle.ValueContent.Number)
                {
                    EDDI.Instance.State[name] = value.AsNumber;
                    store["state"]            = buildState();
                }
                else if (value.Type == Cottle.ValueContent.String)
                {
                    EDDI.Instance.State[name] = value.AsString;
                    store["state"]            = buildState();
                }
                // Ignore other possibilities
                return("");
            }, 2);

            // Variables
            foreach (KeyValuePair <string, Cottle.Value> entry in vars)
            {
                store[entry.Key] = entry.Value;
            }

            return(store);
        }
Exemplo n.º 5
0
        public void TestCargoMissionScenario()
        {
            cargoMonitor.initializeCargoMonitor(new CargoMonitorConfiguration());

            line   = @"{""timestamp"": ""2018-05-05T19:12:10Z"", ""event"": ""Cargo"", ""Inventory"": [ { ""Name"": ""damagedescapepod"", ""Name_Localised"": ""Damaged Escape Pod"", ""Count"": 4, ""Stolen"": 0 }, { ""Name"": ""usscargoblackbox"", ""Name_Localised"": ""Black Box"", ""Count"": 4, ""Stolen"": 4 }, { ""Name"": ""drones"", ""Name_Localised"": ""Limpet"", ""Count"": 21, ""Stolen"": 0 } ] }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleCargoInventoryEvent((CargoInventoryEvent)events[0]);

            cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "Drones");
            Assert.AreEqual("Limpet", cargo.invariantName);
            Assert.AreEqual(21, cargo.total);
            Assert.AreEqual(21, cargo.owned);
            Assert.AreEqual(0, cargo.need + cargo.stolen + cargo.haulage);

            // CargoMissionAcceptedEvent - Check to see if this is a cargo mission and update our inventory accordingly
            line   = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Elite Knights"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 3 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 3, ""DestinationSystem"": ""Merope"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375682327 }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleMissionAcceptedEvent((MissionAcceptedEvent)events[0]);
            line   = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Merope Expeditionary Fleet"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 4 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 4, ""DestinationSystem"": ""HIP 17692"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375660729 }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleMissionAcceptedEvent((MissionAcceptedEvent)events[0]);

            cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators");
            Assert.AreEqual("Structural Regulators", cargo.invariantName);
            Assert.AreEqual(0, cargo.total);
            Assert.AreEqual(7, cargo.need);

            HaulageAmount haulage = cargo.haulageamounts.FirstOrDefault(h => h.id == 375682327);

            Assert.AreEqual(0, cargo.haulage + cargo.stolen + cargo.owned);
            Assert.AreEqual(3, haulage.amount);
            Assert.AreEqual("Mission_Salvage_Planet", haulage.name);
            Assert.AreEqual(DateTime.Parse("2018-05-12T15:20:27Z").ToUniversalTime(), haulage.expiry);

            // CargoCollectedEvent
            line   = @"{""timestamp"":""2018-05-05T19:42:20Z"",""event"":""CollectCargo"",""Type"":""$StructuralRegulators_Name;"",""Stolen"":false}";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleCommodityCollectedEvent((CommodityCollectedEvent)events[0]);
            line   = @"{""timestamp"":""2018-05-05T19:42:20Z"",""event"":""CollectCargo"",""Type"":""$StructuralRegulators_Name;"",""Stolen"":false}";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleCommodityCollectedEvent((CommodityCollectedEvent)events[0]);

            cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators");
            Assert.AreEqual(2, cargo.total);
            Assert.AreEqual(2, cargo.haulage);
            Assert.AreEqual(5, cargo.need);
            Assert.AreEqual(0, cargo.stolen + cargo.owned);
            Assert.AreEqual(3, haulage.amount);

            // CargoMissionAbandonedEvent - If we abandon a mission with cargo it becomes stolen
            line   = @"{ ""timestamp"":""2018-05-05T19:42:20Z"", ""event"":""MissionAbandoned"", ""Name"":""Mission_Salvage_Planet"", ""MissionID"":375682327 }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleMissionAbandonedEvent((MissionAbandonedEvent)events[0]);

            cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators");
            Assert.AreEqual(2, cargo.total);
            Assert.AreEqual(2, cargo.stolen);
            Assert.AreEqual(4, cargo.need);
            Assert.AreEqual(0, cargo.haulage + cargo.owned);

            // CargoMissionCompletedEvent - Check to see if this is a cargo mission and update our inventory accordingly
            line   = @"{ ""timestamp"": ""2018-05-05T22:27:58Z"", ""event"": ""MissionCompleted"", ""Faction"": ""Merope Expeditionary Fleet"", ""Name"": ""Mission_Salvage_Planet_name"", ""MissionID"": 375660729, ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 4, ""DestinationSystem"": ""HIP 17692"", ""Reward"": 624016, ""FactionEffects"": [ { ""Faction"": ""Merope Expeditionary Fleet"", ""Effects"": [ { ""Effect"": ""$MISSIONUTIL_Interaction_Summary_civilUnrest_down;"", ""Effect_Localised"": ""$#MinorFaction; are happy to report improved civil contentment, making a period of civil unrest unlikely."", ""Trend"": ""DownGood"" } ], ""Influence"": [ { ""SystemAddress"": 224644818084, ""Trend"": ""UpGood"" } ], ""Reputation"": ""UpGood"" } ] }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleMissionCompletedEvent((MissionCompletedEvent)events[0]);

            cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators");
            Assert.IsNull(cargo);

            // CargoMissionFailedEvent - If we fail a mission with cargo it becomes stolen
            line   = @"{ ""timestamp"": ""2018-05-05T19:42:20Z"", ""event"": ""MissionAccepted"", ""Faction"": ""Elite Knights"", ""Name"": ""Mission_Salvage_Planet"", ""LocalisedName"": ""Salvage 3 Structural Regulators"", ""Commodity"": ""$StructuralRegulators_Name;"", ""Commodity_Localised"": ""Structural Regulators"", ""Count"": 3, ""DestinationSystem"": ""Merope"", ""Expiry"": ""2018-05-12T15:20:27Z"", ""Wing"": false, ""Influence"": ""Med"", ""Reputation"": ""Med"", ""Reward"": 557296, ""MissionID"": 375682327 }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleMissionAcceptedEvent((MissionAcceptedEvent)events[0]);
            line   = @"{""timestamp"":""2018-05-05T19:42:20Z"",""event"":""CollectCargo"",""Type"":""$StructuralRegulators_Name;"",""Stolen"":false}";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleCommodityCollectedEvent((CommodityCollectedEvent)events[0]);
            line   = @"{ ""timestamp"":""2018-05-05T19:42:20Z"", ""event"":""MissionFailed"", ""Name"":""Mission_Salvage_Planet"", ""MissionID"":375682327 }";
            events = JournalMonitor.ParseJournalEntry(line);
            cargoMonitor._handleMissionFailedEvent((MissionFailedEvent)events[0]);

            cargo = cargoMonitor.inventory.ToList().FirstOrDefault(c => c.edname == "StructuralRegulators");
            Assert.AreEqual(1, cargo.total);
            Assert.AreEqual(1, cargo.stolen);
            Assert.AreEqual(0, cargo.need);
            Assert.AreEqual(0, cargo.haulage + cargo.owned);
        }
Exemplo n.º 6
0
        public void _handleMissionCompletedEvent(MissionCompletedEvent @event)
        {
            Cargo cargo = GetCargoWithEDName(@event.commodityDefinition?.edname);

            if (cargo != null)
            {
                HaulageAmount haulageAmount = cargo.haulageamounts.FirstOrDefault(ha => ha.id == @event.missionid);
                if (haulageAmount != null)
                {
                    int    amount = Math.Min(haulageAmount.amount, @event.amount ?? 0);
                    string type   = @event.name.Split('_').ElementAtOrDefault(1)
                                    .ToLowerInvariant();
                    bool legal = @event.name.ToLowerInvariant().Contains("illegal") ? false : true;
                    switch (type)
                    {
                    case "altruism":
                    case "collect":
                    case "mining":
                    {
                        cargo.owned -= amount;
                    }
                    break;

                    case "delivery":
                    case "rescue":
                    case "smuggle":
                    {
                        cargo.haulage -= amount;
                    }
                    break;

                    case "piracy":
                    {
                        if (cargo.stolen < amount)
                        {
                            cargo.owned -= amount;
                        }
                        else
                        {
                            cargo.stolen -= amount;
                        }
                    }
                    break;

                    case "salvage":
                    {
                        if (legal)
                        {
                            cargo.haulage -= amount;
                        }
                        else
                        {
                            cargo.stolen -= amount;
                        }
                    }
                    break;
                    }
                    cargo.haulageamounts.Remove(haulageAmount);
                }
                else if (cargo.haulage >= @event.amount)
                {
                    cargo.haulage -= @event.amount ?? 0;
                }
                else if (cargo.owned >= @event.amount)
                {
                    cargo.owned -= @event.amount ?? 0;
                }
                RemoveCargo(cargo);
            }

            foreach (CommodityAmount commodityReward in @event.commodityrewards)
            {
                cargo = GetCargoWithEDName(commodityReward.edname);
                if (cargo != null)
                {
                    cargo.owned += commodityReward.amount;
                    cargo.CalculateNeed();
                }
                else
                {
                    Cargo newCargo = new Cargo(commodityReward.edname, commodityReward.amount);
                    newCargo.haulage = 0;
                    newCargo.stolen  = 0;
                    newCargo.owned   = commodityReward.amount;
                    AddCargo(newCargo);
                }
            }
        }
Exemplo n.º 7
0
        private void _handleCargoDepotEvent(CargoDepotEvent @event)
        {
            Cargo         cargo           = new Cargo();
            HaulageAmount haulageAmount   = new HaulageAmount();
            int           amountRemaining = @event.totaltodeliver - @event.delivered;

            switch (@event.updatetype)
            {
            case "Collect":
            {
                cargo = GetCargoWithMissionId(@event.missionid ?? 0);
                if (cargo != null)
                {
                    // Cargo instantiated by either 'Mission accepted' event or previous 'WingUpdate' update
                    haulageAmount        = cargo.haulageamounts.FirstOrDefault(ha => ha.id == @event.missionid);
                    haulageAmount.amount = amountRemaining;

                    // Update commodity definition if intantiated by previous 'WingUpdate' update
                    if (cargo.commodityDef.edname == "Unknown")
                    {
                        cargo.commodityDef = @event.commodityDefinition;
                    }
                }
                else
                {
                    // First exposure to new cargo.
                    cargo = new Cargo(@event.commodityDefinition.edname, 0);
                    AddCargo(cargo);

                    haulageAmount = new HaulageAmount(@event.missionid ?? 0, "MISSION_DeliveryWing", amountRemaining, DateTime.MaxValue, true);
                    cargo.haulageamounts.Add(haulageAmount);
                }
                cargo.haulage += @event.amount ?? 0;
                cargo.CalculateNeed();
                haulageAmount.collected = @event.collected;
                haulageAmount.delivered = @event.delivered;
            }
            break;

            case "Deliver":
            {
                cargo = GetCargoWithMissionId(@event.missionid ?? 0);
                if (cargo != null)
                {
                    // Cargo instantiated by either 'Mission accepted' event, previous 'WingUpdate' or 'Collect' updates
                    haulageAmount        = cargo.haulageamounts.FirstOrDefault(ha => ha.id == @event.missionid);
                    haulageAmount.amount = amountRemaining;

                    //Update commodity definition if intantiated by previous 'WingUpdate' update
                    if (cargo.commodityDef.edname == "Unknown")
                    {
                        cargo.commodityDef = @event.commodityDefinition;
                    }
                }
                else
                {
                    // Cargo instantiated by previous 'Market buy' event
                    cargo = GetCargoWithEDName(@event.commodityDefinition.edname);

                    string type = @event.collected > 0 ? "MISSION_DeliveryWing" : "MISSION_CollectWing";
                    haulageAmount = new HaulageAmount(@event.missionid ?? 0, type, amountRemaining, DateTime.MaxValue, true);
                    cargo.haulageamounts.Add(haulageAmount);
                }

                if (haulageAmount.type.Contains("delivery"))
                {
                    cargo.haulage -= @event.amount ?? 0;
                }
                else
                {
                    cargo.owned -= @event.amount ?? 0;
                }
                cargo.CalculateNeed();
                haulageAmount.collected = @event.collected;
                haulageAmount.delivered = @event.delivered;

                // Check for shared mission completion
                if (haulageAmount.shared && amountRemaining == 0)
                {
                    cargo.haulageamounts.Remove(haulageAmount);
                    RemoveCargo(cargo);
                }
            }
            break;

            case "WingUpdate":
            {
                cargo = GetCargoWithMissionId(@event.missionid ?? 0);
                if (cargo != null)
                {
                    // Cargo instantiated by either 'Mission accepted' event, previous 'WingUpdate' or 'Collect' updates
                    haulageAmount        = cargo.haulageamounts.FirstOrDefault(ha => ha.id == @event.missionid);
                    haulageAmount.amount = amountRemaining;
                }
                else
                {
                    // First exposure to new cargo, use 'Unknown' as placeholder
                    cargo = new Cargo("Unknown", 0);
                    AddCargo(cargo);
                    string type = @event.collected > 0 ? "MISSION_DeliveryWing" : "MISSION_CollectWing";
                    haulageAmount = new HaulageAmount(@event.missionid ?? 0, type, amountRemaining, DateTime.MaxValue, true);
                    cargo.haulageamounts.Add(haulageAmount);
                }

                int amount = Math.Max(@event.collected - haulageAmount.collected, @event.delivered - haulageAmount.delivered);
                if (amount > 0)
                {
                    string updatetype = @event.collected > haulageAmount.collected ? "Collect" : "Deliver";
                    EDDI.Instance.eventHandler(new CargoWingUpdateEvent(DateTime.Now, haulageAmount.id, updatetype, cargo.commodityDef, amount, @event.collected, @event.delivered, @event.totaltodeliver));
                }
                cargo.CalculateNeed();
                haulageAmount.collected = @event.collected;
                haulageAmount.delivered = @event.delivered;

                // Check for shared mission completion
                if (haulageAmount.shared && amountRemaining == 0)
                {
                    cargo.haulageamounts.Remove(haulageAmount);
                    RemoveCargo(cargo);
                }
            }
            break;
            }
        }