예제 #1
0
def dicelog(willie, trigger):
    """
    .dice [logfile] <formula>  - Rolls dice using the XdY format, also does
    basic math and drop lowest (XdYvZ). Saves result in logfile if given.
    """
    if not trigger.group(2):
        return willie.reply('You have to specify the dice you wanna roll.')

    # extract campaign
    if trigger.group(2).startswith('['):
        campaign, rollStr = trigger.group(2)[1:].split(']')
    else:
        campaign = ''
        rollStr = trigger.group(2).strip()
    campaign = campaign.strip()
    rollStr = rollStr.strip()
    # prepare string for mathing
    arr = rollStr.lower().replace(' ', '')
    arr = arr.replace('-', ' - ').replace('+', ' + ').replace('/', ' / ')
    arr = arr.replace('*', ' * ').replace('(', ' ( ').replace(')', ' ) ')
    arr = arr.replace('^', ' ^ ').replace('()', '').split(' ')
    full_string, calc_string = '', ''

    for segment in arr:
        # check for dice
        result = re.search("([0-9]+m)?([0-9]*d[0-9]+)(v[0-9]+)?", segment)
        if result:
            # detect droplowest
            if result.group(3) is not None:
                #check for invalid droplowest
                dropLowest = int(result.group(3)[1:])
                # or makes implied 1dx to be evaluated in case of dx being typed
                if (dropLowest >= int(result.group(2).split('d')[0] or 1)):
                    willie.reply('You\'re trying to drop too many dice.')
                    return
            else:
                dropLowest = 0

            # on to rolling dice!
            value, drops = '(', ''
            # roll...
            dice = rollDice(result.group(2))
            for i in range(0, len(dice)):
                # format output
                if i < dropLowest:
                    if drops == '':
                        drops = '[+'
                    drops += str(dice[i])
                    if i < dropLowest - 1:
                        drops += '+'
                    else:
                        drops += ']'
                else:
                    value += str(dice[i])
                    if i != len(dice) - 1:
                        value += '+'
            no_dice = False
            value += drops + ')'
        else:
            value = segment
        full_string += value
    # and repeat

    # replace, split and join to exclude dropped dice from the math.
    result = calculate(''.join(
        full_string.replace('[', '#').replace(']', '#').split('#')[::2]))
    if result == 'Sorry, no result.':
        willie.reply('Calculation failed, did you try something weird?')
    elif (no_dice):
        willie.reply('For pure math, you can use .c ' + rollStr + ' = ' +
                     result)
    else:
        response = 'You roll ' + rollStr + ': ' + full_string + ' = ' + result
        willie.reply(response)
        campaign = campaign.strip().lower()
        if campaign:
            if campaign in willie.config.dicelog.campaigns.split(','):
                log = open(
                    os.path.join(willie.config.dicelog.logdir,
                                 campaign + '.log'), 'a')
                log.write("At <%s> %s rolled %s\n" %
                          (time.ctime(), trigger.nick, response[9:]))
                log.close()
            else:
                willie.reply("Didn't log because " + campaign +
                             " is not listed as a campaign. sorry!")
예제 #2
0
def dicelog(willie, trigger):
    """
    .dice [logfile] <formula>  - Rolls dice using the XdY format, also does
    basic math and drop lowest (XdYvZ). Saves result in logfile if given.
    """
    if not trigger.group(2):
        return willie.reply('You have to specify the dice you wanna roll.')

    # extract campaign
    if trigger.group(2).startswith('['):
        campaign, rollStr = trigger.group(2)[1:].split(']')
    else:
        campaign = ''
        rollStr = trigger.group(2).strip()
    campaign = campaign.strip()
    rollStr = rollStr.strip()
    # prepare string for mathing
    arr = rollStr.lower().replace(' ','')
    arr = arr.replace('-', ' - ').replace('+', ' + ').replace('/', ' / ')
    arr = arr.replace('*', ' * ').replace('(', ' ( ').replace(')', ' ) ')
    arr = arr.replace('^', ' ^ ').replace('()', '').split(' ')
    full_string, calc_string = '', ''

    for segment in arr:
        # check for dice
        result = re.search("([0-9]+m)?([0-9]*d[0-9]+)(v[0-9]+)?", segment)
        if result:
            # detect droplowest
            if result.group(3) is not None:
                #check for invalid droplowest
                dropLowest = int(result.group(3)[1:])
                # or makes implied 1dx to be evaluated in case of dx being typed
                if (dropLowest >= int(result.group(2).split('d')[0] or 1)):
                    willie.reply('You\'re trying to drop too many dice.')
                    return
            else:
                dropLowest = 0

            # on to rolling dice!
            value, drops = '(', ''
            # roll...
            dice = rollDice(result.group(2))
            for i in range(0, len(dice)):
                # format output
                if i < dropLowest:
                    if drops == '':
                        drops = '[+'
                    drops += str(dice[i])
                    if i < dropLowest - 1:
                        drops += '+'
                    else:
                        drops += ']'
                else:
                    value += str(dice[i])
                    if i != len(dice) - 1:
                        value += '+'
            no_dice = False
            value += drops + ')'
        else:
            value = segment
        full_string += value
    # and repeat

    # replace, split and join to exclude dropped dice from the math.
    result = calculate(''.join(
                full_string.replace('[', '#').replace(']', '#').split('#')[::2]))
    if result == 'Sorry, no result.':
        willie.reply('Calculation failed, did you try something weird?')
    elif(no_dice):
        willie.reply('For pure math, you can use .c '
                     + rollStr + ' = ' + result)
    else:
        response = 'You roll ' + rollStr + ': ' + full_string + ' = ' + result
        willie.reply(response)
        campaign = campaign.strip().lower()
        if campaign:
            if campaign in willie.config.dicelog.campaigns.split(','):
                log = open(os.path.join(willie.config.dicelog.logdir, campaign + '.log'), 'a')
                log.write("At <%s> %s rolled %s\n" % (time.ctime(), trigger.nick, response[9:]))
                log.close()
            else: willie.reply("Didn't log because " + campaign + " is not listed as a campaign. sorry!")
