2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
Custom manager for Objects.
|
|
|
|
|
"""
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.contrib.auth.models import User
|
2010-10-30 18:42:37 +00:00
|
|
|
from django.db.models.fields import exceptions
|
2010-08-29 18:46:58 +00:00
|
|
|
from src.typeclasses.managers import TypedObjectManager
|
2010-09-04 12:18:00 +00:00
|
|
|
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
|
2011-04-21 16:45:18 +00:00
|
|
|
from src.utils import utils
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
# Try to use a custom way to parse id-tagged multimatches.
|
2011-04-23 11:54:08 +00:00
|
|
|
|
|
|
|
|
AT_MULTIMATCH_INPUT = utils.mod_import(*settings.SEARCH_AT_MULTIMATCH_INPUT.rsplit('.', 1))
|
2010-09-12 15:07:12 +00:00
|
|
|
|
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.
|
|
|
|
|
|
2010-10-21 18:58:47 +00:00
|
|
|
user - may be a user object or user id.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
uid = int(user)
|
|
|
|
|
except TypeError:
|
2010-11-10 23:03:38 +00:00
|
|
|
try:
|
|
|
|
|
uid = user.id
|
|
|
|
|
except:
|
|
|
|
|
return None
|
2010-09-04 12:18:00 +00:00
|
|
|
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.
|
|
|
|
|
"""
|
2011-04-21 16:45:18 +00:00
|
|
|
search_string = utils.to_unicode(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
|
2010-10-30 18:42:37 +00:00
|
|
|
def get_objs_with_attr(self, attribute_name, location=None):
|
2010-09-12 15:07:12 +00:00
|
|
|
"""
|
|
|
|
|
Returns all objects having the given attribute_name defined at all.
|
|
|
|
|
"""
|
|
|
|
|
from src.objects.models import ObjAttribute
|
2010-10-30 18:42:37 +00:00
|
|
|
lstring = ""
|
|
|
|
|
if location:
|
|
|
|
|
lstring = ", db_obj__db_location=location"
|
|
|
|
|
attrs = eval("ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring)
|
|
|
|
|
return [attr.obj for attr in attrs]
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
@returns_typeclass_list
|
2010-10-30 18:42:37 +00:00
|
|
|
def get_objs_with_attr_match(self, attribute_name, attribute_value, location=None, exact=False):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
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
|
2010-10-30 18:42:37 +00:00
|
|
|
lstring = ""
|
|
|
|
|
if location:
|
|
|
|
|
lstring = ", db_obj__db_location=location"
|
|
|
|
|
attrs = eval("ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring)
|
|
|
|
|
if exact:
|
|
|
|
|
return [attr.obj for attr in attrs if attribute_value == attr.value]
|
|
|
|
|
else:
|
2011-04-21 16:45:18 +00:00
|
|
|
return [attr.obj for attr in attrs if utils.to_unicode(attribute_value) in str(attr.value)]
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
2010-10-30 18:42:37 +00:00
|
|
|
def get_objs_with_db_property(self, property_name, location=None):
|
2010-09-12 15:07:12 +00:00
|
|
|
"""
|
2010-10-30 18:42:37 +00:00
|
|
|
Returns all objects having a given db field property.
|
|
|
|
|
property_name = search string
|
|
|
|
|
location - actual location object to restrict to
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-10-30 18:42:37 +00:00
|
|
|
lstring = ""
|
|
|
|
|
if location:
|
|
|
|
|
lstring = ".filter(db_location=location)"
|
|
|
|
|
try:
|
|
|
|
|
return eval("self.exclude(db_%s=None)%s" % (property_name, lstring))
|
|
|
|
|
except exceptions.FieldError:
|
|
|
|
|
return []
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
2010-10-30 18:42:37 +00:00
|
|
|
def get_objs_with_db_property_match(self, property_name, property_value, location, exact=False):
|
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-10-30 18:42:37 +00:00
|
|
|
lstring = ""
|
|
|
|
|
if location:
|
|
|
|
|
lstring = ", db_location=location"
|
|
|
|
|
|
2010-09-12 15:07:12 +00:00
|
|
|
try:
|
2010-10-30 18:42:37 +00:00
|
|
|
if exact:
|
|
|
|
|
return eval("self.filter(db_%s__iexact=property_value%s)" % (property_name, lstring))
|
|
|
|
|
else:
|
|
|
|
|
return eval("self.filter(db_%s__icontains=property_value%s)" % (property_name, lstring))
|
|
|
|
|
except exceptions.FieldError:
|
2010-09-12 15:07:12 +00:00
|
|
|
return []
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2010-10-30 18:42:37 +00:00
|
|
|
@returns_typeclass_list
|
|
|
|
|
def get_objs_with_key_or_alias(self, ostring, location, exact=False):
|
|
|
|
|
"""
|
|
|
|
|
Returns objects based on key or alias match
|
|
|
|
|
"""
|
|
|
|
|
lstring_key, lstring_alias, estring = "", "", "icontains"
|
|
|
|
|
if location:
|
|
|
|
|
lstring_key = ", db_location=location"
|
|
|
|
|
lstring_alias = ", db_obj__db_location=location"
|
|
|
|
|
if exact:
|
2011-04-21 16:45:18 +00:00
|
|
|
estring = "__iexact"
|
|
|
|
|
else:
|
|
|
|
|
estring = "__istartswith"
|
|
|
|
|
matches = eval("self.filter(db_key%s=ostring%s)" % (estring, lstring_key))
|
2010-10-30 18:42:37 +00:00
|
|
|
if not matches:
|
2011-04-21 16:45:18 +00:00
|
|
|
alias_matches = eval("self.model.alias_set.related.model.objects.filter(db_key%s=ostring%s)" % (estring, lstring_alias))
|
2010-10-30 18:42:37 +00:00
|
|
|
matches = [alias.db_obj for alias in alias_matches]
|
|
|
|
|
return matches
|
|
|
|
|
|
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.
|
|
|
|
|
"""
|
2010-11-09 14:46:39 +00:00
|
|
|
estring = ""
|
2010-08-29 18:46:58 +00:00
|
|
|
if excludeobj:
|
2010-11-09 14:46:39 +00:00
|
|
|
estring = ".exclude(db_key=excludeobj)"
|
|
|
|
|
return eval("self.filter(db_location__id=location.id)%s" % estring)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
@returns_typeclass_list
|
2011-04-24 11:26:51 +00:00
|
|
|
def object_search(self, ostring, caller=None,
|
2010-08-29 18:46:58 +00:00
|
|
|
global_search=False,
|
2011-02-27 22:55:42 +00:00
|
|
|
attribute_name=None, location=None):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2011-04-23 11:54:08 +00:00
|
|
|
Search as an object and return results. The result is always an Object.
|
|
|
|
|
If * is appended (player search, a Character controlled by this Player
|
|
|
|
|
is looked for. The Character is returned, not the Player. Use player_search
|
2011-04-24 11:26:51 +00:00
|
|
|
to find Player objects. Always returns a list.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
ostring: (string) The string to compare names against.
|
|
|
|
|
Can be a dbref. If name is appended by *, a player is searched for.
|
2011-04-24 11:26:51 +00:00
|
|
|
caller: (Object) The object performing the search.
|
2010-08-29 18:46:58 +00:00
|
|
|
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.
|
2011-02-27 22:55:42 +00:00
|
|
|
location: If None, character.location will be used.
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2010-09-12 15:07:12 +00:00
|
|
|
#ostring = str(ostring).strip()
|
|
|
|
|
|
2011-04-24 11:26:51 +00:00
|
|
|
if not ostring:
|
|
|
|
|
return []
|
2010-09-12 15:07:12 +00:00
|
|
|
|
|
|
|
|
# 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]
|
|
|
|
|
|
2011-04-24 11:26:51 +00:00
|
|
|
if not location and caller and hasattr(caller, "location"):
|
|
|
|
|
location = caller.location
|
|
|
|
|
|
2010-10-30 18:42:37 +00:00
|
|
|
# Test some common self-references
|
|
|
|
|
|
|
|
|
|
if location and ostring == 'here':
|
|
|
|
|
return [location]
|
2011-04-24 11:26:51 +00:00
|
|
|
if caller and ostring in ['me', 'self']:
|
|
|
|
|
return [caller]
|
|
|
|
|
if caller and ostring in ['*me', '*self']:
|
|
|
|
|
return [caller]
|
2010-10-30 18:42:37 +00:00
|
|
|
|
2011-04-23 11:54:08 +00:00
|
|
|
# Test if we are looking for an object controlled by a
|
|
|
|
|
# specific player
|
2010-10-30 18:42:37 +00:00
|
|
|
|
2011-04-21 16:45:18 +00:00
|
|
|
if utils.to_unicode(ostring).startswith("*"):
|
2010-09-12 15:07:12 +00:00
|
|
|
# Player search - try to find obj by its player's name
|
2010-10-30 18:42:37 +00:00
|
|
|
player_match = self.get_object_with_player(ostring)
|
2010-08-29 18:46:58 +00:00
|
|
|
if player_match is not None:
|
2011-04-23 11:54:08 +00:00
|
|
|
return [player_match]
|
|
|
|
|
|
2010-10-30 18:42:37 +00:00
|
|
|
# Search for keys, aliases or other attributes
|
|
|
|
|
|
|
|
|
|
search_locations = [None] # this means a global search
|
|
|
|
|
if not global_search and location:
|
|
|
|
|
# Test if we are referring to the current room
|
|
|
|
|
if location and (ostring.lower() == location.key.lower()
|
|
|
|
|
or ostring.lower() in [alias.lower() for alias in location.aliases]):
|
|
|
|
|
return [location]
|
|
|
|
|
# otherwise, setup the locations to search in
|
2011-04-24 11:26:51 +00:00
|
|
|
search_locations = [location]
|
|
|
|
|
if caller:
|
|
|
|
|
search_locations.append(caller)
|
|
|
|
|
|
2010-10-30 18:42:37 +00:00
|
|
|
def local_and_global_search(ostring, exact=False):
|
|
|
|
|
"Helper method for searching objects"
|
|
|
|
|
matches = []
|
2010-11-09 14:46:39 +00:00
|
|
|
for location in search_locations:
|
2010-10-30 18:42:37 +00:00
|
|
|
if attribute_name:
|
|
|
|
|
# Attribute/property search. First, search for db_<attrname> matches on the model
|
|
|
|
|
matches.extend(self.get_objs_with_db_property_match(attribute_name, ostring, location, exact))
|
|
|
|
|
if not matches:
|
|
|
|
|
# Next, try Attribute matches
|
|
|
|
|
matches.extend(self.get_objs_with_attr_match(attribute_name, ostring, location, exact))
|
|
|
|
|
else:
|
|
|
|
|
# No attribute/property named. Do a normal key/alias-search
|
2010-11-09 14:46:39 +00:00
|
|
|
matches.extend(self.get_objs_with_key_or_alias(ostring, location, exact))
|
2010-10-30 18:42:37 +00:00
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
# Search through all possibilities.
|
|
|
|
|
|
|
|
|
|
match_number = None
|
2010-11-09 14:46:39 +00:00
|
|
|
matches = local_and_global_search(ostring, exact=True)
|
2010-10-30 18:42:37 +00:00
|
|
|
if not matches:
|
|
|
|
|
# if we have no match, check if we are dealing with an "N-keyword" query - if so, strip it.
|
2011-04-23 11:54:08 +00:00
|
|
|
match_number, ostring = AT_MULTIMATCH_INPUT(ostring)
|
2010-10-30 18:42:37 +00:00
|
|
|
if match_number != None and ostring:
|
|
|
|
|
# Run search again, without match number:
|
|
|
|
|
matches = local_and_global_search(ostring, exact=True)
|
|
|
|
|
if ostring and (len(matches) > 1 or not matches):
|
|
|
|
|
# Already multimatch or no matches. Run a fuzzy matching.
|
|
|
|
|
matches = local_and_global_search(ostring, exact=False)
|
|
|
|
|
elif len(matches) > 1:
|
|
|
|
|
# multiple matches already. Run a fuzzy search. This catches partial matches (suggestions)
|
|
|
|
|
matches = local_and_global_search(ostring, exact=False)
|
|
|
|
|
|
|
|
|
|
# deal with the result
|
|
|
|
|
if len(matches) > 1 and match_number != None:
|
|
|
|
|
# We have multiple matches, but a N-type match number is available to separate them.
|
|
|
|
|
try:
|
|
|
|
|
matches = [matches[match_number]]
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
# This is always a list.
|
|
|
|
|
return matches
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
#
|
|
|
|
|
# ObjectManager Copy method
|
|
|
|
|
#
|
|
|
|
|
|
2011-05-01 18:04:15 +00:00
|
|
|
def copy_object(self, original_object, new_key=None,
|
2011-04-07 22:10:51 +00:00
|
|
|
new_location=None, new_player=None, new_home=None,
|
2011-05-01 18:04:15 +00:00
|
|
|
new_permissions=None, new_locks=None, new_aliases=None, new_destination=None):
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
2011-05-01 18:04:15 +00:00
|
|
|
Create and return a new object as a copy of the original object. All will
|
2010-10-30 18:42:37 +00:00
|
|
|
be identical to the original except for the arguments given specifically
|
|
|
|
|
to this method.
|
2010-08-29 18:46:58 +00:00
|
|
|
|
|
|
|
|
original_object (obj) - the object to make a copy from
|
2011-05-01 18:04:15 +00:00
|
|
|
new_key (str) - name the copy differently from the original.
|
2010-10-30 18:42:37 +00:00
|
|
|
new_location (obj) - if not None, change the location
|
|
|
|
|
new_home (obj) - if not None, change the Home
|
|
|
|
|
new_aliases (list of strings) - if not None, change object aliases.
|
2011-05-01 18:04:15 +00:00
|
|
|
new_destination (obj) - if not None, change destination
|
2010-08-29 18:46:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# get all the object's stats
|
|
|
|
|
typeclass_path = original_object.typeclass_path
|
2011-05-01 18:04:15 +00:00
|
|
|
if not new_key:
|
|
|
|
|
new_key = original_object.key
|
2010-10-30 18:42:37 +00:00
|
|
|
if not new_location:
|
|
|
|
|
new_location = original_object.location
|
|
|
|
|
if not new_home:
|
2011-04-07 22:10:51 +00:00
|
|
|
new_home = original_object.home
|
|
|
|
|
if not new_player:
|
|
|
|
|
new_player = original_object.player
|
2010-10-30 18:42:37 +00:00
|
|
|
if not new_aliases:
|
|
|
|
|
new_aliases = original_object.aliases
|
2011-04-07 22:10:51 +00:00
|
|
|
if not new_locks:
|
|
|
|
|
new_locks = original_object.db_lock_storage
|
|
|
|
|
if not new_permissions:
|
|
|
|
|
new_permissions = original_object.permissions
|
2011-05-01 18:04:15 +00:00
|
|
|
if not new_destination:
|
|
|
|
|
new_destination = original_object.destination
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
# create new object
|
2011-04-07 22:10:51 +00:00
|
|
|
from src.utils import create
|
|
|
|
|
from src.scripts.models import ScriptDB
|
2011-05-01 18:04:15 +00:00
|
|
|
new_object = create.create_object(typeclass_path, key=new_key, location=new_location,
|
|
|
|
|
home=new_home, player=new_player, permissions=new_permissions,
|
|
|
|
|
locks=new_locks, aliases=new_aliases, destination=new_destination)
|
2010-08-29 18:46:58 +00:00
|
|
|
if not new_object:
|
|
|
|
|
return None
|
|
|
|
|
|
2011-04-07 22:10:51 +00:00
|
|
|
# copy over all attributes from old to new.
|
|
|
|
|
for attr in original_object.get_all_attributes():
|
|
|
|
|
new_object.set_attribute(attr.key, attr.value)
|
2010-08-29 18:46:58 +00:00
|
|
|
|
2011-04-07 22:10:51 +00:00
|
|
|
# copy over all cmdsets, if any
|
|
|
|
|
for icmdset, cmdset in enumerate(original_object.cmdset.all()):
|
|
|
|
|
if icmdset == 0:
|
|
|
|
|
new_object.cmdset.add_default(cmdset)
|
|
|
|
|
else:
|
|
|
|
|
new_object.cmdset.add(cmdset)
|
|
|
|
|
|
|
|
|
|
# copy over all scripts, if any
|
|
|
|
|
for script in original_object.scripts.all():
|
|
|
|
|
ScriptDB.objects.copy_script(script, new_obj=new_object.dbobj)
|
|
|
|
|
|
2010-08-29 18:46:58 +00:00
|
|
|
return new_object
|