2016-03-30 09:28:49 +02:00
|
|
|
"""
|
|
|
|
games.py: Create a bot that provides game functionality (dice, 8ball, etc).
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2016-04-07 15:32:45 +02:00
|
|
|
import random
|
2016-05-01 02:09:06 +02:00
|
|
|
|
2016-03-30 09:28:49 +02:00
|
|
|
import utils
|
|
|
|
from log import log
|
|
|
|
import world
|
|
|
|
|
2016-05-15 20:58:45 +02:00
|
|
|
gameclient = utils.registerService("Games", manipulatable=True)
|
2016-05-14 23:52:47 +02:00
|
|
|
reply = gameclient.reply # TODO find a better syntax for ServiceBot.reply()
|
2016-04-08 01:06:35 +02:00
|
|
|
|
|
|
|
# commands
|
2016-05-14 23:52:47 +02:00
|
|
|
def dice(irc, source, args):
|
|
|
|
"""<num>d<sides>
|
2016-04-07 15:32:45 +02:00
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
Rolls a die with <sides> sides <num> times.
|
|
|
|
"""
|
|
|
|
if not args:
|
|
|
|
reply(irc, "No string given.")
|
|
|
|
return
|
2016-04-07 15:32:45 +02:00
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
try:
|
|
|
|
# Split num and sides and convert them to int.
|
|
|
|
num, sides = map(int, args[0].split('d', 1))
|
|
|
|
except ValueError:
|
|
|
|
# Invalid syntax. Show the command help.
|
2016-05-15 01:19:29 +02:00
|
|
|
gameclient.help(irc, source, ['dice'])
|
2016-05-14 23:52:47 +02:00
|
|
|
return
|
2016-04-07 15:32:45 +02:00
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
assert 1 < sides <= 100, "Invalid side count (must be 2-100)."
|
|
|
|
assert 1 <= num <= 100, "Cannot roll more than 100 dice at once."
|
2016-04-07 15:32:45 +02:00
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
results = []
|
|
|
|
for _ in range(num):
|
|
|
|
results.append(random.randint(1, sides))
|
2016-04-07 15:09:56 +02:00
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
# Convert results to strings, join them, format, and reply.
|
|
|
|
s = 'You rolled %s: %s (total: %s)' % (args[0], ' '.join([str(x) for x in results]), sum(results))
|
|
|
|
reply(irc, s)
|
2016-04-07 15:09:56 +02:00
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
gameclient.add_cmd(dice, 'd')
|
|
|
|
gameclient.add_cmd(dice)
|
2016-04-07 15:09:56 +02:00
|
|
|
|
2016-04-07 14:28:47 +02:00
|
|
|
# loading
|
2016-03-30 09:28:49 +02:00
|
|
|
def main(irc=None):
|
|
|
|
"""Main function, called during plugin loading at start."""
|
|
|
|
|
2016-04-07 15:32:45 +02:00
|
|
|
# seed the random
|
|
|
|
random.seed()
|
|
|
|
|
2016-05-14 23:52:47 +02:00
|
|
|
def die(irc):
|
|
|
|
utils.unregisterService('games')
|