def test_custom_verbs(self): player = Player("julie", "f") player.verbs["xywobble"] = "p1" monster = NPC("snake", "f") monster.verbs["snakeverb"] = "s1" room = Location("room") chair1 = Item("chair1") chair1.verbs["frobnitz"] = "c1" chair2 = Item("chair2") chair2.verbs["frobnitz"] = "c2" chair_in_inventory = Item("chair3") chair_in_inventory.verbs["kowabooga"] = "c3" box_in_inventory = Item("box") box_in_inventory.verbs["boxverb"] = "c4" player.init_inventory([box_in_inventory, chair_in_inventory]) exit = Exit("e", "dummy", None, None) exit.verbs["exitverb"] = "c5" room.init_inventory([chair1, player, chair2, monster]) room.add_exits([exit]) custom_verbs = mud_context.driver.current_custom_verbs(player) all_verbs = mud_context.driver.current_verbs(player) self.assertEqual( { "xywobble", "snakeverb", "frobnitz", "kowabooga", "boxverb", "exitverb" }, set(custom_verbs)) self.assertEqual(set(), set(custom_verbs) - set(all_verbs))
def test_move(self): hall = Location("hall") attic = Location("attic") rat = Living("rat", "n", race="rodent") hall.init_inventory([rat]) wiretap_hall = Wiretap(hall) wiretap_attic = Wiretap(attic) self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) rat.move(attic) self.assertTrue(rat in attic.livings) self.assertFalse(rat in hall.livings) self.assertEqual(attic, rat.location) self.assertEqual([("hall", "Rat leaves.")], wiretap_hall.msgs) self.assertEqual([("attic", "Rat arrives.")], wiretap_attic.msgs) # now try silent wiretap_hall.clear() wiretap_attic.clear() rat.move(hall, silent=True) self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) self.assertEqual([], wiretap_hall.msgs) self.assertEqual([], wiretap_attic.msgs)
def test_move(self): hall = Location("hall") attic = Location("attic") rat = Living("rat", "n", race="rodent") hall.init_inventory([rat]) wiretap_hall = Wiretap(hall) wiretap_attic = Wiretap(attic) self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) rat.move(attic) self.assertTrue(rat in attic.livings) self.assertFalse(rat in hall.livings) self.assertEqual(attic, rat.location) pubsub.sync() self.assertEqual([("hall", "Rat leaves.")], wiretap_hall.msgs) self.assertEqual([("attic", "Rat arrives.")], wiretap_attic.msgs) # now try silent wiretap_hall.clear() wiretap_attic.clear() rat.move(hall, silent=True) pubsub.sync() self.assertTrue(rat in hall.livings) self.assertFalse(rat in attic.livings) self.assertEqual(hall, rat.location) self.assertEqual([], wiretap_hall.msgs) self.assertEqual([], wiretap_attic.msgs)
def test_custom_verbs(self): player = Player("julie", "f") player.verbs["xywobble"] = "p1" room = Location("room") chair1 = Item("chair1") chair1.verbs["frobnitz"] = "c1" chair2 = Item("chair2") chair2.verbs["frobnitz"] = "c2" chair_in_inventory = Item("chair3") chair_in_inventory.verbs["kowabooga"] = "c3" room.init_inventory([chair1, player, chair2]) # check inventory NOT affecting player custom verbs, but DOES affect location verbs self.assertEqual({"xywobble": "p1"}, player.verbs) self.assertEqual({"frobnitz": "c2", "xywobble": "p1"}, room.verbs) player.insert(chair_in_inventory, player) self.assertEqual({"xywobble": "p1"}, player.verbs) self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3"}, room.verbs) player.remove(chair_in_inventory, player) self.assertEqual({"frobnitz": "c2", "xywobble": "p1"}, room.verbs) player.insert(chair_in_inventory, player) self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3" }, room.verbs) room2 = Location("room2") self.assertEqual({}, room2.verbs) chair1.move(room2, player) self.assertEqual({"xywobble": "p1", "kowabooga": "c3" }, room.verbs) self.assertEqual({"frobnitz": "c1"}, room2.verbs) chair2.move(room2, player) self.assertEqual({"xywobble": "p1", "kowabooga": "c3"}, room.verbs) self.assertEqual({"frobnitz": "c2"}, room2.verbs) player.move(room2) self.assertEqual({}, room.verbs) self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3"}, room2.verbs)
def test_move(self): hall = Location("hall") person = Living("person", "m", race="human") monster = NPC("dragon", "f", race="dragon") monster.aggressive = True key = Item("key") stone = Item("stone") hall.init_inventory([person, key]) stone.move(hall, person) wiretap = Wiretap(hall) self.assertTrue(person in hall) self.assertTrue(key in hall) key.contained_in = person # hack to force move to actually check the source container with self.assertRaises(KeyError): key.move(person, person) key.contained_in = hall # put it back as it was key.move(person, person) self.assertFalse(key in hall) self.assertTrue(key in person) self.assertEqual([], wiretap.msgs, "item.move() should be silent") with self.assertRaises(ActionRefused) as x: key.move(monster, person) # aggressive monster should fail self.assertTrue("not a good idea" in str(x.exception)) monster.aggressive = False key.move(monster, person) # non-aggressive should be ok
def test_move(self): hall = Location("hall") person = Living("person", "m", race="human") monster = NPC("dragon", "f", race="dragon") monster.aggressive = True key = Item("key") stone = Item("stone") hall.init_inventory([person, key]) stone.move(hall, person) wiretap = Wiretap(hall) self.assertTrue(person in hall) self.assertTrue(key in hall) key.contained_in = person # hack to force move to actually check the source container with self.assertRaises(KeyError): key.move(person, person) key.contained_in = hall # put it back as it was key.move(person, person) self.assertFalse(key in hall) self.assertTrue(key in person) self.assertEqual([], wiretap.msgs, "item.move() should be silent") with self.assertRaises(ActionRefused) as x: key.move(monster, person) # aggressive monster should fail self.assertTrue("not a good idea" in str(x.exception)) monster.aggressive = False key.move(monster, person) # non-aggressive should be ok
def test_destroy_player(self): ctx = Context() loc = Location("loc") player = Player("julie", "f") player.privileges = {"wizard"} player.create_wiretap(loc) player.insert(Item("key"), player) loc.init_inventory([player]) self.assertEqual(loc, player.location) self.assertTrue(len(player.inventory) > 0) self.assertTrue(player in loc.livings) player.destroy(ctx) import gc gc.collect() self.assertTrue(len(player.inventory) == 0) self.assertFalse(player in loc.livings) self.assertIsNone(player.location, "destroyed player should end up nowhere (None)")
def test_destroy_player(self): ctx = Context(None, None, None, None) loc = Location("loc") player = Player("julie", "f") player.privileges = {"wizard"} player.create_wiretap(loc) player.insert(Item("key"), player) loc.init_inventory([player]) self.assertEqual(loc, player.location) self.assertTrue(len(player.inventory) > 0) self.assertTrue(player in loc.livings) player.destroy(ctx) import gc gc.collect() self.assertTrue(len(player.inventory) == 0) self.assertFalse(player in loc.livings) self.assertIsNone(player.location, "destroyed player should end up nowhere (None)")
def test_destroy_loc(self): ctx = Context() loc = Location("loc") i = Item("item") liv = Living("rat", "n", race="rodent") loc.add_exits([Exit("north", "somewhere", "exit to somewhere")]) player = Player("julie", "f") player.privileges = {"wizard"} player.create_wiretap(loc) loc.init_inventory([i, liv, player]) self.assertTrue(len(loc.exits) > 0) self.assertTrue(len(loc.items) > 0) self.assertTrue(len(loc.livings) > 0) self.assertEqual(loc, player.location) self.assertEqual(loc, liv.location) loc.destroy(ctx) self.assertTrue(len(loc.exits) == 0) self.assertTrue(len(loc.items) == 0) self.assertTrue(len(loc.livings) == 0) self.assertEqual(_Limbo, player.location) self.assertEqual(_Limbo, liv.location)
def test_destroy_loc(self): ctx = Context(None, None, None, None) loc = Location("loc") i = Item("item") liv = Living("rat", "n", race="rodent") loc.add_exits([Exit("north", "somewhere", "exit to somewhere")]) player = Player("julie", "f") player.privileges = {"wizard"} player.create_wiretap(loc) loc.init_inventory([i, liv, player]) self.assertTrue(len(loc.exits) > 0) self.assertTrue(len(loc.items) > 0) self.assertTrue(len(loc.livings) > 0) self.assertEqual(loc, player.location) self.assertEqual(loc, liv.location) loc.destroy(ctx) self.assertTrue(len(loc.exits) == 0) self.assertTrue(len(loc.items) == 0) self.assertTrue(len(loc.livings) == 0) self.assertEqual(_limbo, player.location) self.assertEqual(_limbo, liv.location)
def test_custom_verbs(self): player = Player("julie", "f") player.verbs["xywobble"] = "p1" monster = NPC("snake", "f") monster.verbs["snakeverb"] = "s1" room = Location("room") chair1 = Item("chair1") chair1.verbs["frobnitz"] = "c1" chair2 = Item("chair2") chair2.verbs["frobnitz"] = "c2" chair_in_inventory = Item("chair3") chair_in_inventory.verbs["kowabooga"] = "c3" box_in_inventory = Item("box") box_in_inventory.verbs["boxverb"] = "c4" player.init_inventory([box_in_inventory, chair_in_inventory]) exit = Exit("e", "dummy", None, None) exit.verbs["exitverb"] = "c5" room.init_inventory([chair1, player, chair2, monster]) room.add_exits([exit]) custom_verbs = mud_context.driver.current_custom_verbs(player) all_verbs = mud_context.driver.current_verbs(player) self.assertEqual({"xywobble", "snakeverb", "frobnitz", "kowabooga", "boxverb", "exitverb"}, set(custom_verbs)) self.assertEqual(set(), set(custom_verbs) - set(all_verbs))
def test_location(self): thingy = Item("thing") with self.assertRaises(TypeError): thingy.location = "foobar" hall = Location("hall") thingy.location = hall self.assertEqual(hall, thingy.contained_in) self.assertEqual(hall, thingy.location) person = Living("person", "m", race="human") key = Item("key") backpack = Container("backpack") person.insert(backpack, person) self.assertIsNone(key.contained_in) self.assertIsNone(key.location) self.assertTrue(backpack in person) self.assertEqual(person, backpack.contained_in) self.assertEqual(_Limbo, backpack.location) hall.init_inventory([person, key]) self.assertEqual(hall, key.contained_in) self.assertEqual(hall, key.location) self.assertEqual(hall, backpack.location) key.move(backpack, person) self.assertEqual(backpack, key.contained_in) self.assertEqual(hall, key.location)
def test_location(self): thingy = Item("thing") with self.assertRaises(TypeError): thingy.location = "foobar" hall = Location("hall") thingy.location = hall self.assertEqual(hall, thingy.contained_in) self.assertEqual(hall, thingy.location) person = Living("person", "m", race="human") key = Item("key") backpack = Container("backpack") person.insert(backpack, person) self.assertIsNone(key.contained_in) self.assertIsNone(key.location) self.assertTrue(backpack in person) self.assertEqual(person, backpack.contained_in) self.assertEqual(_limbo, backpack.location) hall.init_inventory([person, key]) self.assertEqual(hall, key.contained_in) self.assertEqual(hall, key.location) self.assertEqual(hall, backpack.location) key.move(backpack, person) self.assertEqual(backpack, key.contained_in) self.assertEqual(hall, key.location)
def heartbeat(self, ctx): rand = random.random() if rand < 0.07: self.do_socialize("twitch erra") elif rand < 0.14: self.do_socialize("rotate random") elif rand < 0.21: self.location.tell("%s hums softly." % tale.lang.capital(self.title)) drone = Drone("drone", "n", race="bot", title="mindless drone", description="A stupid metallic drone. It just hovers here with no apparent reason. It has a little label attached to it.") drone.add_extradesc({"label"}, "The label reads: \"Wall-E was my cousin\".") drone.aggressive = True hall.init_inventory([table, key, drone]) attic = Location("Tower attic", """ The dark and dusty attic of the wizard tower. There are piles of old scrolls and assorted stuff here of which you assume it once held great magical power. All of it is now covered with a thick layer of dust. """) attic.add_extradesc({"dust", "stuff", "scrolls"}, "The scrolls look ancient. One looks like a spell!") attic.add_extradesc({"spell"}, "The scroll looks like it contains a spell, but on closer inspection, it has become too faded to be understood anymore.") kitchen = Location("Tower kitchen", """ A cozy little kitchen for hungry wizards.
def test_handle_and_notify_action(self): class SpecialPlayer(Player): def init(self): self.handled = False self.handle_verb_called = False self.notify_called = False def handle_verb(self, parsed, actor): self.handle_verb_called = True if parsed.verb in self.verbs: self.handled = True return True else: return False def notify_action(self, parsed, actor): self.notify_called = True player = SpecialPlayer("julie", "f") player.verbs["xywobble"] = "" room = Location("room") class Chair(Item): def init(self): self.handled = False self.handle_verb_called = False self.notify_called = False def handle_verb(self, parsed, actor): self.handle_verb_called = True if parsed.verb in self.verbs: self.handled = True return True else: return False def notify_action(self, parsed, actor): self.notify_called = True chair_in_inventory = Chair("littlechair") chair_in_inventory.verbs["kerwaffle"] = "" player.insert(chair_in_inventory, player) chair = Chair("chair") chair.verbs["frobnitz"] = "" room.init_inventory([player, chair]) # first check if the handle_verb passes to all objects including inventory parsed = ParseResult("kowabungaa12345") handled = room.handle_verb(parsed, player) self.assertFalse(handled) self.assertTrue(chair.handle_verb_called) self.assertTrue(player.handle_verb_called) self.assertTrue(chair_in_inventory.handle_verb_called) self.assertFalse(chair.handled) self.assertFalse(player.handled) self.assertFalse(chair_in_inventory.handled) # check item handling player.init() chair.init() chair_in_inventory.init() parsed = ParseResult("frobnitz") handled = room.handle_verb(parsed, player) self.assertTrue(handled) self.assertTrue(chair.handled) self.assertFalse(player.handled) self.assertFalse(chair_in_inventory.handled) # check living handling player.init() chair.init() chair_in_inventory.init() parsed = ParseResult("xywobble") handled = room.handle_verb(parsed, player) self.assertTrue(handled) self.assertFalse(chair.handled) self.assertTrue(player.handled) self.assertFalse(chair_in_inventory.handled) # check inventory handling player.init() chair.init() chair_in_inventory.init() parsed = ParseResult("kerwaffle") handled = room.handle_verb(parsed, player) self.assertTrue(handled) self.assertFalse(chair.handled) self.assertFalse(player.handled) self.assertTrue(chair_in_inventory.handled) # check notify_action player.init() chair.init() chair_in_inventory.init() room.notify_action(parsed, player) self.assertTrue(chair.notify_called) self.assertTrue(player.notify_called) self.assertTrue(chair_in_inventory.notify_called)
) vomit.verbs = { # register some custom verbs. You can redefine existing verbs, so be careful. "clean": "Clean up the puke.", "cover": "Clean up the puke." # "put": "Put something on something else.", } vomit.add_extradesc( {"puddle", "sick", "vomit", "puke"}, """ Someone appears to have had tummy troubles; a puddle of sick lies in the middle of the corridor. The puddle is . . . kinda gross. It was Salisbury Steak Day. """, ) south_hallway.init_inventory([vomit]) english_paper = Item("english homework", "english paper") english_paper.aliases = {"english paper", "paper", "homework"} english_paper.add_extradesc({"paper", "english paper", "homework"}, "You paid good money for this!") class Locker(Boxlike): def init(self, locked=True, opened=False, combo="8-16-32"): super(Boxlike, self).init() self.txt_title_closed = self._title self.txt_title_open_filled = self._title self.txt_title_open_empty = "empty " + self._title self.txt_descr_closed = "The locker is closed." self.txt_descr_open_filled = "There's something inside {}.".format(self._title) self.txt_descr_open_empty = "There's nothing inside {}.".format(self._title)
towncrier.aliases = {"crier", "town crier"} idiot = VillageIdiot("idiot", "m", title="blubbering idiot", description=""" This person's engine is running but there is nobody behind the wheel. He is a few beers short of a six-pack. Three ice bricks shy of an igloo. Not the sharpest knife in the drawer. Anyway you get the idea: it's an idiot. """) rat = WalkingRat("rat", "n", race="rodent", description="A filthy looking rat. Its whiskers tremble slightly as it peers back at you.") ant = NPC("ant", "n", race="insect", short_description="A single ant seems to have lost its way.") clock = clone(gameclock) clock.short_description = "On the pavement lies a clock, it seems to be working still." square.init_inventory([cursed_gem, normal_gem, paper, trashcan, pouch, insertonly_box, removeonly_box, clock, towncrier, idiot, rat, ant]) class AlleyOfDoors(Location): def notify_player_arrived(self, player, previous_location): if previous_location is self: player.tell("...Weird... The door you just entered seems to go back to the same place you came from...") alley = AlleyOfDoors("Alley of doors", "An alley filled with doors.") descr = "The doors seem to be connected to the computer nearby." door1 = Door(["first door", "door one"], alley, "There's a door marked 'door one'.", long_description=descr, locked=False, opened=True) door2 = Door(["second door", "door two"], alley, "There's a door marked 'door two'.", long_description=descr, locked=True, opened=False) door3 = Door(["third door", "door three"], alley, "There's a door marked 'door three'.", long_description=descr, locked=False, opened=False) door4 = Door(["fourth door", "door four"], alley, "There's a door marked 'door four'.", long_description=descr, locked=True, opened=False) alley.add_exits([ door1, door2, door3, door4,
while self.wearing: pass def vision_problems(self): pass glasses = Item("glasses", "glasses", "pair of ladies eyeglasses.") glasses.aliases = {"glasses"} glasses.add_extradesc({"glasses"}, "The horn-rimmed glasses aren't really your style.") glasses_case = GlassesCase("case", "clamshell case for eyeglasses") glasses_case.aliases = {"case", "clamshell case", "eyeglasses case"} glasses_case.init_inventory([glasses]) gym.init_inventory([glasses_case]) freshman = NPC("freshman", "m", title="freshman", description="It's a freshman. They all look the same.") # sawdust = Item("sawdust", "sawdust") # sawdust.add_extradesc({"sawdust", "pink sawdust"}, "It's pink sawdust, the kind used to soak up spills and messes.") # bucket = Container("bucket", "bucket") # bucket.add_extradesc({"bucket"}, "You see an ordinary blue plastic bucket.") # bucket.init_inventory([sawdust]) trash_can = UntakableContainer("trash can", "large grey trash can") trash_can.aliases = {"trash can", "trashcan", "trash"} trash_can.init_inventory([freshman]) # broom = Item("broom", "broom")
class TestLocations(unittest.TestCase): def setUp(self): mud_context.driver = DummyDriver() self.hall = Location("Main hall", "A very large hall.") self.attic = Location("Attic", "A dark attic.") self.street = Location("Street", "An endless street.") e1 = Exit("up", self.attic, "A ladder leads up.") e2 = Exit(["door", "east"], self.street, "A heavy wooden door to the east blocks the noises from the street outside.") self.hall.add_exits([e1, e2]) self.table = Item("table", "oak table", "a large dark table with a lot of cracks in its surface") self.key = Item("key", "rusty key", "an old rusty key without a label", short_description="Someone forgot a key.") self.magazine = Item("magazine", "university magazine") self.rat = NPC("rat", "n", race="rodent") self.fly = NPC("fly", "n", race="insect", short_description="A fly buzzes around your head.") self.julie = NPC("julie", "f", title="attractive Julie", description="She's quite the looker.") self.julie.aliases = {"chick"} self.player = Player("player", "m") self.pencil = Item("pencil", title="fountain pen") self.pencil.aliases = {"pen"} self.bag = Container("bag") self.notebook_in_bag = Item("notebook") self.bag.insert(self.notebook_in_bag, self.player) self.player.insert(self.pencil, self.player) self.player.insert(self.bag, self.player) self.hall.init_inventory([self.table, self.key, self.magazine, self.rat, self.julie, self.player, self.fly]) def test_names(self): loc = Location("The Attic", "A dusty attic.") self.assertEqual("The Attic", loc.name) self.assertEqual("A dusty attic.", loc.description) def test_contains(self): self.assertTrue(self.julie in self.hall) self.assertTrue(self.magazine in self.hall) self.assertFalse(self.pencil in self.hall) self.assertFalse(self.magazine in self.attic) self.assertFalse(self.julie in self.attic) def test_look(self): expected = ["[Main hall]", "A very large hall.", "A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.", "Someone forgot a key. You see a university magazine and an oak table. Player, attractive Julie, and rat are here. A fly buzzes around your head."] self.assertEqual(expected, strip_text_styles(self.hall.look())) expected = ["[Main hall]", "A very large hall.", "A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.", "Someone forgot a key. You see a university magazine and an oak table. Attractive Julie and rat are here. A fly buzzes around your head."] self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player))) expected = ["[Attic]", "A dark attic."] self.assertEqual(expected, strip_text_styles(self.attic.look())) def test_look_short(self): expected = ["[Attic]"] self.assertEqual(expected, strip_text_styles(self.attic.look(short=True))) expected = ["[Main hall]", "Exits: door, east, up", "You see: key, magazine, table", "Present: fly, julie, player, rat"] self.assertEqual(expected, strip_text_styles(self.hall.look(short=True))) expected = ["[Main hall]", "Exits: door, east, up", "You see: key, magazine, table", "Present: fly, julie, rat"] self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player, short=True))) def test_search_living(self): self.assertEqual(None, self.hall.search_living("<notexisting>")) self.assertEqual(None, self.attic.search_living("<notexisting>")) self.assertEqual(self.rat, self.hall.search_living("rat")) self.assertEqual(self.julie, self.hall.search_living("Julie")) self.assertEqual(self.julie, self.hall.search_living("attractive julie")) self.assertEqual(self.julie, self.hall.search_living("chick")) self.assertEqual(None, self.hall.search_living("bloke")) def test_search_item(self): # almost identical to locate_item so only do a few basic tests self.assertEqual(None, self.player.search_item("<notexisting>")) self.assertEqual(self.pencil, self.player.search_item("pencil")) def test_locate_item(self): item, container = self.player.locate_item("<notexisting>") self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("pencil") self.assertEqual(self.pencil, item) self.assertEqual(self.player, container) item, container = self.player.locate_item("fountain pen") self.assertEqual(self.pencil, item, "need to find the title") item, container = self.player.locate_item("pen") self.assertEqual(self.pencil, item, "need to find the alias") item, container = self.player.locate_item("pencil", include_inventory=False) self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("key") self.assertEqual(self.key, item) self.assertEqual(self.hall, container) item, container = self.player.locate_item("key", include_location=False) self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("KEY") self.assertEqual(self.key, item, "should work case-insensitive") self.assertEqual(self.hall, container, "should work case-insensitive") item, container = self.player.locate_item("notebook") self.assertEqual(self.notebook_in_bag, item) self.assertEqual(self.bag, container, "should search in bags in inventory") item, container = self.player.locate_item("notebook", include_containers_in_inventory=False) self.assertEqual(None, item) self.assertEqual(None, container, "should not search in bags in inventory") def test_tell(self): rat = MsgTraceNPC("rat", "n", "rodent") self.assertTrue(rat._init_called, "init() must be called from __init__") julie = MsgTraceNPC("julie", "f", "human") hall = Location("hall") hall.livings = [rat, julie] hall.tell("roommsg") self.assertEqual(["roommsg"], rat.messages) self.assertEqual(["roommsg"], julie.messages) rat.clearmessages() julie.clearmessages() hall.tell("roommsg", rat, [julie], "juliemsg") self.assertEqual([], rat.messages) self.assertEqual(["juliemsg"], julie.messages) def test_verbs(self): room = Location("room") room.verbs["smurf"] = "" self.assertTrue("smurf" in room.verbs) del room.verbs["smurf"] self.assertFalse("smurf" in room.verbs) def test_enter_leave(self): hall = Location("hall") rat1 = NPC("rat1", "n") rat2 = NPC("rat2", "n") julie = NPC("julie", "f") with self.assertRaises(TypeError): hall.insert(12345, julie) self.assertEqual(_Limbo, rat1.location) self.assertFalse(rat1 in hall.livings) wiretap = Wiretap(hall) hall.insert(rat1, julie) self.assertEqual(hall, rat1.location) self.assertTrue(rat1 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") hall.insert(rat2, julie) self.assertTrue(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") # now test leave wiretap.clear() hall.remove(rat1, julie) self.assertFalse(rat1 in hall.livings) self.assertIsNone(rat1.location) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") hall.remove(rat2, julie) self.assertFalse(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") # test random leave hall.remove(rat1, julie) hall.remove(12345, julie) def test_custom_verbs(self): player = Player("julie", "f") player.verbs["xywobble"] = "p1" room = Location("room") chair1 = Item("chair1") chair1.verbs["frobnitz"] = "c1" chair2 = Item("chair2") chair2.verbs["frobnitz"] = "c2" chair_in_inventory = Item("chair3") chair_in_inventory.verbs["kowabooga"] = "c3" room.init_inventory([chair1, player, chair2]) # check inventory NOT affecting player custom verbs, but DOES affect location verbs self.assertEqual({"xywobble": "p1"}, player.verbs) self.assertEqual({"frobnitz": "c2", "xywobble": "p1"}, room.verbs) player.insert(chair_in_inventory, player) self.assertEqual({"xywobble": "p1"}, player.verbs) self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3"}, room.verbs) player.remove(chair_in_inventory, player) self.assertEqual({"frobnitz": "c2", "xywobble": "p1"}, room.verbs) player.insert(chair_in_inventory, player) self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3" }, room.verbs) room2 = Location("room2") self.assertEqual({}, room2.verbs) chair1.move(room2, player) self.assertEqual({"xywobble": "p1", "kowabooga": "c3" }, room.verbs) self.assertEqual({"frobnitz": "c1"}, room2.verbs) chair2.move(room2, player) self.assertEqual({"xywobble": "p1", "kowabooga": "c3"}, room.verbs) self.assertEqual({"frobnitz": "c2"}, room2.verbs) player.move(room2) self.assertEqual({}, room.verbs) self.assertEqual({"frobnitz": "c2", "xywobble": "p1", "kowabooga": "c3"}, room2.verbs) def test_notify(self): room = Location("room") room2 = Location("room2") player = Player("julie", "f") room.notify_player_arrived(player, room2) room.notify_player_left(player, room2) room.notify_npc_arrived(player, room2) room.notify_npc_left(player, room2) parsed = ParseResult("verb") room.notify_action(parsed, player)
class TestLocations(unittest.TestCase): def setUp(self): mud_context.driver = TestDriver() mud_context.config = DemoStory()._get_config() self.hall = Location("Main hall", "A very large hall.") self.attic = Location("Attic", "A dark attic.") self.street = Location("Street", "An endless street.") e1 = Exit("up", self.attic, "A ladder leads up.") e2 = Exit( ["door", "east"], self.street, "A heavy wooden door to the east blocks the noises from the street outside." ) self.hall.add_exits([e1, e2]) self.table = Item( "table", "oak table", "a large dark table with a lot of cracks in its surface") self.key = Item("key", "rusty key", "an old rusty key without a label", short_description="Someone forgot a key.") self.magazine = Item("magazine", "university magazine") self.magazine2 = Item("magazine", "university magazine") self.rat = NPC("rat", "n", race="rodent") self.rat2 = NPC("rat", "n", race="rodent") self.fly = NPC("fly", "n", race="insect", short_description="A fly buzzes around your head.") self.julie = NPC("julie", "f", title="attractive Julie", description="She's quite the looker.") self.julie.aliases = {"chick"} self.player = Player("player", "m") self.pencil = Item("pencil", title="fountain pen") self.pencil.aliases = {"pen"} self.bag = Container("bag") self.notebook_in_bag = Item("notebook") self.bag.insert(self.notebook_in_bag, self.player) self.player.insert(self.pencil, self.player) self.player.insert(self.bag, self.player) self.hall.init_inventory([ self.table, self.key, self.magazine, self.magazine2, self.rat, self.rat2, self.julie, self.player, self.fly ]) def test_names(self): loc = Location("The Attic", "A dusty attic.") self.assertEqual("The Attic", loc.name) self.assertEqual("A dusty attic.", loc.description) def test_contains(self): self.assertTrue(self.julie in self.hall) self.assertTrue(self.magazine in self.hall) self.assertFalse(self.pencil in self.hall) self.assertFalse(self.magazine in self.attic) self.assertFalse(self.julie in self.attic) def test_look(self): expected = [ "[Main hall]", "A very large hall.", "A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.", "Someone forgot a key. You see two university magazines and an oak table. Player, attractive Julie, and two rats are here. A fly buzzes around your head." ] self.assertEqual(expected, strip_text_styles(self.hall.look())) expected = [ "[Main hall]", "A very large hall.", "A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.", "Someone forgot a key. You see two university magazines and an oak table. Attractive Julie and two rats are here. A fly buzzes around your head." ] self.assertEqual( expected, strip_text_styles(self.hall.look(exclude_living=self.player))) expected = ["[Attic]", "A dark attic."] self.assertEqual(expected, strip_text_styles(self.attic.look())) def test_look_short(self): expected = ["[Attic]"] self.assertEqual(expected, strip_text_styles(self.attic.look(short=True))) expected = [ "[Main hall]", "Exits: door, east, up", "You see: key, two magazines, and table", "Present: fly, julie, player, and two rats" ] self.assertEqual(expected, strip_text_styles(self.hall.look(short=True))) expected = [ "[Main hall]", "Exits: door, east, up", "You see: key, two magazines, and table", "Present: fly, julie, and two rats" ] self.assertEqual( expected, strip_text_styles( self.hall.look(exclude_living=self.player, short=True))) def test_search_living(self): self.assertEqual(None, self.hall.search_living("<notexisting>")) self.assertEqual(None, self.attic.search_living("<notexisting>")) self.assertEqual(self.fly, self.hall.search_living("fly")) self.assertEqual(self.julie, self.hall.search_living("Julie")) self.assertEqual(self.julie, self.hall.search_living("attractive julie")) self.assertEqual(self.julie, self.hall.search_living("chick")) self.assertEqual(None, self.hall.search_living("bloke")) def test_search_item(self): # almost identical to locate_item so only do a few basic tests self.assertEqual(None, self.player.search_item("<notexisting>")) self.assertEqual(self.pencil, self.player.search_item("pencil")) def test_locate_item(self): item, container = self.player.locate_item("<notexisting>") self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("pencil") self.assertEqual(self.pencil, item) self.assertEqual(self.player, container) item, container = self.player.locate_item("fountain pen") self.assertEqual(self.pencil, item, "need to find the title") item, container = self.player.locate_item("pen") self.assertEqual(self.pencil, item, "need to find the alias") item, container = self.player.locate_item("pencil", include_inventory=False) self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("key") self.assertEqual(self.key, item) self.assertEqual(self.hall, container) item, container = self.player.locate_item("key", include_location=False) self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("KEY") self.assertEqual(self.key, item, "should work case-insensitive") self.assertEqual(self.hall, container, "should work case-insensitive") item, container = self.player.locate_item("notebook") self.assertEqual(self.notebook_in_bag, item) self.assertEqual(self.bag, container, "should search in bags in inventory") item, container = self.player.locate_item( "notebook", include_containers_in_inventory=False) self.assertEqual(None, item) self.assertEqual(None, container, "should not search in bags in inventory") def test_tell(self): rat = MsgTraceNPC("rat", "n", "rodent") self.assertTrue(rat._init_called, "init() must be called from __init__") julie = MsgTraceNPC("julie", "f", "human") hall = Location("hall") hall.livings = [rat, julie] hall.tell("roommsg") self.assertEqual(["roommsg"], rat.messages) self.assertEqual(["roommsg"], julie.messages) rat.clearmessages() julie.clearmessages() hall.tell("roommsg", rat, [julie], "juliemsg") self.assertEqual([], rat.messages) self.assertEqual(["juliemsg"], julie.messages) def test_message_nearby_location(self): plaza = Location("plaza") road = Location("road") house = Location("house") attic = Location("attic") plaza.add_exits([ Exit("north", road, "road leads north"), Exit("door", house, "door to a house") ]) road.add_exits([Exit("south", plaza, "plaza to the south")]) house.add_exits([ Exit("door", plaza, "door to the plaza"), Exit("ladder", attic, "dusty attic") ]) attic.add_exits([Exit("ladder", house, "the house")]) wiretap_plaza = Wiretap(plaza) wiretap_road = Wiretap(road) wiretap_house = Wiretap(house) wiretap_attic = Wiretap(attic) plaza.message_nearby_locations("boing") pubsub.sync() self.assertEqual([], wiretap_plaza.msgs, "the plaza doesnt receive tells") self.assertEqual([], wiretap_attic.msgs, "the attic is too far away to receive msgs") self.assertTrue(("road", "boing") in wiretap_road.msgs) self.assertTrue(("road", "The sound is coming from the south.") in wiretap_road.msgs, "road should give sound direction") self.assertTrue(("house", "boing") in wiretap_house.msgs) self.assertTrue( ("house", "You can't hear where the sound is coming from.") in wiretap_house.msgs, "in the house you can't locate the sound direction") def test_nearby(self): plaza = Location("plaza") road = Location("road") house = Location("house") alley = Location("alley") # no exits attic = Location("attic") plaza.add_exits([ Exit("north", road, "road leads north"), Exit("door", house, "door to a house"), Exit("west", alley, "small alleywith no way back") ]) road.add_exits([Exit("south", plaza, "plaza to the south")]) house.add_exits([ Exit("door", plaza, "door to the plaza"), Exit("ladder", attic, "dusty attic") ]) attic.add_exits([Exit("ladder", house, "the house")]) adj = set(plaza.nearby()) self.assertSetEqual({road, house}, adj) adj = set(plaza.nearby(no_traps=False)) self.assertSetEqual({road, house, alley}, adj) def test_verbs(self): room = Location("room") room.verbs["smurf"] = "" self.assertTrue("smurf" in room.verbs) del room.verbs["smurf"] self.assertFalse("smurf" in room.verbs) def test_enter_leave(self): hall = Location("hall") rat1 = NPC("rat1", "n") rat2 = NPC("rat2", "n") julie = NPC("julie", "f") with self.assertRaises(TypeError): hall.insert(12345, julie) self.assertEqual(_limbo, rat1.location) self.assertFalse(rat1 in hall.livings) wiretap = Wiretap(hall) hall.insert(rat1, julie) self.assertEqual(hall, rat1.location) self.assertTrue(rat1 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") hall.insert(rat2, julie) self.assertTrue(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") # now test leave wiretap.clear() hall.remove(rat1, julie) self.assertFalse(rat1 in hall.livings) self.assertIsNone(rat1.location) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") hall.remove(rat2, julie) self.assertFalse(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") # test random leave hall.remove(rat1, julie) hall.remove(12345, julie) def test_custom_verbs(self): player = Player("julie", "f") player.verbs["xywobble"] = "p1" monster = NPC("snake", "f") monster.verbs["snakeverb"] = "s1" room = Location("room") chair1 = Item("chair1") chair1.verbs["frobnitz"] = "c1" chair2 = Item("chair2") chair2.verbs["frobnitz"] = "c2" chair_in_inventory = Item("chair3") chair_in_inventory.verbs["kowabooga"] = "c3" box_in_inventory = Item("box") box_in_inventory.verbs["boxverb"] = "c4" player.init_inventory([box_in_inventory, chair_in_inventory]) exit = Exit("e", "dummy", None, None) exit.verbs["exitverb"] = "c5" room.init_inventory([chair1, player, chair2, monster]) room.add_exits([exit]) custom_verbs = mud_context.driver.current_custom_verbs(player) all_verbs = mud_context.driver.current_verbs(player) self.assertEqual( { "xywobble", "snakeverb", "frobnitz", "kowabooga", "boxverb", "exitverb" }, set(custom_verbs)) self.assertEqual(set(), set(custom_verbs) - set(all_verbs)) def test_notify(self): room = Location("room") room2 = Location("room2") player = Player("julie", "f") room.notify_player_arrived(player, room2) room.notify_player_left(player, room2) room.notify_npc_arrived(player, room2) room.notify_npc_left(player, room2) parsed = ParseResult("verb") room.notify_action(parsed, player)
board.storage_file = "boards/board.json" board.posts = [ { "author": "irmen", "date": "2015-05-23", "subject": "hello and welcome to this world", "text": "Hello all who read this! Welcome to this world." }, { "author": "irmen", "date": "2015-05-23", "subject": "behavior", "text": "Please behave responsibly.\n\nSigned, Irmen" }, ] lane.init_inventory([board]) class CursedGem(Item): def move(self, target: ContainingType, actor: Living = None, *, silent: bool = False, is_player: bool = False, verb: str = "move", direction_names: Sequence[str] = None) -> None: if self.contained_in is actor and "wizard" not in actor.privileges: raise ActionRefused( "The gem is cursed! It sticks to your hand, you can't get rid of it!" )
livingroom.add_extradesc({ "plasma", "tv" }, "You recently bought a bigger TV, but haven't yet found the time to actually watch anything." ) # living room front door is defined in the street zone module kitchen = Location( "Kitchen", "A small but well supplied kitchen. Rather than ordering take-away, " "you prefer cooking your own meals -- unlike most of the other people you know in town. " "A window lets you look outside.") kitchen.add_extradesc({ "window", "outside" }, "Through the kitchen window you can see your small garden and behind that, the children's playground." ) kitchen.init_inventory([Money("cash", 8.0, title="small amount of cash") ]) # not enough, player needs to find more Exit.connect( livingroom, "kitchen", "Your kitchen is adjacent to this room.", "You can see your kitchen. The previous house owners had a door there but you removed it.", kitchen, ["living room", "livingroom", "back"], "The living room is back the way you entered.", None) # ----------------- Neighbours House, Bedroom, Garden ------------------------- neighbors_house = Location("Neighbor's House", "The house of your neighbors across the street.") # exit to street is defined in the street zone module bedroom = Location( "Bedroom",
class TestLocations(unittest.TestCase): def setUp(self): mud_context.driver = TestDriver() mud_context.config = DemoStory()._get_config() self.hall = Location("Main hall", "A very large hall.") self.attic = Location("Attic", "A dark attic.") self.street = Location("Street", "An endless street.") e1 = Exit("up", self.attic, "A ladder leads up.") e2 = Exit(["door", "east"], self.street, "A heavy wooden door to the east blocks the noises from the street outside.") self.hall.add_exits([e1, e2]) self.table = Item("table", "oak table", "a large dark table with a lot of cracks in its surface") self.key = Item("key", "rusty key", "an old rusty key without a label", short_description="Someone forgot a key.") self.magazine = Item("magazine", "university magazine") self.magazine2 = Item("magazine", "university magazine") self.rat = NPC("rat", "n", race="rodent") self.rat2 = NPC("rat", "n", race="rodent") self.fly = NPC("fly", "n", race="insect", short_description="A fly buzzes around your head.") self.julie = NPC("julie", "f", title="attractive Julie", description="She's quite the looker.") self.julie.aliases = {"chick"} self.player = Player("player", "m") self.pencil = Item("pencil", title="fountain pen") self.pencil.aliases = {"pen"} self.bag = Container("bag") self.notebook_in_bag = Item("notebook") self.bag.insert(self.notebook_in_bag, self.player) self.player.insert(self.pencil, self.player) self.player.insert(self.bag, self.player) self.hall.init_inventory([self.table, self.key, self.magazine, self.magazine2, self.rat, self.rat2, self.julie, self.player, self.fly]) def test_names(self): loc = Location("The Attic", "A dusty attic.") self.assertEqual("The Attic", loc.name) self.assertEqual("A dusty attic.", loc.description) def test_contains(self): self.assertTrue(self.julie in self.hall) self.assertTrue(self.magazine in self.hall) self.assertFalse(self.pencil in self.hall) self.assertFalse(self.magazine in self.attic) self.assertFalse(self.julie in self.attic) def test_look(self): expected = ["[Main hall]", "A very large hall.", "A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.", "Someone forgot a key. You see two university magazines and an oak table. Player, attractive Julie, and two rats are here. A fly buzzes around your head."] self.assertEqual(expected, strip_text_styles(self.hall.look())) expected = ["[Main hall]", "A very large hall.", "A heavy wooden door to the east blocks the noises from the street outside. A ladder leads up.", "Someone forgot a key. You see two university magazines and an oak table. Attractive Julie and two rats are here. A fly buzzes around your head."] self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player))) expected = ["[Attic]", "A dark attic."] self.assertEqual(expected, strip_text_styles(self.attic.look())) def test_look_short(self): expected = ["[Attic]"] self.assertEqual(expected, strip_text_styles(self.attic.look(short=True))) expected = ["[Main hall]", "Exits: door, east, up", "You see: key, two magazines, and table", "Present: fly, julie, player, and two rats"] self.assertEqual(expected, strip_text_styles(self.hall.look(short=True))) expected = ["[Main hall]", "Exits: door, east, up", "You see: key, two magazines, and table", "Present: fly, julie, and two rats"] self.assertEqual(expected, strip_text_styles(self.hall.look(exclude_living=self.player, short=True))) def test_search_living(self): self.assertEqual(None, self.hall.search_living("<notexisting>")) self.assertEqual(None, self.attic.search_living("<notexisting>")) self.assertEqual(self.fly, self.hall.search_living("fly")) self.assertEqual(self.julie, self.hall.search_living("Julie")) self.assertEqual(self.julie, self.hall.search_living("attractive julie")) self.assertEqual(self.julie, self.hall.search_living("chick")) self.assertEqual(None, self.hall.search_living("bloke")) def test_search_item(self): # almost identical to locate_item so only do a few basic tests self.assertEqual(None, self.player.search_item("<notexisting>")) self.assertEqual(self.pencil, self.player.search_item("pencil")) def test_locate_item(self): item, container = self.player.locate_item("<notexisting>") self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("pencil") self.assertEqual(self.pencil, item) self.assertEqual(self.player, container) item, container = self.player.locate_item("fountain pen") self.assertEqual(self.pencil, item, "need to find the title") item, container = self.player.locate_item("pen") self.assertEqual(self.pencil, item, "need to find the alias") item, container = self.player.locate_item("pencil", include_inventory=False) self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("key") self.assertEqual(self.key, item) self.assertEqual(self.hall, container) item, container = self.player.locate_item("key", include_location=False) self.assertEqual(None, item) self.assertEqual(None, container) item, container = self.player.locate_item("KEY") self.assertEqual(self.key, item, "should work case-insensitive") self.assertEqual(self.hall, container, "should work case-insensitive") item, container = self.player.locate_item("notebook") self.assertEqual(self.notebook_in_bag, item) self.assertEqual(self.bag, container, "should search in bags in inventory") item, container = self.player.locate_item("notebook", include_containers_in_inventory=False) self.assertEqual(None, item) self.assertEqual(None, container, "should not search in bags in inventory") def test_tell(self): rat = MsgTraceNPC("rat", "n", "rodent") self.assertTrue(rat._init_called, "init() must be called from __init__") julie = MsgTraceNPC("julie", "f", "human") hall = Location("hall") hall.livings = [rat, julie] hall.tell("roommsg") self.assertEqual(["roommsg"], rat.messages) self.assertEqual(["roommsg"], julie.messages) rat.clearmessages() julie.clearmessages() hall.tell("roommsg", rat, [julie], "juliemsg") self.assertEqual([], rat.messages) self.assertEqual(["juliemsg"], julie.messages) def test_message_nearby_location(self): plaza = Location("plaza") road = Location("road") house = Location("house") attic = Location("attic") plaza.add_exits([Exit("north", road, "road leads north"), Exit("door", house, "door to a house")]) road.add_exits([Exit("south", plaza, "plaza to the south")]) house.add_exits([Exit("door", plaza, "door to the plaza"), Exit("ladder", attic, "dusty attic")]) attic.add_exits([Exit("ladder", house, "the house")]) wiretap_plaza = Wiretap(plaza) wiretap_road = Wiretap(road) wiretap_house = Wiretap(house) wiretap_attic = Wiretap(attic) plaza.message_nearby_locations("boing") pubsub.sync() self.assertEqual([], wiretap_plaza.msgs, "the plaza doesnt receive tells") self.assertEqual([], wiretap_attic.msgs, "the attic is too far away to receive msgs") self.assertTrue(("road", "boing") in wiretap_road.msgs) self.assertTrue(("road", "The sound is coming from the south.") in wiretap_road.msgs, "road should give sound direction") self.assertTrue(("house", "boing") in wiretap_house.msgs) self.assertTrue(("house", "You can't hear where the sound is coming from.") in wiretap_house.msgs, "in the house you can't locate the sound direction") def test_nearby(self): plaza = Location("plaza") road = Location("road") house = Location("house") alley = Location("alley") # no exits attic = Location("attic") plaza.add_exits([Exit("north", road, "road leads north"), Exit("door", house, "door to a house"), Exit("west", alley, "small alleywith no way back")]) road.add_exits([Exit("south", plaza, "plaza to the south")]) house.add_exits([Exit("door", plaza, "door to the plaza"), Exit("ladder", attic, "dusty attic")]) attic.add_exits([Exit("ladder", house, "the house")]) adj = set(plaza.nearby()) self.assertSetEqual({road, house}, adj) adj = set(plaza.nearby(no_traps=False)) self.assertSetEqual({road, house, alley}, adj) def test_verbs(self): room = Location("room") room.verbs["smurf"] = "" self.assertTrue("smurf" in room.verbs) del room.verbs["smurf"] self.assertFalse("smurf" in room.verbs) def test_enter_leave(self): hall = Location("hall") rat1 = NPC("rat1", "n") rat2 = NPC("rat2", "n") julie = NPC("julie", "f") with self.assertRaises(TypeError): hall.insert(12345, julie) self.assertEqual(_limbo, rat1.location) self.assertFalse(rat1 in hall.livings) wiretap = Wiretap(hall) hall.insert(rat1, julie) self.assertEqual(hall, rat1.location) self.assertTrue(rat1 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") hall.insert(rat2, julie) self.assertTrue(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "insert shouldn't produce arrival messages") # now test leave wiretap.clear() hall.remove(rat1, julie) self.assertFalse(rat1 in hall.livings) self.assertIsNone(rat1.location) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") hall.remove(rat2, julie) self.assertFalse(rat2 in hall.livings) self.assertEqual([], wiretap.msgs, "remove shouldn't produce exit message") # test random leave hall.remove(rat1, julie) hall.remove(12345, julie) def test_custom_verbs(self): player = Player("julie", "f") player.verbs["xywobble"] = "p1" monster = NPC("snake", "f") monster.verbs["snakeverb"] = "s1" room = Location("room") chair1 = Item("chair1") chair1.verbs["frobnitz"] = "c1" chair2 = Item("chair2") chair2.verbs["frobnitz"] = "c2" chair_in_inventory = Item("chair3") chair_in_inventory.verbs["kowabooga"] = "c3" box_in_inventory = Item("box") box_in_inventory.verbs["boxverb"] = "c4" player.init_inventory([box_in_inventory, chair_in_inventory]) exit = Exit("e", "dummy", None, None) exit.verbs["exitverb"] = "c5" room.init_inventory([chair1, player, chair2, monster]) room.add_exits([exit]) custom_verbs = mud_context.driver.current_custom_verbs(player) all_verbs = mud_context.driver.current_verbs(player) self.assertEqual({"xywobble", "snakeverb", "frobnitz", "kowabooga", "boxverb", "exitverb"}, set(custom_verbs)) self.assertEqual(set(), set(custom_verbs) - set(all_verbs)) def test_notify(self): room = Location("room") room2 = Location("room2") player = Player("julie", "f") room.notify_player_arrived(player, room2) room.notify_player_left(player, room2) room.notify_npc_arrived(player, room2) room.notify_npc_left(player, room2) parsed = ParseResult("verb") room.notify_action(parsed, player)
def test_handle_and_notify_action(self): class SpecialPlayer(Player): def init(self): self.handled = False self.handle_verb_called = False self.notify_called = False def handle_verb(self, parsed, actor): self.handle_verb_called = True if parsed.verb in self.verbs: self.handled = True return True else: return False def notify_action(self, parsed, actor): self.notify_called = True player = SpecialPlayer("julie", "f") player.verbs["xywobble"] = "" room = Location("room") class Chair(Item): def init(self): self.handled = False self.handle_verb_called = False self.notify_called = False def handle_verb(self, parsed, actor): self.handle_verb_called = True if parsed.verb in self.verbs: self.handled = True return True else: return False def notify_action(self, parsed, actor): self.notify_called = True chair_in_inventory = Chair("littlechair") chair_in_inventory.verbs["kerwaffle"] = "" player.insert(chair_in_inventory, player) chair = Chair("chair") chair.verbs["frobnitz"] = "" room.init_inventory([player, chair]) # first check if the handle_verb passes to all objects including inventory parsed = ParseResult("kowabungaa12345") handled = room.handle_verb(parsed, player) self.assertFalse(handled) self.assertTrue(chair.handle_verb_called) self.assertTrue(player.handle_verb_called) self.assertTrue(chair_in_inventory.handle_verb_called) self.assertFalse(chair.handled) self.assertFalse(player.handled) self.assertFalse(chair_in_inventory.handled) # check item handling player.init() chair.init() chair_in_inventory.init() parsed = ParseResult("frobnitz") handled = room.handle_verb(parsed, player) self.assertTrue(handled) self.assertTrue(chair.handled) self.assertFalse(player.handled) self.assertFalse(chair_in_inventory.handled) # check living handling player.init() chair.init() chair_in_inventory.init() parsed = ParseResult("xywobble") handled = room.handle_verb(parsed, player) self.assertTrue(handled) self.assertFalse(chair.handled) self.assertTrue(player.handled) self.assertFalse(chair_in_inventory.handled) # check inventory handling player.init() chair.init() chair_in_inventory.init() parsed = ParseResult("kerwaffle") handled = room.handle_verb(parsed, player) self.assertTrue(handled) self.assertFalse(chair.handled) self.assertFalse(player.handled) self.assertTrue(chair_in_inventory.handled) # check notify_action player.init() chair.init() chair_in_inventory.init() room.notify_action(parsed, player) self.assertTrue(chair.notify_called) self.assertTrue(player.notify_called) self.assertTrue(chair_in_inventory.notify_called)
"The book's title is Cryptography for Dummies. You remember skimming it during detention yesterday but that was a long time ago.", ) book.aliases = {"library book", "cryptography book", "cryptography for dummies", "dog-eared book"} librarian = Librarian( "School Librarian", "f", race="human", title="Ms. Smith, the School Librarian", description="Ms. Smith, the school librarian.", ) librarian.add_extradesc( {"librarian", "school librarian"}, "Female, elderly, hair in a bun. Could she be more stereotypical?" ) library.init_inventory([cubby, book, librarian]) # class GameEnd(Location): # def init(self): # pass # def insert(self, obj, actor): # # Normally you would use notify_player_arrived() to trigger an action. # # but for the game ending, we require an immediate response. # # So instead we hook into the direct arrival of something in this location. # super(GameEnd, self).insert(obj, actor) # try: # obj.story_completed() # player arrived! Great Success! # except AttributeError: # pass