<s> from collections import defaultdict <EOL> import itertools <EOL> from lampost . datastore . dbofield import DBOField <EOL> from lampost . gameops . action import action_handler , add_actions , remove_action , add_action <EOL> from lampost . comm . broadcast import Broadcast , BroadcastMap <EOL> from lampost . context . resource import m_requires <EOL> from lampost . gameops . parser import ParseError , parse_actions , has_action <EOL> from lampost . model . item import Connected <EOL> m_requires ( __name__ , '<STR_LIT>' ) <EOL> class Entity ( Connected ) : <EOL> inven = DBOField ( [ ] , '<STR_LIT>' ) <EOL> status = '<STR_LIT>' <EOL> living = True <EOL> instance = None <EOL> entry_msg = BroadcastMap ( e = '<STR_LIT>' , ea = "<STR_LIT>" ) <EOL> exit_msg = BroadcastMap ( e = '<STR_LIT>' , ea = "<STR_LIT>" ) <EOL> def __init__ ( self ) : <EOL> self . soul = defaultdict ( set ) <EOL> self . inven_actions = defaultdict ( set ) <EOL> self . followers = set ( ) <EOL> self . registrations = set ( ) <EOL> self . _soul_objects = set ( ) <EOL> def baptise ( self ) : <EOL> add_actions ( self . inven_actions , self . inven ) <EOL> def enhance_soul ( self , action ) : <EOL> add_action ( self . soul , action ) <EOL> self . _soul_objects . add ( action ) <EOL> def diminish_soul ( self , action ) : <EOL> remove_action ( self . soul , action ) <EOL> self . _soul_objects . discard ( action ) <EOL> def add_inven ( self , article ) : <EOL> self . inven . append ( article ) <EOL> add_action ( self . inven_actions , article ) <EOL> def remove_inven ( self , article ) : <EOL> self . inven . remove ( article ) <EOL> remove_action ( self . inven_actions , article ) <EOL> def entity_enter_env ( self , * _ ) : <EOL> pass <EOL> def entity_leave_env ( self , * _ ) : <EOL> pass <EOL> def parse ( self , command ) : <EOL> if command . startswith ( "<STR_LIT:'>" ) : <EOL> command = '<STR_LIT>' + command [ <NUM_LIT:1> : ] . strip ( ) <EOL> elif command . startswith ( '<STR_LIT::>' ) : <EOL> command = '<STR_LIT>' + command [ <NUM_LIT:1> : ] . strip ( ) <EOL> try : <EOL> action , act_args = parse_actions ( self , command ) <EOL> act_args [ '<STR_LIT>' ] = command <EOL> act_args [ '<STR_LIT:source>' ] = self <EOL> self . start_action ( action , act_args ) <EOL> except ParseError as error : <EOL> self . handle_parse_error ( error , command ) <EOL> def filter_actions ( self , matches ) : <EOL> return matches <EOL> @ action_handler <EOL> def start_action ( self , action , act_args ) : <EOL> if hasattr ( action , '<STR_LIT>' ) : <EOL> action . prepare_action ( ** act_args ) <EOL> self . process_action ( action , act_args ) <EOL> self . check_follow ( action , act_args ) <EOL> @ action_handler <EOL> def process_action ( self , action , act_args ) : <EOL> response = action ( ** act_args ) <EOL> if isinstance ( response , str ) : <EOL> self . display_line ( response ) <EOL> elif response : <EOL> self . output ( response ) <EOL> def check_follow ( self , action , act_args ) : <EOL> if getattr ( action , '<STR_LIT>' , False ) : <EOL> for follower in self . followers : <EOL> if has_action ( follower , action , act_args [ '<STR_LIT>' ] ) : <EOL> follow_args = act_args . copy ( ) <EOL> follow_args [ '<STR_LIT:source>' ] = follower <EOL> follower . start_action ( action , follow_args ) <EOL> def handle_parse_error ( self , error , command ) : <EOL> self . display_line ( error . client_message , '<STR_LIT>' ) <EOL> def social ( self , ** _ ) : <EOL> pass <EOL> def follow ( self , source , ** _ ) : <EOL> self . followers . add ( source ) <EOL> source . broadcast ( s = "<STR_LIT>" , t = "<STR_LIT>" , e = "<STR_LIT>" , target = self ) <EOL> def unfollow ( self ) : <EOL> following = getattr ( self , '<STR_LIT>' , None ) <EOL> if following : <EOL> self . display_line ( "<STR_LIT>" . format ( following . name ) ) <EOL> following . display_line ( "<STR_LIT>" . format ( self . name ) ) <EOL> following . followers . remove ( self ) <EOL> del self . following <EOL> def examine ( self , source , ** _ ) : <EOL> super ( ) . examine ( source , ** _ ) <EOL> source . display_line ( "<STR_LIT>" . format ( self . name ) ) <EOL> if self . inven : <EOL> for article in self . inven : <EOL> article . glance ( source ) <EOL> else : <EOL> source . display_line ( "<STR_LIT>" ) <EOL> def change_env ( self , new_env , exit_action = None ) : <EOL> if new_env : <EOL> self . leave_env ( exit_action ) <EOL> self . enter_env ( new_env , exit_action ) <EOL> else : <EOL> error ( "<STR_LIT>" , self . name ) <EOL> def leave_env ( self , exit_action = None ) : <EOL> if self . env : <EOL> old_env = self . env <EOL> self . env = None <EOL> exit_msg = Broadcast ( getattr ( exit_action , '<STR_LIT>' , self . exit_msg ) , self , exit_action , silent = True ) <EOL> old_env . entity_leaves ( self , exit_action , exit_msg ) <EOL> def enter_env ( self , new_env , enter_action = None ) : <EOL> entry_msg = Broadcast ( getattr ( enter_action , '<STR_LIT>' , self . entry_msg ) , self , silent = True ) <EOL> entry_msg . target = getattr ( enter_action , '<STR_LIT>' , None ) <EOL> new_env . entity_enters ( self , enter_action , entry_msg ) <EOL> new_instance = getattr ( self . env , '<STR_LIT>' , None ) <EOL> if self . instance != new_instance : <EOL> if self . instance : <EOL> self . instance . remove_entity ( self ) <EOL> del self . instance_id <EOL> if new_instance : <EOL> new_instance . add_entity ( self ) <EOL> self . instance_id = new_instance . instance_id <EOL> if self . instance : <EOL> self . instance_room_id = self . env . dbo_id <EOL> elif self . env . dbo_id : <EOL> self . room_id = self . env . dbo_id <EOL> self . env . first_look ( self ) <EOL> def broadcast ( self , ** kwargs ) : <EOL> broadcast = Broadcast ( ** kwargs ) <EOL> broadcast . source = self <EOL> if self . env : <EOL> self . env . receive_broadcast ( broadcast ) <EOL> def display_line ( self , line , display = '<STR_LIT:default>' ) : <EOL> pass <EOL> def output ( self , response ) : <EOL> pass <EOL> def can_see ( self , target ) : <EOL> return True <EOL> def check_inven ( self , article , quantity ) : <EOL> pass <EOL> def check_drop ( self , article , quantity ) : <EOL> pass <EOL> def die ( self ) : <EOL> self . exit_msg = BroadcastMap ( s = "<STR_LIT>" ) <EOL> for article in reversed ( self . inven ) : <EOL> article . current_slot = None <EOL> article . drop ( self ) <EOL> self . leave_env ( ) <EOL> self . status = '<STR_LIT>' <EOL> self . detach ( ) <EOL> def on_detach ( self ) : <EOL> for follower in self . followers : <EOL> del follower . following <EOL> follower . display_line ( "<STR_LIT>" . format ( self . name ) ) <EOL> for item in list ( itertools . chain ( self . inven , self . _soul_objects ) ) : <EOL> if hasattr ( item , '<STR_LIT>' ) : <EOL> item . detach ( ) <EOL> if hasattr ( item , '<STR_LIT>' ) : <EOL> item . detach_shared ( self ) <EOL> self . unfollow ( ) <EOL> self . leave_env ( ) <EOL> self . equip_slots . clear ( ) <EOL> @ property <EOL> def display_status ( self ) : <EOL> return { '<STR_LIT:status>' : self . status } <EOL> @ property <EOL> def dead ( self ) : <EOL> return self . status == '<STR_LIT>' </s>
