From 8bf93f3e07ffcfaa7cc167f213ef2b7dd27c6186 Mon Sep 17 00:00:00 2001 From: Simon Skov Boisen Date: Sat, 14 Jan 2012 15:19:55 +0100 Subject: [PATCH] Math bot which uses NCalc in order to evaluta math expressions. Issue 49 --- .../CalculatorSprocket/CalculatorSprocket.cs | 99 +++++++++++++++++++ .../CalculatorSprocket.csproj | 73 ++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++ Extensions/CalculatorSprocket/packages.config | 4 + .../DisqusAnnouncer/DisqusAnnouncer.csproj | 3 + Jabbot.sln | 14 +++ .../ExtensionTests/CalculatorSprocketTest.cs | 81 +++++++++++++++ Tests/ExtensionTests/ExtensionTests.csproj | 5 + 8 files changed, 315 insertions(+) create mode 100644 Extensions/CalculatorSprocket/CalculatorSprocket.cs create mode 100644 Extensions/CalculatorSprocket/CalculatorSprocket.csproj create mode 100644 Extensions/CalculatorSprocket/Properties/AssemblyInfo.cs create mode 100644 Extensions/CalculatorSprocket/packages.config create mode 100644 Tests/ExtensionTests/CalculatorSprocketTest.cs diff --git a/Extensions/CalculatorSprocket/CalculatorSprocket.cs b/Extensions/CalculatorSprocket/CalculatorSprocket.cs new file mode 100644 index 0000000..ad1eccf --- /dev/null +++ b/Extensions/CalculatorSprocket/CalculatorSprocket.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Jabbot.CommandSprockets; +using NCalc; + +namespace Jabbot.Extensions +{ + public class CalculatorSprocket : CommandSprocket + { + private readonly string _helpInfo; + + public CalculatorSprocket() + { + _helpInfo = "Hi {0}," + Environment.NewLine + Environment.NewLine + + "I accept the following commands:" + Environment.NewLine + + "info/help:\t" + "this message" + Environment.NewLine + + "expr/calc:\t" + "accepts a math expression to evaluate and returns the result" + Environment.NewLine + Environment.NewLine + + "for expression documentation please refer to ncalcs website:" + Environment.NewLine + + "operators: http://ncalc.codeplex.com/wikipage?title=operators&referringTitle=Home" + Environment.NewLine + + "values: http://ncalc.codeplex.com/wikipage?title=values&referringTitle=Home" + Environment.NewLine + + "functions: http://ncalc.codeplex.com/wikipage?title=functions&referringTitle=Home"; + + } + + public override IEnumerable SupportedInitiators + { + get + { + yield return "calc"; + yield return "mathbot"; + } + } + + public override IEnumerable SupportedCommands + { + get + { + yield return "info"; + yield return "help"; + yield return "expr"; + yield return "calc"; + } + } + + public override bool ExecuteCommand() + { + switch (Command) + { + case "info": + case "help": + return ShowInfo(); + case "expr": + case "calc": + return CalculateExpression(GetExpression()); + default: + return false; + } + } + + private bool CalculateExpression(string mathExpression) + { + var expression = new Expression(mathExpression, EvaluateOptions.IgnoreCase); + object result = null; + try + { + result = expression.Evaluate(); + } + catch (Exception ex) + { + if (ex is ArgumentException || ex is EvaluationException) + Bot.Say(string.Format("Sorry {0} - i couldn't evaluate your expression", Message.Sender), Message.Receiver); + } + + if (result == null) + return false; + + Bot.Say(string.Format("{0} = {1}", mathExpression, result), Message.Receiver); + + return true; + } + + private string GetExpression() + { + var expression = Message.Content.Substring(Message.Content.LastIndexOf(Command, StringComparison.Ordinal) + Command.Length).Trim(); + + return expression; + } + + private bool ShowInfo() + { + Bot.PrivateReply(Message.Sender, string.Format(_helpInfo, Message.Sender)); + + return true; + + } + } +} diff --git a/Extensions/CalculatorSprocket/CalculatorSprocket.csproj b/Extensions/CalculatorSprocket/CalculatorSprocket.csproj new file mode 100644 index 0000000..db0fc44 --- /dev/null +++ b/Extensions/CalculatorSprocket/CalculatorSprocket.csproj @@ -0,0 +1,73 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {6416F073-97F1-4DBB-B8D6-2011BE448D98} + Library + Properties + Jabbot.Extensions + Jabbot.Extensions.CalculatorSprocket + v4.0 + 512 + ..\..\..\jibbr\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\ncalc.1.3.8\lib\NCalc.dll + + + + + + + + + + + + + + + + + + + {FB5CE3F1-1575-440B-A6E9-4E5AFED35D8B} + Jabbot.CommandSprockets + + + {478BFCF7-9397-49A7-AFD4-060B6B749E77} + Jabbot + + + + + + \ No newline at end of file diff --git a/Extensions/CalculatorSprocket/Properties/AssemblyInfo.cs b/Extensions/CalculatorSprocket/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..cc1b320 --- /dev/null +++ b/Extensions/CalculatorSprocket/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("CalculatorSprocket")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("CalculatorSprocket")] +[assembly: AssemblyCopyright("Copyright © 2012")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("053807e1-19be-4fa1-94a2-c71615202601")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Extensions/CalculatorSprocket/packages.config b/Extensions/CalculatorSprocket/packages.config new file mode 100644 index 0000000..69db3b5 --- /dev/null +++ b/Extensions/CalculatorSprocket/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Extensions/DisqusAnnouncer/DisqusAnnouncer.csproj b/Extensions/DisqusAnnouncer/DisqusAnnouncer.csproj index 91dbff2..f8987df 100644 --- a/Extensions/DisqusAnnouncer/DisqusAnnouncer.csproj +++ b/Extensions/DisqusAnnouncer/DisqusAnnouncer.csproj @@ -12,6 +12,8 @@ DisqusAnnouncer v4.0 512 + ..\..\..\jibbr\ + true true @@ -53,6 +55,7 @@ +