aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-09-23 19:59:29 +1200
committerDavid Phillips <david@sighup.nz>2018-09-23 20:42:47 +1200
commitd77856c7e0ad3f00fd5d6a21b454511081815c21 (patch)
tree02452551f933f7f7e5f73c461c657be33fd31fbd
parent2e0e3df2ac93946f1473040e608593df9827e862 (diff)
downloadidalius-d77856c7e0ad3f00fd5d6a21b454511081815c21.tar.xz
Add vote plugin
-rw-r--r--Plugin/Vote.pm88
1 files changed, 88 insertions, 0 deletions
diff --git a/Plugin/Vote.pm b/Plugin/Vote.pm
new file mode 100644
index 0000000..95728c8
--- /dev/null
+++ b/Plugin/Vote.pm
@@ -0,0 +1,88 @@
+package Plugin::Vote;
+
+use strict;
+use warnings;
+
+my %has_voted;
+my %vote_topic;
+my %ayes;
+my %noes;
+
+sub configure {
+ my $self = shift;
+ my $cmdref = shift;
+
+ $cmdref->($self, "vote on", sub { $self->begin(@_); } );
+ $cmdref->($self, "vote end", sub { $self->end(@_); } );
+ $cmdref->($self, "vote yes", sub { $self->yes(@_); } );
+ $cmdref->($self, "vote no", sub { $self->no(@_); } );
+
+ return $self;
+}
+
+sub get_channel {
+ my ($where) = @_;
+
+ return $where unless ref($where) eq "ARRAY";
+ return $where->[0];
+}
+
+sub has_voted {
+ my ($nick, $channel) = @_;
+ return 0 unless $has_voted{$channel};
+ return grep {$_ eq $nick} @{$has_voted{$channel}};
+}
+
+sub begin {
+ my ($self, $irc, $logger, $who, $where, $ided, $rest, @arguments) = @_;
+ my $channel = get_channel($where);
+ my $nick = (split /!/, $who)[0];
+
+ return "Syntax: vote on <topic/question>" unless $rest;
+ return "A vote is currently in progress: $vote_topic{$channel}" if $vote_topic{$channel};
+
+ $ayes{$channel} = $noes{$channel} = 0;
+ $vote_topic{$channel} = $rest;
+ $has_voted{$channel} = ();
+ return "Call to vote (from $nick): $rest. 'vote yes' or 'vote no' to vote";
+}
+
+sub end {
+ my ($self, $irc, $logger, $who, $where, $ided, $rest, @arguments) = @_;
+ my $channel = get_channel($where);
+
+ return "No vote is in progress" unless $vote_topic{$channel};
+
+ my $old_vote_topic = $vote_topic{$channel};
+ $vote_topic{$channel} = undef;
+ $has_voted{$channel} = ();
+ return "The votes are in ($old_vote_topic)! Ayes: $ayes{$channel}. Noes: $noes{$channel}";
+}
+
+sub yes {
+ my ($self, $irc, $logger, $who, $where, $ided, $rest, @arguments) = @_;
+ my $nick = (split /!/, $who)[0];
+ my $channel = get_channel($where);
+
+ return "No vote is in progress" unless $vote_topic{$channel};
+ return "$nick: You have already voted on this" if has_voted($nick, $channel);
+
+ push @{$has_voted{$channel}}, $nick;
+ $ayes{$channel}++;
+ return "$nick: Thank you for your vote";
+}
+
+sub no {
+ my ($self, $irc, $logger, $who, $where, $ided, $rest, @arguments) = @_;
+ my $nick = (split /!/, $who)[0];
+ my $channel = get_channel($where);
+
+ return "No vote is in progress" unless $vote_topic{$channel};
+ return "$nick: You have already voted on this" if has_voted($nick, $channel);
+
+ push @{$has_voted{$channel}}, $nick;
+ $noes{$channel}++;
+ return "$nick: Thank you for your vote";
+}
+
+1;