Worked out some edge cases in masking sdescs, some other side cases remains.
This commit is contained in:
parent
d62dd9afab
commit
7625306b44
1 changed files with 37 additions and 29 deletions
|
|
@ -71,6 +71,7 @@ Tall man (assuming his name is Tom) sees:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from re import escape as re_escape
|
||||||
import itertools
|
import itertools
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from evennia import DefaultObject, DefaultCharacter
|
from evennia import DefaultObject, DefaultCharacter
|
||||||
|
|
@ -211,9 +212,9 @@ def ordered_permutation_regex(sentence):
|
||||||
elif comb:
|
elif comb:
|
||||||
break
|
break
|
||||||
if comb:
|
if comb:
|
||||||
solution.append(_PREFIX + r"[0-9]*%s*" % _NUM_SEP + " ".join(comb))
|
solution.append(_PREFIX + r"[0-9]*%s*%s(?=\s|$)+" % (_NUM_SEP, re_escape(" ".join(comb)).rstrip("\\")))
|
||||||
|
|
||||||
# compile into a match regex, first matching the longest down to the shortest components
|
# combine into a match regex, first matching the longest down to the shortest components
|
||||||
regex = r"|".join(sorted(set(solution), key=lambda o:len(o), reverse=True))
|
regex = r"|".join(sorted(set(solution), key=lambda o:len(o), reverse=True))
|
||||||
return regex
|
return regex
|
||||||
|
|
||||||
|
|
@ -335,6 +336,7 @@ def parse_sdescs_and_recogs(sender, candidates, string, search_mode=False):
|
||||||
# we scan backwards so we can replace in-situ without messing
|
# we scan backwards so we can replace in-situ without messing
|
||||||
# up later occurrences. Given a marker match, query from
|
# up later occurrences. Given a marker match, query from
|
||||||
# start index forward for all candidates.
|
# start index forward for all candidates.
|
||||||
|
#print "marker_match:", marker_match.re.pattern, marker_match.groups()
|
||||||
|
|
||||||
# first see if there is a number given (e.g. 1-tall)
|
# first see if there is a number given (e.g. 1-tall)
|
||||||
num_identifier, _ = marker_match.groups("") # return "" if no match, rather than None
|
num_identifier, _ = marker_match.groups("") # return "" if no match, rather than None
|
||||||
|
|
@ -346,6 +348,8 @@ def parse_sdescs_and_recogs(sender, candidates, string, search_mode=False):
|
||||||
# loop over all candidate regexes and match against the string following the match
|
# loop over all candidate regexes and match against the string following the match
|
||||||
matches = ((reg.match(string[istart:]), obj, text) for reg, obj, text in candidate_regexes)
|
matches = ((reg.match(string[istart:]), obj, text) for reg, obj, text in candidate_regexes)
|
||||||
|
|
||||||
|
#print "matches:", [m[0].re.pattern for m in matches if m[0]]
|
||||||
|
|
||||||
# score matches by how long part of the string was matched
|
# score matches by how long part of the string was matched
|
||||||
matches = [(match.end() if match else -1, obj, text) for match, obj, text in matches]
|
matches = [(match.end() if match else -1, obj, text) for match, obj, text in matches]
|
||||||
maxscore = max(score for score, obj, text in matches)
|
maxscore = max(score for score, obj, text in matches)
|
||||||
|
|
@ -354,6 +358,7 @@ def parse_sdescs_and_recogs(sender, candidates, string, search_mode=False):
|
||||||
bestmatches = [(obj, text) for score, obj, text in matches if maxscore == score != -1]
|
bestmatches = [(obj, text) for score, obj, text in matches if maxscore == score != -1]
|
||||||
nmatches = len(bestmatches)
|
nmatches = len(bestmatches)
|
||||||
|
|
||||||
|
|
||||||
if not nmatches:
|
if not nmatches:
|
||||||
# no matches
|
# no matches
|
||||||
obj = None
|
obj = None
|
||||||
|
|
@ -440,14 +445,10 @@ def send_emote(sender, receivers, emote, anonymous_add="first"):
|
||||||
sender.msg(err.message)
|
sender.msg(err.message)
|
||||||
return
|
return
|
||||||
|
|
||||||
# convert to sdescs
|
if anonymous_add and not "#%i" % sender.id in obj_mapping:
|
||||||
sdesc_mapping = dict((ref, obj.sdesc.get() if hasattr(obj, "sdesc") else obj.key)
|
|
||||||
for ref, obj in obj_mapping.iteritems())
|
|
||||||
|
|
||||||
if anonymous_add and not "#%i" % sender.id in sdesc_mapping:
|
|
||||||
# no self-reference in the emote - add to the end
|
# no self-reference in the emote - add to the end
|
||||||
key = "#%i" % sender.id
|
key = "#%i" % sender.id
|
||||||
sdesc_mapping[key] = sender
|
obj_mapping[key] = sender
|
||||||
if anonymous_add == 'first':
|
if anonymous_add == 'first':
|
||||||
possessive = "" if emote.startswith('\'') else " "
|
possessive = "" if emote.startswith('\'') else " "
|
||||||
emote = "%s%s%s" % ("{%s}" % key, possessive, emote)
|
emote = "%s%s%s" % ("{%s}" % key, possessive, emote)
|
||||||
|
|
@ -457,12 +458,12 @@ def send_emote(sender, receivers, emote, anonymous_add="first"):
|
||||||
# broadcast emote to everyone
|
# broadcast emote to everyone
|
||||||
for receiver in receivers:
|
for receiver in receivers:
|
||||||
# we make a temporary copy that we can modify
|
# we make a temporary copy that we can modify
|
||||||
mapping = copy(sdesc_mapping)
|
|
||||||
# overload mapping with receiver's recogs (which is on the same form)
|
|
||||||
try:
|
try:
|
||||||
mapping.update(receiver.recog.ref2recog)
|
recog_get = receiver.recog.get
|
||||||
|
mapping = dict((ref, recog_get(obj)) for ref, obj in obj_mapping.items())
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
mapping = dict((ref, obj.sdesc.get() if hasattr(obj, "sdesc") else obj.key)
|
||||||
|
for ref, obj in obj_mapping.items())
|
||||||
# handle the language mapping, which always produce different keys ##nn
|
# handle the language mapping, which always produce different keys ##nn
|
||||||
try:
|
try:
|
||||||
process_language = receiver.process_language
|
process_language = receiver.process_language
|
||||||
|
|
@ -678,7 +679,7 @@ class RecogHandler(object):
|
||||||
return self.obj2recog.get(obj, obj.sdesc.get()
|
return self.obj2recog.get(obj, obj.sdesc.get()
|
||||||
if hasattr(obj, "sdesc") else obj.key)
|
if hasattr(obj, "sdesc") else obj.key)
|
||||||
else:
|
else:
|
||||||
# recog_mask logk not passed, disable recog
|
# recog_mask log not passed, disable recog
|
||||||
return obj.sdesc.get() if hasattr(obj, "sdesc") else obj.key
|
return obj.sdesc.get() if hasattr(obj, "sdesc") else obj.key
|
||||||
|
|
||||||
def remove(self, obj):
|
def remove(self, obj):
|
||||||
|
|
@ -699,7 +700,7 @@ class RecogHandler(object):
|
||||||
Returns:
|
Returns:
|
||||||
rec (tuple): Tuple (recog_regex, obj, recog)
|
rec (tuple): Tuple (recog_regex, obj, recog)
|
||||||
"""
|
"""
|
||||||
if obj in self.obj2recog:
|
if obj in self.obj2recog and obj.access(self.obj, "enable_recog", default=True):
|
||||||
return self.obj2regex[obj], obj, self.obj2regex[obj]
|
return self.obj2regex[obj], obj, self.obj2regex[obj]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -776,10 +777,10 @@ class CmdSdesc(RPCommand): # set/look at own sdesc
|
||||||
# strip non-alfanum chars from end of sdesc
|
# strip non-alfanum chars from end of sdesc
|
||||||
sdesc = _RE_CHAREND.sub("", self.args)
|
sdesc = _RE_CHAREND.sub("", self.args)
|
||||||
sdesc = caller.sdesc.add(sdesc)
|
sdesc = caller.sdesc.add(sdesc)
|
||||||
caller.msg("Your sdesc was set to '%s'." % sdesc)
|
caller.msg("%s's sdesc was set to '%s'." % (caller.key, sdesc))
|
||||||
|
|
||||||
|
|
||||||
class CmdPose(Command): # set current pose and default pose
|
class CmdPose(RPCommand): # set current pose and default pose
|
||||||
"""
|
"""
|
||||||
Set a static pose
|
Set a static pose
|
||||||
|
|
||||||
|
|
@ -877,7 +878,7 @@ class CmdPose(Command): # set current pose and default pose
|
||||||
caller.msg("Pose will read '%s %s'." % (target_name, pose))
|
caller.msg("Pose will read '%s %s'." % (target_name, pose))
|
||||||
|
|
||||||
|
|
||||||
class CmdRecog(Command): # assign personal alias to object in room
|
class CmdRecog(RPCommand): # assign personal alias to object in room
|
||||||
"""
|
"""
|
||||||
Recognize another person in the same room.
|
Recognize another person in the same room.
|
||||||
|
|
||||||
|
|
@ -886,25 +887,28 @@ class CmdRecog(Command): # assign personal alias to object in room
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
recog tall man as Griatch
|
recog tall man as Griatch
|
||||||
|
forget griatch
|
||||||
|
|
||||||
This will assign a personal alias for a person.
|
This will assign a personal alias for a person, or
|
||||||
|
forget said alias.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
key = "recog"
|
key = "recog"
|
||||||
aliases = ["recognize"]
|
aliases = ["recognize", "forget"]
|
||||||
|
|
||||||
def parse(self):
|
def parse(self):
|
||||||
"Parse for the sdesc as alias structure"
|
"Parse for the sdesc as alias structure"
|
||||||
if "as" in self.args:
|
if "as" in self.args:
|
||||||
self.sdesc, self.alias = [part.strip() for part in self.args.split(" as ", 2)]
|
self.sdesc, self.alias = [part.strip() for part in self.args.split(" as ", 2)]
|
||||||
else:
|
elif self.args:
|
||||||
self.sdesc = self.alias = None
|
self.sdesc = self.args.strip()
|
||||||
|
self.alias = ""
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"Assign the recog"
|
"Assign the recog"
|
||||||
caller = self.caller
|
caller = self.caller
|
||||||
if not all((self.args, self.sdesc, self.alias)):
|
if not self.args:
|
||||||
caller.msg("Usage: recog <sdesc> as <alias>")
|
caller.msg("Usage: recog <sdesc> as <alias> or forget <alias>")
|
||||||
return
|
return
|
||||||
sdesc = self.sdesc
|
sdesc = self.sdesc
|
||||||
alias = self.alias.rstrip(".?!")
|
alias = self.alias.rstrip(".?!")
|
||||||
|
|
@ -922,13 +926,17 @@ class CmdRecog(Command): # assign personal alias to object in room
|
||||||
for inum, obj in enumerate(matches)]
|
for inum, obj in enumerate(matches)]
|
||||||
caller.msg(_EMOTE_MULTIMATCH_ERROR.format(ref=sdesc,reflist="\n ".join(reflist)))
|
caller.msg(_EMOTE_MULTIMATCH_ERROR.format(ref=sdesc,reflist="\n ".join(reflist)))
|
||||||
else:
|
else:
|
||||||
# we have all we need, add the recog alias
|
|
||||||
obj = matches[0]
|
obj = matches[0]
|
||||||
sdesc = obj.sdesc.get() if hasattr(obj, "sdesc") else obj.key
|
if self.cmdstring == "forget":
|
||||||
alias = caller.recog.add(obj, alias)
|
# remove existing recog
|
||||||
caller.msg("%s will now remember {w%s{n as {w%s{n." % (caller.key, sdesc, alias))
|
caller.recog.remove(obj)
|
||||||
|
caller.msg("%s will know only '%s'." % (caller.key, obj.recog.get(obj)))
|
||||||
|
else:
|
||||||
|
sdesc = obj.sdesc.get() if hasattr(obj, "sdesc") else obj.key
|
||||||
|
alias = caller.recog.add(obj, alias)
|
||||||
|
caller.msg("%s will now remember {w%s{n as {w%s{n." % (caller.key, sdesc, alias))
|
||||||
|
|
||||||
class CmdMask(Command):
|
class CmdMask(RPCommand):
|
||||||
"""
|
"""
|
||||||
Wear a mask
|
Wear a mask
|
||||||
|
|
||||||
|
|
@ -1052,7 +1060,7 @@ class ContribRPObject(DefaultObject):
|
||||||
if "nofound_string" in kwargs:
|
if "nofound_string" in kwargs:
|
||||||
self.msg(kwargs["nofound_string"])
|
self.msg(kwargs["nofound_string"])
|
||||||
else:
|
else:
|
||||||
self.msg("There is nothing here called '%s'." % searchdata)
|
self.msg("There is no '%s' here." % searchdata)
|
||||||
return
|
return
|
||||||
# fall back to normal search
|
# fall back to normal search
|
||||||
return super(ContribRPObject, self).search(searchdata, **kwargs)
|
return super(ContribRPObject, self).search(searchdata, **kwargs)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue