Skip to content

Commit

Permalink
Merge pull request #65 from davidroberts63/VotingSprocket
Browse files Browse the repository at this point in the history
Voting sprocket
  • Loading branch information
shiftkey-tester committed Jan 22, 2012
2 parents cf4af53 + a26caef commit 19aab89
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Extensions/VotingSprocket/License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The VotingSprocket code is licensed under the Apache 2.0 license.
Located at: http://www.apache.org/licenses/LICENSE-2.0.html
41 changes: 41 additions & 0 deletions Extensions/VotingSprocket/Poll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace VotingSprocket
{
internal class Poll
{
private List<string> _ballots;

public Timer Timer { get; private set; }
public Dictionary<string, int> Votes { get; private set; }

public Poll()
{
_ballots = new List<string>();
Votes = new Dictionary<string, int>();
#if DEBUG
Timer = new Timer(250);
#else
Timer = new Timer(2 * 60 * 1000);
#endif
}

public void CastBallot(string sender, string vote)
{
if (HasAlreadyVoted(sender)) return;

_ballots.Add(sender);
if (!Votes.ContainsKey(vote)) Votes.Add(vote, 0);
Votes[vote]++;
}

public bool HasAlreadyVoted(string sender)
{
return _ballots.Contains(sender);
}
}
}
36 changes: 36 additions & 0 deletions Extensions/VotingSprocket/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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("Voting Sprocket")]
[assembly: AssemblyDescription("Jibbr sprocket for voting polls")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("David Roberts")]
[assembly: AssemblyProduct("VotingSprocket")]
[assembly: AssemblyCopyright("Copyright © David Roberts 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("0aa67100-b2fc-4699-b5e0-03a5c3aceeb5")]

// 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("0.0.1.0")]
[assembly: AssemblyFileVersion("0.0.1.0")]
121 changes: 121 additions & 0 deletions Extensions/VotingSprocket/VoteSprocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jabbot.Sprockets.Core;
using Jabbot.Models;
using Jabbot;
using System.Text.RegularExpressions;
using System.Timers;

namespace VotingSprocket
{
public class VoteSprocket : ISprocket
{
private const string Poll_Command_Description = "To start a poll use: poll <roomname> <question>";

private Regex _pollCommand = new Regex("^poll[ ]*(?<room>[a-zA-Z0-9_-]*)[ ]*(?<question>.*)$", RegexOptions.IgnoreCase);
private Regex _voteCommand = new Regex("^vote[ ]*(?<vote>[0-9]*)$", RegexOptions.IgnoreCase);

private Dictionary<string, Poll> _roomPolls = new Dictionary<string, Poll>();
private IBot _bot;

public bool Handle(ChatMessage message, IBot bot)
{
_bot = bot;

var pollMatch = _pollCommand.Match(message.Content);
var voteMatch = _voteCommand.Match(message.Content);

if ((!pollMatch.Success && !voteMatch.Success)
|| (pollMatch.Success && message.Receiver != bot.Name)
) return false;

if (pollMatch.Success)
{
string room = pollMatch.Groups["room"].Value;
string question = pollMatch.Groups["question"].Value;

if (string.IsNullOrWhiteSpace(room) || string.IsNullOrWhiteSpace(question))
{
bot.PrivateReply(message.Sender, Poll_Command_Description);
}
else if (_roomPolls.ContainsKey(room))
{
bot.PrivateReply(message.Sender, "A poll is already in effect for " + room + ".");
}
else if (!bot.Rooms.Contains(room))
{
bot.PrivateReply(message.Sender, "You are not in the room you specified for the poll.");
bot.PrivateReply(message.Sender, Poll_Command_Description);
}
else
{
_roomPolls.Add(room, new Poll());

string broadcast = "A poll has started: " + question;
bot.Say(broadcast, room);
bot.Say("Poll will close in 2 minutes. Public reply with vote 1 or vote 2 etc...", room);

// I don't like breaking the Law of Demeter here. But adhering to it for the
// timer would mean giving the poll the room (okay with) and the bot/ClosePoll.. (not so okay with).
// Yes that means something is probably wrong here and I should fix it but it's late and
// my mind needs a break.
_roomPolls[room].Timer.Elapsed += (s, e) => ClosePollAndSendResults(room);
_roomPolls[room].Timer.Start();
}
}
else if (voteMatch.Success)
{
string room = message.Receiver;
string vote = voteMatch.Groups["vote"].Value;

if (room == bot.Name)
{
bot.PrivateReply(message.Sender, "You must cast your vote publicy in the room the poll is in.");
}
else if (!_roomPolls.ContainsKey(room))
{
bot.PrivateReply(message.Sender, "There is no active poll for you to vote on.");
bot.PrivateReply(message.Sender, Poll_Command_Description);
}
else
{
if (_roomPolls[room].HasAlreadyVoted(message.Sender))
{
bot.PrivateReply(message.Sender, "You can only cast one vote for the current poll in this room.");
}
else
{
_roomPolls[room].CastBallot(message.Sender, vote);
}
}
}

return true;
}

private void ClosePollAndSendResults(string room)
{
if (!_roomPolls.ContainsKey(room)) return;

_roomPolls[room].Timer.Stop();

if (!_roomPolls[room].Votes.Any())
{
_bot.Say("No votes were cast for the poll.", room);
}
else
{
string message = "poll results ";
foreach (var kvp in _roomPolls[room].Votes)
{
message += string.Format("{0} vote{2} for ({1}). ", kvp.Value, kvp.Key, kvp.Value == 1 ? "" : "s");
}
_roomPolls.Remove(room);

_bot.Say(message.Trim(), room);
}
}
}
}
64 changes: 64 additions & 0 deletions Extensions/VotingSprocket/VotingSprocket.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{23F45EDA-A95D-4041-8B78-6F691A98B0CB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>VotingSprocket</RootNamespace>
<AssemblyName>VotingSprocket</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\..\jibbr\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Poll.cs" />
<Compile Include="VoteSprocket.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Jabbot\Jabbot.csproj">
<Project>{478BFCF7-9397-49A7-AFD4-060B6B749E77}</Project>
<Name>Jabbot</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
29 changes: 16 additions & 13 deletions Jabbot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionTests", "Tests\Ext
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuizSprocket", "Extensions\QuizSprocket\QuizSprocket.csproj", "{93DB29BE-BC75-4D59-9313-891A1245940D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VolunteerSprocket", "Extensions\VolunteerSprocket\VolunteerSprocket.csproj", "{87E3E12D-F46E-4425-8375-6882A6DF243A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherSprocket", "Extensions\WeatherSprocket\WeatherSprocket.csproj", "{1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitbucketAnnouncer", "Extensions\BitbucketAnnouncer\BitbucketAnnouncer.csproj", "{4D94EB55-9204-4579-A79C-4D42F4ED85BB}"
Expand All @@ -73,7 +71,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorSprocket", "Exten
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GithubAnnouncements", "Extensions\GithubAnnouncements\GithubAnnouncements.csproj", "{9518C000-0F98-41B0-81E9-5E065CF5387E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VotingSprocket", "Extensions\VotingSprocket\VotingSprocket.csproj", "{23F45EDA-A95D-4041-8B78-6F691A98B0CB}"
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = Jabbot.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Expand Down Expand Up @@ -214,16 +217,6 @@ Global
{93DB29BE-BC75-4D59-9313-891A1245940D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{93DB29BE-BC75-4D59-9313-891A1245940D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{93DB29BE-BC75-4D59-9313-891A1245940D}.Release|x86.ActiveCfg = Release|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Debug|x86.ActiveCfg = Debug|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Any CPU.Build.0 = Release|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{87E3E12D-F46E-4425-8375-6882A6DF243A}.Release|x86.ActiveCfg = Release|Any CPU
{1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BCEA8B3-23C9-4087-9DC1-70EAC94D0787}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -264,6 +257,16 @@ Global
{9518C000-0F98-41B0-81E9-5E065CF5387E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{9518C000-0F98-41B0-81E9-5E065CF5387E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{9518C000-0F98-41B0-81E9-5E065CF5387E}.Release|x86.ActiveCfg = Release|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Debug|x86.ActiveCfg = Debug|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Any CPU.Build.0 = Release|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{23F45EDA-A95D-4041-8B78-6F691A98B0CB}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -277,11 +280,11 @@ Global
{ED3BAD8A-2972-4D52-BE1E-9B73EE43E53C} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{D05250A1-6F48-49F1-967C-BCB25213EA06} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{93DB29BE-BC75-4D59-9313-891A1245940D} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{87E3E12D-F46E-4425-8375-6882A6DF243A} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{1BCEA8B3-23C9-4087-9DC1-70EAC94D0787} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{4D94EB55-9204-4579-A79C-4D42F4ED85BB} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{6416F073-97F1-4DBB-B8D6-2011BE448D98} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{9518C000-0F98-41B0-81E9-5E065CF5387E} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{23F45EDA-A95D-4041-8B78-6F691A98B0CB} = {7170D00A-149E-427F-A7F9-FBA91CE3F9D7}
{3E8A88F3-737F-4F93-92EA-151B1FE3A44B} = {A3A6EC44-1F10-4427-98DC-CF13D11F8C79}
EndGlobalSection
EndGlobal
7 changes: 6 additions & 1 deletion Tests/ExtensionTests/ExtensionTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<Reference Include="xunit, Version=1.9.0.1566, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.1.9.0.1566\lib\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CalculatorSprocketTest.cs" />
<Compile Include="QuizSprocketTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VoteSprocketTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -64,6 +65,10 @@
<Project>{93DB29BE-BC75-4D59-9313-891A1245940D}</Project>
<Name>QuizSprocket</Name>
</ProjectReference>
<ProjectReference Include="..\..\Extensions\VotingSprocket\VotingSprocket.csproj">
<Project>{23F45EDA-A95D-4041-8B78-6F691A98B0CB}</Project>
<Name>VotingSprocket</Name>
</ProjectReference>
<ProjectReference Include="..\..\Jabbot.CommandSprockets\Jabbot.CommandSprockets.csproj">
<Project>{FB5CE3F1-1575-440B-A6E9-4E5AFED35D8B}</Project>
<Name>Jabbot.CommandSprockets</Name>
Expand Down
Loading

0 comments on commit 19aab89

Please sign in to comment.