'Set up the event system when starting.
Note that this hook is called every time the server restarts
(including when it\'s reloaded).  This hook performs the following
tasks:
-   Create temporarily stored events.
-   Generate locals (individual events\' namespace).
-   Load eventfuncs, including user-defined ones.
-   Re-schedule tasks that aren\'t set to fire anymore.
-   Effectively connect the handler to the main script.'

def at_start(self):

    self.ndb.events = {}
    for (typeclass, name, variables, help_text, custom_call, custom_add) in EVENTS:
        self.add_event(typeclass, name, variables, help_text, custom_call, custom_add)
    self.ndb.current_locals = {}
    self.ndb.fresh_locals = {}
    addresses = ['evennia.contrib.ingame_python.eventfuncs']
    addresses.extend(getattr(settings, 'EVENTFUNCS_LOCATIONS', ['world.eventfuncs']))
    for address in addresses:
        if pypath_to_realpath(address):
            self.ndb.fresh_locals.update(all_from_module(address))
    now = datetime.now()
    for (task_id, definition) in tuple(self.db.tasks.items()):
        (future, obj, event_name, locals) = definition
        seconds = (future - now).total_seconds()
        if (seconds < 0):
            seconds = 0
        delay(seconds, complete_task, task_id)
    from evennia.contrib.ingame_python import typeclasses
    CallbackHandler.script = self
    DefaultObject.callbacks = typeclasses.EventObject.callbacks
    try:
        self.ndb.channel = ChannelDB.objects.get(db_key='everror')
    except ChannelDB.DoesNotExist:
        self.ndb.channel = create_channel('everror', desc='Event    errors', locks='control:false();listen:perm(Builders);send:false()')
