Added at_init() call in idmapper and made sure to not deprecate it. Fixed bugs in search and continued work on tutorial world.

This commit is contained in:
Griatch 2015-02-22 11:35:16 +01:00
parent 621a2f73e9
commit 7f0e0d5ef8
8 changed files with 63 additions and 29 deletions

View file

@ -357,7 +357,7 @@ class CmdSetHandler(object):
cmdset.permanent = False cmdset.permanent = False
self.update() self.update()
def delete(self, cmdset=None): def remove(self, cmdset=None):
""" """
Remove a cmdset from the handler. Remove a cmdset from the handler.
@ -409,8 +409,10 @@ class CmdSetHandler(object):
pass pass
# re-sync the cmdsethandler. # re-sync the cmdsethandler.
self.update() self.update()
# legacy alias
delete = remove
def delete_default(self): def remove_default(self):
""" """
This explicitly deletes the default cmdset. It's the This explicitly deletes the default cmdset. It's the
only command that can. only command that can.
@ -428,6 +430,8 @@ class CmdSetHandler(object):
else: else:
self.cmdset_stack = [_EmptyCmdSet(cmdsetobj=self.obj)] self.cmdset_stack = [_EmptyCmdSet(cmdsetobj=self.obj)]
self.update() self.update()
# legacy alias
delete_default = remove_default
def all(self): def all(self):
""" """

View file

@ -1714,9 +1714,12 @@ class CmdExamine(ObjManipCommand):
""" """
Formats a single attribute line. Formats a single attribute line.
""" """
if crop and isinstance(value, basestring): if crop:
if not isinstance(value, basestring):
value = utils.to_str(value, force_string=True)
value = utils.crop(value) value = utils.crop(value)
value = utils.to_unicode(value) value = utils.to_unicode(value)
string = "\n %s = %s" % (attr, value) string = "\n %s = %s" % (attr, value)
string = raw(string) string = raw(string)
return string return string

View file

@ -10,6 +10,7 @@ import random
from evennia import TICKER_HANDLER from evennia import TICKER_HANDLER
from evennia import search_object from evennia import search_object
from evennia import Command, CmdSet from evennia import Command, CmdSet
from evennia import logger
from evennia.contrib.tutorial_world import objects as tut_objects from evennia.contrib.tutorial_world import objects as tut_objects
@ -276,7 +277,7 @@ class Mob(tut_objects.TutorialObject):
if not self.db.aggressive: if not self.db.aggressive:
self.start_hunting() self.start_hunting()
return return
self._set_ticker(self.db.attacking_pace, "do_attack") self._set_ticker(self.db.aggressive_pace, "do_attack")
self.ndb.is_patrolling = False self.ndb.is_patrolling = False
self.ndb.is_hunting = False self.ndb.is_hunting = False
self.ndb.is_attacking = True self.ndb.is_attacking = True
@ -346,7 +347,7 @@ class Mob(tut_objects.TutorialObject):
# no exits! teleport to home to get away. # no exits! teleport to home to get away.
self.move_to(self.home) self.move_to(self.home)
def do_attacking(self, *args, **kwargs): def do_attack(self, *args, **kwargs):
""" """
Called regularly when in attacking mode. In attacking mode Called regularly when in attacking mode. In attacking mode
the mob will bring its weapons to bear on any targets the mob will bring its weapons to bear on any targets
@ -373,9 +374,12 @@ class Mob(tut_objects.TutorialObject):
# defeated room # defeated room
target.msg(self.db.defeat_msg) target.msg(self.db.defeat_msg)
self.location.msg_contents(self.db.defeat_msg_room % target.key, exclude=target) self.location.msg_contents(self.db.defeat_msg_room % target.key, exclude=target)
defeat_location = search_object(self.db.defeat_location) send_defeated_to = search_object(self.db.send_defeated_to)
if defeat_location: if send_defeated_to:
target.move_to(defeat_location, quiet=True) target.move_to(send_defeated_to[0], quiet=True)
else:
logger.log_err("Mob: mob.db.send_defeated_to not found: %s" % self.db.send_defeated_to)
# response methods - called by other objects # response methods - called by other objects

View file

