def set_home(self, home): """ Set object's home. Args: home: (string) Home's name. Must be the key of data info. """ home_obj = None if home: # If has home, search home object. home_obj = utils.search_obj_info_key(home) if not home_obj: logger.log_errmsg("%s can't find home %s!" % (self.get_info_key(), home)) return home_obj = home_obj[0] if self.home == home_obj: # No change. return if self == home_obj: # Can't set home to itself. logger.log_errmsg("%s can't set home to itself!" % self.get_info_key()) return self.home = home_obj
def build_details(model_name, caller=None): """ Build details of a model. Args: model_name: (string) The name of the data model. caller: (command caller) If provide, running messages will send to the caller. """ model_detail = get_model(settings.WORLD_DATA_APP, model_name) # Remove all details objects = search.search_object_attribute(key="details") for obj in objects: obj.attributes.remove("details") # Set details. count = 0 for record in model_detail.objects.all(): location_objs = utils.search_obj_info_key(record.location) # Detail's location. for location in location_objs: for name in record.name.split(";"): location.set_detail(name, record.desc) count += 1 ostring = "Set %d detail(s)." % count print(ostring) if caller: caller.msg(ostring)
def set_obj_destination(self, destination): """ Set object's destination Args: destination: (string) Destination's name. Must be the key of data info. """ destination_obj = None if destination: # If has destination, search destination object. destination_obj = utils.search_obj_info_key(destination) if not destination_obj: logger.log_errmsg("%s can't find destination %s!" % (self.get_info_key(), destination)) return destination_obj = destination_obj[0] if self.destination == destination_obj: # No change. return if self == destination_obj: # Can't set destination to itself. logger.log_errmsg("%s can't set destination to itself!" % self.get_info_key()) return self.destination = destination_obj
def set_location(self, location): """ Set object's location. Args: location: (string) Location's name. Must be the key of data info. """ location_obj = None if location: # If has location, search location object. location_obj = utils.search_obj_info_key(location) if not location_obj: logger.log_errmsg("%s can't find location %s!" % (self.get_info_key(), location)) return location_obj = location_obj[0] if self.location == location_obj: # No change. return if self == location_obj: # Can't set location to itself. logger.log_errmsg("%s can't teleport itself to itself!" % self.get_info_key()) return # try the teleport self.move_to(location_obj, quiet=True, to_none=True)
def build_details(model_name, caller=None): """ Build details of a model. Args: model_name: (string) The name of the data model. caller: (command caller) If provide, running messages will send to the caller. """ model_detail = get_model(settings.WORLD_DATA_APP, model_name) # Remove all details objects = search.search_object_attribute(key="details") for obj in objects: obj.attributes.remove("details") # Set details. count = 0 for record in model_detail.objects.all(): location_objs = utils.search_obj_info_key(record.location) # Detail's location. for location in location_objs: for name in record.name.split(";"): location.set_detail(name, record.desc) count += 1 ostring = "Set %d detail(s)." % count print ostring if caller: caller.msg(ostring)
def attack_clone_target(self, target_key, target_level=0, desc=""): """ Attack the image of a target. This creates a new character object for attack. The origin target will not be affected. Args: target_key: (string) the info key of the target. target_level: (int) target's level desc: (string) string to describe this attack Returns: None """ if target_level == 0: # find the target and get level obj = utils.search_obj_info_key(target_key) if not obj: logger.log_errmsg("Can not find the target %s." % target_key) return obj = obj[0] target_level = obj.db.level # Create a target. target = build_object(target_key) if not target: logger.log_errmsg("Can not create the target %s." % target_key) return target.set_level(target_level) self.attack_target(target, desc)
def get_revealed_map(self): """ Get the map that the character has revealed. Return value: { "rooms": {room1's key: (name, position), room2's key: (name, position), ...}, "exits": {exit1's key: (room1's key, room2's key), exit2's key: (room3's key, room4's key}, ...} } """ rooms = {} exits = {} for room_key in self.db.revealed_map: # get room's information room = utils.search_obj_info_key(room_key) if room: room = room[0] rooms[room_key] = (room.get_name(), room.position) new_exits = room.get_exits() if new_exits: exits.update(new_exits) for path in exits.values(): # add room's neighbours if not path[1] in rooms: neighbour = utils.search_obj_info_key(path[1]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_info_key()] = (neighbour.get_name(), neighbour.position) return {"rooms": rooms, "exits": exits}
def teleport_to(character, obj, *args): """ Teleport the character to specified room. args: target room's key """ if not character: return if not args: return destination = utils.search_obj_info_key(args[0]) if not destination: return destination = destination[0] character.move_to(destination)
def show_location(self): """ show character's location """ if self.location: location_key = self.location.get_info_key() msg = {"current_location": location_key} """ reveal_map: { "rooms": {room1's key: (name, position), room2's key: (name, position), ...}, "exits": {exit1's key: (room1's key, room2's key), exit2's key: (room3's key, room4's key}, ...} } """ reveal_map = None if not location_key in self.db.revealed_map: # reveal map self.db.revealed_map.add(self.location.get_info_key()) rooms = {location_key: (self.location.get_name(), self.location.position)} exits = self.location.get_exits() for path in exits.values(): # add room's neighbours if not path[1] in rooms: neighbour = utils.search_obj_info_key(path[1]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_info_key()] = (neighbour.get_name(), neighbour.position) msg["reveal_map"] = {"rooms": rooms, "exits": exits} # get appearance appearance = self.location.get_appearance(self) appearance.update(self.location.get_surroundings(self)) msg["look_around"] = appearance self.msg(msg)
def do_dialogue(self, data, character): """ Start a dialogue. """ # Get sentence. sentence = DIALOGUE_HANDLER.get_sentence(data["dialogue"], 0) if sentence: npc = None if data["npc"]: npc = utils.search_obj_info_key(data["npc"]) if npc: npc = npc[0] speaker = DIALOGUE_HANDLER.get_dialogue_speaker(character, npc, sentence["speaker"]) dlg = {"speaker": speaker, "dialogue": sentence["dialogue"], "sentence": sentence["sentence"], "content": sentence["content"]} if npc: dlg["npc"] = npc.dbref character.msg({"dialogue": [dlg]})
default_home_key = settings.DEFAULT_HOME_KEY if not default_home_key: # If does not have the default_home_key, get the first room in WORLD_ROOMS. try: model_obj = get_model(settings.WORLD_DATA_APP, settings.WORLD_ROOMS[0]) rooms = model_obj.objects.all() if rooms: default_home_key = rooms[0].key except Exception, e: ostring = "Can not find default_home_key: %s" % e print(ostring) print(traceback.print_exc()) if default_home_key: # If get default_home_key. default_home = utils.search_obj_info_key(default_home_key) if default_home: # Set default home. settings.DEFAULT_HOME = default_home[0].dbref print("settings.DEFAULT_HOME set to: %s" % settings.DEFAULT_HOME) # Set player's default home. default_player_home_key = settings.DEFAULT_PLAYER_HOME_KEY if not default_player_home_key: # If does not have the default_player_home_key, set to the DEFAULT_HOME settings.DEFAULT_PLAYER_HOME = settings.DEFAULT_HOME else: default_player_home = utils.search_obj_info_key(default_player_home_key) if default_player_home: # Set player's default home. settings.DEFAULT_PLAYER_HOME = default_player_home[0].dbref
# set default home default_home_key = settings.DEFAULT_HOME_KEY if not default_home_key: # get the first room in WORLD_ROOMS try: model_obj = get_model(settings.WORLD_DATA_APP, settings.WORLD_ROOMS[0]) rooms = model_obj.objects.all() if rooms: default_home_key = rooms[0].key except Exception, e: ostring = "Can not find default_home_key: %s" % e print ostring print traceback.print_exc() if default_home_key: default_home = utils.search_obj_info_key(default_home_key) if default_home: settings.DEFAULT_HOME = default_home[0].dbref print "settings.DEFAULT_HOME set to: %s" % settings.DEFAULT_HOME # set player's default home default_player_home_key = settings.DEFAULT_PLAYER_HOME_KEY if not default_player_home_key: # set to the DEFAULT_HOME settings.DEFAULT_PLAYER_HOME = settings.DEFAULT_HOME else: default_player_home = utils.search_obj_info_key(default_player_home_key) if default_player_home: settings.DEFAULT_PLAYER_HOME = default_player_home[0].dbref print "settings.DEFAULT_PLAYER_HOME set to: %s" % settings.DEFAULT_PLAYER_HOME