Trunk: Merged the Devel-branch (branches/griatch) into /trunk. This constitutes a major refactoring of Evennia. Development will now continue in trunk. See the wiki and the past posts to the mailing list for info. /Griatch

This commit is contained in:
Griatch 2010-08-29 18:46:58 +00:00
parent df29defbcd
commit f83c2bddf8
222 changed files with 22304 additions and 14371 deletions

View file

@ -0,0 +1,158 @@
"""
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.
"""
from src.scripts.models import ScriptDB
from src.utils import create
from src.utils import logger
def format_script_list(scripts):
"Takes a list of scripts and format the output."
if not scripts:
return "<No scripts>"
string = "id obj\t\tkey\t\tinterval\trepeats\tnext\tpersistent"
for script in scripts:
obj = None
interval = None
repeats = None
try:
obj = script.obj
except AttributeError:
pass
try:
interval = script.interval
except AttributeError:
pass
try:
repeats = script.repeats
except AttributeError:
pass
try:
next_repeat = script.time_until_next_repeat()
except:
pass
if not obj:
obj = "<Global>"
if not interval:
interval = "N/A"
else:
interval = "%ss" % interval
if not repeats:
repeats = "N/A"
else:
repeats = "%ss" % repeats
if not next_repeat:
next_repeat = "N/A"
else:
next_repeat = "%ss" % next_repeat
string += "\n %s %s\t\t%s\t%s\t%s\t%s\t%s\n %s (%s)" % \
(script.id, obj, script.key, interval,
repeats, next_repeat, script.persistent,
script.typeclass_path, script.desc)
return string
class ScriptHandler(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.
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.
"""
self.obj = obj
# this is required to stop a nasty loop in some situations that
# has the handler infinitely recursively re-added to its object.
self.obj.scripts = self
scripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj)
#print "starting scripthandler. %s has scripts %s" % (self.obj, scripts)
if scripts:
okscripts = [script for script in scripts if script.persistent == True]
delscripts = [script for script in scripts if script not in okscripts]
for script in delscripts:
#print "stopping script %s" % script
script.stop()
for script in okscripts:
#print "starting script %s" % script
script.start()
def __str__(self):
"List the scripts tied to this object"
scripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj)
string = ""
for script in scripts:
interval = "inf"
next_repeat = "inf"
repeats = "inf"
if script.interval:
interval = script.interval
if 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,
next_repeat,
interval,
repeats,
script.desc)
return string.strip()
def add(self, scriptclass, key=None, autostart=True):
"""
Add an script to this object. The scriptclass
argument can be either a class object
inheriting from Script, an instantiated script object
or a python path to such a class object.
"""
script = create.create_script(scriptclass, key=key, obj=self.obj, autostart=autostart)
if not script:
logger.log_errmsg("Script %s failed to be created/start." % scriptclass)
def start(self, scriptkey):
"""
Find an already added script and force-start it
"""
scripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptkey)
for script in scripts:
script.start()
def delete(self, scriptkey):
"""
Forcibly delete a script from this object.
"""
delscripts = ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptkey)
for script in delscripts:
script.stop()
def stop(self, scriptkey):
"""
Alias for delete.
"""
self.delete(scriptkey)
def all(self, scriptkey=None):
"""
Get all scripts stored in the handler, alternatively all matching a key.
"""
return ScriptDB.objects.get_all_scripts_on_obj(self.obj, key=scriptkey)
def validate(self):
"""
Runs a validation on this object's scripts only.
This should be called regularly to crank the wheels.
"""
ScriptDB.objects.validate(obj=self.obj)