]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/wheelfortune.rb
plugin(wheelfortune): remove botdata dependency
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / wheelfortune.rb
index 57d5491559d8d12baf632549da219d245d05b019..20213e5d6c674a0808934e7eaf3a6d084397eb50 100644 (file)
@@ -9,11 +9,18 @@
 
 # Wheel-of-Fortune Question/Answer
 class WoFQA
-  attr_accessor :cat, :clue, :answer, :hint
+  attr_accessor :cat, :clue, :hint
+  attr_reader :answer
+  attr_accessor :guessed
   def initialize(cat, clue, ans=nil)
     @cat = cat # category
     @clue = clue # clue phrase
     self.answer = ans
+    @guessed = false
+  end
+
+  def guessed?
+    @guessed
   end
 
   def catclue
@@ -42,8 +49,15 @@ class WoFQA
   end
 
   def announcement
-    ret = self.catclue << "\n"
-    ret << _("Letters called so far: ") << @used.join(" ") << "\n" unless @used.empty?
+    ret = self.catclue
+    if !@used.empty?
+      ret << _(" [Letters called so far: %{red}%{letters}%{nocolor}]") % {
+        :red => Irc.color(:red),
+        :letters => @used.join(" "),
+        :nocolor => Irc.color()
+      }
+    end
+    ret << "\n"
     ret << @hint.join
   end
 
@@ -79,28 +93,74 @@ end
 
 # Wheel-of-Fortune game
 class WoFGame
-  attr_reader :manager, :single, :max, :pending
-  def initialize(manager, single, max)
+  attr_reader :name, :manager, :single, :max, :pending
+  attr_writer :running
+  attr_accessor :must_buy, :price
+  def initialize(name, manager, single, max)
+    @name = name.dup
     @manager = manager
     @single = single.to_i
     @max = max.to_i
     @pending = nil
     @qas = []
     @curr_idx = nil
-    @last_replied = nil
+    @running = false
     @scores = Hash.new
+
+    # the default is to make vowels usable only
+    # after paying a price in points which is
+    # a fraction of the single round score equal
+    # to the number of rounds needed to win the game
+    # TODO customize
+    @must_buy = %w{a e i o u y}
+    @price = @single*@single/@max
   end
 
-  def waiting?
-    !@curr_idx || (@last_replied == @curr_idx)
+  def running?
+    @running
   end
 
   def round
-    @curr_idx+1
+    @curr_idx+1 rescue 0
+  end
+
+  def length
+    @qas.length
+  end
+
+  def qa(round)
+    if @pending and round == self.length + 1
+      @pending
+    else
+      @qas[round-1]
+    end
+  end
+
+  def buy(user)
+    k = user.botuser
+    if @scores.key?(k) and @scores[k][:score] >= @price
+      @scores[k][:score] -= @price
+      return true
+    else
+      return false
+    end
+  end
+
+  def score(user)
+    k = user.botuser
+    if @scores.key?(k)
+      @scores[k][:score]
+    else
+      0
+    end
+  end
+
+  def mark_guessed(qa)
+    qa.guessed = true
   end
 
   def mark_winner(user)
-    @last_replied = @curr_idx
+    @running = false
     k = user.botuser
     if @scores.key?(k)
       @scores[k][:nick] = user.nick
@@ -139,9 +199,12 @@ class WoFGame
     return current
   end
 
-  def check(whatever)
+  def check(whatever, o={})
     cur = self.current
     return nil unless cur
+    if @must_buy.include?(whatever) and not o[:buy]
+      return whatever
+    end
     return cur.check(whatever)
   end
 
@@ -161,32 +224,62 @@ class WoFGame
 end
 
 class WheelOfFortune < Plugin
+  Config.register Config::StringValue.new('wheelfortune.game_name',
+    :default => 'Wheel Of Fortune',
+    :desc => "default name of the Wheel Of Fortune game")
+
   def initialize
     super
     # TODO load/save running games?
     @games = Hash.new
   end
 
