From 90f968e96fe0bc59924f41e6d61e9efee871c003 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sun, 4 Nov 2007 15:13:35 +0000 Subject: factoids plugin: display (selected, random) multiple facts in a single line, but in long form --- data/rbot/plugins/factoids.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'data') diff --git a/data/rbot/plugins/factoids.rb b/data/rbot/plugins/factoids.rb index 78319fdc..5d6401ef 100644 --- a/data/rbot/plugins/factoids.rb +++ b/data/rbot/plugins/factoids.rb @@ -180,6 +180,15 @@ class FactoidsPlugin < Plugin end end + def long_fact(fact,index=nil,total=@factoids.length) + idx = index || @factoids.index(fact)+1 + _("fact #%{idx} of %{total}: %{fact}" % { + :idx => idx, + :total => total, + :fact => fact.to_s(:meta => true) + }) + end + def facts(m, params) total = @factoids.length if params[:words].empty? @@ -187,11 +196,12 @@ class FactoidsPlugin < Plugin else rx = Regexp.new(params[:words].to_s, true) known = @factoids.grep(rx) + reply = [] if known.empty? - m.reply _("I know nothing about %{words}" % params) + reply << _("I know nothing about %{words}" % params) else # TODO config - max_facts = 3 + max_facts = 5 len = known.length if len > max_facts m.reply _("%{len} out of %{total} facts refer to %{words}, I'll only show %{max}" % { @@ -200,9 +210,15 @@ class FactoidsPlugin < Plugin :words => params[:words].to_s, :max => max_facts }) + while known.length > max_facts + known.delete_one + end end - [max_facts, len].min.times { m.reply known.delete_one } + known.each { |f| + reply << long_fact(f) + } end + m.reply reply.join(" -- ") end end @@ -236,11 +252,7 @@ class FactoidsPlugin < Plugin fact = known.pick_one idx = @factoids.index(fact)+1 end - m.reply _("fact #%{idx} of %{total}: %{fact}" % { - :idx => idx, - :total => total, - :fact => fact.to_s(:meta => true) - }) + m.reply long_fact(fact, idx, total) end def edit_fact(m, params) -- cgit v1.2.3