<s> from lampost . comm . broadcast import BroadcastMap <EOL> from lampost . context . config import m_configured <EOL> from lampost . context . resource import m_requires , requires <EOL> from lampost . datastore . dbofield import DBOField <EOL> from lampost . env . movement import Direction <EOL> from lampost . env . room import Room <EOL> from lampost . gameops import target_gen <EOL> from lampost . gameops . action import convert_verbs , ActionError <EOL> from lampost . model . item import BaseItem <EOL> m_requires ( __name__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> m_configured ( __name__ , '<STR_LIT>' ) <EOL> instance_map = { } <EOL> class InstanceManager ( ) : <EOL> def _post_init ( self ) : <EOL> register ( '<STR_LIT>' , self . remove_old ) <EOL> def next_instance ( self ) : <EOL> instance_id = db_counter ( '<STR_LIT>' ) <EOL> area_instance = AreaInstance ( instance_id ) <EOL> instance_map [ instance_id ] = area_instance <EOL> return area_instance <EOL> def remove_old ( self ) : <EOL> stale_pulse = future_pulse ( instance_preserve_hours * <NUM_LIT> * <NUM_LIT> ) <EOL> for instance_id , instance in instance_map . copy ( ) . items ( ) : <EOL> if instance . pulse_stamp < stale_pulse and not [ entity for entity in instance . entities <EOL> if hasattr ( entity , '<STR_LIT>' ) and entity . session ] : <EOL> del instance_map [ instance_id ] <EOL> def get ( self , instance_id ) : <EOL> return instance_map . get ( instance_id ) <EOL> class AreaInstance ( ) : <EOL> def __init__ ( self , instance_id ) : <EOL> self . instance_id = instance_id <EOL> self . entities = set ( ) <EOL> self . rooms = { } <EOL> self . pulse_stamp = current_pulse ( ) <EOL> def add_entity ( self , entity ) : <EOL> self . entities . add ( entity ) <EOL> entity . instance = self <EOL> self . pulse_stamp = current_pulse ( ) <EOL> def remove_entity ( self , entity ) : <EOL> if entity in self . entities : <EOL> del entity . instance <EOL> self . entities . remove ( entity ) <EOL> if not self . entities : <EOL> self . clear_rooms ( ) <EOL> if entity . group : <EOL> entity . group . instance = None <EOL> del instance_map [ self . instance_id ] <EOL> def clear_rooms ( self ) : <EOL> for room in self . rooms . values ( ) : <EOL> room . clean_up ( ) <EOL> def get_room ( self , room_id ) : <EOL> try : <EOL> room = self . rooms [ room_id ] <EOL> except KeyError : <EOL> room = load_object ( room_id , Room ) <EOL> if not room : <EOL> return <EOL> room = room . clone ( ) <EOL> room . instance = self <EOL> self . rooms [ room_id ] = room <EOL> return room <EOL> verb_exit = BroadcastMap ( ea = '<STR_LIT>' ) <EOL> dir_exit = BroadcastMap ( ea = '<STR_LIT>' ) <EOL> verb_entry = BroadcastMap ( ea = '<STR_LIT>' ) <EOL> dir_enter = BroadcastMap ( ea = '<STR_LIT>' ) <EOL> @ requires ( '<STR_LIT>' ) <EOL> class Entrance ( BaseItem ) : <EOL> class_id = '<STR_LIT>' <EOL> verb = DBOField ( '<STR_LIT>' ) <EOL> direction = DBOField ( None ) <EOL> destination = DBOField ( ) <EOL> instanced = DBOField ( True ) <EOL> edit_required = DBOField ( True ) <EOL> msg_class = "<STR_LIT>" <EOL> def on_loaded ( self ) : <EOL> if self . direction : <EOL> self . _dir = Direction . ref_map [ self . direction ] <EOL> self . verbs = ( self . _dir . obj_id , ) , ( self . _dir . desc , ) <EOL> self . target_class = '<STR_LIT>' <EOL> else : <EOL> self . _dir = None <EOL> self . verbs = convert_verbs ( self . verb ) <EOL> self . target_class = [ target_gen . action ] <EOL> if not self . title and self . verb : <EOL> self . title = self . verb <EOL> @ property <EOL> def dest_room ( self ) : <EOL> return load_object ( self . destination , Room ) <EOL> @ property <EOL> def name ( self ) : <EOL> return self . verb or self . _dir . desc <EOL> @ property <EOL> def from_name ( self ) : <EOL> return self . verb if self . verb else Direction . ref_map . get ( self . _dir . rev_key ) . desc <EOL> @ property <EOL> def entry_msg ( self ) : <EOL> return verb_entry if self . verb else verb_entry <EOL> @ property <EOL> def exit_msg ( self ) : <EOL> return verb_exit if self . verb else dir_exit <EOL> def glance ( self , source , ** _ ) : <EOL> if self . _dir : <EOL> source . display_line ( '<STR_LIT>' . format ( self . _dir . desc , self . dest_room . title ) , '<STR_LIT>' ) <EOL> else : <EOL> source . display_line ( self . title , '<STR_LIT>' ) <EOL> def __call__ ( self , source , ** _ ) : <EOL> if self . instanced : <EOL> if source . group : <EOL> if source . group . instance : <EOL> instance = source . group . instance <EOL> if self . destination not in instance . rooms : <EOL> raise ActionError ( "<STR_LIT>" ) <EOL> else : <EOL> instance = source . group . instance = self . instance_manager . next_instance ( ) <EOL> else : <EOL> instance = self . instance_manager . next_instance ( ) <EOL> destination = instance . get_room ( self . destination ) <EOL> else : <EOL> destination = self . dest_room <EOL> source . change_env ( destination , self ) </s>
