Merge branch 'master' into develop

This commit is contained in:
Griatch 2018-01-27 14:20:13 +01:00
commit 4376058ee9
11 changed files with 201 additions and 111 deletions

View file

@ -1,6 +1,7 @@
"""
General Character commands usually available to all characters
"""
import re
from django.conf import settings
from evennia.utils import utils, evtable
from evennia.typeclasses.attributes import NickTemplateInvalid
@ -75,37 +76,41 @@ class CmdLook(COMMAND_DEFAULT_CLASS):
class CmdNick(COMMAND_DEFAULT_CLASS):
"""
define a personal alias/nick
define a personal alias/nick by defining a string to
match and replace it with another on the fly
Usage:
nick[/switches] <string> [= [replacement_string]]
nick[/switches] <template> = <replacement_template>
nick/delete <string> or number
nick/test <test string>
nicks
Switches:
inputline - replace on the inputline (default)
object - replace on object-lookup
account - replace on account-lookup
delete - remove nick by name or by index given by /list
clearall - clear all nicks
list - show all defined aliases (also "nicks" works)
test - test input to see what it matches with
delete - remove nick by index in /list
clearall - clear all nicks
Examples:
nick hi = say Hello, I'm Sarah!
nick/object tom = the tall man
nick build $1 $2 = @create/drop $1;$2 - (template)
nick tell $1 $2=@page $1=$2 - (template)
nick build $1 $2 = @create/drop $1;$2
nick tell $1 $2=@page $1=$2
nick tm?$1=@page tallman=$1
nick tm\=$1=@page tallman=$1
A 'nick' is a personal string replacement. Use $1, $2, ... to catch arguments.
Put the last $-marker without an ending space to catch all remaining text. You
can also use unix-glob matching:
can also use unix-glob matching for the left-hand side <string>:
* - matches everything
? - matches a single character
[seq] - matches all chars in sequence
[!seq] - matches everything not in sequence
? - matches 0 or 1 single characters
[abcd] - matches these chars in any order
[!abcd] - matches everything not among these chars
\= - escape literal '=' you want in your <string>
Note that no objects are actually renamed or changed by this command - your nicks
are only available to you. If you want to permanently add keywords to an object
@ -116,14 +121,36 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
aliases = ["nickname", "nicks"]
locks = "cmd:all()"
def parse(self):
"""
Support escaping of = with \=
"""
super(CmdNick, self).parse()
args = (self.lhs or "") + (" = %s" % self.rhs if self.rhs else "")
parts = re.split(r"(?<!\\)=", args, 1)
self.rhs = None
if len(parts) < 2:
self.lhs = parts[0].strip()
else:
self.lhs, self.rhs = [part.strip() for part in parts]
self.lhs = self.lhs.replace("\=", "=")
def func(self):
"""Create the nickname"""
caller = self.caller
switches = self.switches
nicktypes = [switch for switch in switches if switch in ("object", "account", "inputline")] or ["inputline"]
def _cy(string):
"add color to the special markers"
return re.sub(r"(\$[0-9]+|\*|\?|\[.+?\])", r"|Y\1|n", string)
nicklist = utils.make_iter(caller.nicks.get(return_obj=True) or [])
caller = self.caller
account = self.caller.account or caller
switches = self.switches
nicktypes = [switch for switch in switches if switch in (
"object", "account", "inputline")] or ["inputline"]
nicklist = (utils.make_iter(caller.nicks.get(category="inputline", return_obj=True) or []) +
utils.make_iter(caller.nicks.get(category="object", return_obj=True) or []) +
utils.make_iter(account.nicks.get(category="account", return_obj=True) or []))
if 'list' in switches or self.cmdstring in ("nicks", "@nicks"):
@ -133,24 +160,51 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
table = evtable.EvTable("#", "Type", "Nick match", "Replacement")
for inum, nickobj in enumerate(nicklist):
_, _, nickvalue, replacement = nickobj.value
table.add_row(str(inum + 1), nickobj.db_category, nickvalue, replacement)
table.add_row(str(inum + 1), nickobj.db_category, _cy(nickvalue), _cy(replacement))
string = "|wDefined Nicks:|n\n%s" % table
caller.msg(string)
return
if 'clearall' in switches:
caller.nicks.clear()
caller.account.nicks.clear()
caller.msg("Cleared all nicks.")
return
if 'delete' in switches or 'del' in switches:
if not self.args or not self.lhs:
caller.msg("usage nick/delete #num ('nicks' for list)")
return
# see if a number was given
arg = self.args.lstrip("#")
if arg.isdigit():
# we are given a index in nicklist
delindex = int(arg)
if 0 < delindex <= len(nicklist):
oldnick = nicklist[delindex - 1]
_, _, old_nickstring, old_replstring = oldnick.value
else:
caller.msg("Not a valid nick index. See 'nicks' for a list.")
return
nicktype = oldnick.category
nicktypestr = "%s-nick" % nicktype.capitalize()
if nicktype == "account":
account.nicks.remove(old_nickstring, category=nicktype)
else:
caller.nicks.remove(old_nickstring, category=nicktype)
caller.msg("%s removed: '|w%s|n' -> |w%s|n." % (
nicktypestr, old_nickstring, old_replstring))
return
if not self.args or not self.lhs:
caller.msg("Usage: nick[/switches] nickname = [realname]")
return
# setting new nicks
nickstring = self.lhs
replstring = self.rhs
old_nickstring = None
old_replstring = None
if replstring == nickstring:
caller.msg("No point in setting nick same as the string to replace...")
@ -160,47 +214,40 @@ class CmdNick(COMMAND_DEFAULT_CLASS):
errstring = ""
string = ""
for nicktype in nicktypes:
oldnick = caller.nicks.get(key=nickstring, category=nicktype, return_obj=True)
if nicktype == "account":
obj = account
else:
obj = caller
nicktypestr = "%s-nick" % nicktype.capitalize()
old_nickstring = None
old_replstring = None
oldnick = obj.nicks.get(key=nickstring, category=nicktype, return_obj=True)
if oldnick:
_, _, old_nickstring, old_replstring = oldnick.value
else:
# no old nick, see if a number was given
arg = self.args.lstrip("#")
if arg.isdigit():
# we are given a index in nicklist
delindex = int(arg)
if 0 < delindex <= len(nicklist):
oldnick = nicklist[delindex - 1]
_, _, old_nickstring, old_replstring = oldnick.value
else:
errstring += "Not a valid nick index."
else:
errstring += "Nick not found."
if "delete" in switches or "del" in switches:
# clear the nick
if old_nickstring and caller.nicks.has(old_nickstring, category=nicktype):
caller.nicks.remove(old_nickstring, category=nicktype)
string += "\nNick removed: '|w%s|n' -> |w%s|n." % (old_nickstring, old_replstring)
else:
errstring += "\nNick '|w%s|n' was not deleted." % old_nickstring
elif replstring:
if replstring:
# creating new nick
errstring = ""
if oldnick:
string += "\nNick '|w%s|n' updated to map to '|w%s|n'." % (old_nickstring, replstring)
if replstring == old_replstring:
string += "\nIdentical %s already set." % nicktypestr.lower()
else:
string += "\n%s '|w%s|n' updated to map to '|w%s|n'." % (
nicktypestr, old_nickstring, replstring)
else:
string += "\nNick '|w%s|n' mapped to '|w%s|n'." % (nickstring, replstring)
string += "\n%s '|w%s|n' mapped to '|w%s|n'." % (nicktypestr, nickstring, replstring)
try:
caller.nicks.add(nickstring, replstring, category=nicktype)
obj.nicks.add(nickstring, replstring, category=nicktype)
except NickTemplateInvalid:
caller.msg("You must use the same $-markers both in the nick and in the replacement.")
return
elif old_nickstring and old_replstring:
# just looking at the nick
string += "\nNick '|w%s|n' maps to '|w%s|n'." % (old_nickstring, old_replstring)
string += "\n%s '|w%s|n' maps to '|w%s|n'." % (nicktypestr, old_nickstring, old_replstring)
errstring = ""
string = errstring if errstring else string
caller.msg(string)
caller.msg(_cy(string))
class CmdInventory(COMMAND_DEFAULT_CLASS):