Browse files

added support for random calls

  • Loading branch information...
1 parent 6b51aef commit 26a167382e6e605c7e1f3c408d2a8f76001d789d @chriswarren chriswarren committed May 1, 2012
View
2 lib/ceiling_cat/plugins/base.rb
@@ -96,4 +96,4 @@ def pluralize(n, singular, plural=nil)
end
end
end
-end
+end
View
27 lib/ceiling_cat/plugins/call_and_response.rb
@@ -4,17 +4,20 @@ class CallAndResponse < CeilingCat::Plugin::Base
def handle
if !super && event.type == :chat
if match = self.class.list.find{|car| body =~ Regexp.new(car[:call],true) }
- response = [match[:response]].flatten # Support old responses which are strings, not arrays
- reply response[Kernel.rand(response.size)]
+ if Kernel.rand(10) >= (match[:frequency] || 0)
+ response = [match[:response]].flatten # Support old responses which are strings, not arrays
+ reply response[Kernel.rand(response.size)]
+ end
return nil
end
end
end
def self.commands
[{:command => "list calls", :description => "List current calls and their responses", :method => "list"},
- {:command => "add call", :description => "Add an response to display when a phrase is said, split by a | - '!add call I see what you did there... | You caught me! | Oh no I'm busted!'", :method => "add"},
- {:command => "remove call", :description => "Remove an call and response by call '!remove call I see what you did there...'", :method => "remove"}]
+ {:command => "add call", :description => "Add a response to display when a phrase is said, split by a | - '!add call I see what you did there... | You caught me! | Oh no I'm busted!'", :method => "add"},
+ {:command => "add random call", :description => "Add a response to display a phrase X of every 10 times, split by a | - '!add random call 5 I see what you did there... | You caught me! | Oh no I'm busted!'", :method => "add_random"},
+ {:command => "remove call", :description => "Remove a call and response by call '!remove call I see what you did there...'", :method => "remove"}]
end
def self.description
@@ -36,7 +39,7 @@ def list
messages << "Current Calls and Responses"
messages += store["call_and_responses"].collect{|car| "-- #{car[:call]} | #{[car[:response]].flatten.join(" | ")}"} # Support old responses which are strings, not arrays
else
- messages << "There are no call and respones set up yet! You should add one with '!add call When someone says this | I say this | or this'"
+ messages << "There are no call and responses set up yet! You should add one with '!add call When someone says this | I say this | or this'"
end
reply messages
end
@@ -47,7 +50,17 @@ def add
if self.class.add(:call => call.strip,:response => response.map(&:strip))
reply "Call and Response added."
else
- reply "Unable to add that call. A call and a response are required, split by a | - 'When someones says this | I say this | or this'"
+ reply "Unable to add that call. A call and a response are required, split by a | - '!add call When someone says this | I say this | or this'"
+ end
+ end
+
+ def add_random
+ message = body_without_nick_or_command("add random call")
+ x, frequency, call, *response = message.split(/(\d)\s+([^|]+)\|\s+(.+)/)
+ if self.class.add(:call => call.strip,:response => response.map(&:strip), :frequency => frequency.to_i)
+ reply "Call and Response added."
+ else
+ reply "Unable to add that call. A frequency, call, and response are required, split by a | - '!add random call 5 When someone says this | I say this | or this'"
end
end
@@ -63,7 +76,7 @@ def self.list
def self.add(opts={})
return false unless opts[:call] && opts[:response]
store["call_and_responses"] ||= []
- store["call_and_responses"] = (store["call_and_responses"] + [{:call => opts[:call], :response => opts[:response]}]).uniq
+ store["call_and_responses"] = (store["call_and_responses"] + [{:call => opts[:call], :response => opts[:response], :frequency => opts[:frequency]}]).uniq
end
def self.remove(call)
View
42 spec/ceiling_cat/plugins/call_and_response_spec.rb
@@ -19,14 +19,14 @@
describe "base methods" do
it "should add a call and response" do
CeilingCat::Plugin::CallAndResponse.add(:call => "foo", :response => "bar")
- CeilingCat::Plugin::CallAndResponse.list.should include({:call => "foo", :response => "bar"})
+ CeilingCat::Plugin::CallAndResponse.list.should include({:call => "foo", :response => "bar", :frequency => nil})
end
it "should remove a call and response" do
CeilingCat::Plugin::CallAndResponse.add(:call => "foo", :response => "bar")
- CeilingCat::Plugin::CallAndResponse.list.should include({:call => "foo", :response => "bar"})
+ CeilingCat::Plugin::CallAndResponse.list.should include({:call => "foo", :response => "bar", :frequency => nil})
CeilingCat::Plugin::CallAndResponse.remove("foo")
- CeilingCat::Plugin::CallAndResponse.list.should_not include({:call => "foo", :response => "bar"})
+ CeilingCat::Plugin::CallAndResponse.list.should_not include({:call => "foo", :response => "bar", :frequency => nil})
end
end
@@ -86,11 +86,23 @@
end
end
+ describe "calling the 'add random call' command" do
+ it "should add a call with a random flag" do
+ event = CeilingCat::Event.new(@room,"!add random call 5 you say something | something witty", @registered_user)
+ @room.should_receive(:say).with("Call and Response added.")
+ CeilingCat::Plugin::CallAndResponse.new(event).handle
+ CeilingCat::Plugin::CallAndResponse.list.should include({:call => "you say something", :response => ["something witty"], :frequency => 5})
+ end
+ end
+
describe "calling the 'remove call' command" do
it "should remove a call" do
+ CeilingCat::Plugin::CallAndResponse.add(:call => "you say something", :response => "i say something back")
event = CeilingCat::Event.new(@room,"!remove call you say something", @registered_user)
@room.should_receive(:say).with("Call removed.")
+ CeilingCat::Plugin::CallAndResponse.list.should include({:call => "you say something", :response => "i say something back", :frequency => nil})
CeilingCat::Plugin::CallAndResponse.new(event).handle
+ CeilingCat::Plugin::CallAndResponse.list.should_not include({:call => "you say something", :response => "i say something back", :frequency => nil})
end
end
end
@@ -151,6 +163,30 @@
@room.should_receive(:say).with(/#{@response.join("|")}/)
CeilingCat::Plugin::CallAndResponse.new(event).handle
end
+
+ it "should respond when frequency is set and the random number is greater than the frequency" do
+ @call = "Who's the cat that won't cop out when there's danger all about?"
+ @response = ["SHAFT!", "BATMAN!"]
+ @frequency = 5
+ CeilingCat::Plugin::CallAndResponse.add(:call => @call, :response => @response, :frequency => @frequency)
+
+ Kernel.stub(:rand).and_return(6,0) # 0 to handle the later use of rand to pick a random response
+ event = CeilingCat::Event.new(@room, @call, @registered_user)
+ @room.should_receive(:say).with(/#{@response.join("|")}/)
+ CeilingCat::Plugin::CallAndResponse.new(event).handle
+ end
+
+ it "should not respond when frequency is set and the random number is less than the frequency" do
+ @call = "Who's the cat that won't cop out when there's danger all about?"
+ @response = ["SHAFT!", "BATMAN!"]
+ @frequency = 5
+ CeilingCat::Plugin::CallAndResponse.add(:call => @call, :response => @response, :frequency => @frequency)
+
+ Kernel.stub(:rand).and_return(4,0) # 0 to handle the later use of rand to pick a random response
+ event = CeilingCat::Event.new(@room, @call, @registered_user)
+ @room.should_not_receive(:say)
+ CeilingCat::Plugin::CallAndResponse.new(event).handle
+ end
end
describe "when a call matches a command" do

0 comments on commit 26a1673

Please sign in to comment.