Refactored ScriptHandler into ScriptContainer. Now uses dictionaries with finer control for script launching.
This commit is contained in:
parent
f1c7471691
commit
1f246484cd
2 changed files with 41 additions and 15 deletions
|
|
@ -2,7 +2,7 @@ from django.conf import settings
|
||||||
from evennia.utils.utils import class_from_module
|
from evennia.utils.utils import class_from_module
|
||||||
|
|
||||||
|
|
||||||
class GlobalHandler(object):
|
class GlobalContainer(object):
|
||||||
"""
|
"""
|
||||||
Simple Handler object loaded by the Evennia API to contain and manage a game's Global Scripts.
|
Simple Handler object loaded by the Evennia API to contain and manage a game's Global Scripts.
|
||||||
|
|
||||||
|
|
@ -14,15 +14,15 @@ class GlobalHandler(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.typeclass_storage = dict()
|
self.script_data = dict()
|
||||||
self.script_storage = dict()
|
self.script_storage = dict()
|
||||||
for key, typeclass_path in settings.GLOBAL_SCRIPTS.items():
|
self.script_data.update(settings.GLOBAL_SCRIPTS)
|
||||||
self.typeclass_storage[key] = class_from_module(typeclass_path)
|
self.typeclass_storage = dict()
|
||||||
for key, typeclass in self.typeclass_storage.items():
|
|
||||||
found = typeclass.objects.filter(db_key=key).first()
|
for key, data in settings.GLOBAL_SCRIPTS.items():
|
||||||
if not found:
|
self.typeclass_storage[key] = class_from_module(data['typeclass'])
|
||||||
found = typeclass.create(key=key, typeclass=typeclass, persistent=True)
|
for key in self.script_data.keys():
|
||||||
self.script_storage[key] = found
|
self._load_script(key)
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
|
|
||||||
|
|
@ -35,14 +35,35 @@ class GlobalHandler(object):
|
||||||
return self.script_storage[item]
|
return self.script_storage[item]
|
||||||
else:
|
else:
|
||||||
# Oops, something happened to our Global Script. Let's re-create it.
|
# Oops, something happened to our Global Script. Let's re-create it.
|
||||||
reloaded = self.typeclass_storage[item].create(key=item, typeclass=self.typeclass_storage[item],
|
return self._load_script(item)
|
||||||
persistent=True)
|
|
||||||
self.script_storage[item] = reloaded
|
|
||||||
return reloaded
|
|
||||||
|
|
||||||
def __getattr__(self, item):
|
def __getattr__(self, item):
|
||||||
return self[item]
|
return self[item]
|
||||||
|
|
||||||
|
def _load_script(self, item):
|
||||||
|
typeclass = self.typeclass_storage[item]
|
||||||
|
found = typeclass.objects.filter(db_key=item).first()
|
||||||
|
interval = self.script_data[item].get('interval', None)
|
||||||
|
start_delay = self.script_data[item].get('start_delay', None)
|
||||||
|
repeats = self.script_data[item].get('repeats', 0)
|
||||||
|
desc = self.script_data[item].get('desc', '')
|
||||||
|
|
||||||
|
if not found:
|
||||||
|
new_script = typeclass.create(key=item, persistent=True, interval=interval, start_delay=start_delay,
|
||||||
|
repeats=repeats, desc=desc)
|
||||||
|
new_script.start()
|
||||||
|
self.script_storage[item] = new_script
|
||||||
|
return new_script
|
||||||
|
|
||||||
|
if (found.interval != interval) or (found.start_delay != start_delay) or (found.repeats != repeats):
|
||||||
|
found.restart(interval=interval, start_delay=start_delay, repeats=repeats)
|
||||||
|
if found.desc != desc:
|
||||||
|
found.desc = desc
|
||||||
|
self.script_storage[item] = found
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create singleton of the GlobalHandler for the API.
|
# Create singleton of the GlobalHandler for the API.
|
||||||
GLOBAL_SCRIPTS = GlobalHandler()
|
GLOBAL_SCRIPTS = GlobalContainer()
|
||||||
|
|
|
||||||
|
|
@ -552,8 +552,13 @@ PROTOTYPEFUNC_MODULES = ["evennia.utils.prototypefuncs",
|
||||||
# The 'key' is a way to quickly index them, and it will also be the
|
# The 'key' is a way to quickly index them, and it will also be the
|
||||||
# Script Typeclasss's key so it can be quickly retrieved.
|
# Script Typeclasss's key so it can be quickly retrieved.
|
||||||
|
|
||||||
|
# Values are a dictionary that uses the example format. Available keys
|
||||||
|
# are typeclass (required), interval, repeats, start_delay, and desc
|
||||||
|
# only typeclass is required.
|
||||||
|
|
||||||
GLOBAL_SCRIPTS = {
|
GLOBAL_SCRIPTS = {
|
||||||
# 'key': 'typeclass.path.here',
|
# 'key': {'typeclass': 'typeclass.path.here',
|
||||||
|
# 'repeats': -1, 'interval': 50, 'desc': 'Example script'},
|
||||||
}
|
}
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue