def test_rune_fired(self): self.realm.state['target']='Emily' self.realm.metalineReceived(simpleml("You concentrate on the sowulu rune on an ink-stained snowy marble tablet, and its searing image suddenly flares on Emily's skin.",None,None)) self.realm.receive_gui_line('rv') self.realm.metalineReceived(simpleml("You concentrate on the pithakhan rune on an ink-stained snowy marble tablet, and its searing image suddenly flares on Emily's skin.",None,None)) self.realm.receive_gui_line('rv') self.realm.metalineReceived(simpleml("The residual effects of the sowulu rune around Emily fade.",None,None)) self.realm.receive_gui_line('rv')
def setUp(self): self.realm = RootRealm(Mock()) self.p = Mock() self.realm.addProtocol(self.p) self.realm.telnet = Mock() self.ml = simpleml('foo', sentinel.fores, sentinel.backs) self.ml2 = simpleml("bar", sentinel.fores, sentinel.backs) self.ml2_written = self.ml2.copy() self.ml2_written.insert(0, '\n')
def test_with_wrap(self): ml = simpleml("bar", sentinel.fore1, sentinel.fore2) ml.wrapped = Mock() ml.wrapped.return_value = ml2 = simpleml("foo", sentinel.fore, sentinel.back) self.realm.write(ml) assert ml.wrapped.called rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list] assert rcvd == [ml2]
def test_insert_metaline(self): ml_one = simpleml('foo', 'foo', 'foo') self.la.insert_metaline(1, simpleml('bar', 'bar', 'bar')) res = self.la.apply(ml_one) assert res == Metaline('fbaroo', RunLengthList([(0, 'foo'), (1, 'bar'), (4, 'foo')]), RunLengthList([(0, 'foo'), (1, 'bar'), (4, 'foo')]))
def test_rebounding_on(self): self.realm.state['last_command_type']='attack' self.realm.metalineReceived(simpleml('You suddenly perceive the vague outline of an aura of rebounding around Iniar.',None,None)) self.realm.state['last_command_type']='raze' self.realm.metalineReceived(simpleml('A shimmering translucent shield forms around Iniar.',None,None)) self.realm.metalineReceived(simpleml('The shimmering translucent shield around Iniar fades away.',None,None)) self.realm.metalineReceived(simpleml('Iniar\'s aura of weapons rebounding disappears.',None,None)) #print([l.line for l in self.lines_gotten]) #print([l for l in self.telnet_lines_gotten])
def test_receives_repeated_normal_CR_LF_in_broken_godwars_mode_fine(self): self.tc.fix_broken_godwars_line_endings = True self.tc.dataReceived("foo\r\n\r\n") expected = [simpleml("foo", fg_code(WHITE, False), bg_code(BLACK)), simpleml("", fg_code(WHITE, False), bg_code(BLACK))] for ml in expected: ml.wrap = True lines = [line for ((line,), kwargs) in self.e.metalineReceived.call_args_list] assert lines == expected, lines
def test_trace_writes_after_during_matching_triggers(self): self.realm.tracing = True self.realm.triggers.append(self.tracing_trigger) inline = Metaline('baz', set(), set()) self.realm.metalineReceived(inline) expected_lines = [simpleml("\nTRACE: %s matched!" % self.tracing_trigger, fg_code(WHITE, False), bg_code(BLACK)), simpleml("\nTRACE: Foo", fg_code(WHITE, False), bg_code(BLACK))] print self.lines_gotten print expected = [inline] + expected_lines print expected assert self.lines_gotten == expected
def barrier_off(self,match,realm): realm.display_line=False my_target=match.group(1).lower() if not my_target in self.raze_data: self.raze_data[my_target]=ShieldStatus() else: self.raze_data[my_target].barrier=False realm.root.fireEvent('barrierEvent',my_target,0) if realm.root.get_state('target').lower()==my_target: realm.write(simpleml('BARRIER DOWN, BARRIER DOWN',fg_code(YELLOW,True),bg_code(GREEN))) if self.raze_data[my_target].all_stripped: realm.write(simpleml('ALL SHIELDS DOWN, ALL SHIELDS DOWN',fg_code(YELLOW,True),bg_code(GREEN))) if self.on_shields_down!=None: self.on_shields_down(realm, self.raze_data[my_target])
def prompt_time_display(match, realm): """Add a pink coloured timestamp to the prompt.""" now = datetime.now() metaline = simpleml((now.strftime("(%H:%M:%S.%%s) ") % str(now.microsecond)[:2]), HexFGCode(0xFF, 0xA0, 0xA0), bg_code(BLACK)) realm.alterer.insert_metaline(match.end(1), metaline)
def write(self, line, soft_line_start = False): """Write a line to the screen. This forcibly converts its argument to a Metaline. """ if not isinstance(line, (basestring, Metaline)): line = str(line) if isinstance(line, basestring): metaline = simpleml(line, fg_code(WHITE, False), bg_code(BLACK)) metaline.wrap = False metaline.soft_line_start = soft_line_start else: metaline = line #we don't need to close off the ends of the note, because thanks to #the magic of the ColourCodeParser, each new line is started by the #implied colour, so notes can't bleed out into text (though the #reverse can be true). #this needs to be before the futzing with NLs and GA, because textwrap #obliterates all other newlines. metaline = metaline.wrapped(self.wrapper) #we don't actually append newlines at the end, but the start. This #simplifies things, because we don't use a newline where a soft line #end meets a soft line start, so there's only one place in this code #that can add newlines. if self._last_line_end is not None: if self._last_line_end == 'hard' or not metaline.soft_line_start: metaline.insert(0, '\n') for prot in self.protocols: prot.metalineReceived(metaline,self.active_channels) self._last_line_end = metaline.line_end
def rebound_off(self, match,realm): realm.display_line=False my_target=match.group(1).lower() if not my_target in self.raze_data: self.raze_data[my_target]=ShieldStatus() else: self.raze_data[my_target].aura=False realm.root.fireEvent('reboundingEvent',my_target,0) if realm.root.get_state('target').lower()==my_target: realm.cwrite('--- REBOUNDING IS ---- %s'%self.raze_data[my_target].aura) realm.write(simpleml('REBOUNDING DOWN, REBOUNDING DOWN',fg_code(YELLOW,True),bg_code(GREEN))) if self.raze_data[my_target].all_stripped: realm.write(simpleml('ALL SHIELDS DOWN, ALL SHIELDS DOWN',fg_code(YELLOW,True),bg_code(GREEN))) if self.on_shields_down!=None: self.on_shields_down(realm, self.raze_data[my_target])
def test_trace_writes_after_during_alias_matching(self): self.realm.tracing = True self.realm.aliases.append(self.tracing_alias) inline = Metaline('baz', RunLengthList([(0, fg_code(WHITE, False))]), RunLengthList([(0, bg_code(BLACK))]), soft_line_start = True) self.realm.send('baz') expected_lines = [simpleml("\nTRACE: %s matched!" % self.tracing_alias, fg_code(WHITE, False), bg_code(BLACK)), simpleml("\nTRACE: Foo", fg_code(WHITE, False), bg_code(BLACK))] print self.lines_gotten print expected = [inline] + expected_lines print expected assert self.lines_gotten == expected
def test_trace_thunk_remembers_tracing_when_attempted(self): self.realm.write = Mock() self.realm.triggers.append(self.trace_thunk_twiddling_trigger) self.realm.metalineReceived(simpleml("Foo", None, None)) print self.realm.write.call_args_list assert self.realm.write.call_args_list == [(("TRACE: Foo", False), {})]
def test_respects_no_line_end(self): self.realm._last_line_end = None ml = simpleml("foo", sentinel.fore, sentinel.back) expected = ml self.realm.write(ml) rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list] assert rcvd == [expected], rcvd
def telnetConnectionMade(self): """The MUD's been connected to.""" message = time.strftime("Connection opened at %H:%M:%S.") colour = HexFGCode(0xFF, 0xAA, 0x00) #lovely orange metaline = simpleml(message, colour, bg_code(BLACK)) self.write(metaline) for prot in self.protocols: prot.connectionMade()
def test_line_no_insertion_sle_sls(self): self.realm._last_line_end = 'soft' ml = simpleml("foo", sentinel.fore, sentinel.back) ml.soft_line_start = True expected = ml.copy() self.realm.write(ml) rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list] assert rcvd == [expected]
def test_fixes_LF_CR_normally(self): self.tc.fix_broken_godwars_line_endings = True self.tc.dataReceived("foo\n\r") expected = [simpleml("foo", fg_code(WHITE, False), bg_code(BLACK))] expected[0].wrap = True lines = [line for ((line,), kwargs) in self.e.metalineReceived.call_args_list] assert lines == expected, lines
def write(self, line, display_line = True): if not isinstance(line, (basestring, Metaline)): line = str(line) if isinstance(line, basestring): metaline = simpleml(line, fg_code(WHITE, False), bg_code(BLACK)) metaline.wrap = False else: metaline = line self.send_to_client('display_line', [metaline_to_json(metaline),int(display_line)])
def test_insert_metaline(): ml_one = Metaline("foo baz", RunLengthList([(0, "foo"), (4, "baz")]), RunLengthList([(0, "foo"), (4, "baz")])) ml_two = simpleml("bar ", "bar", "bar") ml_one.insert_metaline(4, ml_two) assert ml_one == Metaline( "foo bar baz", RunLengthList([(0, "foo"), (4, "bar"), (8, "baz")]), RunLengthList([(0, "foo"), (4, "bar"), (8, "baz")]), )
def barrier_off(self, match, realm): realm.display_line = False my_target = match.group(1).lower() if not my_target in self.raze_data: self.raze_data[my_target] = ShieldStatus() else: self.raze_data[my_target].barrier = False realm.root.fireEvent('barrierEvent', my_target, 0) if realm.root.get_state('target').lower() == my_target: realm.write( simpleml('BARRIER DOWN, BARRIER DOWN', fg_code(YELLOW, True), bg_code(GREEN))) if self.raze_data[my_target].all_stripped: realm.write( simpleml('ALL SHIELDS DOWN, ALL SHIELDS DOWN', fg_code(YELLOW, True), bg_code(GREEN))) if self.on_shields_down != None: self.on_shields_down(realm, self.raze_data[my_target])
def no_target_rebound_on(self, match, realm): realm.display_line=False my_target=realm.root.get_state('target').lower() if not my_target in self.raze_data: self.raze_data[my_target]=ShieldStatus(aura=True) else: self.raze_data[my_target].aura=True realm.write(simpleml('REBOUNDING ON, REBOUNDING ON',fg_code(YELLOW,True),bg_code(RED))) realm.root.fireEvent('reboundingEvent',my_target,1) if self.on_shields_up!=None: self.on_shields_up(realm, self.raze_data[my_target])
def rebound_off(self, match, realm): realm.display_line = False my_target = match.group(1).lower() if not my_target in self.raze_data: self.raze_data[my_target] = ShieldStatus() else: self.raze_data[my_target].aura = False realm.root.fireEvent('reboundingEvent', my_target, 0) if realm.root.get_state('target').lower() == my_target: realm.cwrite('--- REBOUNDING IS ---- %s' % self.raze_data[my_target].aura) realm.write( simpleml('REBOUNDING DOWN, REBOUNDING DOWN', fg_code(YELLOW, True), bg_code(GREEN))) if self.raze_data[my_target].all_stripped: realm.write( simpleml('ALL SHIELDS DOWN, ALL SHIELDS DOWN', fg_code(YELLOW, True), bg_code(GREEN))) if self.on_shields_down != None: self.on_shields_down(realm, self.raze_data[my_target])
def barrier_on(self, match,realm): realm.display_line=False my_target = match.group(1).lower() if not my_target in self.raze_data: self.raze_data[my_target]=ShieldStatus(barrier=True) else: self.raze_data[my_target].barrier=True realm.root.fireEvent('barrierEvent',my_target,1) if realm.root.get_state('target').lower()==my_target: new_line = 'BARRIER ON, BARRIER ON' realm.write(simpleml(new_line, fg_code(YELLOW,True),bg_code(RED))) if self.on_shields_up!=None: self.on_shields_up(realm,self.raze_data[my_target])
def no_target_rebound_on(self, match, realm): realm.display_line = False my_target = realm.root.get_state('target').lower() if not my_target in self.raze_data: self.raze_data[my_target] = ShieldStatus(aura=True) else: self.raze_data[my_target].aura = True realm.write( simpleml('REBOUNDING ON, REBOUNDING ON', fg_code(YELLOW, True), bg_code(RED))) realm.root.fireEvent('reboundingEvent', my_target, 1) if self.on_shields_up != None: self.on_shields_up(realm, self.raze_data[my_target])
def clientConnectionMade(self, client): """We've got our user's stuff running. Benissimo!""" colour = HexFGCode(0xFF, 0xAA, 0x00) if self.telnet is None: message = "Trying to connect..." factory = TelnetClientFactory(self) from twisted.internet import reactor reactor.connectTCP(self.mod.host, self.mod.port, factory) else: self.client.close() message = "Client reloaded!" self.client = client self.write(simpleml(message, colour))
def rebounding_soon(self, match, realm): #realm.display_line = False my_target=match.group(1).lower() if my_target==realm.root.get_state('target').lower(): realm.write(simpleml('REBOUNDING SOON, REBOUNDING SOON!', fg_code(YELLOW,True),bg_code(RED))) def delayed_aura(realm): realm.write("DONE!") if not my_target in self.raze_data: self.raze_data[my_target]=ShieldStatus(aura=True) else: self.raze_data[my_target].aura=True if self.on_shields_up!=None: self.on_shields_up(realm, self.raze_data[my_target]) self.aura_timer=realm.root.set_timer(7, delayed_aura, realm.root)
def barrier_on(self, match, realm): realm.display_line = False my_target = match.group(1).lower() if not my_target in self.raze_data: self.raze_data[my_target] = ShieldStatus(barrier=True) else: self.raze_data[my_target].barrier = True realm.root.fireEvent('barrierEvent', my_target, 1) if realm.root.get_state('target').lower() == my_target: new_line = 'BARRIER ON, BARRIER ON' realm.write(simpleml(new_line, fg_code(YELLOW, True), bg_code(RED))) if self.on_shields_up != None: self.on_shields_up(realm, self.raze_data[my_target])
def rebounding_soon(self, match, realm): #realm.display_line = False my_target = match.group(1).lower() if my_target == realm.root.get_state('target').lower(): realm.write( simpleml('REBOUNDING SOON, REBOUNDING SOON!', fg_code(YELLOW, True), bg_code(RED))) def delayed_aura(realm): realm.write("DONE!") if not my_target in self.raze_data: self.raze_data[my_target] = ShieldStatus(aura=True) else: self.raze_data[my_target].aura = True if self.on_shields_up != None: self.on_shields_up(realm, self.raze_data[my_target]) self.aura_timer = realm.root.set_timer(7, delayed_aura, realm.root)
def connectionLost(self): """The link to the MUD died. It is guaranteed that this will be called before close on connection event receivers. """ message = time.strftime("Connection closed at %H:%M:%S.") colour = HexFGCode(0xFF, 0xAA, 0x00) #lovely orange metaline = simpleml(message, colour, bg_code(BLACK)) self.write(metaline) for prot in self.protocols: prot.connectionLost() #we might be waiting on the connection to die before we send out #close events if self._closing_down: for prot in self.protocols: prot.close() self._closing_down = True
def test_simple_trigger(self): self.realm.metalineReceived(simpleml('abc', None, None)) return_line = self.lines_gotten[1].line assert self.lines_gotten[1].line == '\nsuccess'
def test_last_line_end_setting(self): ml = simpleml("foo", sentinel.fore, sentinel.back) ml.line_end = sentinel.line_end self.realm.write(ml) assert self.realm._last_line_end == sentinel.line_end
def test_insert_then_insert_metaline_bumps(self): self.la.insert(1, 'foo') self.la.insert_metaline(2, simpleml('bar', 'bar', 'bar')) res = self.la.apply(simpleml('baz', None, None)) assert res.line == "bfooabarz", res.line
def test_list_trigger(self): self.realm.metalineReceived(simpleml('highjump', None, None)) return_line = self.lines_gotten[1].line assert return_line=='\nsuccess2'
def test_line_insertion_sle_no_sls(self): self.realm._last_line_end = 'soft' self.realm.write(simpleml("foo", sentinel.fore, sentinel.back)) rcvd = [ml for ((ml,), _) in self.p.metalineReceived.call_args_list] assert rcvd == [simpleml("\nfoo", sentinel.fore, sentinel.back)]
def setUp(self): self.realm = RootRealm(Mock()) self.realm.telnet = Mock() self.p = Mock() self.realm.addProtocol(self.p) self.noting_line = simpleml("foo", Mock(), Mock())