Created
February 10, 2018 22:16
-
-
Save zhudotexe/eeeec23bfabb468b3716d8fa7eac6add to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_resistances(damage, resistances, immunities, vulnerabilities): | |
COMMENT_REGEX = r'\[(?P<comment>.*?)\]' | |
ROLL_STRING_REGEX = r'\[.*?]' | |
comments = re.findall(COMMENT_REGEX, damage) | |
roll_strings = re.split(ROLL_STRING_REGEX, damage) | |
formatted_comments = [] | |
formatted_roll_strings = [] | |
for t, _ in enumerate(comments): | |
if not roll_strings[t].replace(' ', '') == '': | |
formatted_roll_strings.append(roll_strings[t]) | |
formatted_comments.append(comments[t]) | |
else: | |
if len(formatted_comments) > 0: | |
formatted_comments[-1] += ' ' + comments[t] | |
else: | |
pass # eh, it'll error anyway | |
if not roll_strings[-1].replace(' ', '') == '': | |
formatted_roll_strings.append(roll_strings[-1]) | |
if formatted_comments: | |
formatted_comments.append(formatted_comments[-1]) # carry over thingies | |
else: | |
formatted_comments.append('') | |
for index, comment in enumerate(formatted_comments): | |
roll_string = formatted_roll_strings[index].replace(' ', '') | |
preop = '' | |
if roll_string[0] in '-+*/().<>=': # case: +6[blud] | |
preop = roll_string[0] | |
roll_string = roll_string[1:] | |
if not comment.endswith('^'): | |
for resistance in resistances: | |
if resistance.lower() in comment.lower() and len(resistance) > 0: | |
roll_string = '({0}) / 2'.format(roll_string) | |
break | |
for immunity in immunities: | |
if immunity.lower() in comment.lower() and len(immunity) > 0: | |
roll_string = '({0}) * 0'.format(roll_string) | |
break | |
for vulnerability in vulnerabilities: | |
if vulnerability.lower() in comment.lower() and len(vulnerability) > 0: | |
roll_string = '({0}) * 2'.format(roll_string) | |
break | |
formatted_roll_strings[index] = '{0}{1}{2}'.format(preop, roll_string, | |
"[{}]".format(comment) if comment is not '' else "") | |
if formatted_roll_strings: | |
damage = ''.join(formatted_roll_strings) | |
return damage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment