Add $random default inlinefunc because I expected it to be there

This commit is contained in:
Griatch 2020-07-17 14:25:24 +02:00
parent 98b040634b
commit f7b4193c84
5 changed files with 79 additions and 21 deletions

View file

@ -62,7 +62,7 @@ without arguments starts a full interactive Python console.
now accept any input, including generators and single values. now accept any input, including generators and single values.
- EvTable should now correctly handle columns with wider asian-characters in them. - EvTable should now correctly handle columns with wider asian-characters in them.
- Update Twisted requirement to >=2.3.0 to close security vulnerability - Update Twisted requirement to >=2.3.0 to close security vulnerability
- Add `$random` inlinefunc, supports minval,maxval arguments that can be ints and floats.
## Evennia 0.9 (2018-2019) ## Evennia 0.9 (2018-2019)

View file

@ -2406,7 +2406,13 @@ class CmdExamine(ObjManipCommand):
""" """
Helper function that creates a nice report about an object. Helper function that creates a nice report about an object.
returns a string. Args:
obj (any): Object to analyze.
avail_cmdset (CmdSet): Current cmdset for object.
Returns:
str: The formatted string.
""" """
string = "\n|wName/key|n: |c%s|n (%s)" % (obj.name, obj.dbref) string = "\n|wName/key|n: |c%s|n (%s)" % (obj.name, obj.dbref)
if hasattr(obj, "aliases") and obj.aliases.all(): if hasattr(obj, "aliases") and obj.aliases.all():
@ -2633,7 +2639,7 @@ class CmdExamine(ObjManipCommand):
if obj_attrs: if obj_attrs:
for attrname in obj_attrs: for attrname in obj_attrs:
# we are only interested in specific attributes # we are only interested in specific attributes
caller.msg(self.format_attributes(obj, attrname, crop=False)) caller.msg(self.format_attributes(obj, attrname, crop=False), options={"raw": True})
else: else:
session = None session = None
if obj.sessions.count(): if obj.sessions.count():

View file

@ -540,7 +540,8 @@ class TBRangeTurnHandler(DefaultScript):
room as its object. room as its object.
Fights persist until only one participant is left with any HP or all Fights persist until only one participant is left with any HP or all
remaining participants choose to end the combat with the 'disengage' command. remaining participants choose to end the combat with the 'disengage'
command.
""" """
def at_script_creation(self): def at_script_creation(self):

View file

@ -221,7 +221,9 @@ class SessionHandler(dict):
elif isinstance(data, (str, bytes)): elif isinstance(data, (str, bytes)):
data = _utf8(data) data = _utf8(data)
if _INLINEFUNC_ENABLED and not raw and isinstance(self, ServerSessionHandler): if (_INLINEFUNC_ENABLED
and not raw
and isinstance(self, ServerSessionHandler)):
# only parse inlinefuncs on the outgoing path (sessionhandler->) # only parse inlinefuncs on the outgoing path (sessionhandler->)
data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session)

View file

@ -62,6 +62,7 @@ Error handling:
import re import re
import fnmatch import fnmatch
import random as base_random
from django.conf import settings from django.conf import settings
from evennia.utils import utils, logger from evennia.utils import utils, logger
@ -69,6 +70,53 @@ from evennia.utils import utils, logger
# example/testing inline functions # example/testing inline functions
def random(*args, **kwargs):
"""
Inlinefunc. Returns a random number between
0 and 1, from 0 to a maximum value, or within a given range (inclusive).
Args:
minval (str, optional): Minimum value. If not given, assumed 0.
maxval (str, optional): Maximum value.
Keyword argumuents:
session (Session): Session getting the string.
Notes:
If either of the min/maxvalue has a '.' in it, a floating-point random
value will be returned. Otherwise it will be an integer value in the
given range.
Example:
`$random()`
`$random(5)`
`$random(5, 10)`
"""
nargs = len(args)
if nargs == 1:
# only maxval given
minval, maxval = '0', args[0]
elif nargs > 1:
minval, maxval = args[:2]
else:
minval, maxval = ('0', '1')
if "." in minval or "." in maxval:
# float mode
try:
minval, maxval = float(minval), float(maxval)
except ValueError:
minval, maxval = 0, 1
return "{:.2f}".format(minval + maxval * base_random.random())
else:
# int mode
try:
minval, maxval = int(minval), int(maxval)
except ValueError:
minval, maxval = 0, 1
return str(base_random.randint(minval, maxval))
def pad(*args, **kwargs): def pad(*args, **kwargs):
""" """
@ -79,7 +127,8 @@ def pad(*args, **kwargs):
width (str, optional): Will be converted to integer. Width width (str, optional): Will be converted to integer. Width
of padding. of padding.
align (str, optional): Alignment of padding; one of 'c', 'l' or 'r'. align (str, optional): Alignment of padding; one of 'c', 'l' or 'r'.
fillchar (str, optional): Character used for padding. Defaults to a space. fillchar (str, optional): Character used for padding. Defaults to a
space.
Kwargs: Kwargs:
session (Session): Session performing the pad. session (Session): Session performing the pad.