Further caching and optimization, making some operations noticeable faster in the end.

This commit is contained in:
Griatch 2012-04-26 17:47:25 +02:00
parent 1a6ef5d983
commit 6e08c011a1
13 changed files with 78 additions and 73 deletions

View file

@ -2,25 +2,25 @@
The script handler makes sure to check through all stored scripts
to make sure they are still relevant.
An scripthandler is automatically added to all game objects. You
access it through the property 'scripts' on the game object.
access it through the property 'scripts' on the game object.
"""
from src.scripts.models import ScriptDB
from src.utils import create
from src.utils import create
from src.utils import logger
class ScriptHandler(object):
"""
Implements the handler. This sits on each game object.
Implements the handler. This sits on each game object.
"""
def __init__(self, obj):
"""
Set up internal state.
obj - a reference to the object this handler is attached to.
obj - a reference to the object this handler is attached to.
We retrieve all scripts attached to this object and check
if they are all peristent. If they are not, they are just
cruft left over from a server shutdown.
cruft left over from a server shutdown.
"""
self.obj = obj
@ -32,10 +32,10 @@ class ScriptHandler(object):
interval = "inf"
next_repeat = "inf"
repeats = "inf"
if script.interval > 0:
if script.interval > 0:
interval = script.interval
if script.repeats:
repeats = script.repeats
repeats = script.repeats
try: next_repeat = script.time_until_next_repeat()
except: next_repeat = "?"
string += "\n '%s' (%s/%s, %s repeats): %s" % (script.key,
@ -47,19 +47,19 @@ class ScriptHandler(object):
def add(self, scriptclass, key=None, autostart=True):
"""
Add an script to this object.
Add an script to this object.
scriptclass - either a class object
inheriting from Script, an instantiated script object
or a python path to such a class object.
key - optional identifier for the script (often set in script definition)
autostart - start the script upon adding it
"""
"""
script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=autostart)
if not script:
logger.log_errmsg("Script %s could not be created and/or started." % scriptclass)
return False
return True
return False
return True
def start(self, scriptid):
"""
@ -68,14 +68,14 @@ class ScriptHandler(object):
scripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptid)
num = 0
for script in scripts:
num += script.start()
return num
num += script.start()
return num
def delete(self, scriptid):
"""
Forcibly delete a script from this object.
scriptid can be a script key or the path to a script (in the
scriptid can be a script key or the path to a script (in the
latter case all scripts with this path will be deleted!)
"""
@ -85,7 +85,7 @@ class ScriptHandler(object):
num = 0
for script in delscripts:
num += script.stop()
return num
return num
def stop(self, scriptid):
"""
@ -105,4 +105,4 @@ class ScriptHandler(object):
This should be called regularly to crank the wheels.
"""
ScriptDB.objects.validate(obj=self.obj, init_mode=init_mode)

View file

@ -32,7 +32,7 @@ class ScriptClass(TypeClass):
parent doesn't work.
"""
try:
return other.id == self.id
return other.dbid == self.dbid
except Exception:
return False
@ -61,7 +61,7 @@ class ScriptClass(TypeClass):
def _step_err_callback(self, e):
"callback for runner errors"
cname = self.__class__.__name__
estring = "Script %s(#%i) of type '%s': at_repeat() error '%s'." % (self.key, self.id, cname, e.getErrorMessage())
estring = "Script %s(#%i) of type '%s': at_repeat() error '%s'." % (self.key, self.dbid, cname, e.getErrorMessage())
try:
self.dbobj.db_obj.msg(estring)
except Exception:
@ -183,7 +183,7 @@ class ScriptClass(TypeClass):
try:
self._stop_task()
except Exception, e:
logger.log_trace("Stopping script %s(%s)" % (self.key, self.id))
logger.log_trace("Stopping script %s(%s)" % (self.key, self.dbid))
pass
try:
self.dbobj.delete()