def test_aliases(self): loc = Location("hall", "empty hall") exit = Exit("up", "attic", "ladder to attic") door = Door("door", "street", "door to street") exit2 = Exit(["down", "hatch", "manhole"], "underground", "hatch to underground") door2 = Door(["east", "garden"], "garden", "door east to garden") self.assertEqual("up", exit.name) self.assertEqual("door", door.name) loc.add_exits([exit, door, exit2, door2]) self.assertEqual( {"up", "door", "down", "hatch", "manhole", "east", "garden"}, set(loc.exits.keys())) self.assertEqual(loc.exits["down"], loc.exits["hatch"])
def make_exit(c_exit: SimpleNamespace) -> Exit: """Create an instance of a door or exit for the given circle exit""" if c_exit.type in ("normal", "pickproof"): # @todo other door types? reverse doors? locks/keys? door = Door(c_exit.direction, make_location(c_exit.roomlink), c_exit.desc) door.aliases |= c_exit.keywords return door else: exit = Exit(c_exit.direction, make_location(c_exit.roomlink), c_exit.desc) exit.aliases |= c_exit.keywords return exit
def make_exit(c_exit): """Create an instance of a door or exit for the given circle exit""" if c_exit.type in ("normal", "pickproof"): xt = Door(c_exit.direction, make_location(c_exit.roomlink), c_exit.desc) else: xt = Exit(c_exit.direction, make_location(c_exit.roomlink), c_exit.desc) xt.aliases |= c_exit.keywords return xt
def test_with_key(self): player = Player("julie", "f") key = Key("key", "door key") key.key_for(code=12345) hall = Location("hall") door = Door("north", hall, "a locked door", locked=True, opened=False) door2 = Door("south", hall, "another locked door", locked=True, opened=False) with self.assertRaises(ActionRefused): door.unlock(player) with self.assertRaises(ActionRefused): door.unlock(player, key) door.key_code = 12345 door2.key_code = 9999 key.key_for(door) self.assertTrue(door.locked) door.unlock(player, key) self.assertFalse(door.locked) with self.assertRaises(ActionRefused): door2.unlock(key) door.locked = True with self.assertRaises(ActionRefused): door.unlock(player) key.move(player, player) door.unlock(player) self.assertFalse(door.locked) door.lock(player) self.assertTrue(door.locked) door.unlock(player) player.remove(key, player) with self.assertRaises(ActionRefused): door.lock(player) door.key_code = None with self.assertRaises(LocationIntegrityError): key.key_for(door)
def test_door_pair(self): loc1 = Location("room1", "room one") loc2 = Location("room2", "room two") key = Key("key") door_one_two = Door("two", loc2, "door to room two", locked=True, opened=False) door_two_one = door_one_two.reverse_door( "one", loc1, "door to room one", reverse_open_msg="door one open", reverse_close_msg="door one close", this_open_msg="door two open", this_close_msg="door two close") loc1.add_exits([door_one_two]) loc2.add_exits([door_two_one]) door_one_two.key_code = 555 key.key_for(door_one_two) pubsub1 = PubsubCollector() pubsub2 = PubsubCollector() loc1.get_wiretap().subscribe(pubsub1) loc2.get_wiretap().subscribe(pubsub2) self.assertTrue(door_two_one.locked) self.assertFalse(door_two_one.opened) lucy = Living("lucy", "f") door_two_one.unlock(lucy, item=key) self.assertFalse(door_one_two.locked) door_two_one.open(lucy) self.assertTrue(door_one_two.opened) pubsub.sync() self.assertEqual(["door one open"], pubsub1.messages) self.assertEqual([], pubsub2.messages) door_one_two.close(lucy) door_one_two.lock(lucy, item=key) self.assertTrue(door_two_one.locked) self.assertFalse(door_two_one.opened) pubsub1.clear() pubsub2.clear() pubsub.sync() self.assertEqual([], pubsub1.messages) self.assertEqual(["door two close"], pubsub2.messages)
def test_title_name(self): door = Door("north", "hall", "a locked door", locked=True, opened=False) self.assertEqual("north", door.name) self.assertEqual("Exit to <unbound:hall>", door.title) exit = Exit("outside", "town.square", "someplace") self.assertEqual("outside", exit.name) self.assertEqual("Exit to <unbound:town.square>", exit.title) class ModuleDummy(object): pass zones = ModuleDummy() zones.town = ModuleDummy() zones.town.square = Location("square") exit._bind_target(zones) self.assertEqual("Exit to square", exit.title) self.assertEqual("exit to square", exit.name)
def test_actions(self): player = Player("julie", "f") hall = Location("hall") attic = Location("attic") unbound_exit = Exit("random", "foo.bar", "a random exit") with self.assertRaises(Exception): self.assertFalse(unbound_exit.allow_passage( player)) # should fail because not bound exit1 = Exit("ladder", attic, "first ladder to attic") exit1.allow_passage(player) door = Door("north", hall, "open unlocked door", locked=False, opened=True) with self.assertRaises(ActionRefused) as x: door.open(player) # fail, it's already open self.assertEqual("It's already open.", str(x.exception)) door.close(player) self.assertFalse(door.opened, "must be closed") with self.assertRaises(ActionRefused) as x: door.lock(player) # default door can't be locked self.assertEqual("You don't seem to have the means to lock it.", str(x.exception)) with self.assertRaises(ActionRefused) as x: door.unlock(player) # fail, it's not locked self.assertEqual("It's not locked.", str(x.exception)) door = Door("north", hall, "open locked door", locked=False, opened=True) with self.assertRaises(ActionRefused) as x: door.open(player) # fail, it's already open self.assertEqual("It's already open.", str(x.exception)) door.close(player) door.locked = True with self.assertRaises(ActionRefused) as x: door.lock(player) # it's already locked self.assertEqual("It's already locked.", str(x.exception)) self.assertTrue(door.locked) with self.assertRaises(ActionRefused) as x: door.unlock(player) # you can't unlock it self.assertEqual("You don't seem to have the means to unlock it.", str(x.exception)) self.assertTrue(door.locked) door = Door("north", hall, "closed unlocked door", locked=False, opened=False) door.open(player) self.assertTrue(door.opened) door.close(player) self.assertFalse(door.opened) with self.assertRaises(ActionRefused) as x: door.close(player) # it's already closed self.assertEqual("It's already closed.", str(x.exception)) door = Door("north", hall, "closed locked door", locked=True, opened=False) with self.assertRaises(ActionRefused) as x: door.open(player) # can't open it, it's locked self.assertEqual("You try to open it, but it's locked.", str(x.exception)) with self.assertRaises(ActionRefused) as x: door.close(player) # it's already closed self.assertEqual("It's already closed.", str(x.exception)) door = Door("north", hall, "Some door.") self.assertEqual("Some door.", door.short_description) self.assertEqual("Some door. It is open and unlocked.", door.description) self.assertTrue(door.opened) self.assertFalse(door.locked) door = Door("north", hall, "Some door.", "This is a peculiar door leading north.") self.assertEqual("Some door.", door.short_description) self.assertEqual( "This is a peculiar door leading north. It is open and unlocked.", door.description)
def test_state(self): Door("out", "xyz", "short desc", locked=False, opened=False) Door("out", "xyz", "short desc", locked=False, opened=True) Door("out", "xyz", "short desc", locked=True, opened=False) with self.assertRaises(ValueError): Door("out", "xyz", "short desc", locked=True, opened=True)
raise StoryCompleted livingroom = Location( "Living room", "The living room in your home in the outskirts of the city.") closet = Location("Closet", "A small room.") outside = GameEnd("Outside", "It is beautiful weather outside.") # define the exits that connect the locations door = Door( ["garden", "door"], outside, "A door leads to the garden.", "There's a heavy door here that leads to the garden outside the house.", locked=True, opened=False, key_code="1" ) # oneway door, once outside you're finished, so no reason to go back in door.enter_msg = "You step through your garden door into the great unknown that is the outside." livingroom.add_exits([door]) Exit.connect(livingroom, "closet", "There's a small closet in your house.", "", closet, ["living room", "back"], "You can see the living room where you came from.", "") # define items and NPCs class Cat(Living):
# player has entered! player.story_completed() livingroom = Location( "Living room", "The living room in your home in the outskirts of the city.") closet = Location("Closet", "A small room.") outside = GameEnd("Outside", "It is beautiful weather outside.") # define the exits that connect the locations door = Door( ["garden", "door"], outside, "A door leads to the garden.", "There's a heavy door here that leads to the garden outside the house.", locked=True, opened=False) door.key_code = 1 # use an exit with an unbound target (string), the driver will link this up: closet_exit = Exit("closet", "house.closet", "There's a small closet in your house.") livingroom.add_exits([door, closet_exit]) # use another exit with a bound target (object): closet.add_exits( [Exit("living room", livingroom, "You can see the living room.")]) # define items and NPCs
north_street = Location("Rose Street", "The northern part of Rose Street.") south_street = Location("Rose Street", "The southern part of Rose Street.") crossing = Location("Crossing", "Town Crossing.") crossing.add_exits([ Exit("west", "magnolia_st.street2", "Westwards lies Magnolia Street."), Exit("east", "magnolia_st.street3", "Magnolia Street extends to the east, eventually leading towards the factory."), Exit("north", north_street, "A part of Rose Street lies to the north."), Exit("south", south_street, "Rose Street continues to the south.") ]) playground = Location("Playground", "Children's playground. You see a rusty merry-go-round, and a little swing. " "To the west, a house is visible.") playground.add_extradesc({"west", "house"}, "You can see your house from here!") playground.add_exits([ Door("fence", _limbo, "On the north end of the playground is a sturdy looking padlocked fence.", locked=True, opened=False), # this door is never meant to be opened Exit(["east", "street"], north_street, "Rose Street is back east."), zones.magnolia_st.street_gate ]) carpark = Location("Car Parking", "There are a few cars still parked over here. Their owners are nowhere to be seen. " "One yellow car grabs your attention.") carpark.add_extradesc({"cars"}, "They look abandoned, but their doors are all locked.") carpark.add_extradesc({"car", "yellow"}, "It is a small two seater!") carpark.add_exits([ Exit(["gate", "street"], north_street, "Rose street is back through the gate.") ]) parking_gate = Door(["gate", "parking"], carpark, "Through the iron gate you can see the car parking. A few cars are still parked there, it seems.", locked=True, opened=False) parking_gate.key_code = 111
pharmacy = Location("Pharmacy", "A pharmacy.") pharmacy.add_exits([ Exit(["east", "outside", "street"], street1, "Magnolia street is outside towards the east.") ]) factory = Location("ArtiGrow factory", "This area is the ArtiGrow fertilizer factory.") factory.add_exits([ Exit(["west", "street"], street3, "You can leave the factory to the west, back to Magnolia Street.") ]) street1.add_exits([ houses.house_door, Exit(["pharmacy", "west"], pharmacy, "The west end of the street leads to the pharmacy."), Exit(["town", "east"], street2, "The street extends eastwards, towards the rest of the town.") ]) playground_gate = Door(["north", "gate", "playground"], "rose_st.playground", "To the north there is a small gate that connects to the children's playground.", opened=False) street_gate = playground_gate.reverse_door(["gate", "south"], street2, "The gate that leads back to Magnolia Street is south.") street2.add_exits([ Exit(["west"], street1, "The street extends to the west, where your house is."), Exit(["east", "crossing"], "rose_st.crossing", "There's a crossing to the east."), Exit(["south", "house", "neighbors"], houses.neighbors_house, "You can see the house from the neighbors across the street, to the south."), playground_gate ]) street3.add_exits([ Exit(["factory", "east"], factory, "Eastwards you'll enter the ArtiGrow factory area."), Exit(["west", "crossing"], "rose_st.crossing", "There's a crossing to the west.") ])
) 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." ) # Exits front_door = Door( ["door", "outside", "street"], "magnolia_st.street1", "Your front door leads outside, to the street.", "There's a heavy front door here that leads to the streets outside.", opened=False) house_door = front_door.reverse_door( ["house", "north", "inside"], livingroom, "You can go back inside your house.", "It's your house, on the north side of the street.", reverse_open_msg="Someone outside opens the door.", reverse_close_msg="Someone outside closes the door.", this_open_msg="Someone in the house opens the door.", this_close_msg="Someone in the house closes the door.") livingroom.add_exits([ Exit( "kitchen", 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."
class AlleyOfDoors(Location): def notify_player_arrived(self, player: Player, previous_location: Location) -> None: 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_descr=descr, locked=False, opened=True) door2 = Door(["second door", "door two"], alley, "There's a door marked `door two'.", long_descr=descr, locked=True, opened=False) door3 = Door(["third door", "door three"], alley, "There's a door marked `door three'.", long_descr=descr, locked=False, opened=False) door4 = Door(["fourth door", "door four"],
{ 'location': { 'name': "Living room", 'descr': "The living room in your home in the new testing story." }, 'exits': [ Exit(directions="closet", target_location="house.closet", short_descr="There's a small closet in your house.", long_descr=None), ], 'doors': [ Door(directions=["garden", "door"], target_location="house.outside", short_descr="A door leads to the garden.", long_descr="There's a heavy door in this test.", locked=True, opened=False, key_code="1"), Door( directions=["bathroom"], target_location="house.bathroom", short_descr="A door leads to the bathroom.", long_descr=None, locked=False, opened=False, key_code=None, ) ], 'objects': [] },
crossing = Location("Crossing", "Town Crossing.") crossing.add_exits([ Exit("west", "magnolia_st.street2", "Westwards lies Magnolia Street."), Exit("east", "magnolia_st.street3", "Magnolia Street extends to the east, eventually leading towards the factory."), ]) Exit.connect(crossing, "north", "A part of Rose Street lies to the north.", None, north_street, ["south", "crossing"], "The street goes south towards the crossing.", None) Exit.connect(crossing, "south", "Rose Street continues to the south.", None, south_street, ["north", "crossing"], "The crossing is back towards the north.", None) playground = Location("Playground", "Children's playground. You see a rusty merry-go-round, and a little swing. " "To the west, a house is visible.") playground.add_extradesc({"west", "house"}, "You can see your house from here!") playground.add_exits([ Door("fence", _limbo, "On the north end of the playground is a sturdy looking padlocked fence.", locked=True, opened=False, key_code="999999999"), # this door is never meant to be opened ]) Exit.connect(playground, ["east", "street"], "Rose Street is back east.", None, north_street, ["west", "playground"], "The children's playground is to the west.", None) class CarPark(Location): def init(self): self.verbs = {"drive": "Drive a car", "open": "Open something", "enter": "Enter something", "sit": "Sit somewhere", "use": "Use something", "start": "Start something" }
) raise StoryCompleted # Create location ##### Living Room ##### livingroom_descr = """ The living room in your home in the outskirts of the city.""" livingroom = Location(name="Living room", descr=livingroom_descr) # Build Exits garden_door = Door( directions=["garden", "door"], target_location="house.outside", short_descr="A door leads to the garden.", long_descr= "There's a heavy door here that leads to the garden outside the house.", locked=True, opened=False, key_code="1") lr_closet = Exit(directions="closet", target_location="house.closet", short_descr="There is a small closet in your house.", long_descr=None) livingroom.add_exits([garden_door, lr_closet]) # Insert items & npcs livingroom.insert(cat, None) livingroom.insert(elastic_band.clone(), None)