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 attr_accessor :must_buy, :price
85 def initialize(name, manager, single, max)
96 # the default is to make vowels usable only
97 # after paying a price in points which is
98 # a fraction of the single round score equal
99 # to the number of rounds needed to win the game
101 @must_buy = %w{a e i o u y}
102 @price = @single*@single/@max
115 if @scores.key?(k) and @scores[k][:score] >= @price
116 @scores[k][:score] -= @price
132 def mark_winner(user)
136 @scores[k][:nick] = user.nick
137 @scores[k][:score] += @single
139 @scores[k] = { :nick => user.nick, :score => @single }
141 if @scores[k][:score] >= @max
150 @scores.each { |k, val|
151 table << ["%s (%s)" % [val[:nick], k], val[:score]]
153 table.sort! { |a, b| b.last <=> a.last }
157 return nil unless @curr_idx
162 # don't advance if there are no further QAs
163 return nil if @curr_idx == @qas.length - 1
172 def check(whatever, o={})
174 return nil unless cur
175 if @must_buy.include?(whatever) and not o[:buy]
178 return cur.check(whatever)
181 def start_add_qa(cat, clue)
182 return [nil, @pending] if @pending
183 @pending = WoFQA.new(cat.dup, clue.dup)
184 return [true, @pending]
187 def finish_add_qa(ans)
188 return nil unless @pending
189 @pending.answer = ans.dup
196 class WheelOfFortune < Plugin
197 Config.register Config::StringValue.new('wheelfortune.game_name',
198 :default => 'Wheel Of Fortune',
199 :desc => "default name of the Wheel Of Fortune game")
203 # TODO load/save running games?
208 chan = p[:chan] || m.channel
210 m.reply _("you must specify a channel")
213 ch = chan.irc_downcase(m.server.casemap).intern
216 m.reply _("there's already a %{name} game on %{chan}, managed by %{who}") % {
224 name = @bot.config['wheelfortune.game_name'] if name.empty?
225 @games[ch] = game = WoFGame.new(name, m.botuser, p[:single], p[:max])
226 @bot.say chan, _("%{who} just created a new %{name} game to %{max} points (%{single} per question, %{price} per vowel)") % {
228 :who => game.manager,
230 :single => game.single,
233 @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>\"") % {
239 ch = p[:chan].irc_downcase(m.server.casemap).intern
241 m.reply _("there's no %{name} game running on %{chan}") % {
242 :name => @bot.config['wheelfortune.game_name'],
252 m.reply _("sorry, the answer cannot contain the '*' character")
257 worked, qa = game.start_add_qa(cat, clue)
259 str = ans.empty? ? _("ok, new clue added for %{chan}: %{catclue}") : nil
261 str = _("there's already a pending clue for %{chan}: %{catclue}")
263 m.reply _(str) % { :chan => p[:chan], :catclue => qa.catclue } if str
264 return unless worked or !ans.empty?
267 qa = game.finish_add_qa(ans)
269 str = _("ok, new QA added for %{chan}: %{catclue} => %{ans}")
271 str = _("there's no pending clue for %{chan}!")
273 m.reply _(str) % { :chan => p[:chan], :catclue => qa ? qa.catclue : nil, :ans => qa ? qa.answer : nil}
274 announce(m, p.merge({ :next => true }) ) unless game.running?
276 m.reply _("something went wrong, I can't seem to understand what you're trying to set up")
280 def announce(m, p={})
281 chan = p[:chan] || m.channel
282 ch = chan.irc_downcase(m.server.casemap).intern
284 m.reply _("there's no %{name} game running on %{chan}") % {
285 :name => @bot.config['wheelfortune.game_name'],
291 qa = p[:next] ? game.next : game.current
293 m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
301 @bot.say chan, _("%{bold}%{color}%{name}%{bold}, round %{count}:%{nocolor} %{qa}") % {
303 :color => Irc.color(:green),
305 :count => game.round,
306 :nocolor => Irc.color(),
307 :qa => qa.announcement,
312 def score_table(chan, game, opts={})
313 limit = opts[:limit] || -1
314 table = game.score_table[0..limit]
316 @bot.say chan, _("no scores")
319 nick_wd = table.map { |a| a.first.length }.max
320 score_wd = table.first.last.to_s.length
322 @bot.say chan, "%*s : %*u" % [nick_wd, t.first, score_wd, t.last]
326 def react_on_check(m, ch, game, check)
327 debug "check: #{check.inspect}"
331 warning "game #{game}, qa #{game.current} checked nil against #{m.message}"
334 # m.reply "STUPID! YOU SO STUPID!"
337 m.nickreply _("You must buy the %{vowel}") % {
342 when Numeric, :missing
343 # TODO may alter score depening on how many letters were guessed
344 # TODO what happens when the last hint reveals the whole answer?
347 want_more = game.mark_winner(m.source)
348 m.reply _("%{who} got it! The answer was: %{ans}") % {
349 :who => m.sourcenick,
350 :ans => game.current.answer
352 if want_more == :done
354 m.reply _("%{bold}%{color}%{name}%{bold}%{nocolor}: %{who} %{bold}wins%{bold} after %{count} rounds!\nThe final score is") % {
356 :color => Irc.color(:green),
357 :who => m.sourcenick,
359 :count => game.round,
360 :nocolor => Irc.color()
362 score_table(m.channel, game)
365 m.reply _("%{bold}%{color}%{name}%{bold}, round %{count}%{nocolor} -- score so far:") % {
367 :color => Irc.color(:green),
369 :count => game.round,
370 :nocolor => Irc.color()
372 score_table(m.channel, game)
373 announce(m, :next => true)
377 warning "game #{game}, qa #{game.current} checked #{check} against #{m.message}"
382 return unless m.kind_of?(PrivMessage) and not m.address?
383 ch = m.channel.irc_downcase(m.server.casemap).intern
384 return unless game = @games[ch]
385 return unless game.running?
386 check = game.check(m.message, :buy => false)
387 react_on_check(m, ch, game, check)
391 ch = m.channel.irc_downcase(m.server.casemap).intern
394 m.reply _("there's no %{name} game running on %{chan}") % {
395 :name => @bot.config['wheelfortune.game_name'],
400 m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
408 bought = game.buy(m.source)
410 m.reply _("%{who} buys a %{vowel} for %{price} points") % {
415 check = game.check(vowel, :buy => true)
416 react_on_check(m, ch, game, check)
418 m.reply _("you can't buy a %{vowel}, %{who}: it costs %{price} points and you only have %{score}") % {
421 :price => game.price,
422 :score => game.score(m.source)
429 ch = m.channel.irc_downcase(m.server.casemap).intern
431 m.reply _("there's no %{name} game running on %{chan}") % {
432 :name => @bot.config['wheelfortune.game_name'],
441 game = @games.delete(ch)
443 @bot.say chan, _("%{name} game cancelled after %{count} rounds. Partial score:") % {
447 score_table(chan, game)
451 @games.each_key { |k| do_cancel(k) }
456 plugin = WheelOfFortune.new
458 plugin.map "wof", :action => 'announce', :private => false
459 plugin.map "wof cancel", :action => 'cancel', :private => false
460 plugin.map "wof [:chan] play [*name] for :single [points] to :max [points]", :action => 'setup_game'
461 plugin.map "wof :chan [category: *cat,] clue: *clue[, answer: *ans]", :action => 'setup_qa', :public => false
462 plugin.map "wof :chan answer: *ans", :action => 'setup_qa', :public => false
463 plugin.map "wof buy :vowel", :action => 'buy', :requirements => { :vowel => /./u }