]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/games/wheelfortune.rb
wheelfortune: vowels must now be bought
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / wheelfortune.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Hangman/Wheel Of Fortune
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2007 Giuseppe Bilotta
8 # License:: rbot
9
10 # Wheel-of-Fortune Question/Answer
11 class WoFQA
12   attr_accessor :cat, :clue, :answer, :hint
13   def initialize(cat, clue, ans=nil)
14     @cat = cat # category
15     @clue = clue # clue phrase
16     self.answer = ans
17   end
18
19   def catclue
20     ret = ""
21     ret << "(" + cat + ") " unless cat.empty?
22     ret << clue
23   end
24
25   def answer=(ans)
26     if !ans
27       @answer = nil
28       @split = []
29       @hint = []
30       return
31     end
32     @answer = ans.dup.downcase
33     @split = @answer.scan(/./u)
34     @hint = @split.inject([]) { |list, ch|
35       if ch !~ /[a-z]/
36         list << ch
37       else
38         list << "*"
39       end
40     }
41     @used = []
42   end
43
44   def announcement
45     ret = self.catclue << "\n"
46     ret << _("Letters called so far: ") << @used.join(" ") << "\n" unless @used.empty?
47     ret << @hint.join
48   end
49
50   def check(ans_or_letter)
51     d = ans_or_letter.downcase
52     if d == @answer
53       return :gotit
54     elsif d.length == 1
55       if @used.include?(d)
56         return :used
57       else
58         @used << d
59         @used.sort!
60         if @split.include?(d)
61           count = 0
62           @split.each_with_index { |c, i|
63             if c == d
64               @hint[i] = d.upcase
65               count += 1
66             end
67           }
68           return count
69         else
70           return :missing
71         end
72       end
73     else
74       return :wrong
75     end
76   end
77
78 end
79
80 # Wheel-of-Fortune game
81 class WoFGame
82   attr_reader :name, :manager, :single, :max, :pending
83   attr_writer :running
84   attr_accessor :must_buy, :price
85   def initialize(name, manager, single, max)
86     @name = name.dup
87     @manager = manager
88     @single = single.to_i
89     @max = max.to_i
90     @pending = nil
91     @qas = []
92     @curr_idx = nil
93     @running = false
94     @scores = Hash.new
95
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
100     # TODO customize
101     @must_buy = %w{a e i o u y}
102     @price = @single*@single/@max
103   end
104
105   def running?
106     @running
107   end
108
109   def round
110     @curr_idx+1 rescue 0
111   end
112
113   def buy(user)
114     k = user.botuser
115     if @scores.key?(k) and @scores[k][:score] >= @price
116       @scores[k][:score] -= @price
117       return true
118     else
119       return false
120     end
121   end
122
123   def score(user)
124     k = user.botuser
125     if @scores.key?(k)
126       @scores[k][:score]
127     else
128       0
129     end
130   end
131
132   def mark_winner(user)
133     @running = false
134     k = user.botuser
135     if @scores.key?(k)
136       @scores[k][:nick] = user.nick
137       @scores[k][:score] += @single
138     else
139       @scores[k] = { :nick => user.nick, :score => @single }
140     end
141     if @scores[k][:score] >= @max
142       return :done
143     else
144       return :more
145     end
146   end
147
148   def score_table
149     table = []
150     @scores.each { |k, val|
151       table << ["%s (%s)" % [val[:nick], k], val[:score]]
152     }
153     table.sort! { |a, b| b.last <=> a.last }
154   end
155
156   def current
157     return nil unless @curr_idx
158     @qas[@curr_idx]
159   end
160
161   def next
162     # don't advance if there are no further QAs
163     return nil if @curr_idx == @qas.length - 1
164     if @curr_idx
165       @curr_idx += 1
166     else
167       @curr_idx = 0
168     end
169     return current
170   end
171
172   def check(whatever, o={})
173     cur = self.current
174     return nil unless cur
175     if @must_buy.include?(whatever) and not o[:buy]
176       return whatever
177     end
178     return cur.check(whatever)
179   end
180
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]
185   end
186
187   def finish_add_qa(ans)
188     return nil unless @pending
189     @pending.answer = ans.dup
190     @qas << @pending
191     @pending = nil
192     return @qas.last
193   end
194 end
195
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")
200
201   def initialize
202     super
203     # TODO load/save running games?
204     @games = Hash.new
205   end
206
207   def setup_game(m, p)
208     chan = p[:chan] || m.channel
209     if !chan
210       m.reply _("you must specify a channel")
211       return
212     end
213     ch = chan.irc_downcase(m.server.casemap).intern
214
215     if game = @games[ch]
216       m.reply _("there's already a %{name} game on %{chan}, managed by %{who}") % {
217         :name => game.name,
218         :chan => chan,
219         :who => game.manager
220       }
221       return
222     end
223     name = p[:name].to_s
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)") % {
227       :name => game.name,
228       :who => game.manager,
229       :max => game.max,
230       :single => game.single,
231       :price => game.price
232     }
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>\"") % {
234       :chan => chan
235     }
236   end
237
238   def setup_qa(m, p)
239     ch = p[:chan].irc_downcase(m.server.casemap).intern
240     if !@games.key?(ch)
241       m.reply _("there's no %{name} game running on %{chan}") % {
242         :name => @bot.config['wheelfortune.game_name'],
243         :chan => p[:chan]
244       }
245       return
246     end
247     game = @games[ch]
248     cat = p[:cat].to_s
249     clue = p[:clue].to_s
250     ans = p[:ans].to_s
251     if ans.include?('*')
252       m.reply _("sorry, the answer cannot contain the '*' character")
253       return
254     end
255
256     if !clue.empty?
257       worked, qa = game.start_add_qa(cat, clue)
258       if worked
259         str = ans.empty? ?  _("ok, new clue added for %{chan}: %{catclue}") : nil
260       else
261         str = _("there's already a pending clue for %{chan}: %{catclue}")
262       end
263       m.reply _(str) % { :chan => p[:chan], :catclue => qa.catclue } if str
264       return unless worked or !ans.empty?
265     end
266     if !ans.empty?
267       qa = game.finish_add_qa(ans)
268       if qa
269         str = _("ok, new QA added for %{chan}: %{catclue} => %{ans}")
270       else
271         str = _("there's no pending clue for %{chan}!")
272       end
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?
275     else
276       m.reply _("something went wrong, I can't seem to understand what you're trying to set up")
277     end
278   end
279
280   def announce(m, p={})
281     chan = p[:chan] || m.channel
282     ch = chan.irc_downcase(m.server.casemap).intern
283     if !@games.key?(ch)
284       m.reply _("there's no %{name} game running on %{chan}") % {
285         :name => @bot.config['wheelfortune.game_name'],
286         :chan => chan
287       }
288       return
289     end
290     game = @games[ch]
291     qa = p[:next] ? game.next : game.current
292     if !qa
293       m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
294         :name => game.name,
295         :chan => chan,
296         :who => game.manager
297       }
298       return
299     end
300
301     @bot.say chan, _("%{bold}%{color}%{name}%{bold}, round %{count}:%{nocolor} %{qa}") % {
302       :bold => Bold,
303       :color => Irc.color(:green),
304       :name => game.name,
305       :count => game.round,
306       :nocolor => Irc.color(),
307       :qa => qa.announcement,
308     }
309     game.running = true
310   end
311
312   def score_table(chan, game, opts={})
313     limit = opts[:limit] || -1
314     table = game.score_table[0..limit]
315     if table.length == 0
316       @bot.say chan, _("no scores")
317       return
318     end
319     nick_wd = table.map { |a| a.first.length }.max
320     score_wd = table.first.last.to_s.length
321     table.each { |t|
322       @bot.say chan, "%*s : %*u" % [nick_wd, t.first, score_wd, t.last]
323     }
324   end
325
326   def react_on_check(m, ch, game, check)
327     debug "check: #{check.inspect}"
328     case check
329     when nil
330       # can this happen?
331       warning "game #{game}, qa #{game.current} checked nil against #{m.message}"
332       return
333     when :used
334       # m.reply "STUPID! YOU SO STUPID!"
335       return
336     when *game.must_buy
337       m.nickreply _("You must buy the %{vowel}") % {
338         :vowel => check
339       }
340     when :wrong
341       return
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?
345       announce(m)
346     when :gotit
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
351       }
352       if want_more == :done
353         # max score reached
354         m.reply _("%{bold}%{color}%{name}%{bold}%{nocolor}: %{who} %{bold}wins%{bold} after %{count} rounds!\nThe final score is") % {
355           :bold => Bold,
356           :color => Irc.color(:green),
357           :who => m.sourcenick,
358           :name => game.name,
359           :count => game.round,
360           :nocolor => Irc.color()
361         }
362         score_table(m.channel, game)
363         @games.delete(ch)
364       else :more
365         m.reply _("%{bold}%{color}%{name}%{bold}, round %{count}%{nocolor} -- score so far:") % {
366           :bold => Bold,
367           :color => Irc.color(:green),
368           :name => game.name,
369           :count => game.round,
370           :nocolor => Irc.color()
371         }
372         score_table(m.channel, game)
373         announce(m, :next => true)
374       end
375     else
376       # can this happen?
377       warning "game #{game}, qa #{game.current} checked #{check} against #{m.message}"
378     end
379   end
380
381   def listen(m)
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)
388   end
389
390   def buy(m, p)
391     ch = m.channel.irc_downcase(m.server.casemap).intern
392     game = @games[ch]
393     if not game
394       m.reply _("there's no %{name} game running on %{chan}") % {
395         :name => @bot.config['wheelfortune.game_name'],
396         :chan => m.channel
397       }
398       return
399     elsif !game.running?
400       m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
401         :name => game.name,
402         :chan => chan,
403         :who => game.manager
404       }
405       return
406     else
407       vowel = p[:vowel]
408       bought = game.buy(m.source)
409       if bought
410         m.reply _("%{who} buys a %{vowel} for %{price} points") % {
411           :who => m.source,
412           :vowel => vowel,
413           :price => game.price
414         }
415         check = game.check(vowel, :buy => true)
416         react_on_check(m, ch, game, check)
417       else
418         m.reply _("you can't buy a %{vowel}, %{who}: it costs %{price} points and you only have %{score}") % {
419           :who => m.source,
420           :vowel => vowel,
421           :price => game.price,
422           :score => game.score(m.source)
423         }
424       end
425     end
426   end
427
428   def cancel(m, p)
429     ch = m.channel.irc_downcase(m.server.casemap).intern
430     if !@games.key?(ch)
431       m.reply _("there's no %{name} game running on %{chan}") % {
432         :name => @bot.config['wheelfortune.game_name'],
433         :chan => m.channel
434       }
435       return
436     end
437     do_cancel(ch)
438   end
439
440   def do_cancel(ch)
441     game = @games.delete(ch)
442     chan = ch.to_s
443     @bot.say chan, _("%{name} game cancelled after %{count} rounds. Partial score:") % {
444       :name => game.name,
445       :count => game.round
446     }
447     score_table(chan, game)
448   end
449
450   def cleanup
451     @games.each_key { |k| do_cancel(k) }
452     super
453   end
454 end
455
456 plugin = WheelOfFortune.new
457
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 }