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_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)
carpark.add_extradesc({"cars"}, "They look abandoned. The doors are all locked, except the doors of the yellow convertible.") carpark.add_extradesc({"convertible", "yellow"}, "It is a small two-seater and the doors are open. You can't believe your eyes, " "but the key is actually still in the ignition!") # not enough to buy the medicine, player needs to find more, or haggle: carpark.init_inventory([Money("wallet", 16.0, title="someone's wallet", short_descr="Someone's wallet lies on the pavement, they seem to have lost it. There's some money in it.")]) parking_gate, _ = Door.connect(north_street, ["gate", "parking"], "Through the iron gate you can see the car parking. A few cars are still parked there, it seems.", None, carpark, ["gate", "street"], "Rose street is back through the gate.", None, locked=True, opened=False, key_code="carpark-gate") parking_key = Key("key", "rusty key", descr="It is what appears to be an old key, with a label on it.", short_descr="On the ground is a key, it's become quite rusty.") parking_key.key_for(parking_gate) parking_key.add_extradesc({"label"}, "The label says: `parking area gate'.") class StorageRoom(Location): @call_periodically(5.0, 20.0) def shiver_from_cold(self, ctx: Context) -> None: # it's cold in the storage room, it makes people shiver if self.livings: living = random.choice(list(self.livings)) living.do_socialize("shiver") butcher = Location("Butcher shop", "The town's butcher shop. Usually there's quite a few people waiting in line, but now it is deserted.") butcher.insert(parking_key, None)
carpark.add_extradesc({"cars"}, "They look abandoned. The doors are all locked, except the doors of the yellow convertible.") carpark.add_extradesc({"convertible", "yellow"}, "It is a small two-seater and the doors are open. You can't believe your eyes, " "but the key is actually still in the ignition!") # not enough to buy the medicine, player needs to find more, or haggle: carpark.init_inventory([Money("wallet", 16.0, title="someone's wallet", short_descr="Someone's wallet lies on the pavement, " "they seem to have lost it. There's some money in it.")]) parking_gate, _ = Door.connect(north_street, ["gate", "parking"], "Through the iron gate you can see the car parking. A few cars are still parked there, it seems.", None, carpark, ["gate", "street"], "Rose street is back through the gate.", None, locked=True, opened=False, key_code="carpark-gate") parking_key = Key("key", "rusty key", descr="It is what appears to be an old key, with a label on it.", short_descr="On the ground is a key, it's become quite rusty.") parking_key.key_for(parking_gate) parking_key.add_extradesc({"label"}, "The label says: `parking area gate'.") class StorageRoom(Location): @call_periodically(5.0, 20.0) def shiver_from_cold(self, ctx: Context) -> None: # it's cold in the storage room, it makes people shiver if self.livings: living = random.choice(list(self.livings)) living.do_socialize("shiver") butcher = Location("Butcher shop", "The town's butcher shop. Usually there's quite a few people waiting in line, but now it is deserted.") butcher.insert(parking_key, None)
class GameEnd(Location): def notify_player_arrived(self, player: Player, previous_location: Location) -> None: # player has entered, and thus the story ends player.tell("\n") player.tell("\n") player.tell( "<bright>Congratulations on escaping the house!</> Someone else has to look after Garfield now though..." ) raise StoryCompleted key = Key( "key", "small rusty key", descr= "This key is small and rusty. It has a label attached, reading \"garden door\"." ) rooms = { 'livingroom': ####### Living Room { '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),
self.tell_others("{Actor} stares at {target} incomprehensibly.", target=actor) else: message = (parsed.message or parsed.unparsed).lower().split() if self.name in message or "cat" in message: self.tell_others( "{Actor} looks up at {target} and wiggles %s tail." % self.possessive, target=actor) cat = Cat( "garfield", "m", race="cat", descr= "A very obese cat, orange and black. It looks tired, but glances at you happily." ) livingroom.insert(cat, None) key = Key( "key", "small rusty key", descr= "This key is small and rusty. It has a label attached, reading \"garden door\"." ) key.key_for(door) closet.insert(key, None) closet.insert(woodenYstick.clone(), None) livingroom.insert(elastic_band.clone(), None)
if random.random() > 0.7: self.location.tell("%s purrs happily." % capital(self.title)) else: self.location.tell("%s yawns sleepily." % capital(self.title)) # it's possible to stop the periodical calling by setting: call_periodically(0)(Cat.do_purr) def notify_action(self, parsed: ParseResult, actor: Living) -> None: if actor is self or parsed.verb in self.verbs: return # avoid reacting to ourselves, or reacting to verbs we already have a handler for if parsed.verb in ("pet", "stroke", "tickle", "cuddle", "hug", "caress", "rub"): self.tell_others("{Actor} curls up in a ball and purrs contently.") elif parsed.verb in AGGRESSIVE_VERBS: if self in parsed.who_info: # only give aggressive response when directed at the cat. self.tell_others("{Actor} hisses! I wouldn't make %s angry if I were you!" % self.objective) elif parsed.verb in ("hello", "hi", "greet", "meow", "purr"): self.tell_others("{Actor} stares at {target} incomprehensibly.", target=actor) else: message = (parsed.message or parsed.unparsed).lower().split() if self.name in message or "cat" in message: self.tell_others("{Actor} looks up at {target} and wiggles %s tail." % self.possessive, target=actor) cat = Cat("garfield", "m", race="cat", descr="A very obese cat, orange and black. It looks tired, but glances at you happily.") livingroom.insert(cat, None) key = Key("key", "small rusty key", descr="This key is small and rusty. It has a label attached, reading \"garden door\".") key.key_for(door) closet.insert(key, None) closet.insert(woodenYstick.clone(), None) livingroom.insert(elastic_band.clone(), None)
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 butcher = Location("Butcher shop", "The town's butcher shop. Usually there's quite a few people waiting in line, but now it is deserted.") storage_room = Location("Storage Cell", "The butcher's meat storage cell. Brrrrr, it is cold here!") butcher.add_exits([ Exit(["north", "street"], south_street, "Rose street is back to the north."), Door(["door", "storage"], storage_room, "A door leads to the storage room.") ]) storage_room.add_exits([ Door(["door", "shop"], butcher, "The door leads back to the shop.") ]) north_street.add_exits([ Exit(["west", "playground"], playground, "The children's playground is to the west."), Exit(["south", "crossing"], crossing, "The street goes south towards the crossing."), parking_gate ]) south_street.add_exits([ Exit(["north", "crossing"], crossing, "The crossing is back towards the north."), Exit(["south", "butcher"], butcher, "The butcher shop is to the south.") ]) parking_key = Key("key", "rusty key", "It is what appears to be an old key, with a label on it.", "On the ground is a key, it's become quite rusty.") parking_key.key_for(parking_gate) parking_key.add_extradesc({"label"}, "The label says: 'parking area gate'.") butcher.insert(parking_key, None)
def make_item(vnum): """Create an instance of an item for the given vnum""" c_obj = objs[vnum] aliases = list(c_obj.aliases) name = aliases[0] aliases = set(aliases[1:]) title = c_obj.shortdesc if title.startswith("the ") or title.startswith("The "): title = title[4:] if title.startswith("a ") or title.startswith("A "): title = title[2:] if vnum in circle_bulletin_boards: # it's a bulletin board item = BulletinBoard(name, title, short_description=c_obj.longdesc) item.storage_file = circle_bulletin_boards[ vnum] # note that some instances reuse the same board item.load() # remove the item name from the extradesc c_obj.extradesc = [ ed for ed in c_obj.extradesc if item.name not in ed["keywords"] ] elif c_obj.type == "container": if c_obj.typespecific.get("closeable"): item = Boxlike(name, title, short_description=c_obj.longdesc) item.opened = True if "closed" in c_obj.typespecific: item.opened = not c_obj.typespecific["closed"] else: item = Container(name, title, short_description=c_obj.longdesc) elif c_obj.type == "weapon": item = Weapon(name, title, short_description=c_obj.longdesc) # @todo weapon attrs elif c_obj.type == "armor": item = Armour(name, title, short_description=c_obj.longdesc) # @todo armour attrs elif c_obj.type == "key": item = Key(name, title, short_description=c_obj.longdesc) item.key_for(code=vnum) # the key code is just the item's vnum elif c_obj.type == "note": # doesn't yet occur in the obj files though item = Note(name, title, short_description=c_obj.longdesc) elif c_obj.type == "food": item = Food(name, title, short_description=c_obj.longdesc) item.affect_fullness = c_obj.typespecific["filling"] item.poisoned = c_obj.typespecific.get("ispoisoned", False) elif c_obj.type == "light": item = Light(name, title, short_description=c_obj.longdesc) item.capacity = c_obj.typespecific["capacity"] elif c_obj.type == "scroll": item = Scroll(name, title, short_description=c_obj.longdesc) item.spell_level = c_obj.typespecific["level"] item.spells.add(c_obj.typespecific["spell1"]) if "spell2" in c_obj.typespecific: item.spells.add(c_obj.typespecific["spell2"]) if "spell3" in c_obj.typespecific: item.spells.add(c_obj.typespecific["spell3"]) elif c_obj.type in ("staff", "wand"): item = MagicItem(name, title, short_description=c_obj.longdesc) item.level = c_obj.typespecific["level"] item.capacity = c_obj.typespecific["capacity"] item.remaining = c_obj.typespecific["remaining"] item.spell = c_obj.typespecific["spell"] elif c_obj.type == "trash": item = Trash(name, title, short_description=c_obj.longdesc) elif c_obj.type == "drinkcontainer": item = Drink(name, title, short_description=c_obj.longdesc) item.capacity = c_obj.typespecific["capacity"] item.quantity = c_obj.typespecific["remaining"] item.contents = c_obj.typespecific["drinktype"] drinktype = Drink.drinktypes[item.contents] item.affect_drunkness = drinktype.drunkness item.affect_fullness = drinktype.fullness item.affect_thirst = drinktype.thirst item.poisoned = c_obj.typespecific.get("ispoisoned", False) elif c_obj.type == "potion": item = Potion(name, title, short_description=c_obj.longdesc) item.spell_level = c_obj.typespecific["level"] item.spells.add(c_obj.typespecific["spell1"]) if "spell2" in c_obj.typespecific: item.spells.add(c_obj.typespecific["spell2"]) if "spell3" in c_obj.typespecific: item.spells.add(c_obj.typespecific["spell3"]) elif c_obj.type == "money": item = Money(name, title, short_description=c_obj.longdesc) item.value = c_obj.typespecific["amount"] elif c_obj.type == "boat": item = Boat(name, title, short_description=c_obj.longdesc) elif c_obj.type == "worn": item = Wearable(name, title, short_description=c_obj.longdesc) # @todo worn attrs elif c_obj.type == "fountain": item = Fountain(name, title, short_description=c_obj.longdesc) item.capacity = c_obj.typespecific["capacity"] item.quantity = c_obj.typespecific["remaining"] item.contents = c_obj.typespecific["drinktype"] item.poisoned = c_obj.typespecific.get("ispoisoned", False) elif c_obj.type in ("treasure", "other"): item = Item(name, title, short_description=c_obj.longdesc) else: raise ValueError("invalid obj type: " + c_obj.type) for ed in c_obj.extradesc: item.add_extradesc(ed["keywords"], ed["text"]) item.vnum = vnum # keep the vnum item.aliases = aliases item.value = c_obj.cost item.rent = c_obj.rent item.weight = c_obj.weight # @todo: affects, effects, wear converted_items.add(vnum) return item