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|
47 ret << _(" [Letters called so far: %{red}%{letters}%{nocolor}]") % {
48 :red => Irc.color(:red),
49 :letters => @used.join(" "),
50 :nocolor => Irc.color()
57 def check(ans_or_letter)
58 d = ans_or_letter.downcase
69 @split.each_with_index { |c, i|
87 # Wheel-of-Fortune game
89 attr_reader :name, :manager, :single, :max, :pending
91 attr_accessor :must_buy, :price
92 def initialize(name, manager, single, max)
103 # the default is to make vowels usable only
104 # after paying a price in points which is
105 # a fraction of the single round score equal
106 # to the number of rounds needed to win the game
108 @must_buy = %w{a e i o u y}
109 @price = @single*@single/@max
126 if @scores.key?(k) and @scores[k][:score] >= @price
127 @scores[k][:score] -= @price
143 def mark_winner(user)
147 @scores[k][:nick] = user.nick
148 @scores[k][:score] += @single
150 @scores[k] = { :nick => user.nick, :score => @single }
152 if @scores[k][:score] >= @max
161 @scores.each { |k, val|
162 table << ["%s (%s)" % [val[:nick], k], val[:score]]
164 table.sort! { |a, b| b.last <=> a.last }
168 return nil unless @curr_idx
173 # don't advance if there are no further QAs
174 return nil if @curr_idx == @qas.length - 1
183 def check(whatever, o={})
185 return nil unless cur
186 if @must_buy.include?(whatever) and not o[:buy]
189 return cur.check(whatever)
192 def start_add_qa(cat, clue)
193 return [nil, @pending] if @pending
194 @pending = WoFQA.new(cat.dup, clue.dup)
195 return [true, @pending]
198 def finish_add_qa(ans)
199 return nil unless @pending
200 @pending.answer = ans.dup
207 class WheelOfFortune < Plugin
208 Config.register Config::StringValue.new('wheelfortune.game_name',
209 :default => 'Wheel Of Fortune',
210 :desc => "default name of the Wheel Of Fortune game")
214 # TODO load/save running games?
219 chan = p[:chan] || m.channel
221 m.reply _("you must specify a channel")
224 ch = chan.irc_downcase(m.server.casemap).intern
227 m.reply _("there's already a %{name} game on %{chan}, managed by %{who}") % {
236 name = m.source.get_botdata("wheelfortune.game_name") || @bot.config['wheelfortune.game_name']
238 m.source.set_botdata("wheelfortune.game_name", name.dup)
240 @games[ch] = game = WoFGame.new(name, m.botuser, p[:single], p[:max])
241 @bot.say chan, _("%{who} just created a new %{name} game to %{max} points (%{single} per question, %{price} per vowel)") % {
243 :who => game.manager,
245 :single => game.single,
248 @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>\"") % {
254 ch = p[:chan].irc_downcase(m.server.casemap).intern
256 m.reply _("there's no %{name} game running on %{chan}") % {
257 :name => @bot.config['wheelfortune.game_name'],
264 if m.botuser != game.manager and !m.botuser.permit?('wheelfortune::manage::other::add')
265 m.reply _("you can't add questions to the %{name} game on %{chan}") % {
275 m.reply _("sorry, the answer cannot contain the '*' character")
280 worked, qa = game.start_add_qa(cat, clue)
282 str = ans.empty? ? _("ok, clue added for %{name} round %{count} on %{chan}: %{catclue}") : nil
284 str = _("there's already a pending clue for %{name} round %{count} on %{chan}: %{catclue}")
288 :catclue => qa.catclue,
290 :count => game.length+1
292 return unless worked and !ans.empty?
295 qa = game.finish_add_qa(ans)
297 str = _("ok, QA added for %{name} round %{count} on %{chan}: %{catclue} => %{ans}")
299 str = _("there's no pending clue for %{name} on %{chan}!")
303 :catclue => qa ? qa.catclue : nil,
304 :ans => qa ? qa.answer : nil,
306 :count => game.length
308 announce(m, p.merge({ :next => true }) ) unless game.running?
310 m.reply _("something went wrong, I can't seem to understand what you're trying to set up") if clue.empty?
314 def announce(m, p={})
315 chan = p[:chan] || m.channel
316 ch = chan.irc_downcase(m.server.casemap).intern
318 m.reply _("there's no %{name} game running on %{chan}") % {
319 :name => @bot.config['wheelfortune.game_name'],
325 qa = p[:next] ? game.next : game.current
327 m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
335 @bot.say chan, _("%{bold}%{color}%{name}%{bold}, round %{count}:%{nocolor} %{qa}") % {
337 :color => Irc.color(:green),
339 :count => game.round,
340 :nocolor => Irc.color(),
341 :qa => qa.announcement,
346 def score_table(chan, game, opts={})
347 limit = opts[:limit] || -1
348 table = game.score_table[0..limit]
350 @bot.say chan, _("no scores")
353 nick_wd = table.map { |a| a.first.length }.max
354 score_wd = table.first.last.to_s.length
356 @bot.say chan, "%*s : %*u" % [nick_wd, t.first, score_wd, t.last]
360 def react_on_check(m, ch, game, check)
361 debug "check: #{check.inspect}"
365 warning "game #{game}, qa #{game.current} checked nil against #{m.message}"
368 # m.reply "STUPID! YOU SO STUPID!"
371 m.nickreply _("You must buy the %{vowel}") % {
376 when Numeric, :missing
377 # TODO may alter score depening on how many letters were guessed
378 # TODO what happens when the last hint reveals the whole answer?
381 want_more = game.mark_winner(m.source)
382 m.reply _("%{who} got it! The answer was: %{ans}") % {
383 :who => m.sourcenick,
384 :ans => game.current.answer
386 if want_more == :done
388 m.reply _("%{bold}%{color}%{name}%{bold}%{nocolor}: %{who} %{bold}wins%{bold} after %{count} rounds!\nThe final score is") % {
390 :color => Irc.color(:green),
391 :who => m.sourcenick,
393 :count => game.round,
394 :nocolor => Irc.color()
396 score_table(m.channel, game)
399 m.reply _("%{bold}%{color}%{name}%{bold}, round %{count}%{nocolor} -- score so far:") % {
401 :color => Irc.color(:green),
403 :count => game.round,
404 :nocolor => Irc.color()
406 score_table(m.channel, game)
407 announce(m, :next => true)
411 warning "game #{game}, qa #{game.current} checked #{check} against #{m.message}"
416 return unless m.kind_of?(PrivMessage) and not m.address?
417 ch = m.channel.irc_downcase(m.server.casemap).intern
418 return unless game = @games[ch]
419 return unless game.running?
420 check = game.check(m.message, :buy => false)
421 react_on_check(m, ch, game, check)
425 ch = m.channel.irc_downcase(m.server.casemap).intern
428 m.reply _("there's no %{name} game running on %{chan}") % {
429 :name => @bot.config['wheelfortune.game_name'],
434 m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
442 bought = game.buy(m.source)
444 m.reply _("%{who} buys a %{vowel} for %{price} points") % {
449 check = game.check(vowel, :buy => true)
450 react_on_check(m, ch, game, check)
452 m.reply _("you can't buy a %{vowel}, %{who}: it costs %{price} points and you only have %{score}") % {
455 :price => game.price,
456 :score => game.score(m.source)
463 ch = m.channel.irc_downcase(m.server.casemap).intern
465 m.reply _("there's no %{name} game running on %{chan}") % {
466 :name => @bot.config['wheelfortune.game_name'],
471 # is the botuser the manager or allowed to cancel someone else's game?
472 if m.botuser == game.manager or m.botuser.permit?('wheelfortune::manage::other::cancel')
475 m.reply _("you can't cancel the current game")
480 game = @games.delete(ch)
482 @bot.say chan, _("%{name} game cancelled after %{count} rounds. Partial score:") % {
486 score_table(chan, game)
490 @games.each_key { |k| do_cancel(k) }
495 plugin = WheelOfFortune.new
497 plugin.map "wof", :action => 'announce', :private => false
498 plugin.map "wof cancel", :action => 'cancel', :private => false
499 plugin.map "wof [:chan] play [*name] for :single [points] to :max [points]", :action => 'setup_game'
500 plugin.map "wof :chan [category: *cat,] clue: *clue[, answer: *ans]", :action => 'setup_qa', :public => false
501 plugin.map "wof :chan answer: *ans", :action => 'setup_qa', :public => false
502 plugin.map "wof buy :vowel", :action => 'buy', :requirements => { :vowel => /./u }