4 # :title: Hangman/Wheel Of Fortune
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2007 Giuseppe Bilotta
10 # Wheel-of-Fortune Question/Answer
12 attr_accessor :cat, :clue, :answer, :hint
13 def initialize(cat, clue, ans=nil)
15 @clue = clue # clue phrase
21 ret << "(" + cat + ") " unless cat.empty?
32 @answer = ans.dup.downcase
33 @split = @answer.scan(/./u)
34 @hint = @split.inject([]) { |list, ch|
45 ret = self.catclue << "\n"
46 ret << _("Letters called so far: ") << @used.join(" ") << "\n" unless @used.empty?
50 def check(ans_or_letter)
51 d = ans_or_letter.downcase
62 @split.each_with_index { |c, i|
80 # Wheel-of-Fortune game
82 attr_reader :name, :manager, :single, :max, :pending
84 def initialize(name, manager, single, max)
104 def mark_winner(user)
108 @scores[k][:nick] = user.nick
109 @scores[k][:score] += @single
111 @scores[k] = { :nick => user.nick, :score => @single }
113 if @scores[k][:score] >= @max
122 @scores.each { |k, val|
123 table << ["%s (%s)" % [val[:nick], k], val[:score]]
125 table.sort! { |a, b| b.last <=> a.last }
129 return nil unless @curr_idx
134 # don't advance if there are no further QAs
135 return nil if @curr_idx == @qas.length - 1
146 return nil unless cur
147 return cur.check(whatever)
150 def start_add_qa(cat, clue)
151 return [nil, @pending] if @pending
152 @pending = WoFQA.new(cat.dup, clue.dup)
153 return [true, @pending]
156 def finish_add_qa(ans)
157 return nil unless @pending
158 @pending.answer = ans.dup
165 class WheelOfFortune < Plugin
166 Config.register Config::StringValue.new('wheelfortune.game_name',
167 :default => 'Wheel Of Fortune',
168 :desc => "default name of the Wheel Of Fortune game")
172 # TODO load/save running games?
177 chan = p[:chan] || m.channel
179 m.reply _("you must specify a channel")
182 ch = chan.irc_downcase(m.server.casemap).intern
185 m.reply _("there's already a %{name} game on %{chan}, managed by %{who}") % {
193 name = @bot.config['wheelfortune.game_name'] if name.empty?
194 @games[ch] = game = WoFGame.new(name, m.botuser, p[:single], p[:max])
195 @bot.say chan, _("%{who} just created a new %{name} game to %{max} points (%{single} per question)") % {
197 :who => game.manager,
199 :single => game.single
201 @bot.say m.source, _("ok, the game has been created. now add clues and answers with \"wof %{chan} [category: <category>,] clue: <clue>, answer: <ans>\". if the clue and answer don't fit in one line, add the answer separately with \"wof %{chan} answer <answer>\"") % {
207 ch = p[:chan].irc_downcase(m.server.casemap).intern
209 m.reply _("there's no %{name} game running on %{chan}") % {
210 :name => @bot.config['wheelfortune.game_name'],
220 m.reply _("sorry, the answer cannot contain the '*' character")
225 worked, qa = game.start_add_qa(cat, clue)
227 str = ans.empty? ? _("ok, new clue added for %{chan}: %{catclue}") : nil
229 str = _("there's already a pending clue for %{chan}: %{catclue}")
231 m.reply _(str) % { :chan => p[:chan], :catclue => qa.catclue } if str
232 return unless worked or !ans.empty?
235 qa = game.finish_add_qa(ans)
237 str = _("ok, new QA added for %{chan}: %{catclue} => %{ans}")
239 str = _("there's no pending clue for %{chan}!")
241 m.reply _(str) % { :chan => p[:chan], :catclue => qa ? qa.catclue : nil, :ans => qa ? qa.answer : nil}
242 announce(m, p.merge({ :next => true }) ) unless game.running?
244 m.reply _("something went wrong, I can't seem to understand what you're trying to set up")
248 def announce(m, p={})
249 chan = p[:chan] || m.channel
250 ch = chan.irc_downcase(m.server.casemap).intern
252 m.reply _("there's no %{name} game running on %{chan}") % {
253 :name => @bot.config['wheelfortune.game_name'],
259 qa = p[:next] ? game.next : game.current
261 m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
269 @bot.say chan, qa.announcement
273 def score_table(chan, game, opts={})
274 limit = opts[:limit] || -1
275 table = game.score_table[0..limit]
277 @bot.say chan, _("no scores")
280 nick_wd = table.map { |a| a.first.length }.max
281 score_wd = table.first.last.to_s.length
283 @bot.say chan, "%*s : %*u" % [nick_wd, t.first, score_wd, t.last]
288 return unless m.kind_of?(PrivMessage) and not m.address?
289 ch = m.channel.irc_downcase(m.server.casemap).intern
290 return unless game = @games[ch]
291 return unless game.running?
292 check = game.check(m.message)
293 debug "check: #{check.inspect}"
297 warning "game #{game}, qa #{game.current} checked nil against #{m.message}"
300 # m.reply "STUPID! YOU SO STUPID!"
304 when Numeric, :missing
305 # TODO may alter score depening on how many letters were guessed
306 # TODO what happens when the last hint reveals the whole answer?
309 want_more = game.mark_winner(m.source)
310 m.reply _("%{who} got it! The answer was: %{ans}") % {
311 :who => m.sourcenick,
312 :ans => game.current.answer
314 if want_more == :done
316 m.reply _("%{who} wins the game after %{count} rounds!") % {
317 :who => m.sourcenick,
320 score_table(m.channel, game)
323 score_table(m.channel, game)
324 announce(m, :next => true)
328 warning "game #{game}, qa #{game.current} checked #{check} against #{m.message}"
333 ch = m.channel.irc_downcase(m.server.casemap).intern
335 m.reply _("there's no %{name} game running on %{chan}") % {
336 :name => @bot.config['wheelfortune.game_name'],
345 game = @games.delete(ch)
347 @bot.say chan, _("%{name} game cancelled after %{count} rounds. Partial score:") % {
351 score_table(chan, game)
355 @games.each_key { |k| do_cancel(k) }
360 plugin = WheelOfFortune.new
362 plugin.map "wof", :action => 'announce', :private => false
363 plugin.map "wof cancel", :action => 'cancel', :private => false
364 plugin.map "wof [:chan] play [*name] for :single [points] to :max [points]", :action => 'setup_game'
365 plugin.map "wof :chan [category: *cat,] clue: *clue[, answer: *ans]", :action => 'setup_qa', :public => false
366 plugin.map "wof :chan answer: *ans", :action => 'setup_qa', :public => false