Fix errors in unittests and conflicts with the events handler

This commit is contained in:
Vincent Le Goff 2017-03-29 11:36:04 -07:00 committed by Griatch
parent fb299adaaa
commit ae79ca96a7
4 changed files with 36 additions and 12 deletions

View file

@ -151,14 +151,15 @@ class EventsHandler(object):
Returns: Returns:
True to report the event was called without interruption, True to report the event was called without interruption,
False otherwise. False otherwise. If the EventHandler isn't found, return
None.
""" """
handler = type(self).script handler = type(self).script
if handler: if handler:
return handler.call_event(self.obj, event_name, *args, **kwargs) return handler.call_event(self.obj, event_name, *args, **kwargs)
return False return None
@staticmethod @staticmethod
def format_event(event): def format_event(event):

View file

@ -75,7 +75,7 @@ class EventHandler(DefaultScript):
# Place the script in the EventsHandler # Place the script in the EventsHandler
Handler.script = self Handler.script = self
DefaultObject.events = typeclasses.PatchedObject.events DefaultObject.events = typeclasses.EventObject.events
# Create the channel if non-existent # Create the channel if non-existent
try: try:

View file

@ -13,6 +13,7 @@ from evennia.utils import ansi, utils
from evennia.utils.create import create_object, create_script from evennia.utils.create import create_object, create_script
from evennia.utils.test_resources import EvenniaTest from evennia.utils.test_resources import EvenniaTest
from evennia.contrib.events.commands import CmdEvent from evennia.contrib.events.commands import CmdEvent
from evennia.contrib.events.handler import EventsHandler
# Force settings # Force settings
settings.EVENTS_CALENDAR = "standard" settings.EVENTS_CALENDAR = "standard"
@ -32,6 +33,7 @@ class TestEventHandler(EvenniaTest):
def tearDown(self): def tearDown(self):
"""Stop the event handler.""" """Stop the event handler."""
self.handler.stop() self.handler.stop()
EventsHandler.script = None
super(TestEventHandler, self).tearDown() super(TestEventHandler, self).tearDown()
def test_start(self): def test_start(self):
@ -265,6 +267,7 @@ class TestCmdEvent(CommandTest):
db_typeclass_path="evennia.contrib.events.scripts.TimeEventScript"): db_typeclass_path="evennia.contrib.events.scripts.TimeEventScript"):
script.stop() script.stop()
EventsHandler.script = None
super(TestCmdEvent, self).tearDown() super(TestCmdEvent, self).tearDown()
def test_list(self): def test_list(self):
@ -412,6 +415,7 @@ class TestDefaultEvents(CommandTest):
def tearDown(self): def tearDown(self):
"""Stop the event handler.""" """Stop the event handler."""
self.handler.stop() self.handler.stop()
EventsHandler.script = None
super(TestDefaultEvents, self).tearDown() super(TestDefaultEvents, self).tearDown()
def test_exit(self): def test_exit(self):

View file

@ -1,5 +1,16 @@
""" """
Patched typeclasses for Evennia. Patched typeclasses for Evennia.
These typeclasses are not inherited from DefaultObject and other
Evennia default types. They softly "patch" some of these object hooks
however. While this adds a new layer in this module, it's (normally)
more simple to use from game designers, since it doesn't require a
new inheritance. These replaced hooks are only active if the event
system is active. You shouldn't need to change this module, just
override the hooks as you usually do in your custom typeclasses.
Calling super() would call the Default hooks (which would call the
event hook without further ado).
""" """
from evennia import DefaultCharacter, DefaultExit, DefaultObject, DefaultRoom from evennia import DefaultCharacter, DefaultExit, DefaultObject, DefaultRoom
@ -9,7 +20,7 @@ from evennia.contrib.events.custom import (
create_event_type, patch_hook, create_time_event) create_event_type, patch_hook, create_time_event)
from evennia.contrib.events.handler import EventsHandler from evennia.contrib.events.handler import EventsHandler
class PatchedCharacter: class EventCharacter:
"""Patched typeclass for DefaultCharcter.""" """Patched typeclass for DefaultCharcter."""
@ -86,6 +97,7 @@ class PatchedCharacter:
origin = source_location origin = source_location
destination = character.location destination = character.location
exits = []
if origin: if origin:
exits = [o for o in destination.contents if o.location is destination and o.destination is origin] exits = [o for o in destination.contents if o.location is destination and o.destination is origin]
if exits: if exits:
@ -133,6 +145,9 @@ class PatchedCharacter:
if can: if can:
can = origin.events.call("can_move", character, origin) can = origin.events.call("can_move", character, origin)
if can is None:
return True
return can return can
return True return True
@ -212,13 +227,7 @@ class PatchedCharacter:
hook(character) hook(character)
class PatchedObject(object): class EventExit(object):
@lazy_property
def events(self):
"""Return the EventsHandler."""
return EventsHandler(self)
class PatchedExit(object):
"""Patched exit to patch some hooks of DefaultExit.""" """Patched exit to patch some hooks of DefaultExit."""
@ -254,7 +263,7 @@ class PatchedExit(object):
exit, exit.location, exit.destination) exit, exit.location, exit.destination)
class PatchedRoom: class EventRoom:
"""Soft-patching of room's default hooks.""" """Soft-patching of room's default hooks."""
@ -273,6 +282,16 @@ class PatchedRoom:
room.events.call("delete", room) room.events.call("delete", room)
return True return True
class EventObject(object):
"""Patched default object."""
@lazy_property
def events(self):
"""Return the EventsHandler."""
return EventsHandler(self)
## Default events ## Default events
# Character events # Character events
create_event_type(DefaultCharacter, "can_move", ["character", create_event_type(DefaultCharacter, "can_move", ["character",