+  def help(plugin, topic="")
+    case topic
+    when 'play'
+      _("wof [<channel>] play [<name>] for <single> to <max> => starts a wheel-of-fortune game on channel <channel> (default: current channel), named <name> (default: wheelfortune.game_name config setting, or the last game name used by the user), with <single> points per round. the game is won when a player reachers <max> points. vowels cost <single>*<single>/<max> points. The user that starts the game is the game manager and must set up the clues and answers in private. All the other users have to learn the answer to each clue by saying single consonants or the whole sentence. Every time a consonant is guessed, the bot will reveal the partial answer, showing the missing letters as * (asterisks).")
+    when 'category', 'clue', 'answer'
+      _("wof <channel> [category: <cat>,] clue: <clue>, answer: <ans> => set up a new question for the wheel-of-fortune game being played on channel <channel>. This command must be sent in private by the game manager. The category <cat> can be omitted. If you make mistakes, you can use 'wof replace' (see help) before the question gets asked")
+    when 'replace'
+      _("wof <channel> replace <round> [category: <cat>,] [clue: <clue>,] [answer: <ans>] => fix the question for round <round> of the wheel-of-fortune game being played on <channel> by replacing the category and/or clue and/or answer")
+    when 'cancel'
+      _("wof cancel => cancels the wheel-of-fortune being played on the current channel")
+    when 'buy'
+      _("wof buy <vowel> => buy the vowel <vowel>: the user buying the vowel will lose points equal to the vowel price, and the corresponding vowel will be revealed in the answer (if present)")
+    else
+      _("wof: wheel-of-fortune plugin. topics: play, category, clue, answer, replace, cancel, buy")
+    end
+  end
+
   def setup_game(m, p)
     chan = p[:chan] || m.channel
     if !chan
       m.reply _("you must specify a channel")
       return
     end
-    ch = p[:chan].irc_downcase(m.server.casemap).intern
+    ch = chan.irc_downcase(m.server.casemap).intern
 
