<s> from zope . interface import implements <EOL> from twisted . internet import defer <EOL> from twisted . python import log <EOL> from imaginary import iimaginary , eimaginary , text as T <EOL> from imaginary . text import flatten <EOL> from imaginary . action import Action <EOL> class Player ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( iimaginary . IEventObserver ) <EOL> proto = None <EOL> useColors = True <EOL> def __init__ ( self , actor ) : <EOL> self . actor = actor <EOL> self . player = iimaginary . IActor ( actor ) <EOL> self . player . setEphemeralIntelligence ( self ) <EOL> def setProtocol ( self , proto ) : <EOL> if self . proto is not None : <EOL> self . send ( "<STR_LIT>" ) <EOL> self . disconnect ( ) <EOL> self . proto = proto <EOL> self . termAttrs = T . AttributeSet ( ) <EOL> def parse ( self , line ) : <EOL> def cbParse ( result ) : <EOL> pass <EOL> def ebParse ( err ) : <EOL> err . trap ( eimaginary . NoSuchCommand ) <EOL> self . proto . write ( '<STR_LIT>' ) <EOL> def ebAmbiguity ( err ) : <EOL> err . trap ( eimaginary . AmbiguousArgument ) <EOL> exc = err . value <EOL> msg = "<STR_LIT>" + exc . partValue + "<STR_LIT>" <EOL> def format ( potentialTarget ) : <EOL> concept = iimaginary . IConcept ( potentialTarget ) <EOL> vt102 = concept . vt102 ( self . player ) <EOL> flattened = list ( flatten ( vt102 , useColors = False ) ) <EOL> return "<STR_LIT>" . join ( flattened ) <EOL> for obj in exc . objects [ : - <NUM_LIT:1> ] : <EOL> msg += format ( obj ) <EOL> msg += "<STR_LIT:U+002CU+0020>" <EOL> msg += "<STR_LIT>" <EOL> msg += format ( exc . objects [ - <NUM_LIT:1> ] ) <EOL> msg += "<STR_LIT:?>" <EOL> self . send ( ( msg , "<STR_LIT:\r\n>" ) ) <EOL> def ebUnexpected ( err ) : <EOL> log . err ( err ) <EOL> self . proto . write ( '<STR_LIT>' ) <EOL> self . send ( ( '<STR_LIT>' , line , '<STR_LIT:\n>' ) ) <EOL> d = defer . maybeDeferred ( Action . parse , self . actor , line ) <EOL> d . addCallbacks ( cbParse , ebParse ) <EOL> d . addErrback ( ebAmbiguity ) <EOL> d . addErrback ( ebUnexpected ) <EOL> return d <EOL> def disconnect ( self ) : <EOL> if self . proto and self . proto . terminal : <EOL> self . proto . player = None <EOL> self . proto . terminal . loseConnection ( ) <EOL> if self . player . getIntelligence ( ) is self : <EOL> self . player . setEphemeralIntelligence ( None ) <EOL> def prepare ( self , concept ) : <EOL> """<STR_LIT>""" <EOL> stuff = concept . vt102 ( self . actor ) <EOL> def send ( ) : <EOL> if self . proto is not None : <EOL> self . send ( stuff ) <EOL> return send <EOL> def send ( self , stuff ) : <EOL> """<STR_LIT>""" <EOL> flatterStuff = T . flatten ( stuff , useColors = self . useColors , <EOL> currentAttrs = self . termAttrs ) <EOL> txt = u'<STR_LIT>' . join ( list ( flatterStuff ) ) <EOL> bytes = txt . encode ( '<STR_LIT:utf-8>' ) <EOL> self . proto . write ( bytes ) </s>