@ -297,7 +297,7 @@ class LightSource(TutorialObject):
When burned out, the object will be deleted. When burned out, the object will be deleted.
""" """
def __init__(self): def at_init(self):
""" """
If this is called with the Attribute is_giving_light already If this is called with the Attribute is_giving_light already
set, we know that the timer got killed by a server set, we know that the timer got killed by a server
@ -795,7 +795,7 @@ class CmdAttack(Command):
self.caller.db.combat_parry_mode = False self.caller.db.combat_parry_mode = False
else: else:
self.caller.msg("You fumble with your weapon, unsure of whether to stab, slash or parry ...") self.caller.msg("You fumble with your weapon, unsure of whether to stab, slash or parry ...")
self.caller.location.msg_contents("%s fumbles with their weapon." % self.obj.key) self.caller.location.msg_contents("%s fumbles with their weapon." % self.caller, exclude=self.caller)
self.caller.db.combat_parry_mode = False self.caller.db.combat_parry_mode = False
return return

View file

@ -440,6 +440,7 @@ class DarkCmdSet(CmdSet):
""" """
key = "darkroom_cmdset" key = "darkroom_cmdset"
mergetype = "Replace" mergetype = "Replace"
priority = 2
def at_cmdset_creation(self): def at_cmdset_creation(self):
"populate the cmdset." "populate the cmdset."
@ -472,6 +473,7 @@ class DarkRoom(TutorialRoom):
self.db.tutorial_info = "This is a room with custom command sets on itself." self.db.tutorial_info = "This is a room with custom command sets on itself."
# the room starts dark. # the room starts dark.
self.db.is_lit = False self.db.is_lit = False
self.cmdsets.add(DarkCmdSet, permanent=True)
def _carries_light(self, obj): def _carries_light(self, obj):
""" """
@ -502,22 +504,22 @@ class DarkRoom(TutorialRoom):
if any(self._carries_light(obj) for obj in self.contents): if any(self._carries_light(obj) for obj in self.contents):
# people are carrying lights # people are carrying lights
if not self.db.is_lit: if not self.db.is_lit:
self.cmdset.remove(DarkCmdSet)
self.db.is_lit = True self.db.is_lit = True
for char in (obj for obj in self.contents if obj.has_player): for char in (obj for obj in self.contents if obj.has_player):
# this won't do anything if it is already removed # this won't do anything if it is already removed
char.cmdset.delete(DarkCmdSet)
char.msg("The room is lit up.") char.msg("The room is lit up.")
else: else:
# noone is carrying light - darken the room # noone is carrying light - darken the room
if self.db.is_lit:
self.db.is_lit = False
self.cmdset.add(DarkCmdSet, permanent=True)
for char in (obj for obj in self.contents if obj.has_player): for char in (obj for obj in self.contents if obj.has_player):
if self.db.is_lit: if char.is_superuser:
self.db.is_lit = False char.msg("You are Superuser, so you are not affected by the dark state.")
if char.is_superuser: else:
char.msg("You are Superuser, so you are not affected by the dark state.") # put players in darkness
else: char.msg("The room is completely dark.")
# put players in darkness
char.cmdset.add(DarkCmdSet)
char.msg("The room is completely dark.")
def at_object_receive(self, obj, source_location): def at_object_receive(self, obj, source_location):
""" """
@ -535,8 +537,8 @@ class DarkRoom(TutorialRoom):
DarkCmdSet if necessary. This also works if they are DarkCmdSet if necessary. This also works if they are
teleported away. teleported away.
""" """
obj.cmdset.delete(DarkCmdSet)
self.check_light_state() self.check_light_state()
obj.cmdset.delete(DarkCmdSet)
#------------------------------------------------------------ #------------------------------------------------------------
# #

View file

@ -386,9 +386,9 @@ class DefaultObject(ObjectDB):
if is_string: if is_string:
# searchdata is a string; wrap some common self-references # searchdata is a string; wrap some common self-references
if searchdata.lower() in ("here", ): if searchdata.lower() in ("here", ):
return self.location return [self.location] if quiet else self.location
if searchdata.lower() in ("me", "self",): if searchdata.lower() in ("me", "self",):
return self return [self] if quiet else self
if use_nicks: if use_nicks:
# do nick-replacement on search # do nick-replacement on search
@ -961,8 +961,6 @@ class DefaultObject(ObjectDB):
def at_init(self): def at_init(self):
""" """
DEPRECATED: Use __init__ instead.
This is always called whenever this object is initiated -- This is always called whenever this object is initiated --
that is, whenever it its typeclass is cached from memory. This that is, whenever it its typeclass is cached from memory. This
happens on-demand first time the object is used or activated happens on-demand first time the object is used or activated

