# -*- mode: ruby; -*- #=begin header # "Dice Thrower" agent # # $Author: knu $ # $Date: 2001/01/31 15:31:07 $ # # Copyright (C) 1998-2000 Hiroshi IGARASHI # # #This script was translated from a perl script: # madoka 4.0 yr_dice # yuurii/dice.mpi # (c)1998 cookie # #It responds to phrases like "2d6" and throws dice. #=end agent = IRC::PassiveAgent::new("Dice Thrower") class << agent def start(client) super end def stop super end def notifyMessage(message) #p(message) case message.command when CMD_PRIVMSG #putlog("debug", "recieved PRIVMSG.") dice(message.params[0], message.prefix, message.trailing) end end private DICE_ERR = [ '', 'The number of dice is invalid.', 'Sorry, I don\'t have such kind of dice.', 'Sorry, I don\'t have so many dice.', 'Oops, dice fell out of the table!', 'Divided by zero.', ] def _dice(src) srand err = 0 lim = {4=>3, 6=>20, 8=>10, 10=>20, 12=>2, 20=>5, 30=>2, 100=>2} n, m = *(src.split(/d/i)) n = Integer(n) if m == '%' m = 100 l = 0 else m = Integer(m) l = 1 end err = 1 if n <= 0 # or n > Integer(n) err = 2 if lim[m].nil? # $err = 3 if ($n > $nmax || $n > $lim{$m}); # no use err = 4 if rand(1000) < n * 5 dices = [] dice = 0 for i in 0..(n - 1) dices[i] = dice if i < 10 dice += rand(m) + l dices[i] = dice - dices[i] if i < 10 end m = '%' if l == 0 [err, n, m, dice, dices] end def dice(chanr, from2, mes) fromc = User::parse(from2).nick if chanr == @client.nick chan = from2 else chan = chanr end if mes =~ /^\s*([\d\s\t\.dD\(\)\+\-\*\/\%]+)\s*$/ d1 = $1 d2 = d1.dup if d2 =~ /d/i d1.tr!('D', 'd') while d1 =~ /\d+d(\d+|%)/ src = $& leprintln("src:"+src) if $DEBUG err, n, m, d, ds = *_dice(src) d1.sub!(src, d) d1.gsub!(/\(([\d+\.]+)\)/, $1) d2.sub!(src, "#{d}(#{n}D#{m})") leprintln("d1:"+d1) if $DEBUG leprintln("d2:"+d2) if $DEBUG end end sleep(2) if err > 0 privmsg("#{fromc}: #{DICE_ERR[err]}", chan) else privmsg("#{fromc}: #{d2} = " + d1.to_s, chan) if d end end end end agent