2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Custom manager for Objects.
|
|
|
|
|
"""
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
from src.typeclasses.managers import TypedObjectManager
|
2010-09-04 12:18:00 +00:00
|
|
|
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
|
2010-08-29 18:46:58 +00:00
|
|
|
from src.utils import create
|
|
|
|
|
|
|
|
|
|
# Try to use a custom way to parse id-tagged multimatches.
|
|
|
|
|
try:
|
|
|
|
|
IDPARSER = __import__(
|
|
|
|
|
settings.ALTERNATE_OBJECT_SEARCH_MULTIMATCH_PARSER).object_multimatch_parser
|
|
|
|
|
except Exception:
|
|
|
|
|
from src.objects.object_search_funcs import object_multimatch_parser as IDPARSER
|
|
|
|
|
|
|
|
|
|
#
|
2010-09-12 15:07:12 +00:00
|
|
|
# Helper functions for the ObjectManger's search methods
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def match_list(searchlist, ostring, exact_match=True,
|
|
|
|
|
attribute_name=None):
|
|
|
|
|
"""
|
|
|
|
|
Helper function.
|
|
|
|
|
does name/attribute matching through a list of objects.
|
|
|
|
|
"""
|
|
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
if not ostring:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
if not attribute_name:
|
|
|
|
|
attribute_name = "key"
|
|
|
|
|
|
|
|
|
|
if isinstance(ostring, basestring):
|
|
|
|
|
# strings are case-insensitive
|
|
|
|
|
ostring = ostring.lower()
|
2010-08-29 18:46:58 +00:00
|
|
|
if exact_match:
|
|
|
|
|
return [prospect for prospect in searchlist
|
2010-09-12 15:07:12 +00:00
|
|
|
if (hasattr(prospect, attribute_name) and
|
|
|
|
|
ostring == str(getattr(prospect, attribute_name)).lower())
|
|
|
|
|
or (prospect.has_attribute(attribute_name) and
|
|
|
|
|
ostring == str(prospect.get_attribute(attribute_name)).lower())]
|
2010-08-29 18:46:58 +00:00
|
|
|
else:
|
|
|
|
|
return [prospect for prospect in searchlist
|
2010-09-12 15:07:12 +00:00
|
|
|
if (hasattr(prospect, attribute_name) and
|
|
|
|
|
ostring in str(getattr(prospect, attribute_name)).lower())
|
|
|
|
|
or (prospect.has_attribute(attribute_name) and
|
|
|
|
|
ostring in str(prospect.get_attribute(attribute_name)).lower())]
|
|
|
|
|
else:
|
|
|
|
|
# If it's not a string, we don't convert to lowercase. This is also
|
|
|
|
|
# always treated as an exact match.
|
|
|
|
|
return [prospect for prospect in searchlist
|
|
|
|
|
if (hasattr(prospect, attribute_name) and
|
|
|
|
|
ostring == getattr(prospect, attribute_name))
|
|
|
|
|
or (prospect.has_attribute(attribute_name)
|
|
|
|
|
and ostring == prospect.get_attribute(attribute_name))]
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
def separable_search(ostring, searchlist,
|
|
|
|
|
attribute_name='db_key', exact_match=False):
|
|
|
|
|
"""
|
|
|
|
|
Searches a list for a object match to ostring or separator+keywords.
|
|
|
|
|
|
|
|
|
|
This version handles search criteria defined by IDPARSER. By default this
|
|
|
|
|
is of the type N-keyword, used to differentiate several objects of the
|
|
|
|
|
exact same name, e.g. 1-box, 2-box etc.
|
|
|
|
|
|
|
|
|
|
ostring: (string) The string to match against.
|
|
|
|
|
searchlist: (List of Objects) The objects to perform attribute comparisons on.
|
|
|
|
|
attribute_name: (string) attribute name to search.
|
|
|
|
|
exact_match: (bool) 'exact' or 'fuzzy' matching.
|
|
|
|
|
|
|
|
|
|
Note that the fuzzy matching gives precedence to exact matches; so if your
|
|
|
|
|
search query matches an object in the list exactly, it will be the only result.
|
|
|
|
|
This means that if the list contains [box,box11,box12], the search string 'box'
|
|
|
|
|
will only match the first entry since it is exact. The search 'box1' will however
|
|
|
|
|
match both box11 and box12 since neither is an exact match.
|
|
|
|
|
|
|
|
|
|
This method always returns a list, also for a single result.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Full search - this may return multiple matches.
|
|
|
|
|
results = match_list(searchlist, ostring, exact_match, attribute_name)
|
|
|
|
|
|
|
|
|
|
# Deal with results of search
|
|
|
|
|
match_number = None
|
|
|
|
|
if not results:
|
|
|
|
|
# if we have no match, check if we are dealing
|
|
|
|
|
# with a "N-keyword" query, if so, strip it out.
|
|
|
|
|
match_number, ostring = IDPARSER(ostring)
|
|
|
|
|
if match_number != None and ostring:
|
|
|
|
|
# Run the search again, without the match number
|
|
|
|
|
results = match_list(searchlist, ostring, exact_match, attribute_name)
|
|
|
|
|
elif not exact_match:
|
|
|
|
|
# we have results, but are using fuzzy matching; run
|
|
|
|
|
# second sweep in results to catch eventual exact matches
|
|
|
|
|
# (these are given precedence, so a search for 'ball' in
|
|
|
|
|
# ['ball', 'ball2'] will correctly return the first ball
|
|
|
|
|
# only).
|
|
|
|
|
exact_results = match_list(results, ostring, True, attribute_name)
|
|
|
|
|
if exact_results:
|
|
|
|
|
results = exact_results
|
|
|
|
|
|
|
|
|
|
if len(results) > 1 and match_number != None:
|
|
|
|
|
# We have multiple matches, but a N-type match number
|
|
|
|
|
# is available to separate them.
|
|
|
|
|
try:
|
|
|
|
|
results = [results[match_number]]
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
# this is always a list.
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
class ObjectManager(TypedObjectManager):
|
|
|
|
|
"""
|
|
|
|
|
This is the main ObjectManager for all in-game objects. It
|
|
|
|
|
implements search functions specialized for objects of this
|
|
|
|
|
type, such as searches based on user, contents or location.
|
|
|
|
|
|
|
|
|
|
See src.dbobjects.TypedObjectManager for more general
|
|
|
|
|
search methods.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# ObjectManager Get methods
|
|
|
|
|
#
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# user/player related
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-04 12:18:00 +00:00
|
|
|
@returns_typeclass
|
2010-08-29 18:46:58 +00:00
|
|
|
def get_object_with_user(self, user):
|
|
|
|
|
"""
|
|
|
|
|
Matches objects with obj.player.user matching the argument.
|
2010-09-04 12:18:00 +00:00
|
|
|
A player<->user is a one-to-relationship, so this always
|
|
|
|
|
returns just one result or None.
|
|
|
|
|
|
|
|
|
|
user - mayb be a user object or user id.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
uid = int(user)
|
|
|
|
|
except TypeError:
|
2010-09-04 12:18:00 +00:00
|
|
|
uid = user.id
|
|
|
|
|
try:
|
|
|
|
|
return self.get(db_player__user__id=uid)
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# This returns typeclass since get_object_with_user and get_dbref does.
|
2010-09-12 15:07:12 +00:00
|
|
|
def get_object_with_player(self, search_string):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Search for an object based on its player's name or dbref.
|
|
|
|
|
This search
|
|
|
|
|
is sometimes initiated by appending a * to the beginning of
|
|
|
|
|
the search criterion (e.g. in local_and_global_search).
|
|
|
|
|
search_string: (string) The name or dbref to search for.
|
|
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
search_string = str(search_string).lstrip('*')
|
2010-08-29 18:46:58 +00:00
|
|
|
dbref = self.dbref(search_string)
|
2010-09-12 15:07:12 +00:00
|
|
|
if not dbref:
|
|
|
|
|
# not a dbref. Search by name.
|
|
|
|
|
player_matches = User.objects.filter(username__iexact=search_string)
|
|
|
|
|
if player_matches:
|
|
|
|
|
dbref = player_matches[0].id
|
|
|
|
|
# use the id to find the player
|
|
|
|
|
return self.get_object_with_user(dbref)
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
# attr/property related
|
|
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_objs_with_attr(self, attribute_name):
|
|
|
|
|
"""
|
|
|
|
|
Returns all objects having the given attribute_name defined at all.
|
|
|
|
|
"""
|
|
|
|
|
from src.objects.models import ObjAttribute
|
|
|
|
|
return [attr.obj for attr in ObjAttribute.objects.filter(db_key=attribute_name)]
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_objs_with_attr_match(self, attribute_name, attribute_value):
|
|
|
|
|
"""
|
|
|
|
|
Returns all objects having the valid
|
|
|
|
|
attrname set to the given value. Note that no conversion is made
|
|
|
|
|
to attribute_value, and so it can accept also non-strings.
|
2010-09-12 15:07:12 +00:00
|
|
|
"""
|
|
|
|
|
from src.objects.models import ObjAttribute
|
|
|
|
|
return [attr.obj for attr in ObjAttribute.objects.filter(db_key=attribute_name)
|
|
|
|
|
if attribute_value == attr.value]
|
|
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_objs_with_db_property(self, property_name):
|
|
|
|
|
"""
|
|
|
|
|
Returns all objects having a given db field property
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
return [prospect for prospect in self.all()
|
|
|
|
|
if hasattr(prospect, 'db_%s' % property_name)
|
|
|
|
|
or hasattr(prospect, property_name)]
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
2010-09-12 15:07:12 +00:00
|
|
|
def get_objs_with_db_property_match(self, property_name, property_value):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
Returns all objects having a given db field property
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
try:
|
|
|
|
|
return eval("self.filter(db_%s=%s)" % (property_name, property_value))
|
|
|
|
|
except Exception:
|
|
|
|
|
return []
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
# main search methods and helper functions
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_contents(self, location, excludeobj=None):
|
|
|
|
|
"""
|
|
|
|
|
Get all objects that has a location
|
|
|
|
|
set to this one.
|
|
|
|
|
"""
|
|
|
|
|
oquery = self.filter(db_location__id=location.id)
|
|
|
|
|
if excludeobj:
|
|
|
|
|
oquery = oquery.exclude(db_key=excludeobj)
|
|
|
|
|
return oquery
|
|
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def alias_list_search(self, ostring, objlist):
|
|
|
|
|
"""
|
|
|
|
|
Search a list of objects by trying to match their aliases.
|
|
|
|
|
"""
|
|
|
|
|
matches = []
|
|
|
|
|
for obj in (obj for obj in objlist
|
|
|
|
|
if hasattr(obj, 'aliases') and
|
|
|
|
|
ostring in obj.aliases):
|
|
|
|
|
matches.append(obj)
|
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
|
|
|
|
def object_search(self, character, ostring,
|
|
|
|
|
global_search=False,
|
|
|
|
|
attribute_name=None):
|
|
|
|
|
"""
|
|
|
|
|
Search as an object and return results.
|
|
|
|
|
|
|
|
|
|
character: (Object) The object performing the search.
|
|
|
|
|
ostring: (string) The string to compare names against.
|
|
|
|
|
Can be a dbref. If name is appended by *, a player is searched for.
|
|
|
|
|
global_search: Search all objects, not just the current location/inventory
|
|
|
|
|
attribute_name: (string) Which attribute to search in each object.
|
2010-09-12 15:07:12 +00:00
|
|
|
If None, the default 'key' attribute is used.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
#ostring = str(ostring).strip()
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
if not ostring or not character:
|
|
|
|
|
return None
|
|
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
location = character.location
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Easiest case - dbref matching (always exact)
|
2010-08-29 18:46:58 +00:00
|
|
|
dbref = self.dbref(ostring)
|
2010-09-12 15:07:12 +00:00
|
|
|
if dbref:
|
2010-08-29 18:46:58 +00:00
|
|
|
dbref_match = self.dbref_search(dbref)
|
|
|
|
|
if dbref_match:
|
|
|
|
|
return [dbref_match]
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
# not a dbref. Search by attribute/property.
|
|
|
|
|
|
|
|
|
|
if not attribute_name:
|
|
|
|
|
# If the search string is one of the following, return immediately with
|
|
|
|
|
# the appropriate result.
|
|
|
|
|
if location and ostring == 'here':
|
|
|
|
|
return [location]
|
|
|
|
|
if character and ostring in ['me', 'self']:
|
|
|
|
|
return [character]
|
|
|
|
|
if character and ostring in ['*me', '*self']:
|
|
|
|
|
return [character.player]
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
attribute_name = 'key'
|
|
|
|
|
|
|
|
|
|
if str(ostring).startswith("*"):
|
|
|
|
|
# Player search - try to find obj by its player's name
|
|
|
|
|
player_string = ostring.lstrip("*")
|
2010-09-12 16:19:02 +00:00
|
|
|
player_match = self.get_object_with_player(player_string)
|
2010-08-29 18:46:58 +00:00
|
|
|
if player_match is not None:
|
2010-09-04 12:18:00 +00:00
|
|
|
return [player_match]
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
# find suitable objects
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
if global_search or not location:
|
2010-09-12 15:07:12 +00:00
|
|
|
# search all objects in database
|
|
|
|
|
objlist = self.get_objs_with_db_property(attribute_name)
|
|
|
|
|
if not objlist:
|
|
|
|
|
objlist = self.get_objs_with_attr(attribute_name)
|
|
|
|
|
else:
|
|
|
|
|
# local search
|
|
|
|
|
objlist = character.contents
|
|
|
|
|
objlist.extend(location.contents)
|
|
|
|
|
objlist.append(location) #easy to forget!
|
|
|
|
|
if not objlist:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
# do the search on the found objects
|
|
|
|
|
matches = separable_search(ostring, objlist,
|
|
|
|
|
attribute_name, exact_match=False)
|
|
|
|
|
|
|
|
|
|
if not matches and attribute_name in ('key', 'name'):
|
|
|
|
|
# No matches. If we tried to match a key/name field, we also try to
|
|
|
|
|
# see if an alias works better.
|
|
|
|
|
matches = self.alias_list_search(ostring, objlist)
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# ObjectManager Copy method
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def copy_object(self, original_object, new_name=None,
|
|
|
|
|
new_location=None, new_home=None, aliases=None):
|
|
|
|
|
"""
|
|
|
|
|
Create and return a new object as a copy of the source object. All will
|
|
|
|
|
be identical to the original except for the dbref and the 'user' field
|
|
|
|
|
which will be set to None.
|
|
|
|
|
|
|
|
|
|
original_object (obj) - the object to make a copy from
|
|
|
|
|
new_name (str) - name the copy differently from the original.
|
|
|
|
|
new_location (obj) - if None, we create the new object in the same place as the old one.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# get all the object's stats
|
|
|
|
|
name = original_object.key
|
|
|
|
|
if new_name:
|
|
|
|
|
name = new_name
|
|
|
|
|
typeclass_path = original_object.typeclass_path
|
|
|
|
|
|
|
|
|
|
# create new object
|
|
|
|
|
from src import create
|
|
|
|
|
new_object = create.create_object(name, typeclass_path, new_location,
|
|
|
|
|
new_home, user=None, aliases=None)
|
|
|
|
|
if not new_object:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
for attr in original_object.attr():
|
|
|
|
|
# copy over all attributes from old to new.
|
|
|
|
|
new_object.attr(attr.attr_name, attr.value)
|
|
|
|
|
|
|
|
|
|
return new_object
|