]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/roshambo.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / roshambo.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Roshambo (rock-paper-scissors) plugin for rbot
5 #
6 # Author:: Hans Fugal
7 # Copyright:: (C) 2004 Hans Fugal
8 #
9 # Play the game of roshambo (rock-paper-scissors)
10 #
11 # Distributed under the same license as rbot itself
12
13 require 'time'
14
15 class RoshamboPlugin < Plugin
16
17   def initialize
18     super 
19     @scoreboard = {}
20     @beats = { :rock => :scissors, :paper => :rock, :scissors => :paper}
21     @plays = @beats.keys
22   end
23
24   def help(plugin, topic="")
25     "roshambo <rock|paper|scissors> or rps <rock|paper|scissors> => play roshambo"
26   end
27
28   def rps(m, params)
29     # simultaneity
30     bot_choice = @plays.pick_one
31
32     # init scoreboard
33     if not @scoreboard.has_key?(m.sourcenick) or (Time.now - @scoreboard[m.sourcenick]['timestamp']) > 3600
34       @scoreboard[m.sourcenick] = { 'me' => 0, 'you' => 0, 'timestamp' => Time.now }
35     end
36     human_choice = params[:play].to_sym
37     s = score(bot_choice, human_choice)
38     @scoreboard[m.sourcenick]['timestamp'] = Time.now
39     myscore=@scoreboard[m.sourcenick]['me']
40     yourscore=@scoreboard[m.sourcenick]['you']
41     case s
42     when 1
43       yourscore = @scoreboard[m.sourcenick]['you'] += 1
44       m.reply "#{bot_choice}. You win. Score: me #{myscore} you #{yourscore}"
45     when 0
46       m.reply "#{bot_choice}. We tie. Score: me #{myscore} you #{yourscore}"
47     when -1
48       myscore = @scoreboard[m.sourcenick]['me'] += 1
49       m.reply "#{bot_choice}! I win! Score: me #{myscore} you #{yourscore}"
50     end
51   end
52
53   def score(bot_choice, human_choice)
54     return -1 if @beats[bot_choice] == human_choice
55     return 1 if @beats[human_choice] == bot_choice
56     return 0
57   end
58 end
59
60 plugin = RoshamboPlugin.new
61 plugin.map "roshambo :play", :action => :rps, :requirements => { :play => /rock|paper|scissors/ }
62 plugin.map "rps :play", :action => :rps, :requirements => { :play => /rock|paper|scissors/ }