contrib/dice.py code cleaning, update markup
Renames variable lparts to len_parts, removes redundant code
This commit is contained in:
parent
9b391fbe6b
commit
eef348b4ba
1 changed files with 20 additions and 20 deletions
|
|
@ -97,7 +97,7 @@ def roll_dice(dicenum, dicetype, modifier=None, conditional=None, return_tuple=F
|
||||||
if modifier:
|
if modifier:
|
||||||
# make sure to check types well before eval
|
# make sure to check types well before eval
|
||||||
mod, modvalue = modifier
|
mod, modvalue = modifier
|
||||||
if not mod in ('+', '-', '*', '/'):
|
if mod not in ('+', '-', '*', '/'):
|
||||||
raise TypeError("Non-supported dice modifier: %s" % mod)
|
raise TypeError("Non-supported dice modifier: %s" % mod)
|
||||||
modvalue = int(modvalue) # for safety
|
modvalue = int(modvalue) # for safety
|
||||||
result = eval("%s %s %s" % (result, mod, modvalue))
|
result = eval("%s %s %s" % (result, mod, modvalue))
|
||||||
|
|
@ -105,13 +105,13 @@ def roll_dice(dicenum, dicetype, modifier=None, conditional=None, return_tuple=F
|
||||||
if conditional:
|
if conditional:
|
||||||
# make sure to check types well before eval
|
# make sure to check types well before eval
|
||||||
cond, condvalue = conditional
|
cond, condvalue = conditional
|
||||||
if not cond in ('>', '<', '>=', '<=', '!=', '=='):
|
if cond not in ('>', '<', '>=', '<=', '!=', '=='):
|
||||||
raise TypeError("Non-supported dice result conditional: %s" % conditional)
|
raise TypeError("Non-supported dice result conditional: %s" % conditional)
|
||||||
condvalue = int(condvalue) # for safety
|
condvalue = int(condvalue) # for safety
|
||||||
outcome = eval("%s %s %s" % (result, cond, condvalue)) # True/False
|
outcome = eval("%s %s %s" % (result, cond, condvalue)) # True/False
|
||||||
diff = abs(result - condvalue)
|
diff = abs(result - condvalue)
|
||||||
if return_tuple:
|
if return_tuple:
|
||||||
return (result, outcome, diff, rolls)
|
return result, outcome, diff, rolls
|
||||||
else:
|
else:
|
||||||
if conditional:
|
if conditional:
|
||||||
return outcome
|
return outcome
|
||||||
|
|
@ -155,7 +155,7 @@ class CmdDice(default_cmds.MuxCommand):
|
||||||
locks = "cmd:all()"
|
locks = "cmd:all()"
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"Mostly parsing for calling the dice roller function"
|
"""Mostly parsing for calling the dice roller function"""
|
||||||
|
|
||||||
if not self.args:
|
if not self.args:
|
||||||
self.caller.msg("Usage: @dice <nr>d<sides> [modifier] [conditional]")
|
self.caller.msg("Usage: @dice <nr>d<sides> [modifier] [conditional]")
|
||||||
|
|
@ -163,35 +163,33 @@ class CmdDice(default_cmds.MuxCommand):
|
||||||
argstring = "".join(str(arg) for arg in self.args)
|
argstring = "".join(str(arg) for arg in self.args)
|
||||||
|
|
||||||
parts = [part for part in RE_PARTS.split(self.args) if part]
|
parts = [part for part in RE_PARTS.split(self.args) if part]
|
||||||
lparts = len(parts)
|
len_parts = len(parts)
|
||||||
|
|
||||||
ndice = 0
|
|
||||||
nsides = 0
|
|
||||||
modifier = None
|
modifier = None
|
||||||
conditional = None
|
conditional = None
|
||||||
|
|
||||||
if lparts < 3 or parts[1] != 'd':
|
if len_parts < 3 or parts[1] != 'd':
|
||||||
self.caller.msg("You must specify the die roll(s) as <nr>d<sides>. For example, 2d6 means rolling a 6-sided die 2 times.")
|
self.caller.msg("You must specify the die roll(s) as <nr>d<sides>."
|
||||||
|
" For example, 2d6 means rolling a 6-sided die 2 times.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Limit the number of dice and sides a character can roll to prevent server slow down and crashes
|
# Limit the number of dice and sides a character can roll to prevent server slow down and crashes
|
||||||
ndicelimit = 10000 # Maximum number of dice
|
ndicelimit = 10000 # Maximum number of dice
|
||||||
nsidelimit = 10000 # Maximum number of sides
|
nsidelimit = 10000 # Maximum number of sides
|
||||||
if int(parts[0]) > ndicelimit or int(parts[2]) > nsidelimit:
|
if int(parts[0]) > ndicelimit or int(parts[2]) > nsidelimit:
|
||||||
self.caller.msg("The maximum roll allowed is %sd%s." % (ndicelimit, nsidelimit))
|
self.caller.msg("The maximum roll allowed is %sd%s." % (ndicelimit, nsidelimit))
|
||||||
return
|
return
|
||||||
|
|
||||||
ndice, nsides = parts[0], parts[2]
|
ndice, nsides = parts[0], parts[2]
|
||||||
if lparts == 3:
|
if len_parts == 3:
|
||||||
# just something like 1d6
|
# just something like 1d6
|
||||||
pass
|
pass
|
||||||
elif lparts == 5:
|
elif len_parts == 5:
|
||||||
# either e.g. 1d6 + 3 or something like 1d6 > 3
|
# either e.g. 1d6 + 3 or something like 1d6 > 3
|
||||||
if parts[3] in ('+', '-', '*', '/'):
|
if parts[3] in ('+', '-', '*', '/'):
|
||||||
modifier = (parts[3], parts[4])
|
modifier = (parts[3], parts[4])
|
||||||
else: # assume it is a conditional
|
else: # assume it is a conditional
|
||||||
conditional = (parts[3], parts[4])
|
conditional = (parts[3], parts[4])
|
||||||
elif lparts == 7:
|
elif len_parts == 7:
|
||||||
# the whole sequence, e.g. 1d6 + 3 > 5
|
# the whole sequence, e.g. 1d6 + 3 > 5
|
||||||
modifier = (parts[3], parts[4])
|
modifier = (parts[3], parts[4])
|
||||||
conditional = (parts[5], parts[6])
|
conditional = (parts[5], parts[6])
|
||||||
|
|
@ -207,7 +205,8 @@ class CmdDice(default_cmds.MuxCommand):
|
||||||
conditional=conditional,
|
conditional=conditional,
|
||||||
return_tuple=True)
|
return_tuple=True)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.caller.msg("You need to enter valid integer numbers, modifiers and operators. {w%s{n was not understood." % self.args)
|
self.caller.msg("You need to enter valid integer numbers, modifiers and operators."
|
||||||
|
" |w%s|n was not understood." % self.args)
|
||||||
return
|
return
|
||||||
# format output
|
# format output
|
||||||
if len(rolls) > 1:
|
if len(rolls) > 1:
|
||||||
|
|
@ -217,12 +216,12 @@ class CmdDice(default_cmds.MuxCommand):
|
||||||
if outcome is None:
|
if outcome is None:
|
||||||
outcomestring = ""
|
outcomestring = ""
|
||||||
elif outcome:
|
elif outcome:
|
||||||
outcomestring = " This is a {gsuccess{n (by %s)." % diff
|
outcomestring = " This is a |gsuccess|n (by %s)." % diff
|
||||||
else:
|
else:
|
||||||
outcomestring = " This is a {rfailure{n (by %s)." % diff
|
outcomestring = " This is a |rfailure|n (by %s)." % diff
|
||||||
yourollstring = "You roll %s%s."
|
yourollstring = "You roll %s%s."
|
||||||
roomrollstring = "%s rolls %s%s."
|
roomrollstring = "%s rolls %s%s."
|
||||||
resultstring = " Roll(s): %s. Total result is {w%s{n."
|
resultstring = " Roll(s): %s. Total result is |w%s|n."
|
||||||
|
|
||||||
if 'secret' in self.switches:
|
if 'secret' in self.switches:
|
||||||
# don't echo to the room at all
|
# don't echo to the room at all
|
||||||
|
|
@ -250,11 +249,12 @@ class CmdDice(default_cmds.MuxCommand):
|
||||||
string += outcomestring
|
string += outcomestring
|
||||||
self.caller.location.msg_contents(string)
|
self.caller.location.msg_contents(string)
|
||||||
|
|
||||||
|
|
||||||
class DiceCmdSet(CmdSet):
|
class DiceCmdSet(CmdSet):
|
||||||
"""
|
"""
|
||||||
a small cmdset for testing purposes.
|
a small cmdset for testing purposes.
|
||||||
Add with @py self.cmdset.add("contrib.dice.DiceCmdSet")
|
Add with @py self.cmdset.add("contrib.dice.DiceCmdSet")
|
||||||
"""
|
"""
|
||||||
def at_cmdset_creation(self):
|
def at_cmdset_creation(self):
|
||||||
"Called when set is created"
|
"""Called when set is created"""
|
||||||
self.add(CmdDice())
|
self.add(CmdDice())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue