mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-22 02:32:42 +01:00
Updated for the new release.
This commit is contained in:
parent
af9f1ca174
commit
f14341ef26
@ -42,22 +42,31 @@ import utils
|
|||||||
import ircmsgs
|
import ircmsgs
|
||||||
import ircutils
|
import ircutils
|
||||||
import privmsgs
|
import privmsgs
|
||||||
|
import registry
|
||||||
import callbacks
|
import callbacks
|
||||||
|
|
||||||
|
|
||||||
def configure(onStart, afterConnect, advanced):
|
def configure(advanced):
|
||||||
# This will be called by setup.py to configure this module. onStart and
|
|
||||||
# afterConnect are both lists. Append to onStart the commands you would
|
|
||||||
# like to be run when the bot is started; append to afterConnect the
|
|
||||||
# commands you would like to be run when the bot has finished connecting.
|
|
||||||
from questions import expect, anything, something, yn
|
from questions import expect, anything, something, yn
|
||||||
onStart.append('load Random')
|
conf.registerPlugin('Random', True)
|
||||||
if yn('Do you want to specify a seed to be used for the RNG')=='y':
|
if yn('Do you want to specify a seed to be used for the RNG'):
|
||||||
seed = something('What seed? It must be an int or long.')
|
seed = something('What seed? It must be an integer or long.')
|
||||||
while not seed.isdigit():
|
while not seed.isdigit():
|
||||||
print 'That\'s not a valid seed.'
|
print 'That\'s not a valid seed.'
|
||||||
seed = something('What seed?')
|
seed = something('What seed?')
|
||||||
onStart.append('seed %s' % seed)
|
conf.supybot.plugins.Random.seed.setValue(seed)
|
||||||
|
|
||||||
|
class Seed(registry.Value):
|
||||||
|
def set(self, s):
|
||||||
|
try:
|
||||||
|
self.setValue(long(s))
|
||||||
|
except ValueError:
|
||||||
|
raise registry.InvalidRegistryValue, 'Value must be an integer.'
|
||||||
|
|
||||||
|
conf.registerPlugin('Random')
|
||||||
|
conf.registerGlobalValue(conf.supybot.plugins.Random, 'seed', Seed(0, """
|
||||||
|
Sets the seed of the random number generator. The seen must be a valid
|
||||||
|
Python integer or long."""))
|
||||||
|
|
||||||
class Random(callbacks.Privmsg):
|
class Random(callbacks.Privmsg):
|
||||||
rng = random.Random()
|
rng = random.Random()
|
||||||
@ -67,7 +76,7 @@ class Random(callbacks.Privmsg):
|
|||||||
Returns the next random number from the random number
|
Returns the next random number from the random number
|
||||||
generator.
|
generator.
|
||||||
"""
|
"""
|
||||||
irc.reply(msg, str(self.rng.random()))
|
irc.reply(str(self.rng.random()))
|
||||||
|
|
||||||
def seed(self, irc, msg, args):
|
def seed(self, irc, msg, args):
|
||||||
"""<seed>
|
"""<seed>
|
||||||
@ -80,10 +89,10 @@ class Random(callbacks.Privmsg):
|
|||||||
seed = long(seed)
|
seed = long(seed)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# It wasn't a valid long!
|
# It wasn't a valid long!
|
||||||
irc.error(msg, '<seed> must be a valid int or long.')
|
irc.error('<seed> must be a valid int or long.')
|
||||||
return
|
return
|
||||||
self.rng.seed(seed)
|
self.rng.seed(seed)
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.replySuccess()
|
||||||
|
|
||||||
def range(self, irc, msg, args):
|
def range(self, irc, msg, args):
|
||||||
"""<start> <end>
|
"""<start> <end>
|
||||||
@ -96,10 +105,10 @@ class Random(callbacks.Privmsg):
|
|||||||
end = int(end)
|
end = int(end)
|
||||||
start = int(start)
|
start = int(start)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, '<start> and <end> must both be integers.')
|
irc.error('<start> and <end> must both be integers.')
|
||||||
return
|
return
|
||||||
# .randrange() doesn't include the endpoint, so we use end+1.
|
# .randrange() doesn't include the endpoint, so we use end+1.
|
||||||
irc.reply(msg, str(self.rng.randrange(start, end+1)))
|
irc.reply(str(self.rng.randrange(start, end+1)))
|
||||||
|
|
||||||
def sample(self, irc, msg, args):
|
def sample(self, irc, msg, args):
|
||||||
"""<number of items> [<text> ...]
|
"""<number of items> [<text> ...]
|
||||||
@ -113,14 +122,14 @@ class Random(callbacks.Privmsg):
|
|||||||
except IndexError: # raised by .pop(0)
|
except IndexError: # raised by .pop(0)
|
||||||
raise callbacks.ArgumentError
|
raise callbacks.ArgumentError
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, '<number of items> must be an integer.')
|
irc.error('<number of items> must be an integer.')
|
||||||
return
|
return
|
||||||
if n > len(args):
|
if n > len(args):
|
||||||
irc.error(msg, '<number of items> must be less than the number '
|
irc.error('<number of items> must be less than the number '
|
||||||
'of arguments.')
|
'of arguments.')
|
||||||
return
|
return
|
||||||
sample = self.rng.sample(args, n)
|
sample = self.rng.sample(args, n)
|
||||||
irc.reply(msg, utils.commaAndify(map(repr, sample)))
|
irc.reply(utils.commaAndify(map(repr, sample)))
|
||||||
|
|
||||||
def diceroll(self, irc, msg, args):
|
def diceroll(self, irc, msg, args):
|
||||||
"""[<number of sides>]
|
"""[<number of sides>]
|
||||||
@ -134,7 +143,7 @@ class Random(callbacks.Privmsg):
|
|||||||
n = 6
|
n = 6
|
||||||
n = int(n)
|
n = int(n)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'Dice have integer numbers of sides. Use one.')
|
irc.error('Dice have integer numbers of sides. Use one.')
|
||||||
return
|
return
|
||||||
s = 'rolls a %s' % self.rng.randrange(1, n)
|
s = 'rolls a %s' % self.rng.randrange(1, n)
|
||||||
irc.queueMsg(ircmsgs.action(ircutils.replyTo(msg), s))
|
irc.queueMsg(ircmsgs.action(ircutils.replyTo(msg), s))
|
||||||
|
Loading…
Reference in New Issue
Block a user