]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins/roshambo.rb
move rbot into lib - still rearranging for packaging/installation
[user/henk/code/ruby/rbot.git] / lib / rbot / plugins / roshambo.rb
1 # Play the game of roshambo (rock-paper-scissors)
2 # Copyright (C) 2004 Hans Fugal
3 # Distributed under the same license as rbot itself
4 require 'time'
5 class RoshamboPlugin < Plugin
6   def initialize
7     super 
8     @scoreboard = {}
9   end
10   def help(plugin, topic="")
11     "roshambo <rock|paper|scissors> => play roshambo"
12   end
13   def privmsg(m)
14     # simultaneity
15     choice = choose
16
17     # init scoreboard
18     if (not @scoreboard.has_key?(m.sourcenick) or (Time.now - @scoreboard[m.sourcenick]['timestamp']) > 3600)
19       @scoreboard[m.sourcenick] = {'me'=>0,'you'=>0,'timestamp'=>Time.now}
20     end
21     case m.params
22     when 'rock','paper','scissors'
23       s = score(choice,m.params)
24       @scoreboard[m.sourcenick]['timestamp'] = Time.now
25       myscore=@scoreboard[m.sourcenick]['me']
26       yourscore=@scoreboard[m.sourcenick]['you']
27       case s
28       when 1
29         yourscore=@scoreboard[m.sourcenick]['you'] += 1
30         m.reply "#{choice}. You win. Score: me #{myscore} you #{yourscore}"
31       when 0
32         m.reply "#{choice}. We tie. Score: me #{myscore} you #{yourscore}"
33       when -1
34         myscore=@scoreboard[m.sourcenick]['me'] += 1
35         m.reply "#{choice}! I win! Score: me #{myscore} you #{yourscore}"
36       end
37     else
38       m.reply "incorrect usage: " + help(m.plugin)
39     end
40   end
41       
42   def choose
43     ['rock','paper','scissors'][rand(3)]
44   end
45   def score(a,b)
46     beats = {'rock'=>'scissors', 'paper'=>'rock', 'scissors'=>'paper'}
47     return -1 if beats[a] == b
48     return 1 if beats[b] == a
49     return 0
50   end
51 end
52 plugin = RoshamboPlugin.new
53 plugin.register("roshambo")
54 plugin.register("rps")