def test_pubsub_async(self): sync() s = topic("test1async") subber = Subber("sub1") subber2 = Subber("sub2") s.subscribe(subber) s.subscribe(subber2) s2 = topic("test1async") result = s2.send("event1") self.assertIsNone(result) self.assertEqual([], subber.messages) self.assertEqual([], subber2.messages) events, idle, subbers = pending()["test1async"] self.assertEqual(1, events) result = sync() events, idle, subbers = pending()["test1async"] self.assertEqual(0, events) self.assertIsNone(result) self.assertEqual([("test1async", "event1")], subber.messages) self.assertEqual([("test1async", "event1")], subber2.messages) subber.clear() subber2.clear() s2.send("event2") result = sync("test1async") self.assertEqual(2, len(result)) self.assertTrue("sub1" in result) self.assertTrue("sub2" in result) self.assertEqual([("test1async", "event2")], subber.messages) self.assertEqual([("test1async", "event2")], subber2.messages)
def test_pubsub_async(self): sync() s = topic("test1async") subber = Subber("sub1") subber2 = Subber("sub2") s.subscribe(subber) s.subscribe(subber2) s2 = topic("test1async") result = s2.send("event1") self.assertIsNone(result) self.assertEqual([], subber.messages) self.assertEqual([], subber2.messages) events, idle, subbers = pending()["test1async"] self.assertEqual(1, events) result = sync() events, idle, subbers = pending()["test1async"] self.assertEqual(0, events) self.assertEqual([], result) self.assertEqual([("test1async", "event1")], subber.messages) self.assertEqual([("test1async", "event1")], subber2.messages) subber.clear() subber2.clear() s2.send("event2") result = sync("test1async") self.assertEqual(2, len(result)) self.assertTrue("sub1" in result) self.assertTrue("sub2" in result) self.assertEqual([("test1async", "event2")], subber.messages) self.assertEqual([("test1async", "event2")], subber2.messages)
def notify_npc_arrived(self, npc: Living, previous_location: Location) -> None: # Using this notification override his is the best way to react to a certain # creatures (NPCs) arriving in the shop. # You could sniff the location's messages via a wiretap, but that often requires # nasty string parsing because they are messages meant for humans really. # We use pubsub to notify anyone interested. if npc.name == "rat": topic("shoppe-rat-arrival").send(npc)
def test_destroy(self): sync() s1 = topic("testA") s2 = topic("testB") s1.send("123") p = pending() self.assertIn("testA", p) self.assertIn("testB", p) s1.destroy() self.assertEqual("<defunct>", s1.name) p = pending() self.assertNotIn("testA", p) self.assertIn("testB", p) s2.destroy() p = pending() self.assertNotIn("testA", p) self.assertNotIn("testB", p)
def test_idletime(self): sync() s = topic("testA") self.assertLess(s.idle_time, 0.1) time.sleep(0.2) self.assertGreater(s.idle_time, 0.1) s.send("event") self.assertLess(s.idle_time, 0.1)
def test_weakrefs(self): s = topic("test222") subber = Subber("sub1") s.subscribe(subber) del subber gc.collect() result = s.send("after gc", True) self.assertEqual(0, len(result))
def test_pubsub(self): s = topic("test1") subber = Subber("sub1") subber2 = Subber("sub2") s.subscribe(subber) s.subscribe(subber) s.subscribe(subber2) s.subscribe(subber2) s2 = topic("test1") result = s2.send([1, 2, 3]) self.assertEqual(2, len(result)) self.assertTrue("sub1" in result) self.assertTrue("sub2" in result) # check explicit unsubscribe s2.unsubscribe(subber) s2.unsubscribe(subber) s2.unsubscribe(subber2) result = s2.send("after unsubscribing") self.assertEqual(0, len(result))
def test_unsubscribe_all(self): s1 = topic("testA") s2 = topic("testB") s3 = topic("testC") subber = Subber("sub1") s1.subscribe(subber) s2.subscribe(subber) s3.subscribe(subber) s1.send("one") s2.send("two") s3.send("three") self.assertEqual([('testA', 'one'), ('testB', 'two'), ('testC', 'three')], subber.messages) subber.clear() unsubscribe_all(subber) unsubscribe_all(subber) s1.send("one") s2.send("two") s3.send("three") self.assertEqual([], subber.messages)
def test_unsubscribe_all(self): s1 = topic("testA") s2 = topic("testB") s3 = topic("testC") subber = Subber("sub1") s1.subscribe(subber) s2.subscribe(subber) s3.subscribe(subber) s1.send("one") s2.send("two") s3.send("three") sync() self.assertEqual({('testA', 'one'), ('testB', 'two'), ('testC', 'three')}, set(subber.messages)) subber.clear() unsubscribe_all(subber) unsubscribe_all(subber) s1.send("one") s2.send("two") s3.send("three") sync() self.assertEqual([], subber.messages)
def test_weakrefs2(self): class Wiretap(Listener): def __init__(self): self.messages=[] def create_tap(self): tap = topic("wiretaptest") tap.subscribe(self) def pubsub_event(self, topicname, event): self.messages.append((topicname, event)) return 99 wiretap = Wiretap() wiretap.create_tap() t = topic("wiretaptest") result = t.send("hi") self.assertEqual(1, len(result)) self.assertEqual([('wiretaptest', 'hi')], wiretap.messages) del wiretap gc.collect() result = t.send("after gc") self.assertEqual(0, len(result))
def test_pubsub_sync(self): sync() s = topic("testsync") subber = Subber("sub1") subber2 = Subber("sub2") s.subscribe(subber) s.subscribe(subber) s.subscribe(subber2) s.subscribe(subber2) result = s.send([1, 2, 3], True) self.assertEqual([("testsync", [1, 2, 3])], subber.messages) self.assertEqual([("testsync", [1, 2, 3])], subber2.messages) self.assertEqual(2, len(result)) self.assertTrue("sub1" in result) self.assertTrue("sub2" in result) # check explicit unsubscribe s.unsubscribe(subber) s.unsubscribe(subber) s.unsubscribe(subber2) result = s.send("after unsubscribing", True) self.assertEqual(0, len(result))
def test_weakrefs2(self): class Wiretap(Listener): def __init__(self): self.messages = [] def create_tap(self): tap = topic("wiretaptest") tap.subscribe(self) def pubsub_event(self, topicname, event): self.messages.append((topicname, event)) return 99 wiretap = Wiretap() wiretap.create_tap() t = topic("wiretaptest") result = t.send("hi", True) self.assertEqual(1, len(result)) self.assertEqual([('wiretaptest', 'hi')], wiretap.messages) del wiretap gc.collect() result = t.send("after gc", True) self.assertEqual(0, len(result))
def test_aggregate_topic_name(self): s1 = topic(("t", 42)) s2 = topic(("t", 55)) s3 = topic(("t", 42)) self.assertTrue(s1 is s3) self.assertFalse(s1 is s2)
def test_global_namespace(self): s1 = topic("s1") s2 = topic("s2") s3 = topic("s1") self.assertTrue(s1 is s3) self.assertFalse(s1 is s2)
def notify_player_arrived(self, player: Player, previous_location: Location) -> None: # same as above, but for players entering the scene topic("shoppe-player-arrival").send(player)
The central town, which is the place where mud players start/log in 'Tale' mud driver, mudlib and interactive fiction framework Copyright by Irmen de Jong ([email protected]) """ from __future__ import absolute_import, print_function, division, unicode_literals from tale.base import Location, Exit, Item, Container, Living, Key from tale.npc import NPC from tale.player import Player from tale.errors import ActionRefused from tale.items.basic import Boxlike, Wearable from tale import pubsub from tale import lang pending_actions = pubsub.topic("driver-pending-actions") def init(driver): # called when zone is first loaded # board.load() pass gym = Location("Gymnasium", """ The gymnasium is where the Parsely Greens have their home games. It's currently empty of sweaty athletes and cheering fans. """) locker_room = Location("Locker Room", """ You are in a stereotypical high school locker room.
def create_tap(self): tap = topic("wiretaptest") tap.subscribe(self)
def test_notyet(self): s = topic("notyet") subber = RefusingSubber("refuser") s.subscribe(subber) s.send("event", True) self.assertEqual([], subber.messages)
stick = woodenYstick.clone() elastic = elastic_band.clone() shopkeeper.init_inventory([gem2, gem3, toothpick, stick, elastic]) shopkeeper.set_shop(shopinfo) # some stuff and people that are present in the shoppe shop.insert(clock, None) shop.insert(paper, None) lamp = Item("lamp", "rather small lamp") lamp.value = 600 customer = CustomerJames( "James", "m", title="Sir James", descr="Sir James is trying to sell something, it looks like a lamp.") lamp.add_extradesc( {"lamp"}, "The lamp looks quite old, but otherwise is rather unremarkable." " There is something weird going on with the cord though!") lamp.add_extradesc({ "cord" }, "Even when the lamp doesn't move, the power cord keeps snaking around as if it were alive. How odd." ) customer.insert(lamp, customer) shop.insert(customer, None) # shopkeeper and the customer want to act on creatures entering the shop: topic("shoppe-rat-arrival").subscribe(shopkeeper) topic("shoppe-rat-arrival").subscribe(customer) topic("shoppe-player-arrival").subscribe(shopkeeper) topic("shoppe-player-arrival").subscribe(customer)
clock.value = 500 paper = newspaper.clone() gem2 = diamond.clone() gem2.value = 80000 gem3 = gem.clone() gem3.value = 9055 stick = woodenYstick.clone() elastic = elastic_band.clone() shopkeeper.init_inventory([gem2, gem3, toothpick, stick, elastic]) shopkeeper.set_shop(shopinfo) # some stuff and people that are present in the shoppe shop.insert(clock, None) shop.insert(paper, None) lamp = Item("lamp", "rather small lamp") lamp.value = 600 customer = CustomerJames("James", "m", title="Sir James", descr="Sir James is trying to sell something, it looks like a lamp.") lamp.add_extradesc({"lamp"}, "The lamp looks quite old, but otherwise is rather unremarkable." " There is something weird going on with the cord though!") lamp.add_extradesc({"cord"}, "Even when the lamp doesn't move, the power cord keeps snaking around as if it were alive. How odd.") customer.insert(lamp, customer) shop.insert(customer, None) # shopkeeper and the customer want to act on creatures entering the shop: topic("shoppe-rat-arrival").subscribe(shopkeeper) topic("shoppe-rat-arrival").subscribe(customer) topic("shoppe-player-arrival").subscribe(shopkeeper) topic("shoppe-player-arrival").subscribe(customer)