* Implemented a non-persistent cache in src/cache.py. The cache is lost when restarting the server but it has the advantage of not hitting the database, and so is useful for implementing things that should be remembered over time but does not need to be persistently saved in the database at every point, like fast-updating combat systems, timers etc. Using the cache can substantially cut down on database access at the cost of memory comsumption. It is easiest accessed through the object model using normal dot notation. So to store a variable in volatile memory e.g. from your script parent, you can do things like self.scripted_obj.cache.myvariable = variable and be sure that later (unless there was a reboot) doing self.scripted_obj.cache.myvariable will return the value you stored there.

* OBS - doing e.g. self.scripted_obj.myvariable = variable was always iffy and since a few revisions back this will NOT work - this is because the objects are now consistently synced with the database (in the past this was not done consistently which caused strange behaviour).
* Fixed some bugs in the multi-word command handler. It can handle multi-word exits as well now.
This commit is contained in:
Griatch 2009-11-01 15:12:38 +00:00
parent af19724bb2
commit 642932a403
9 changed files with 177 additions and 58 deletions

View file

@ -174,7 +174,9 @@ class ObjectManager(models.Manager):
defines_global.OTYPE_GOING])
def local_object_script_parent_search(self, script_parent, location):
o_query = self.filter(script_parent__exact=script_parent).filter(location__iexact=location)
o_query = self.filter(script_parent__exact=script_parent)
if o_query:
o_query = o_query.filter(location__iexact=location)
return o_query.exclude(type__in=[defines_global.OTYPE_GARBAGE,
defines_global.OTYPE_GOING])
@ -367,12 +369,13 @@ class ObjectManager(models.Manager):
# If the search string is one of the following, return immediately with
# the appropriate result.
if searcher.get_location().dbref_match(ostring) or ostring == 'here':
return [searcher.get_location()]
elif ostring == 'me' and searcher:
return [searcher]
if search_query[0] == "*":
if search_query and search_query[0] == "*":
# Player search- gotta search by name or alias
search_target = search_query[1:]
player_match = self.player_name_search(search_target)

View file

@ -21,6 +21,7 @@ from src import scripthandler
from src import defines_global
from src import session_mgr
from src import logger
from src import cache
# Import as the absolute path to avoid local variable clashes.
import src.flags
@ -887,6 +888,8 @@ class Object(models.Model):
functions_general.log_errmsg("Object '%s(#%d)' has invalid location: #%s" % \
(self.name,self.id,self.location_id))
return False
def get_scriptlink(self):
"""
@ -905,6 +908,20 @@ class Object(models.Model):
return None
# Set a property to make accessing the scriptlink more transparent.
scriptlink = property(fget=get_scriptlink)
def get_cache(self):
"""
Returns an object's volatile cache (in-memory storage)
"""
return cache.get(self.dbref())
def del_cache(self):
"""
Cleans the object cache for this object
"""
cache.flush(self.dbref())
cache = property(fget=get_cache, fdel=del_cache)
def get_script_parent(self):
"""
@ -1121,7 +1138,7 @@ class Object(models.Model):
"""
Returns the player's current state.
"""
return self.state
return self.cache.state
def set_state(self, state_name=None):
"""
@ -1136,7 +1153,7 @@ class Object(models.Model):
"""
if not self.is_player():
return False
if self.is_superuser():
# we have to deal with superusers separately since
# they would always appear to have the genperm.admin_nostate
@ -1148,12 +1165,12 @@ class Object(models.Model):
# for other users we request the permission as normal.
nostate = self.has_perm("genperms.admin_nostate")
# we never enter other states if we are in the interactive batch processor.
nostate = nostate or self.state == "_interactive batch processor"
# we never enter other states if we are already in the interactive batch processor.
nostate = nostate or self.get_state() == "_interactive batch processor"
if nostate:
return False
self.state = state_name
self.cache.state = state_name
return True