예제 #3
0
파일: dice.py 프로젝트: Kallerami/jambot
def dice(bot, trigger):
    """
    .dice <formula> - Rolls dice using the XdY format, also does basic math and
    drop lowest (XdYvZ).
    """
    no_dice = True
    if not trigger.group(2):
        return bot.reply('You have to specify the dice you wanna roll.')
    arr = trigger.group(2).lower().replace(' ', '')
    arr = arr.replace('-', ' - ').replace('+', ' + ').replace('/', ' / ')
    arr = arr.replace('*', ' * ').replace('(', ' ( ').replace(')', ' ) ')
    arr = arr.replace('^', ' ^ ').replace('()', '').split(' ')
    full_string = ''

    for segment in arr:
        #check for dice
        result = re.search("([0-9]+m)?([0-9]*d[0-9]+)(v[0-9]+)?", segment)
        if result:
            #detect droplowest
            if result.group(3) is not None:
                #check for invalid droplowest
                dropLowest = int(result.group(3)[1:])
                if(dropLowest >= int(result.group(2).split('d')[0] or 1)):
                    bot.reply('You\'re trying to drop too many dice.')
                    return
            else:
                dropLowest = 0
            #dicerolling
            value, drops = '(', ''
            dice = rollDice(result.group(2))
            for i in range(0, len(dice)):
                if i < dropLowest:
                    if drops == '':
                        drops = '[+'
                    drops += str(dice[i])
                    if i < dropLowest - 1:
                        drops += '+'
                    else:
                        drops += ']'
                else:
                    value += str(dice[i])
                    if i != len(dice) - 1:
                        value += '+'
            no_dice = False
            value += drops + ')'
        else:
            value = segment
        full_string += value
    #repeat next segment

    #we're replacing, splitting and joining to exclude []'s from the math.
    result = calculate(''.join(
                full_string.replace('[', '#').replace(']', '#').split('#')[::2]))
    if result == 'Sorry, no result.':
        bot.reply('Calculation failed, did you try something weird?')
    elif(no_dice):
        bot.reply('For pure math, you can use .c '
                     + trigger.group(2) + ' = ' + result)
    else:
        bot.reply('You roll ' + trigger.group(2) + ': ' + full_string + ' = '
                     + result)
예제 #4
0
파일: dice.py 프로젝트: Shokodemon/willie
def dice(willie, trigger):
    """.dice <formula> - Rolls dice using the XdY format, also does basic math and drop lowest (XdYvZ)."""
    no_dice = True
    if trigger.group(2) == None:
        return willie.reply("You have to specify the dice you wanna roll.")
    arr = trigger.group(2).lower().strip(" ")
    arr = (
        arr.replace("-", " - ")
        .replace("+", " + ")
        .replace("/", " / ")
        .replace("*", " * ")
        .replace("(", " ( ")
        .replace(")", " ) ")
        .replace("^", " ^ ")
        .replace("()", "")
        .split(" ")
    )
    full_string, calc_string = "", ""

    for segment in arr:
        # check for dice
        result = re.search("([0-9]+m)?([0-9]*d[0-9]+)(v[0-9]+)?", segment)
        if result:
            # detect droplowest
            if result.group(3) is not None:
                # check for invalid droplowest
                dropLowest = int(result.group(3)[1:])
                if result.group(2).lower().startswith("d"):
                    if dropLowest != 0:
                        willie.reply("You're trying to drop too many dice.")
                        return
                elif dropLowest >= int("0" + result.group(2).lower().split("d")[0]):
                    willie.reply("You're trying to drop too many dice.")
                    return
            else:
                dropLowest = 0
            # dicerolling
            value, drops = "(", ""
            dice = rollDice(result.group(2).lower())
            for i in range(0, len(dice)):
                if i < dropLowest:
                    if drops == "":
                        drops = "[+"
                    drops += str(dice[i])
                    if i < dropLowest - 1:
                        drops += "+"
                    else:
                        drops += "]"
                else:
                    value += str(dice[i])
                    if i != len(dice) - 1:
                        value += "+"
            no_dice = False
            value += drops + ")"
        else:
            value = segment
        full_string += value
    # repeat next segment

    # we're replacing, splitting and joining to exclude []'s from the math.
    result = calculate("".join(full_string.replace("[", "#").replace("]", "#").split("#")[::2]))
    if result == "Sorry, no result.":
        willie.reply("Calculation failed, did you try something weird?")
    elif no_dice:
        willie.reply("For pure math, you can use .c " + trigger.group(2) + " = " + result)
    else:
        willie.reply("You roll " + trigger.group(2) + " (" + full_string + "): " + result)