messing with dice
This commit is contained in:
parent
136acd126d
commit
c21b358f9b
1 changed files with 6 additions and 5 deletions
|
@ -14,7 +14,7 @@ sign_re = re.compile(r'[+-]?(?:\d*d)?(?:\d+|F)', re.I)
|
||||||
split_re = re.compile(r'([\d+-]*)d?(F|\d*)', re.I)
|
split_re = re.compile(r'([\d+-]*)d?(F|\d*)', re.I)
|
||||||
|
|
||||||
|
|
||||||
def nrolls(count, n):
|
def n_rolls(count, n):
|
||||||
"""roll an n-sided die count times"""
|
"""roll an n-sided die count times"""
|
||||||
if n == "F":
|
if n == "F":
|
||||||
return [random.randint(-1, 1) for x in xrange(min(count, 100))]
|
return [random.randint(-1, 1) for x in xrange(min(count, 100))]
|
||||||
|
@ -36,7 +36,7 @@ def nrolls(count, n):
|
||||||
#@hook.regex(valid_diceroll, re.I)
|
#@hook.regex(valid_diceroll, re.I)
|
||||||
@hook.command
|
@hook.command
|
||||||
def dice(inp):
|
def dice(inp):
|
||||||
"""dice <diceroll> -- Simulates dicerolls. Example of <diceroll>:
|
"""dice <dice roll> -- Simulates dice rolls. Example of <dice roll>:
|
||||||
'dice 2d20-d5+4 roll 2'. D20s, subtract 1D5, add 4"""
|
'dice 2d20-d5+4 roll 2'. D20s, subtract 1D5, add 4"""
|
||||||
|
|
||||||
try: # if inp is a re.match object...
|
try: # if inp is a re.match object...
|
||||||
|
@ -59,7 +59,7 @@ def dice(inp):
|
||||||
count, side = split_re.match(roll).groups()
|
count, side = split_re.match(roll).groups()
|
||||||
count = int(count) if count not in " +-" else 1
|
count = int(count) if count not in " +-" else 1
|
||||||
if side.upper() == "F": # fudge dice are basically 1d3-2
|
if side.upper() == "F": # fudge dice are basically 1d3-2
|
||||||
for fudge in nrolls(count, "F"):
|
for fudge in n_rolls(count, "F"):
|
||||||
if fudge == 1:
|
if fudge == 1:
|
||||||
rolls.append("\x033+\x0F")
|
rolls.append("\x033+\x0F")
|
||||||
elif fudge == -1:
|
elif fudge == -1:
|
||||||
|
@ -73,14 +73,15 @@ def dice(inp):
|
||||||
side = int(side)
|
side = int(side)
|
||||||
try:
|
try:
|
||||||
if count > 0:
|
if count > 0:
|
||||||
d = nrolls(count, side)
|
d = n_rolls(count, side)
|
||||||
rolls += map(str, d)
|
rolls += map(str, d)
|
||||||
total += sum(d)
|
total += sum(d)
|
||||||
else:
|
else:
|
||||||
d = nrolls(-count, side)
|
d = n_rolls(-count, side)
|
||||||
rolls += [str(-x) for x in d]
|
rolls += [str(-x) for x in d]
|
||||||
total -= sum(d)
|
total -= sum(d)
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
|
# I have never seen this happen. If you make this happen, you win a cookie
|
||||||
return "Thanks for overflowing a float, jerk >:["
|
return "Thanks for overflowing a float, jerk >:["
|
||||||
|
|
||||||
if desc:
|
if desc:
|
||||||
|
|
Reference in a new issue