/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotEmote(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "void botEmote(int emoteType, string message = null)", Description = "Send out emote from current item. 100 = Smile, 200 = Frown, 300 = Speak, 400 = Raw.", Example = @"botEmote(300, ""Hello World"");" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Action <int, string> botEmote = (type, parameter) => { Item item = (Item)jsEngine.GetValue("me").ToObject(); IMessage emote = new WorldEmoteEvent() { Origin = item, EmoteType = (WorldEmoteType)type, Parameter = parameter }; ModuleManager.GetManager <IMessageManager>().SendMessageToSiblings(new List <IMessage>() { emote }, item, item); }; jsEngine.SetValue("botEmote", botEmote); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public JSOnTick(IModuleManager moduleManager) { ModuleManager = moduleManager; Logger = ModuleManager.GetLogger(); TickInterval = moduleManager.GetConfiguration() .GetSection("Modules") .GetSection("Script") .GetSection("Managers") .GetSection("Javascript") .GetValue <int>("TickInterval"); Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "onTick [event]", Description = "Opportunity to execute code at regular intervals", Example = @"var onTick = function() {\n};\n" }; ModuleManager.ModuleManagerReadyEvent += () => { ItemManager = ModuleManager.GetManager <IItemManager>(); JavascriptItemDataManager = ModuleManager.GetManager <IJavascriptItemDataManager>(); JavascriptItemDataRepo = ModuleManager.GetRepository <IJavascriptItemDataRepo>(); JavascriptItemDataManager.AddFunctionDefinition(Definition); ModuleManager.Ticks += OnTick; }; }
/// <summary> /// Insert code snippet /// </summary> public void InsertFunction(JavascriptFunctionDefinition function) { Script = $"// {function.Description}\n{function.Prototype}\n{Script}"; // if (snippetName == "OnUse(Item item, ItemCommand itemCommand, Item origin)") // { // Script = @"// Respond to a javascript callback command //var onUse = function(item, itemCommand, origin) { // botEmote(300, ""I like "" + itemCommand.data.like); //};" + $"\n{Script}"; // } // if (snippetName == "OnItemRead") // { // Script = @"// Add javascript (onUse) command to item //var onItemRead = function(item) { // botAddProperty(""BeforeOurTime.Models.Modules.Core.Models.Properties.CommandItemProperty"", { // ""commands"": [{ // ""itemId"": item.Id, // ""id"": ""22a73822-6655-4b7b-aa2d-100b5c4a00a7"", // ""data"": { // ""like"": 5 // }, // ""name"": ""Run Javascript Callback"" // }] // }); //};" + $"\n{Script}"; // } // if (snippetName == "OnTick()") // { // Script = @"// Opportunity to execute code at regular interval //var onTick = function() { //};" + $"\n{Script}"; // } }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotMoveItem(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "void botMoveItem(object itemId, object toId, object originId)", Description = "Move this item to new location", Example = @"botMoveItem(me.Id, ""07f91a80-a9d3-4d97-b696-40ce0e95df91"", me.Id);" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Action <object, object, object> botMoveItem = (itemId, toId, originId) => { var itemManager = ModuleManager.GetManager <IItemManager>(); var moveItem = itemManager.Read(Guid.Parse(itemId.ToString())); var toItem = itemManager.Read(Guid.Parse(toId.ToString())); var originItem = (originId != null) ? itemManager.Read(Guid.Parse(originId.ToString())) : null; itemManager.Move(moveItem, toItem, originItem); }; jsEngine.SetValue("botMoveItem", botMoveItem); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotGetItemProperty(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "object botGetItemProperty(Item item, string partialPropertyName)", Description = "Get property of an item. Returns null if no property found.", Example = @"var property = botGetItemProperty(me, ""VisibleItemProperty""); botLog(""Item is named "" + property.name);" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Func <Item, string, object> botGetItemProperty = (Item item, string partialPropertyName) => { var key = item.Properties.Keys .Where(x => x.Name.Contains(partialPropertyName)) .FirstOrDefault(); return((item.Properties.ContainsKey(key)) ? item.Properties[key] : null); }; jsEngine.SetValue("botGetItemProperty", botGetItemProperty); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotStringify(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "string botStringify(object obj)", Description = "Stringify a javascript or c# object. Regular JSON.stringify will not work on a c# object.", Example = @"var json = botStringify(object obj);" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Func <object, string> botStringify = JsonConvert.SerializeObject; jsEngine.SetValue("botStringify", botStringify); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotListCount(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "int botListCount(IList list)", Description = "Count the number of items in a c# list", Example = @"var count = BotListCount(item.children);" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Func <IList, int> listCount = (IList list) => { return(list.Count); }; jsEngine.SetValue("botListCount", listCount); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotLog(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "void botLog(string message, int? level = null)", Description = "Write a message to the server log file", Example = @"botLog(""Error executing javascript"");" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Action <object, int?> botLog = (message, level) => { level = level ?? (int?)LogLevel.Information; ModuleManager.GetLogger().Log((LogLevel)level, message.ToString()); }; jsEngine.SetValue("botLog", botLog); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotReadItem(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "item botReadItem(object itemId)", Description = "Read an item", Example = @"var item = botReadItem(""c558c1f9-7d01-45f3-bc35-dcab52b5a37c""); botEmote(300, ""I just read item "" + item.id);" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Func <object, Item> botReadItem = (object itemId) => { var guidItemId = Guid.Parse(itemId.ToString()); return(ModuleManager.GetManager <IItemManager>().Read(guidItemId)); }; jsEngine.SetValue("botReadItem", botReadItem); }; }
/// <summary> /// Constructor /// </summary> /// <param name="moduleManager"></param> public BotAddItemProperty(IModuleManager moduleManager) { ModuleManager = moduleManager; Definition = new JavascriptFunctionDefinition() { Global = true, Prototype = "void botAddItemProperty(string propertyClass, object propertyObj)", Description = "Dynamically add a property to an item", Example = @"var onItemRead = function(item) { botAddItemProperty(""BeforeOurTime.Models.Modules.Core.Models.Properties.CommandItemProperty"", { ""commands"": [{ ""itemId"": item.Id, ""id"": ""22a73822-6655-4b7b-aa2d-100b5c4a00a7"", ""data"": { ""like"": 5 }, ""name"": ""Run Javascript Callback"" }] }); };" }; ModuleManager.ModuleManagerReadyEvent += () => { ModuleManager.GetManager <IJavascriptItemDataManager>().AddFunctionDefinition(Definition); var jsEngine = ModuleManager.GetManager <IJavascriptItemDataManager>().GetJSEngine(); Func <string, object, bool> botAddItemProperty = (string typeName, object propertyObj) => { Item item = (Item)jsEngine.GetValue("me").ToObject(); Type propertyType = Type.GetType(typeName + ",BeforeOurTime.Models"); var itemProperty = (IItemProperty)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(propertyObj), propertyType); item.AddProperty(propertyType, itemProperty); return(true); }; jsEngine.SetValue("botAddItemProperty", botAddItemProperty); }; }
/// <summary> /// Add a javascript function definition /// </summary> /// <param name="function"></param> public void AddFunctionDefinition(JavascriptFunctionDefinition function) { FunctionDefinitions.Add(function); }