def test_many_exits_and_commands(self): with locks.authority_of(locks.SYSTEM): self.exit_s1 = db.Exit("s1", self.lobby, self.lobby) self.exit_s2 = db.Exit("s2", self.lobby, self.lobby) db.store(self.exit_s1) db.store(self.exit_s2) self.assert_response("s", startswith="Which command do you mean")
def test_many_exits_one_nospace(self): with locks.authority_of(locks.SYSTEM): self.exit_zzza = db.Exit("zzza", self.lobby, self.lobby) self.exit_zzzb = db.Exit("zzzb", self.lobby, self.lobby) db.store(self.exit_zzza) db.store(self.exit_zzzb) self.assert_response("zzz foo", "Spaaaaaaaaaaaaaace. (foo).")
def execute(self, player, args): type_name = args["type"] object_name = args["name"] try: mod_name, class_name = type_name.rsplit(".", 1) except ValueError: raise utils.UserError("Object type should be of the form " "module.Class") try: module = importlib.import_module(mod_name) object_class = getattr(module, class_name) except ImportError: raise utils.UserError("I don't know of this module: " "{}".format(mod_name)) except AttributeError: raise utils.UserError("{} doesn't have this class: " "{}".format(mod_name, class_name)) if db.Room in object_class.mro(): raise utils.UserError( 'Use "dig", not "create", to make new rooms.') if db.Exit in object_class.mro(): raise utils.UserError('Use "open", not "create", to make new ' 'exits.') new_item = object_class(object_name, owner=player, location=player) db.store(new_item) player.send("Created item #{}, {}.".format(new_item.uid, new_item.name))
def test_exit_nospace(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") self.zzzfoo = db.Exit("zzzfoo", self.lobby, self.foyer) db.store(self.foyer) db.store(self.zzzfoo) self.assert_response("zzzfoo", "Spaaaaaaaaaaaaaace. (foo).")
def setUp(self): super(AttrLockTestCase, self).setUp() self.obj_owner = db.Player("Objowner", "password") db.store(self.obj_owner) self.attr_owner = db.Player("Attrowner", "password") db.store(self.attr_owner) self.setter = db.Player("Setter", "password") db.store(self.setter) self.getter = db.Player("Getter", "password") db.store(self.getter) self.players = [self.obj_owner, self.attr_owner, self.getter, self.setter] with locks.authority_of(self.obj_owner): self.obj = db.Object("Object") db.store(self.obj) with locks.authority_of(self.attr_owner): self.obj.attr = "value" self.obj.lock_attr("attr", get_lock=locks.Is(self.getter), set_lock=locks.Is(self.setter))
def test_many_exits_one_command(self): with locks.authority_of(locks.SYSTEM): self.exit_h1 = db.Exit("h1", self.lobby, self.lobby) self.exit_h2 = db.Exit("h2", self.lobby, self.lobby) db.store(self.exit_h1) db.store(self.exit_h2) self.assert_response("h", startswith="Available commands:")
def execute(self, player, args): type_name = args["type"] object_name = args["name"] try: mod_name, class_name = type_name.rsplit(".", 1) except ValueError: raise utils.UserError("Object type should be of the form " "module.Class") try: module = importlib.import_module(mod_name) object_class = getattr(module, class_name) except ImportError: raise utils.UserError("I don't know of this module: " "{}".format(mod_name)) except AttributeError: raise utils.UserError("{} doesn't have this class: " "{}".format(mod_name, class_name)) if db.Room in object_class.mro(): raise utils.UserError('Use "dig", not "create", to make new rooms.') if db.Exit in object_class.mro(): raise utils.UserError('Use "open", not "create", to make new ' 'exits.') new_item = object_class(object_name, owner=player, location=player) db.store(new_item) player.send("Created item #{}, {}.".format(new_item.uid, new_item.name))
def test_position_string(self): with locks.authority_of(locks.SYSTEM): model = db.Object("model") db.store(model) model.position = "vogueing for the camera" self.assertEqual(model.position_string(), "model (vogueing for the camera)")
def test_destroy_emit_elsewhere(self): with locks.authority_of(self.player): new_room = db.Room("a room") db.store(new_room) self.player.send_line("destroy #{}".format(new_room.uid)) self.assertNotEqual(self.neighbor.last_response(), "Player destroys a room.")
def setUp(self): self.patch(db, "_objects", {}) self.patch(db, "_nextUid", 0) with locks.authority_of(locks.SYSTEM): self.lobby = db.Room("lobby") db.store(self.lobby) self.player = self.new_player("Player") self.neighbor = self.new_player("PlayersNeighbor")
def test_create(self): expected_uid = db._nextUid with locks.authority_of(locks.SYSTEM): obj = db.Object("foo") self.assertEqual(obj.uid, None) db.store(obj) self.assertEqual(obj.uid, expected_uid) self.assertEqual(db._nextUid, expected_uid + 1)
def test_nearbyobject_me(self): pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "me", self.player) with locks.authority_of(locks.SYSTEM): me = db.Object("me", self.lobby) db.store(me) pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "me", me)
def test_nearbyobject_here(self): pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "here", self.lobby) with locks.authority_of(locks.SYSTEM): here = db.Object("here", self.lobby) db.store(here) pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "here", here)
def test_exit_permissions(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") self.exit = db.Exit("exit", self.lobby, self.foyer) self.exit.locks.go = locks.Fail() db.store(self.foyer) db.store(self.exit) self.assert_response("exit", "You can't go through exit.")
def test_ambiguous_exit(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") self.exit_ju = db.Exit("jump", self.lobby, self.foyer) self.exit_jo = db.Exit("joust", self.lobby, self.foyer) for obj in self.foyer, self.exit_ju, self.exit_jo: db.store(obj) self.assert_response("j", "Which exit do you mean? (joust, jump)")
def test_exit_invocation(self): with locks.authority_of(locks.SYSTEM): self.foyer = db.Room("foyer") db.store(self.foyer) self.exit = db.Exit("exit", self.lobby, self.foyer) db.store(self.exit) self.assertEqual(self.player.location, self.lobby) self.player.send_line("exit") self.assertEqual(self.player.location, self.foyer)
def test_has(self): with locks.authority_of(self.player): key = db.Object("a key") key.location = self.player db.store(key) lock = locks.Has(key) self.assertTrue(lock(self.player)) self.assertFalse(lock(self.player2))
def test_update(self): with locks.authority_of(locks.SYSTEM): obj = db.Object("foo") db.store(obj) obj.name = "bar" db.store(obj) obj = db.get(obj.uid) self.assertEqual(obj.name, "bar") self.assertEqual(obj.type, "thing")
def new_player(self, name): """ Create a new player with the given name, store it in the database, and return it. """ newbie = PlayerMock(name, "password") newbie.location = self.lobby newbie.enter_mode(handler.NormalMode()) db.store(newbie) return newbie
def setUp(self): super(ParserTestCase, self).setUp() self.setup_objects() tricky_names = ["me you", "cup of mead", "here there", "heretical thoughts"] # These are for confounding the me/here keywords. for name in tricky_names: with locks.authority_of(locks.SYSTEM): self.objects[name] = db.Object(name, self.lobby) db.store(self.objects[name])
def test_open(self): with locks.authority_of(self.player): destination = db.Room("destination") db.store(destination) self.assert_response("open north to #{}".format(destination.uid), "Opened north to destination.") exit = db.get(destination.uid + 1) self.assertTrue(isinstance(exit, db.Exit)) self.assertIdentical(exit.location, self.lobby) self.assertIdentical(exit.destination, destination)
def execute(self, player, args): name_words = args["name"] destination = args["destination"] to = name_words.pop() name = " ".join(name_words) if to != "to": # this is me compensating for pyparsing's failings raise pyp.ParseException(name, len(name)-len(to), None, None) exit = db.Exit(name, player.location, destination) db.store(exit) player.send("Opened {} to {}.".format(exit, destination))
def execute(self, player, args): name_words = args["name"] destination = args["destination"] to = name_words.pop() name = " ".join(name_words) if to != "to": # this is me compensating for pyparsing's failings raise pyp.ParseException(name, len(name) - len(to), None, None) exit = db.Exit(name, player.location, destination) db.store(exit) player.send("Opened {} to {}.".format(exit, destination))
def handle(self, player, line): # Just as in LoginMode, the player arg will be None since no one is # logged in. if line == 'cancel': player.exit_mode() # Drop back to LoginMode player.mode.greet() return if self.stage == 'name': if line.find(" ") > -1: self.protocol.sendLine("Please type only the username; it may " "not contain any spaces. Try again:") return if db.player_name_taken(line): self.protocol.sendLine("That name is already taken. If it's " "yours, type 'cancel' to log in. " "Otherwise, try another name:") return self.name = line self.protocol.sendLine( "Welcome, {}! Please enter a password.".format(self.name)) self.stage = 'password1' return elif self.stage == 'password1': self.password = line self.protocol.sendLine("Please enter it again.") self.stage = 'password2' return elif self.stage == 'password2': if self.password == line: player = db.Player(self.name, self.password) self.protocol.player = player db.store(player) factory.allProtocols[player.name] = self.protocol with locks.authority_of(player): player.enter_mode(handler.NormalMode()) self.protocol.sendLine("Hello, {}!".format(player.name)) self.protocol.sendLine("") from muss.commands.world import Look Look().execute(player, {"obj": player.location}) player.emit("{} has connected for the first time.".format( player.name), exceptions=[player]) return else: self.protocol.sendLine("Passwords don't match; try again. " "Please enter a password.") self.stage = 'password1' return
def execute(self, player, args): # ... but the tradeoff is we have to do the validity checking down here. obj_grammar = parser.ReachableOrUid(player) attr_grammar = pyp.Word(pyp.alphas + "_", pyp.alphanums + "_") try: obj = obj_grammar.parseString(args["obj"], parseAll=True)[0] except pyp.ParseException: name = args["obj"].strip() raise utils.UserError( "I don't know what object you mean by '{}'".format(name)) try: attr = attr_grammar.parseString(args["attr"], parseAll=True)[0] except pyp.ParseException: name = args["attr"].strip() raise utils.UserError( "'{}' is not a valid attribute name.".format(name)) value_string = args["value"].strip() if value_string.isdigit(): value = int(args["value"]) elif value_string == "True": value = True elif value_string == "False": value = False elif value_string == "None": value = None else: try: pattern = parser.PythonQuoted value = pattern.parseString(args["value"], parseAll=True)[0] value = value.decode("string-escape") except pyp.ParseException: try: pattern = parser.ReachableOrUid(player) value = pattern.parseString(args["value"], parseAll=True)[0] except pyp.ParseException: # okay, I give up raise utils.UserError( "'{}' is not a valid attribute value.".format( args["value"].strip())) name = obj.name # In case it changes, so we can report the old one try: setattr(obj, attr, value) except ValueError as e: raise utils.UserError(str(e)) db.store(obj) player.send("Set {}'s {} attribute to {}".format(name, attr, value))
def handle(self, player, line): # Just as in LoginMode, the player arg will be None since no one is # logged in. if line == 'cancel': player.exit_mode() # Drop back to LoginMode player.mode.greet() return if self.stage == 'name': if line.find(" ") > -1: self.protocol.sendLine("Please type only the username; it may " "not contain any spaces. Try again:") return if db.player_name_taken(line): self.protocol.sendLine("That name is already taken. If it's " "yours, type 'cancel' to log in. " "Otherwise, try another name:") return self.name = line self.protocol.sendLine("Welcome, {}! Please enter a password." .format(self.name)) self.stage = 'password1' return elif self.stage == 'password1': self.password = line self.protocol.sendLine("Please enter it again.") self.stage = 'password2' return elif self.stage == 'password2': if self.password == line: player = db.Player(self.name, self.password) self.protocol.player = player db.store(player) factory.allProtocols[player.name] = self.protocol with locks.authority_of(player): player.enter_mode(handler.NormalMode()) self.protocol.sendLine("Hello, {}!".format(player.name)) self.protocol.sendLine("") from muss.commands.world import Look Look().execute(player, {"obj": player.location}) player.emit("{} has connected for the first time." .format(player.name), exceptions=[player]) return else: self.protocol.sendLine("Passwords don't match; try again. " "Please enter a password.") self.stage = 'password1' return
def execute(self, player, args): # ... but the tradeoff is we have to do the validity checking down here. obj_grammar = parser.ReachableOrUid(player) attr_grammar = pyp.Word(pyp.alphas + "_", pyp.alphanums + "_") try: obj = obj_grammar.parseString(args["obj"], parseAll=True)[0] except pyp.ParseException: name = args["obj"].strip() raise utils.UserError("I don't know what object you mean by '{}'" .format(name)) try: attr = attr_grammar.parseString(args["attr"], parseAll=True)[0] except pyp.ParseException: name = args["attr"].strip() raise utils.UserError("'{}' is not a valid attribute name." .format(name)) value_string = args["value"].strip() if value_string.isdigit(): value = int(args["value"]) elif value_string == "True": value = True elif value_string == "False": value = False elif value_string == "None": value = None else: try: pattern = parser.PythonQuoted value = pattern.parseString(args["value"], parseAll=True)[0] value = value.decode("string-escape") except pyp.ParseException: try: pattern = parser.ReachableOrUid(player) value = pattern.parseString(args["value"], parseAll=True)[0] except pyp.ParseException: # okay, I give up raise utils.UserError("'{}' is not a valid attribute value." .format(args["value"].strip())) name = obj.name # In case it changes, so we can report the old one try: setattr(obj, attr, value) except ValueError as e: raise utils.UserError(str(e)) db.store(obj) player.send("Set {}'s {} attribute to {}".format(name, attr, value))
def setup_objects(self): """ Generates the following clutter: OBJECTS IN THE LOBBY: abacus, ant, balloon, Bucket, cat, frog, Fodor's Guide, horse IN PLAYER'S INVENTORY: Anabot doll, ape plushie, apple, cat, cherry, cheese, horse figurine, monster mask, monocle, moose, millipede IN NEIGHBOR'S INVENTORY: apple IN FROG'S INVENTORY: hat All of these are stored in self.objects[name], EXCEPT: * the cat in the room is objects["room_cat"] * the cat in player's inventory is objects["inv_cat"] * the apple in neighbor's inventory is ["neighbor_apple"] All are plain db.Objects, EXCEPT: * monocle and monster mask are equipment.Equipment * Bucket is a db.Container The player owns all the objects in its inventory. SYSTEM owns the rest. """ self.objects = {} with locks.authority_of(self.player): for inv_object in [ "apple", "horse figurine", "ape plushie", "Anabot doll", "cherry", "cheese", "moose", "millipede" ]: self.objects[inv_object] = db.Object(inv_object, self.player) self.objects["monocle"] = equipment.Equipment( "monocle", self.player) self.objects["monster mask"] = equipment.Equipment( "monster mask", self.player) with locks.authority_of(locks.SYSTEM): for room_object in [ "frog", "ant", "horse", "Fodor's Guide", "abacus", "balloon" ]: self.objects[room_object] = db.Object(room_object, location=self.lobby) self.objects["Bucket"] = db.Container("Bucket", self.lobby) self.objects["room_cat"] = db.Object("cat", self.lobby) self.objects["inv_cat"] = db.Object("cat", self.player) self.objects["neighbor_apple"] = db.Object("apple", self.neighbor) self.objects["hat"] = db.Object("hat", self.objects["frog"]) for key in self.objects: db.store(self.objects[key])
def test_retrieve_one(self): with locks.authority_of(locks.SYSTEM): obj_created = db.Object("foo") db.store(obj_created) obj_found = db.get(obj_created.uid) self.assertEqual(obj_created, obj_found) self.assertTrue(obj_created is obj_found) self.assertEqual(obj_found.name, "foo") self.assertEqual(obj_found.type, "thing") found = db.find_all(lambda x: x.uid == obj_created.uid) self.assertEqual(len(found), 1) self.assertEqual(obj_created, found.pop()) self.assertEqual(obj_created, db.get(obj_created.uid))
def setup_objects(self): """ Generates the following clutter: OBJECTS IN THE LOBBY: abacus, ant, balloon, Bucket, cat, frog, Fodor's Guide, horse IN PLAYER'S INVENTORY: Anabot doll, ape plushie, apple, cat, cherry, cheese, horse figurine, monster mask, monocle, moose, millipede IN NEIGHBOR'S INVENTORY: apple IN FROG'S INVENTORY: hat All of these are stored in self.objects[name], EXCEPT: * the cat in the room is objects["room_cat"] * the cat in player's inventory is objects["inv_cat"] * the apple in neighbor's inventory is ["neighbor_apple"] All are plain db.Objects, EXCEPT: * monocle and monster mask are equipment.Equipment * Bucket is a db.Container The player owns all the objects in its inventory. SYSTEM owns the rest. """ self.objects = {} with locks.authority_of(self.player): for inv_object in ["apple", "horse figurine", "ape plushie", "Anabot doll", "cherry", "cheese", "moose", "millipede"]: self.objects[inv_object] = db.Object(inv_object, self.player) self.objects["monocle"] = equipment.Equipment("monocle", self.player) self.objects["monster mask"] = equipment.Equipment("monster mask", self.player) with locks.authority_of(locks.SYSTEM): for room_object in ["frog", "ant", "horse", "Fodor's Guide", "abacus", "balloon"]: self.objects[room_object] = db.Object(room_object, location=self.lobby) self.objects["Bucket"] = db.Container("Bucket", self.lobby) self.objects["room_cat"] = db.Object("cat", self.lobby) self.objects["inv_cat"] = db.Object("cat", self.player) self.objects["neighbor_apple"] = db.Object("apple", self.neighbor) self.objects["hat"] = db.Object("hat", self.objects["frog"]) for key in self.objects: db.store(self.objects[key])
def test_move_get_drop_container(self): with locks.authority_of(locks.SYSTEM): magician = db.Object("magician") rabbit = db.Object("stubborn rabbit") db.store(magician) db.store(rabbit) with locks.authority_of(rabbit): rabbit.locks.take = locks.Fail() rabbit.locks.drop = locks.Fail() rabbit.locks.insert = locks.Is(magician) with locks.authority_of(magician): carrot = db.Object("carrot", magician) celery = db.Object("celery", magician) hat = db.Container("hat", magician) db.store(carrot) db.store(celery) db.store(hat) try: rabbit.location = magician except locks.LockFailedError as e: self.assertEqual(str(e), "You cannot take stubborn rabbit.") else: self.fail() carrot.location = rabbit with locks.authority_of(rabbit): rabbit.locks.take = locks.Is(magician) rabbit.location = magician try: rabbit.location = hat except locks.LockFailedError as e: self.assertEqual(str(e), "You cannot drop stubborn rabbit.") else: self.fail() celery.location = rabbit with locks.authority_of(rabbit): rabbit.locks.drop = locks.Is(magician) rabbit.location = hat rabbit.location = magician
def test_contents_string(self): with locks.authority_of(locks.SYSTEM): room = db.Room("room") self.patch(db, '_objects', {0: room}) player = db.Player("player", "password") hat = equipment.Equipment("hat") db.store(player) db.store(hat) # not room because we already patched it in for location in [room, player]: with locks.authority_of(locks.SYSTEM): hat.equipped = False hat.location = location self.assertIn("hat", location.contents_string()) self.assertNotIn("hat", location.equipment_string()) with locks.authority_of(locks.SYSTEM): hat.equip() self.assertTrue(hat.equipped) self.assertNotIn("hat", location.contents_string()) self.assertIn("hat", location.equipment_string())
def test_move_insert_remove(self): with locks.authority_of(locks.SYSTEM): hat = db.Object("hat") magician = db.Object("magician") db.store(hat) db.store(magician) try: with locks.authority_of(magician): rabbit = db.Object("rabbit", hat) except locks.LockFailedError as e: self.assertEqual(str(e), "You can't put that in hat.") else: self.fail() with locks.authority_of(hat): hat.locks.insert = locks.Is(magician) with locks.authority_of(magician): rabbit = db.Object("rabbit", hat) db.store(rabbit) try: rabbit.location = magician except locks.LockFailedError as e: self.assertEqual(str(e), "You can't remove that from hat.") else: self.fail() with locks.authority_of(hat): hat.locks.remove = locks.Is(magician) rabbit.location = magician self.assertEqual(rabbit.location, magician)
def finish(room_name, to_exit_name, from_exit_name): room = db.Room(room_name) db.store(room) if to_exit_name != "": exit_to = db.Exit(to_exit_name, player.location, room) db.store(exit_to) if from_exit_name != "": exit_from = db.Exit(from_exit_name, room, player.location) db.store(exit_from) player.send("Dug room #{}, {}.".format(room.uid, room.name))
def setUp(self): super(LockTestCase, self).setUp() with locks.authority_of(locks.SYSTEM): self.player = db.Player("Player", "password") db.store(self.player) self.player2 = db.Player("PlayerTwo", "password") db.store(self.player2) self.obj = db.Object("object", owner=self.player) with locks.authority_of(self.player2): self.obj.foreign_attr = 0 db.store(self.obj)