View file

@ -51,13 +51,19 @@ call the handler's save() and restore() methods when the server reboots.
from twisted.internet.defer import inlineCallbacks from twisted.internet.defer import inlineCallbacks
from evennia.scripts.scripts import ExtendedLoopingCall from evennia.scripts.scripts import ExtendedLoopingCall
from evennia.server.models import ServerConfig from evennia.server.models import ServerConfig
from evennia.utils.logger import log_trace from evennia.utils.logger import log_trace, log_err
from evennia.utils.dbserialize import dbserialize, dbunserialize, pack_dbobj, unpack_dbobj from evennia.utils.dbserialize import dbserialize, dbunserialize, pack_dbobj, unpack_dbobj
_GA = object.__getattribute__ _GA = object.__getattribute__
_SA = object.__setattr__ _SA = object.__setattr__
_ERROR_ADD_INTERVAL = \
"""TickerHandler: Tried to add a ticker with invalid interval:
obj={obj}, interval={interval}, args={args}, kwargs={kwargs}
store_key={store_key}
Ticker was not added."""
class Ticker(object): class Ticker(object):
""" """
Represents a repeatedly running task that calls Represents a repeatedly running task that calls
@ -93,7 +99,6 @@ class Ticker(object):
""" """
Set up the ticker Set up the ticker
""" """
print "Ticker __init__", interval
self.interval = interval self.interval = interval
self.subscriptions = {} self.subscriptions = {}
# set up a twisted asynchronous repeat call # set up a twisted asynchronous repeat call
@ -113,7 +118,6 @@ class Ticker(object):
if not subs: if not subs:
self.task.stop() self.task.stop()
elif subs: elif subs:
print "validating tickerhandler:", subs, start_delay, self.interval
self.task.start(self.interval, now=False, start_delay=start_delay) self.task.start(self.interval, now=False, start_delay=start_delay)
def add(self, store_key, obj, *args, **kwargs): def add(self, store_key, obj, *args, **kwargs):
@ -156,6 +160,11 @@ class TickerPool(object):
""" """
Add new ticker subscriber Add new ticker subscriber
""" """
if not interval:
log_err(_ERROR_ADD_INTERVAL.format(store_key=store_key, obj=obj,
interval=interval, args=args, kwargs=kwargs))
return
if interval not in self.tickers: if interval not in self.tickers:
self.tickers[interval] = self.ticker_class(interval) self.tickers[interval] = self.ticker_class(interval)
self.tickers[interval].add(store_key, obj, *args, **kwargs) self.tickers[interval].add(store_key, obj, *args, **kwargs)

View file

@ -65,7 +65,7 @@ class SharedMemoryModelBase(ModelBase):
cached_instance = cls.get_cached_instance(instance_key) cached_instance = cls.get_cached_instance(instance_key)
if cached_instance is None: if cached_instance is None:
cached_instance = new_instance() cached_instance = new_instance()
cls.cache_instance(cached_instance) cls.cache_instance(cached_instance, new=True)
return cached_instance return cached_instance
@ -246,12 +246,26 @@ class SharedMemoryModel(Model):
return cls.__dbclass__.__instance_cache__.get(id) return cls.__dbclass__.__instance_cache__.get(id)
@classmethod @classmethod
def cache_instance(cls, instance): def cache_instance(cls, instance, new=False):
""" """
Method to store an instance in the cache. Method to store an instance in the cache.
Args:
instance (Class instance): the instance to cache
new (bool, optional): this is the first time this
instance is cached (i.e. this is not an update
operation).
""" """
if instance._get_pk_val() is not None: if instance._get_pk_val() is not None:
cls.__dbclass__.__instance_cache__[instance._get_pk_val()] = instance cls.__dbclass__.__instance_cache__[instance._get_pk_val()] = instance
if new:
try:
# trigger the at_init hook only
# at first initialization
instance.at_init()
except AttributeError:
pass
@classmethod @classmethod
def get_all_cached_instances(cls): def get_all_cached_instances(cls):