/// <summary>Find a module in outfitting that matches our existing module and provide its price</summary> private static void setShipModuleOutfittingValues(Module existing, List <Module> outfittingModules, string name, ref dynamic vaProxy) { if (existing != null && outfittingModules != null) { foreach (Module Module in outfittingModules) { if (existing.EDDBID == Module.EDDBID) { // Found it vaProxy.SetDecimal(name + " station cost", (decimal?)Module.price); if (Module.price < existing.price) { // And it's cheaper vaProxy.SetDecimal(name + " station discount", existing.price - Module.price); vaProxy.SetText(name + " station discount (spoken)", Translations.Humanize(existing.price - Module.price)); } return; } } } // Not found so remove any existing vaProxy.SetDecimal("Ship " + name + " station cost", (decimal?)null); vaProxy.SetDecimal("Ship " + name + " station discount", (decimal?)null); vaProxy.SetText("Ship " + name + " station discount (spoken)", (string)null); }
private static void setStarSystemValues(StarSystem system, string prefix, ref dynamic vaProxy) { if (system == null) { return; } Logging.Debug("Setting system information (" + prefix + ")"); try { vaProxy.SetText(prefix + " name", system?.name); vaProxy.SetText(prefix + " name (spoken)", Translations.StarSystem(system?.name)); vaProxy.SetDecimal(prefix + " population", system?.population); vaProxy.SetText(prefix + " population (spoken)", Translations.Humanize(system?.population)); vaProxy.SetText(prefix + " allegiance", system?.allegiance); vaProxy.SetText(prefix + " government", system?.government); vaProxy.SetText(prefix + " faction", system?.faction); vaProxy.SetText(prefix + " primary economy", system?.primaryeconomy); vaProxy.SetText(prefix + " state", (system?.systemState ?? SystemState.None).localizedName); vaProxy.SetText(prefix + " security", system?.security); vaProxy.SetText(prefix + " power", system?.power); vaProxy.SetText(prefix + " power (spoken)", Translations.Power(EDDI.Instance.CurrentStarSystem?.power)); vaProxy.SetText(prefix + " power state", system?.powerstate); vaProxy.SetDecimal(prefix + " X", system?.x); vaProxy.SetDecimal(prefix + " Y", system?.y); vaProxy.SetDecimal(prefix + " Z", system?.z); vaProxy.SetInt(prefix + " visits", system?.visits); vaProxy.SetDate(prefix + " previous visit", system?.visits > 1 ? system.lastvisit : null); vaProxy.SetDecimal(prefix + " minutes since previous visit", system?.visits > 1 && system?.lastvisit.HasValue == true ? (decimal)(long)(DateTime.UtcNow - system.lastvisit.Value).TotalMinutes : (decimal?)null); vaProxy.SetText(prefix + " comment", system?.comment); vaProxy.SetDecimal(prefix + " distance from home", system?.distancefromhome); if (system != null) { foreach (Station Station in system.stations) { vaProxy.SetText(prefix + " station name", Station.name); } vaProxy.SetInt(prefix + " stations", system.stations.Count); vaProxy.SetInt(prefix + " orbital stations", system.stations.Count(s => !s.IsPlanetary())); vaProxy.SetInt(prefix + " starports", system.stations.Count(s => s.IsStarport())); vaProxy.SetInt(prefix + " outposts", system.stations.Count(s => s.IsOutpost())); vaProxy.SetInt(prefix + " planetary stations", system.stations.Count(s => s.IsPlanetary())); vaProxy.SetInt(prefix + " planetary outposts", system.stations.Count(s => s.IsPlanetaryOutpost())); vaProxy.SetInt(prefix + " planetary ports", system.stations.Count(s => s.IsPlanetaryPort())); Body primaryBody = null; if (system.bodies != null && system.bodies.Count > 0) { primaryBody = (system.bodies[0].distance == 0 ? system.bodies[0] : null); } setBodyValues(primaryBody, prefix + " main star", vaProxy); } setStatus(ref vaProxy, "Operational"); } catch (Exception e) { setStatus(ref vaProxy, "Failed to set system information", e); } Logging.Debug("Set system information (" + prefix + ")"); }
private static void setCommanderValues(Commander cmdr, ref dynamic vaProxy) { try { vaProxy.SetText("Name", cmdr?.name); vaProxy.SetInt("Combat rating", cmdr?.combatrating?.rank); vaProxy.SetText("Combat rank", cmdr?.combatrating?.localizedName); vaProxy.SetInt("Trade rating", cmdr?.traderating?.rank); vaProxy.SetText("Trade rank", cmdr?.traderating?.localizedName); vaProxy.SetInt("Explore rating", cmdr?.explorationrating?.rank); vaProxy.SetText("Explore rank", cmdr?.explorationrating?.localizedName); vaProxy.SetInt("Empire rating", cmdr?.empirerating?.rank); vaProxy.SetText("Empire rank", cmdr?.empirerating?.maleRank.localizedName); vaProxy.SetInt("Federation rating", cmdr?.federationrating?.rank); vaProxy.SetText("Federation rank", cmdr?.federationrating?.localizedName); vaProxy.SetDecimal("Credits", cmdr?.credits); vaProxy.SetText("Credits (spoken)", Translations.Humanize(cmdr?.credits)); vaProxy.SetDecimal("Debt", cmdr?.debt); vaProxy.SetText("Debt (spoken)", Translations.Humanize(cmdr?.debt)); vaProxy.SetText("Title", cmdr?.title ?? Eddi.Properties.EddiResources.Commander); vaProxy.SetText("Gender", cmdr?.gender ?? Eddi.Properties.MainWindow.tab_commander_gender_n); // Backwards-compatibility with 1.x vaProxy.SetText("System rank", cmdr?.title); setStatus(ref vaProxy, "Operational"); } catch (Exception e) { setStatus(ref vaProxy, "Failed to set commander information", e); } Logging.Debug("Set commander information"); }
private static void setCommanderValues(Commander cmdr, ref dynamic vaProxy) { try { vaProxy.SetText("Name", cmdr == null ? null : cmdr.name); vaProxy.SetInt("Combat rating", cmdr == null || cmdr.combatrating == null ? (int?)null : cmdr.combatrating.rank); vaProxy.SetText("Combat rank", cmdr == null || cmdr.combatrating == null ? null : cmdr.combatrating.name); vaProxy.SetInt("Trade rating", cmdr == null || cmdr.traderating == null ? (int?)null : cmdr.traderating.rank); vaProxy.SetText("Trade rank", cmdr == null || cmdr.traderating == null ? null : cmdr.traderating.name); vaProxy.SetInt("Explore rating", cmdr == null || cmdr.explorationrating == null ? (int?)null : cmdr.explorationrating.rank); vaProxy.SetText("Explore rank", cmdr == null || cmdr.explorationrating == null ? null : cmdr.explorationrating.name); vaProxy.SetInt("Empire rating", cmdr == null || cmdr.empirerating == null ? (int?)null : cmdr.empirerating.rank); vaProxy.SetText("Empire rank", cmdr == null || cmdr.empirerating == null ? null : cmdr.empirerating.name); vaProxy.SetInt("Federation rating", cmdr == null || cmdr.federationrating == null ? (int?)null : cmdr.federationrating.rank); vaProxy.SetText("Federation rank", cmdr == null || cmdr.federationrating == null ? null : cmdr.federationrating.name); vaProxy.SetDecimal("Credits", cmdr == null ? (decimal?)null : cmdr.credits); vaProxy.SetText("Credits (spoken)", cmdr == null ? null : Translations.Humanize(cmdr.credits)); vaProxy.SetDecimal("Debt", cmdr == null ? (decimal?)null : cmdr.debt); vaProxy.SetText("Debt (spoken)", cmdr == null ? null : Translations.Humanize(cmdr.debt)); vaProxy.SetText("Title", cmdr == null ? null : cmdr.title); vaProxy.SetDecimal("Insurance", cmdr == null ? null : cmdr.insurance); // Backwards-compatibility with 1.x vaProxy.SetText("System rank", cmdr == null ? null : cmdr.title); setStatus(ref vaProxy, "Operational"); } catch (Exception e) { setStatus(ref vaProxy, "Failed to set commander information", e); } Logging.Debug("Set commander information"); }
public void TestSpeechHumanize10() { Assert.AreEqual("100 thousand", Translations.Humanize(100000)); }
public void TestSpeechHumanize7() { Assert.AreEqual("51 million", Translations.Humanize(51000000)); }
public void TestSpeechHumanize3() { Assert.AreEqual("zero", Translations.Humanize(0)); }
public void TestSpeechHumanize1() { Assert.AreEqual("on the way to 12 and a half thousand", Translations.Humanize(12345)); }
public void TestNullInput() { Assert.IsNull(Translations.Humanize(null)); }
public void TestIntToIntegerMantissa(int number, string expected) { Assert.AreEqual(expected, Translations.Humanize(number, true)); }
public void TestSpeechHumanize15() { Assert.AreEqual("minus 51 million", Translations.Humanize(-51000000)); }
public void TestSpeechHumanize14() { Assert.AreEqual("minus 0.0016", Translations.Humanize(-0.0015555555M)); }
/// <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["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["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); // // 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))).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) => { State result = State.FromName(values[0].AsString); if (result == null) { result = State.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["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, false); } 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["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); }
public void TestSpeechHumanize19() { Assert.AreEqual("nearly 646 thousand", Translations.Humanize(6.459E5M)); }
public void TestSpeechHumanize18() { Assert.AreEqual("over 912 quadrillion", Translations.Humanize(9.1235E17M)); }
public void TestNumbersWith3DigitMantissa(int number, string expected) { Assert.AreEqual(expected, Translations.Humanize(number, false)); }
public void TestIntToFloatingMantissa(int number, string expected) { Assert.AreEqual(expected, Translations.Humanize(number, false)); }
public void TestSpeechHumanize18() { Assert.AreEqual("minus 100 thousand", Translations.Humanize(-100000)); }
public void TestDoubleToFloatingMantissa(double number, string expected) { Assert.AreEqual(expected, Translations.Humanize((decimal)number, false)); }
public void TestSpeechHumanize19() { Assert.AreEqual("over minus 12", Translations.Humanize(-12.1M)); }
public void TestDoubleToIntegerMantissa(double number, string expected) { Assert.AreEqual(expected, Translations.Humanize((decimal)number, true)); }
public void TestSpeechHumanize20() { Assert.AreEqual("just over minus 12", Translations.Humanize(-12.01M)); }
public void TestSpeechHumanize2() { Assert.AreEqual(null, Translations.Humanize(null)); }
/// <summary> /// Build a store from a list of variables /// </summary> private BuiltinStore buildStore(Dictionary <string, Cottle.Value> vars) { BuiltinStore store = new BuiltinStore(); // 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.Faction(val); } if (translation == val) { translation = Translations.Power(val); } if (translation == val) { translation = Translations.StarSystem(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["Pause"] = new NativeFunction((values) => { return(@"<break time =""" + values[0].AsNumber + @"ms"" />"); }, 1); // // 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.CallSign(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))).TotalSeconds; return(now - date); }, 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) { // 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) => { State result = State.FromName(values[0].AsString); if (result == null) { result = State.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) => { 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["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["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); }
public void TestSpeechHumanize6() { Assert.AreEqual("0.0016", Translations.Humanize(0.0015555555M)); }
/// <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); }
public void TestSpeechHumanize8() { Assert.AreEqual("just over 51 million", Translations.Humanize(51000001)); }
/// <summary> /// Build a store from a list of variables /// </summary> private BuiltinStore buildStore(Dictionary <string, Cottle.Value> vars) { BuiltinStore store = new BuiltinStore(); // Function to call another script store["F"] = new NativeFunction((values) => { return(new ScriptResolver(scripts).resolve(values[0].AsString, store)); }, 1); // Translation functions store["P"] = new NativeFunction((values) => { string val = values[0].AsString; string translation = val; if (translation == val) { translation = Translations.Faction(val); } if (translation == val) { translation = Translations.Power(val); } if (translation == val) { translation = Translations.StarSystem(val); } return(translation); }, 1); // Helper functions store["OneOf"] = new NativeFunction((values) => { return(new ScriptResolver(scripts).resolveScript(values[random.Next(values.Count)].AsString, store)); }); store["Occasionally"] = new NativeFunction((values) => { if (random.Next((int)values[0].AsNumber) == 0) { return(new ScriptResolver(scripts).resolveScript(values[1].AsString, store)); } else { return(""); } }, 2); store["Humanise"] = new NativeFunction((values) => { return(Translations.Humanize(values[0].AsNumber)); }, 1); store["Pause"] = new NativeFunction((values) => { return(@"<break time =""" + values[0].AsNumber + @"ms"" />"); }, 1); // // 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.CallSign(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["ShipDetails"] = new NativeFunction((values) => { int?localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber); Ship result = findShip(localId, null); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 0, 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); return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["StationDetails"] = new NativeFunction((values) => { if (values.Count == 0) { return(null); } StarSystem system; if (values.Count == 1) { // Current system system = EDDI.Instance.CurrentStarSystem; } else { 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.FromEDName(values[0].AsString); } return(result == null ? new ReflectionValue(new object()) : new ReflectionValue(result)); }, 1); store["StateDetails"] = new NativeFunction((values) => { State result = State.FromName(values[0].AsString); if (result == null) { result = State.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) => { 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); // Variables foreach (KeyValuePair <string, Cottle.Value> entry in vars) { store[entry.Key] = entry.Value; } return(store); }
protected static void setShipValues(Ship ship, string prefix, ref dynamic vaProxy) { if (ship != null && ship != vaShipyard.FirstOrDefault(s => s.LocalId == ship.LocalId)) { Logging.Debug("Setting ship information (" + prefix + ")"); try { vaProxy.SetText(prefix + " manufacturer", ship?.manufacturer); vaProxy.SetText(prefix + " model", ship?.model); vaProxy.SetText(prefix + " model (spoken)", ship?.SpokenModel()); if (((ShipMonitor)EDDI.Instance.ObtainMonitor("Ship monitor")).GetCurrentShip() != null && EDDI.Instance.Cmdr != null && EDDI.Instance.Cmdr.name != null) { vaProxy.SetText(prefix + " callsign", ship == null ? null : ship.manufacturer + " " + EDDI.Instance.Cmdr.name.Substring(0, 3).ToUpperInvariant()); vaProxy.SetText(prefix + " callsign (spoken)", ship == null ? null : ship.SpokenManufacturer() + " " + Translations.ICAO(EDDI.Instance.Cmdr.name.Substring(0, 3).ToUpperInvariant())); } vaProxy.SetText(prefix + " name", ship?.name); vaProxy.SetText(prefix + " name (spoken)", ship?.phoneticname); vaProxy.SetText(prefix + " ident", ship?.ident); vaProxy.SetText(prefix + " ident (spoken)", Translations.ICAO(ship?.ident, false)); vaProxy.SetText(prefix + " role", ship?.Role?.localizedName); vaProxy.SetText(prefix + " size", ship?.size?.ToString()); vaProxy.SetDecimal(prefix + " value", ship?.value); vaProxy.SetText(prefix + " value (spoken)", Translations.Humanize(ship?.value)); vaProxy.SetDecimal(prefix + " health", ship?.health); vaProxy.SetInt(prefix + " cargo capacity", ship?.cargocapacity); setShipModuleValues(ship?.bulkheads, prefix + " bulkheads", ref vaProxy); setShipModuleOutfittingValues(ship?.bulkheads, EDDI.Instance.CurrentStation?.outfitting, prefix + " bulkheads", ref vaProxy); setShipModuleValues(ship?.powerplant, prefix + " power plant", ref vaProxy); setShipModuleOutfittingValues(ship?.powerplant, EDDI.Instance.CurrentStation?.outfitting, prefix + " power plant", ref vaProxy); setShipModuleValues(ship?.thrusters, prefix + " thrusters", ref vaProxy); setShipModuleOutfittingValues(ship?.thrusters, EDDI.Instance.CurrentStation?.outfitting, prefix + " thrusters", ref vaProxy); setShipModuleValues(ship?.frameshiftdrive, prefix + " frame shift drive", ref vaProxy); setShipModuleOutfittingValues(ship?.frameshiftdrive, EDDI.Instance.CurrentStation?.outfitting, prefix + " frame shift drive", ref vaProxy); setShipModuleValues(ship?.lifesupport, prefix + " life support", ref vaProxy); setShipModuleOutfittingValues(ship?.lifesupport, EDDI.Instance.CurrentStation?.outfitting, prefix + " life support", ref vaProxy); setShipModuleValues(ship?.powerdistributor, prefix + " power distributor", ref vaProxy); setShipModuleOutfittingValues(ship?.powerdistributor, EDDI.Instance.CurrentStation?.outfitting, prefix + " power distributor", ref vaProxy); setShipModuleValues(ship?.sensors, prefix + " sensors", ref vaProxy); setShipModuleOutfittingValues(ship?.sensors, EDDI.Instance.CurrentStation?.outfitting, prefix + " sensors", ref vaProxy); setShipModuleValues(ship?.fueltank, prefix + " fuel tank", ref vaProxy); setShipModuleOutfittingValues(ship?.fueltank, EDDI.Instance.CurrentStation?.outfitting, prefix + " fuel tank", ref vaProxy); // Special for fuel tank - capacity and total capacity vaProxy.SetDecimal(prefix + " fuel tank capacity", ship?.fueltankcapacity); vaProxy.SetDecimal(prefix + " total fuel tank capacity", ship?.fueltanktotalcapacity); // Hardpoints if (ship != null) { int numTinyHardpoints = 0; int numSmallHardpoints = 0; int numMediumHardpoints = 0; int numLargeHardpoints = 0; int numHugeHardpoints = 0; foreach (Hardpoint Hardpoint in ship.hardpoints) { string baseHardpointName = prefix; switch (Hardpoint.size) { case 0: baseHardpointName = prefix + " tiny hardpoint " + ++numTinyHardpoints; break; case 1: baseHardpointName = prefix + " small hardpoint " + ++numSmallHardpoints; break; case 2: baseHardpointName = prefix + " medium hardpoint " + ++numMediumHardpoints; break; case 3: baseHardpointName = prefix + " large hardpoint " + ++numLargeHardpoints; break; case 4: baseHardpointName = prefix + " huge hardpoint " + ++numHugeHardpoints; break; } vaProxy.SetBoolean(baseHardpointName + " occupied", Hardpoint.module != null); setShipModuleValues(Hardpoint.module, baseHardpointName + " module", ref vaProxy); setShipModuleOutfittingValues(ship == null ? null : Hardpoint.module, EDDI.Instance.CurrentStation?.outfitting, baseHardpointName + " module", ref vaProxy); } vaProxy.SetInt(prefix + " hardpoints", numSmallHardpoints + numMediumHardpoints + numLargeHardpoints + numHugeHardpoints); vaProxy.SetInt(prefix + " utility slots", numTinyHardpoints); // Compartments int curCompartment = 0; foreach (Compartment Compartment in ship.compartments) { string baseCompartmentName = prefix + " compartment " + ++curCompartment; vaProxy.SetInt(baseCompartmentName + " size", Compartment.size); vaProxy.SetBoolean(baseCompartmentName + " occupied", Compartment.module != null); setShipModuleValues(Compartment.module, baseCompartmentName + " module", ref vaProxy); setShipModuleOutfittingValues(ship == null ? null : Compartment.module, EDDI.Instance.CurrentStation?.outfitting, baseCompartmentName + " module", ref vaProxy); } vaProxy.SetInt(prefix + " compartments", curCompartment); } // Fetch the star system in which the ship is stored if (ship != null && ship.starsystem != null) { vaProxy.SetText(prefix + " system", ship.starsystem); vaProxy.SetText(prefix + " station", ship.station); StarSystem StoredShipStarSystem = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(ship.starsystem); // Work out the distance to the system where the ship is stored if we can // CurrentStarSystem might not have been initialised yet so we check. If not, it may be set on the next pass of the setValues() method. if (EDDI.Instance.CurrentStarSystem != null) { if (EDDI.Instance.CurrentStarSystem.x != null & StoredShipStarSystem?.x != null) { decimal dx = (EDDI.Instance.CurrentStarSystem.x - StoredShipStarSystem.x) ?? 0M; decimal dy = (EDDI.Instance.CurrentStarSystem.y - StoredShipStarSystem.y) ?? 0M; decimal dz = (EDDI.Instance.CurrentStarSystem.z - StoredShipStarSystem.z) ?? 0M; decimal distance = (decimal)(Math.Sqrt((double)(dx * dx + dy * dy + dz * dz))); vaProxy.SetDecimal(prefix + " distance", distance); } } else { // We don't know how far away the ship is vaProxy.SetDecimal(prefix + " distance", null); } } setStatus(ref vaProxy, "Operational"); } catch (Exception e) { Logging.Error("Failed to set VoiceAttack ship information", e); setStatus(ref vaProxy, "Failed to set ship information", e); } Logging.Debug("Set ship information"); } }
public void Test2ndDigitRangeNegative(int number, string expected) { Assert.AreEqual(expected, Translations.Humanize(number, false)); }