# Dice - roll dice when asked, intended for RPGs
# Copyright (C) 2010  Brian S. Stephan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import re
import random

from irclib import irclib

from Module import Module

# Rolls dice, for RPGs mostly

class Dice(Module):

    def __init__(self, config, server, modlist):
        super(Dice, self).__init__(config, server, modlist)
        modlist.append(self)

    def register_handlers(self, server):
        server.add_global_handler('pubmsg', self.on_pubmsg)
        server.add_global_handler('privmsg', self.on_privmsg)

    def unregister_handlers(self):
        self.server.remove_global_handler('pubmsg', self.on_pubmsg)
        self.server.remove_global_handler('privmsg', self.on_privmsg)

    def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
        overallroll = what

        rolls = re.split(';\s*', overallroll)
        for roll in rolls:
            pattern = '^(?:(\d+)#)?(?:(\d+)/)?(\d+)?d(\d+)(?:(\+|\-)(\d+))?(?:\s+(.*))?'
            regex = re.compile(pattern)
            matches = regex.search(roll)

            if matches is not None:
                # set variables including defaults for unspecified stuff
                faces = int(matches.group(4))
                comment = matches.group(7)

                if matches.group(1) is None:
                    times = 1
                else:
                    times = int(matches.group(1))

                if matches.group(3) is None:
                    dice = 1
                else:
                    dice = int(matches.group(3))

                if matches.group(2) is None:
                    top = dice
                else:
                    top = int(matches.group(2))

                if matches.group(5) is None or matches.group(6) is None:
                    modifier = 0
                else:
                    if str(matches.group(5)) == '-':
                        modifier = -1 * int(matches.group(6))
                    else:
                        modifier = int(matches.group(6))

                result = ''
                if comment is not None:
                    result += comment + ': '

                for t in range(times):
                    ressubstr = ""
                    rolls = []
                    for d in range(dice):
                        rolls.append(str(random.randint(1, faces)))
                    rolls.sort()
                    rolls.reverse()
                    ressubstr = ','.join(rolls[0:top])
                    sum = 0
                    for r in rolls[0:top]:
                        sum += int(r)
                    sumplus = sum + modifier
                    result += str(sumplus) + ' [' + ressubstr
                    if modifier != 0:
                        if modifier > 0:
                            result += ' + ' + str(modifier)
                        else:
                            result += ' - ' + str(-1 * modifier)
                    result += ']'

                    if t != times-1:
                        result += ', '

                return self.reply(connection, replypath, result)

# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;