-    if @games.key?(ch)
-      m.reply _("there's already a Wheel-of-Fortune game on %{chan}, managed by %{who}") % {
+    if game = @games[ch]
+      m.reply _("there's already a %{name} game on %{chan}, managed by %{who}") % {
+        :name => game.name,
         :chan => chan,
-        :who => @games[ch].manager
+        :who => game.manager
       }
       return
     end
-    @games[ch] = game = WoFGame.new(m.botuser, p[:single], p[:max])
-    @bot.say chan, _("%{who} just created a new Wheel-of-Fortune game to %{max} points (%{single} per question)") % {
+    name = p[:name].to_s
+    if name.empty?
+      name = @registry["game_name_#{m.source.to_s}"] || @bot.config['wheelfortune.game_name']
+    else
+      @registry["game_name_#{m.source.to_s}"] = name
+    end
+    @games[ch] = game = WoFGame.new(name, m.botuser, p[:single], p[:max])
+    @bot.say chan, _("%{who} just created a new %{name} game to %{max} points (%{single} per question, %{price} per vowel)") % {
+      :name => game.name,
       :who => game.manager,
       :max => game.max,
-      :single => game.single
+      :single => game.single,
+      :price => game.price
     }
     @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>\"") % {
       :chan => chan
@@ -196,12 +289,21 @@ class WheelOfFortune < Plugin
   def setup_qa(m, p)
     ch = p[:chan].irc_downcase(m.server.casemap).intern
     if !@games.key?(ch)
-      m.reply _("there's no Wheel-of-Fortune game running on %{chan}") % {
+      m.reply _("there's no %{name} game running on %{chan}") % {
+        :name => @bot.config['wheelfortune.game_name'],
         :chan => p[:chan]
       }
       return
     end
     game = @games[ch]
+
+    if m.botuser != game.manager and !m.botuser.permit?('wheelfortune::manage::other::add')
+      m.reply _("you can't add questions to the %{name} game on %{chan}") % {
+        :name => game.name,
+        :chan => p[:chan]
+      }
+    end
+
     cat = p[:cat].to_s
     clue = p[:clue].to_s
     ans = p[:ans].to_s
@@ -213,63 +315,157 @@ class WheelOfFortune < Plugin
     if !clue.empty?
       worked, qa = game.start_add_qa(cat, clue)
       if worked
-        str = ans.empty? ?  _("ok, new clue added for %{chan}: %{catclue}") : nil
+        str = ans.empty? ?  _("ok, clue added for %{name} round %{count} on %{chan}: %{catclue}") : nil
       else
-        str = _("there's already a pending clue for %{chan}: %{catclue}")
+        str = _("there's already a pending clue for %{name} round %{count} on %{chan}: %{catclue}")
       end
-      m.reply _(str) % { :chan => p[:chan], :catclue => qa.catclue } if str
-      return unless worked or !ans.empty?
+      m.reply _(str) % {
+        :chan => p[:chan],
+        :catclue => qa.catclue,
+        :name => game.name,
+        :count => game.length+1
+      } if str
+      return unless worked and !ans.empty?
     end
     if !ans.empty?
       qa = game.finish_add_qa(ans)
       if qa
-        str = _("ok, new QA added for %{chan}: %{catclue} => %{ans}")
+        str = _("ok, QA added for %{name} round %{count} on %{chan}: %{catclue} => %{ans}")
       else
-        str = _("there's no pending clue for %{chan}!")
+        str = _("there's no pending clue for %{name} on %{chan}!")
       end
-      m.reply _(str) % { :chan => p[:chan], :catclue => qa ? qa.catclue : nil, :ans => qa ? qa.answer : nil}
-      announce(m, p.merge({ :next => true }) ) if game.waiting?
+      m.reply _(str) % {
+        :chan => p[:chan],
+        :catclue => qa ? qa.catclue : nil,
+        :ans => qa ? qa.answer : nil,
+        :name => game.name,
+        :count => game.length
+      }
+      announce(m, p) unless game.running?
     else
-      m.reply _("something went wrong, I can't seem to understand what you're trying to set up")
+      m.reply _("something went wrong, I can't seem to understand what you're trying to set up") if clue.empty?
+    end
+  end
+
+  def replace_qa(m, p)
+    ch = p[:chan].irc_downcase(m.server.casemap).intern
+    if !@games.key?(ch)
+      m.reply _("there's no %{name} game running on %{chan}") % {
+        :name => @bot.config['wheelfortune.game_name'],
+        :chan => p[:chan]
+      }
+      return
+    end
+    game = @games[ch]
+
+    if m.botuser != game.manager and !m.botuser.permit?('wheelfortune::manage::other::add')
+      m.reply _("you can't replace questions to the %{name} game on %{chan}") % {
+        :name => game.name,
+        :chan => p[:chan]
+      }
+    end
+
+    round = p[:round].to_i
+
+    min = game.round
+    max = game.length
+    max += 1 if game.pending
+    if round <= min or round > max
+      if min == max
+        m.reply _("there are no questions in the %{name} game on %{chan} which can be replaced") % {
+          :name => game.name,
+          :chan => p[:chan]
+        }
+      else
+        m.reply _("you can only replace questions between rounds %{min} and %{max} in the %{name} game on %{chan}") % {
+          :name => game.name,
+          :min => min,
+          :max => max,
+          :chan => p[:chan]
+        }
+      end
+      return
+    end
+
+    cat = p[:cat].to_s
+    clue = p[:clue].to_s
+    ans = p[:ans].to_s
+    if ans.include?('*')
+      m.reply _("sorry, the answer cannot contain the '*' character")
+      return
     end
+
+    qa = game.qa(round)
+    qa.cat = cat unless cat.empty?
+    qa.clue = clue unless clue.empty?
+    unless ans.empty?
+      if game.pending and round == max
+        game.finish_add_qa(ans)
+      else
+        qa.answer = ans
+      end
+    end
+
+    str = _("ok, replaced QA for %{name} round %{count} on %{chan}: %{catclue} => %{ans}")
+    m.reply str % {
+      :chan => p[:chan],
+      :catclue => qa ? qa.catclue : nil,
+      :ans => qa ? qa.answer : nil,
+      :name => game.name,
+      :count => round
+    }
   end
 
   def announce(m, p={})
     chan = p[:chan] || m.channel
     ch = chan.irc_downcase(m.server.casemap).intern
     if !@games.key?(ch)
-      m.reply _("there's no Wheel-of-Fortune game running on %{chan}") % { :chan => p[:chan] }
+      m.reply _("there's no %{name} game running on %{chan}") % {
+        :name => @bot.config['wheelfortune.game_name'],
+        :chan => chan
+      }
       return
     end
     game = @games[ch]
-    qa = p[:next] ? game.next : game.current
+    qa = game.current
+    if !qa or qa.guessed?
+      qa = game.next
+    end
     if !qa
-      m.reply _("there are no Wheel-of-Fortune questions for %{chan}, I'm waiting for %{who} to add them") % {
+      m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
+        :name => game.name,
         :chan => chan,
         :who => game.manager
       }
       return
     end
 
-    @bot.say chan, qa.announcement
+    @bot.say chan, _("%{bold}%{color}%{name}%{bold}, round %{count}:%{nocolor} %{qa}") % {
+      :bold => Bold,
+      :color => Irc.color(:green),
+      :name => game.name,
+      :count => game.round,
+      :nocolor => Irc.color(),
+      :qa => qa.announcement,
+    }
+    game.running = true
   end
 
   def score_table(chan, game, opts={})
     limit = opts[:limit] || -1
     table = game.score_table[0..limit]
+    if table.length == 0
+      @bot.say chan, _("no scores")
+      return
+    end
     nick_wd = table.map { |a| a.first.length }.max
-    score_wd = table.first.to_s.length
+    score_wd = table.first.last.to_s.length
     table.each { |t|
       @bot.say chan, "%*s : %*u" % [nick_wd, t.first, score_wd, t.last]
     }
   end
 
-  def listen(m)
-    return unless m.kind_of?(PrivMessage) and not m.address?
-    ch = m.channel.irc_downcase(m.server.casemap).intern
-    return unless game = @games[ch]
-    return if game.waiting?
-    check = game.check(m.message)
+  def react_on_check(m, ch, game, check)
     debug "check: #{check.inspect}"
     case check
     when nil
@@ -279,6 +475,8 @@ class WheelOfFortune < Plugin
     when :used
       # m.reply "STUPID! YOU SO STUPID!"
       return
+    when *game.must_buy
+      m.reply _("You must buy the %{vowel}") % {:vowel => check}, :nick => true
     when :wrong
       return
     when Numeric, :missing
@@ -286,6 +484,7 @@ class WheelOfFortune < Plugin
       # TODO what happens when the last hint reveals the whole answer?
       announce(m)
     when :gotit
+      game.mark_guessed(game.current)
       want_more = game.mark_winner(m.source)
       m.reply _("%{who} got it! The answer was: %{ans}") % {
         :who => m.sourcenick,
@@ -293,20 +492,26 @@ class WheelOfFortune < Plugin
       }
       if want_more == :done
         # max score reached
-
-        m.reply _("%{who} wins the game after %{count} rounds!") % {
-          :who => table.first.first,
-          :count => game.round
+        m.reply _("%{bold}%{color}%{name}%{bold}%{nocolor}: %{who} %{bold}wins%{bold} after %{count} rounds!\nThe final score is") % {
+          :bold => Bold,
+          :color => Irc.color(:green),
+          :who => m.sourcenick,
+          :name => game.name,
+          :count => game.round,
+          :nocolor => Irc.color()
         }
         score_table(m.channel, game)
         @games.delete(ch)
-      else :more
-        table = game.score_table
-        nick_wd = table.map { |a| a.first.length }.max
-        score_wd = table.first.to_s.length
-        m.reply _("Score after %{count} rounds") % { :count => game.round }
+      else
+        m.reply _("%{bold}%{color}%{name}%{bold}, round %{count}%{nocolor} -- score so far:") % {
+          :bold => Bold,
+          :color => Irc.color(:green),
+          :name => game.name,
+          :count => game.round,
+          :nocolor => Irc.color()
+        }
         score_table(m.channel, game)
-        announce(m, :next => true)
+        announce(m)
       end
     else
       # can this happen?
@@ -314,21 +519,78 @@ class WheelOfFortune < Plugin
     end
   end
 
+  def message(m)
+    return if m.address?
+    ch = m.channel.irc_downcase(m.server.casemap).intern
+    return unless game = @games[ch]
+    return unless game.running?
+    return unless game.current and not game.current.guessed?
+    check = game.check(m.message, :buy => false)
+    react_on_check(m, ch, game, check)
+  end
+
+  def buy(m, p)
+    ch = m.channel.irc_downcase(m.server.casemap).intern
+    game = @games[ch]
+    if not game
+      m.reply _("there's no %{name} game running on %{chan}") % {
+        :name => @bot.config['wheelfortune.game_name'],
+        :chan => m.channel
+      }
+      return
+    elsif !game.running?
+      m.reply _("there are no %{name} questions for %{chan}, I'm waiting for %{who} to add them") % {
+        :name => game.name,
+        :chan => chan,
+        :who => game.manager
+      }
+      return
+    else
+      vowel = p[:vowel]
+      bought = game.buy(m.source)
+      if bought
+        m.reply _("%{who} buys a %{vowel} for %{price} points") % {
+          :who => m.source,
+          :vowel => vowel,
+          :price => game.price
+        }
+        check = game.check(vowel, :buy => true)
+        react_on_check(m, ch, game, check)
+      else
+        m.reply _("you can't buy a %{vowel}, %{who}: it costs %{price} points and you only have %{score}") % {
+          :who => m.source,
+          :vowel => vowel,
+          :price => game.price,
+          :score => game.score(m.source)
+        }
+      end
+    end
+  end
+
   def cancel(m, p)
     ch = m.channel.irc_downcase(m.server.casemap).intern
     if !@games.key?(ch)
-      m.reply _("there's no Wheel-of-Fortune game running on %{chan}") % {
+      m.reply _("there's no %{name} game running on %{chan}") % {
+        :name => @bot.config['wheelfortune.game_name'],
         :chan => m.channel
       }
       return
     end
-    do_cancel(ch)
+    # is the botuser the manager or allowed to cancel someone else's game?
+    if m.botuser == @games[ch].manager or m.botuser.permit?('wheelfortune::manage::other::cancel')
+      do_cancel(ch)
+    else
+      m.reply _("you can't cancel the current game")
+    end
   end
 
   def do_cancel(ch)
     game = @games.delete(ch)
     chan = ch.to_s
-    @bot.say chan, _("Wheel-of-Fortune game cancelled after %{count} rounds. Partial score:")
+    @bot.say chan, _("%{name} game cancelled after %{count} rounds. Partial score:") % {
+      :name => game.name,
+      :count => game.round
+    }
     score_table(chan, game)
   end
 
@@ -342,6 +604,10 @@ plugin = WheelOfFortune.new
 
 plugin.map "wof", :action => 'announce', :private => false
 plugin.map "wof cancel", :action => 'cancel', :private => false
-plugin.map "wof [:chan] play for :single [points] to :max [points]", :action => 'setup_game'
+plugin.map "wof [:chan] play [*name] for :single [points] to :max [points]", :action => 'setup_game'
 plugin.map "wof :chan [category: *cat,] clue: *clue[, answer: *ans]", :action => 'setup_qa', :public => false
 plugin.map "wof :chan answer: *ans", :action => 'setup_qa', :public => false
+plugin.map "wof :chan replace :round [category: *cat,] clue: *clue[, answer: *ans]", :action => 'replace_qa', :public => false
+plugin.map "wof :chan replace :round [category: *cat,] answer: *ans", :action => 'replace_qa', :public => false
+plugin.map "wof :chan replace :round category: *cat[, clue: *clue[, answer: *ans]]", :action => 'replace_qa', :public => false
+plugin.map "wof buy :vowel", :action => 'buy', :requirements => { :vowel => /./u }