def get_revealed_map(self): """ Get the map that the character has revealed. Return value: { "rooms": {room1's key: {"name": name, "icon": icon, "area": area, "pos": position}, room2's key: {"name": name, "icon": icon, "area": area, "pos": position}, ...}, "exits": {exit1's key: {"from": room1's key, "to": room2's key}, exit2's key: {"from": room3's key, "to": room4's key}, ...} } """ rooms = {} exits = {} for room_key in self.db.revealed_map: # get room's information room = utils.search_obj_data_key(room_key) if room: room = room[0] rooms[room_key] = { "name": room.get_name(), "icon": room.icon, "area": room.location and room.location.get_data_key(), "pos": 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["to"] in rooms: neighbour = utils.search_obj_data_key(path["to"]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_data_key()] = { "name": neighbour.get_name(), "icon": neighbour.icon, "area": neighbour.location and neighbour.location.get_data_key(), "pos": neighbour.position } return {"rooms": rooms, "exits": exits}
def get_revealed_map(self): """ Get the map that the character has revealed. Return value: { "rooms": {room1's key: {"name": name, "icon": icon, "area": area, "pos": position}, room2's key: {"name": name, "icon": icon, "area": area, "pos": position}, ...}, "exits": {exit1's key: {"from": room1's key, "to": room2's key}, exit2's key: {"from": room3's key, "to": room4's key}, ...} } """ rooms = {} exits = {} for room_key in self.db.revealed_map: # get room's information room = utils.search_obj_data_key(room_key) if room: room = room[0] rooms[room_key] = {"name": room.get_name(), "icon": room.icon, "area": room.location and room.location.get_data_key(), "pos": 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["to"] in rooms: neighbour = utils.search_obj_data_key(path["to"]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_data_key()] = {"name": neighbour.get_name(), "icon": neighbour.icon, "area": neighbour.location and neighbour.location.get_data_key(), "pos": neighbour.position} return {"rooms": rooms, "exits": exits}
def attack_temp_target(self, target_key, target_level=0, desc=""): """ Attack a temporary clone 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 object. target_level: (int) target object's level desc: (string) string to describe this attack Returns: (boolean) fight begins """ if target_level == 0: # Find the target and get its level. obj = search_obj_data_key(target_key) if obj: obj = obj[0] target_level = obj.db.level # Create a target. target = build_object(target_key, target_level, reset_location=False) if not target: logger.log_errmsg("Can not create the target %s." % target_key) return False target.is_temp = True return self.attack_target(target, desc)
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_data_key(home) if not home_obj: logger.log_errmsg("%s can't find home %s!" % (self.get_data_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_data_key()) return self.home = home_obj
def func(self, event_key, character, obj): """ Start a dialogue. Args: event_key: (string) event's key. character: (object) relative character. obj: (object) the event object. """ # get action data model_obj = apps.get_model(settings.WORLD_DATA_APP, self.model_name) records = model_obj.objects.filter(event_key=event_key) # Get sentence. rand = random.random() # If matches the odds, put the character in combat. # There can be several mods with different odds. for record in records: if rand <= record.odds: # Make dialogue. npc = None if record.npc: npc = utils.search_obj_data_key(record.npc) if npc: npc = npc[0] character.show_dialogue(npc, record.dialogue, 0) return rand -= record.odds
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: (boolean) fight begins """ if target_level == 0: # Find the target and get its level. obj = utils.search_obj_data_key(target_key) if not obj: logger.log_errmsg("Can not find the target %s." % target_key) return False obj = obj[0] target_level = obj.db.level # Create a target. target = build_object(target_key, set_location=False) if not target: logger.log_errmsg("Can not create the target %s." % target_key) return False target.set_level(target_level) target.is_clone = True return self.attack_target(target, desc)
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 = apps.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_data_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 reborn(self): """ Reborn after being killed. """ # Reborn at its home. home = None if not home: default_home_key = GAME_SETTINGS.get("default_player_home_key") if default_home_key: rooms = utils.search_obj_data_key(default_home_key) if rooms: home = rooms[0] if not home: rooms = search.search_object(settings.DEFAULT_HOME) if rooms: home = rooms[0] if home: self.move_to(home, quiet=True) # Recover properties. self.recover() self.show_status() self.msg( {"msg": _("You are reborn at {C%s{n.") % self.home.get_name()})
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_data_key(location) if not location_obj: logger.log_errmsg("%s can't find location %s!" % (self.get_data_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_data_key()) return # try the teleport self.move_to(location_obj, quiet=True, to_none=True)
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_data_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 set_obj_destination(self, destination): """ Set object's destination Args: destination: (string) Destination's name. Must be the key of data info. """ if not destination: # remove destination self.destination = destination return # set new destination destination_obj = None if destination: # If has destination, search destination object. destination_obj = utils.search_obj_data_key(destination) if not destination_obj: logger.log_errmsg("%s can't find destination %s!" % (self.get_data_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_data_key()) return self.destination = destination_obj
def reset_default_locations(): """ Reset default home and start location, get new positions from settings.DEFAULT_HOME_KEY and settings.START_LOCATION_KEY. If they are empty, set them to to the first room in settings.WORLD_ROOMS. """ # Set default home. default_home_key = GAME_SETTINGS.get("default_home_key") if not default_home_key: # If does not have the default_home_key, get the first room in WORLD_ROOMS. try: rooms = CM.WORLD_ROOMS.all() if rooms: default_home_key = rooms[0].key except Exception as 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_data_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 start location. start_location_key = GAME_SETTINGS.get("start_location_key") if not start_location_key: # If does not have the start_location_key, get the first room in WORLD_ROOMS try: rooms = CM.WORLD_ROOMS.all() if rooms: start_location_key = rooms[0].key except Exception as e: ostring = "Can not find start_location_key: %s" % e print(ostring) print(traceback.print_exc()) if start_location_key: # If get start_location_key. start_location = utils.search_obj_data_key(start_location_key) if start_location: settings.START_LOCATION = start_location[0].dbref print("settings.START_LOCATION set to: %s" % settings.START_LOCATION)
def do_dialogue(self, event, character): """ Start a dialogue. """ # Get sentence. npc = None if event["npc"]: npc = utils.search_obj_data_key(event["npc"]) if npc: npc = npc[0] character.show_dialogue(npc, event["dialogue"], 0)
def func(self): """ Implement the function. """ if not self.args: return False room_key = self.args[0] destination = utils.search_obj_data_key(room_key) if not destination: return destination = destination[0] return self.caller.move_to(destination)
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_data_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_data_key(path[1]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_data_key()] = (neighbour.get_name(), neighbour.position) return {"rooms": rooms, "exits": exits}
def show_location(self): """ show character's location """ if self.location: location_key = self.location.get_data_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_data_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_data_key(path[1]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_data_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 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_data_key(args[0]) if not destination: return destination = destination[0] character.move_to(destination)
def do_dialogue(self, event, character): """ Start a dialogue. """ # Get sentence. npc = None if event["npc"]: npc = utils.search_obj_data_key(event["npc"]) if npc: npc = npc[0] sentence_list = DIALOGUE_HANDLER.get_next_sentences_list(character, npc, event["dialogue"], 0, True) character.msg({"dialogues_list": sentence_list})
def resume_last_dialogue(self): """ Restore player's dialogues when he return to game. Returns: None """ if not GAME_SETTINGS.get("auto_resume_dialogues"): # Can not auto resume dialogues. return if not self.db.current_dialogue: return current = self.db.current_dialogue if not current["sentences"]: return # Check dialogue's location if self.location.get_data_key() != current["location"]: # If player's location has changed, return. return # Check npc. npc_talking = None if current["npc"]: npc_list = utils.search_obj_data_key(current["npc"]) npc_in_location = [ npc for npc in npc_list if npc.location == self.location ] if not npc_in_location: # If the NPC has left it's location, return. return npc_talking = npc_in_location[0] sentences = [ DIALOGUE_HANDLER.get_sentence(s[0], s[1]) for s in current["sentence"] ] dialogue = DIALOGUE_HANDLER.create_output_sentences( sentences, self, npc_talking) self.msg({"dialogue": dialogue}) return
def show_location(self): """ show character's location """ if self.location: location_key = self.location.get_data_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_data_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_data_key(path[1]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_data_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 func(self): "Run the say command" caller = self.caller if not self.args: return if not "target" in self.args: caller.msg({"alert": _("You should choose a target to say.")}) return if not "message" in self.args: caller.msg({"alert": _("You should say something.")}) return target_type = self.args["type"] target = self.args["target"] message = self.args["message"] obj = None try: if target_type == ConversationType.CHANNEL.value: obj = ChannelDB.objects.filter(db_key=target) elif target_type == ConversationType.LOCAL.value: obj = search_obj_data_key(target) elif target_type == ConversationType.PRIVATE.value: obj = caller.search_dbref(target) obj = obj[0] except Exception as e: ostring = "Can not find %s %s: %s" % (target_type, target, e) logger.log_tracemsg(ostring) if not obj: self.msg({"alert": _("You can not talk to it.")}) return obj.get_message(caller, message)
def resume_last_dialogue(self): """ Restore player's dialogues when he return to game. Returns: None """ if not GAME_SETTINGS.get("auto_resume_dialogues"): # Can not auto resume dialogues. return if not self.db.current_dialogue: return current = self.db.current_dialogue if not current["sentences_begin"]: return # Check dialogue's location if self.location.get_data_key() != current["location"]: # If player's location has changed, return. return # Check npc. npc_talking = None if current["npc"]: npc_list = utils.search_obj_data_key(current["npc"]) npc_in_location = [npc for npc in npc_list if npc.location == self.location] if not npc_in_location: # If the NPC has left it's location, return. return npc_talking = npc_in_location[0] sentences_list = [DIALOGUE_HANDLER.get_sentence(s[0], s[1]) for s in current["sentences_begin"]] output = DIALOGUE_HANDLER.create_output_sentences(sentences_list, self, npc_talking) self.msg({"dialogues_list": [output]}) return
def reborn(self): """ Reborn after being killed. """ # Reborn at its home. home = None if not home: home_key = self.system.location if home_key: rooms = utils.search_obj_data_key(home_key) if rooms: home = rooms[0] if not home: rooms = search.search_object(settings.DEFAULT_HOME) if rooms: home = rooms[0] if home: self.move_to(home, quiet=True) # Recover properties. self.recover()
def show_location(self): """ show character's location """ if self.location: location_key = self.location.get_data_key() area = self.location.location and self.location.location.get_appearance(self) msg = {"current_location": {"key": location_key, "area": area}} """ reveal_map: { "rooms": {room1's key: {"name": name, "icon": icon, "area": area, "pos": position}, room2's key: {"name": name, "icon": icon, "area": area, "pos": position}, ...}, "exits": {exit1's key: {"from": room1's key, "to": room2's key}, exit2's key: {"from": room3's key, "to": 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_data_key()) rooms = {location_key: {"name": self.location.get_name(), "icon": self.location.icon, "area": self.location.location and self.location.location.get_data_key(), "pos": self.location.position}} exits = self.location.get_exits() for path in exits.values(): # add room's neighbours if not path["to"] in rooms: neighbour = utils.search_obj_data_key(path["to"]) if neighbour: neighbour = neighbour[0] rooms[neighbour.get_data_key()] = {"name": neighbour.get_name(), "icon": neighbour.icon, "area": neighbour.location and neighbour.location.get_data_key(), "pos": 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 create_character(new_player, nickname, permissions=None, character_key=None, level=1, typeclass=None, location=None, home=None): """ Helper function, creates a character based on a player's name. """ if not character_key: character_key = GAME_SETTINGS.get("default_player_character_key") if not typeclass: typeclass = settings.BASE_PLAYER_CHARACTER_TYPECLASS if not permissions: permissions = settings.PERMISSION_ACCOUNT_DEFAULT if not home: home = settings.DEFAULT_HOME try: default_home_key = GAME_SETTINGS.get("default_player_home_key") if default_home_key: rooms = utils.search_obj_data_key(default_home_key) home = rooms[0] except: pass if not location: location = home try: start_location_key = GAME_SETTINGS.get("start_location_key") if start_location_key: rooms = utils.search_obj_data_key(start_location_key) location = rooms[0] except: pass new_character = create.create_object(typeclass, key=new_player.key, location=location, home=home, permissions=permissions) # set character info new_character.set_data_key(character_key, level) # set playable character list new_player.db._playable_characters.append(new_character) # allow only the character itself and the player to puppet this character (and Immortals). new_character.locks.add( "puppet:id(%i) or pid(%i) or perm(Immortals) or pperm(Immortals)" % (new_character.id, new_player.id)) # If no description is set, set a default description if not new_character.db.desc: new_character.db.desc = "This is a Player." # Add nickname if not nickname: nickname = character_key new_character.set_nickname(nickname) # We need to set this to have @ic auto-connect to this character new_player.db._last_puppet = new_character return new_character
# Set default home. default_home_key = GAME_SETTINGS.get("default_home_key") if not default_home_key: # If does not have the default_home_key, get the first room in WORLD_ROOMS. try: rooms = CM.WORLD_ROOMS.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_data_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 start location. start_location_key = GAME_SETTINGS.get("start_location_key") if not start_location_key: # If does not have the start_location_key, get the first room in WORLD_ROOMS try: rooms = CM.WORLD_ROOMS.all() if rooms: start_location_key = rooms[0].key except Exception, e: ostring = "Can not find start_location_key: %s" % e
def func(self): "Move caller to the exit." caller = self.caller if not caller.is_alive(): caller.msg({"alert": _("You are died.")}) return if not self.args: caller.msg({"alert": _("Should appoint an exit to go.")}) return obj = caller.search_dbref(self.args, location=caller.location) if not obj: # try to goto direction target = self.args.strip().lower() if target in {"n", "s", "w", "e", "ne", "nw", "sw", "se"}: # get exits in directions exits = caller.location.get_exits() candidate_exits = [] from_room = caller.location for e in exits: exit_obj = search_obj_data_key(e) if not exit_obj: continue exit_obj = exit_obj[0] to_room = exit_obj.destination degree = self.get_degree(from_room, to_room) direction = self.get_direction(degree) if direction == target: candidate_exits.append(exit_obj) if len(candidate_exits) == 1: obj = candidate_exits[0] elif len(candidate_exits) > 1: # There is more than one exit in that direction. caller.msg({ "alert": _("There is more than one exit in that direction.") }) return if obj: # goto this exit if obj.access(self.caller, 'traverse'): # we may traverse the exit. # MudderyLockedExit handles locks in at_before_traverse(). if obj.at_before_traverse(self.caller): obj.at_traverse(caller, obj.destination) else: # exit is locked if obj.db.err_traverse: # if exit has a better error message, let's use it. caller.msg({"alert": self.obj.db.err_traverse}) else: # No shorthand error message. Call hook. obj.at_failed_traverse(caller) else: # Can not find exit. caller.msg({"alert": _("Can not go there.")}) return
def func(self): "Do checks, create account and login." session = self.caller try: playername = self.args["playername"] nickname = self.args["nickname"] password = self.args["password"] except Exception: string = 'Syntax error!' string += '\nUsage:' string += '\n {"cmd":"create_connect",' string += '\n "args":{' string += '\n "playername":<playername>,' string += '\n "nickname":<nickname>,' string += '\n "password":<password>' string += '\n }' logger.log_errmsg(string) self.caller.msg({"alert":string}) return # sanity checks if not re.findall('^[\w. @+-]+$', playername) or not (0 < len(playername) <= 30): # this echoes the restrictions made by django's auth # module (except not allowing spaces, for convenience of # logging in). string = "\n\r Playername can max be 30 characters or fewer. Letters, spaces, digits and @/./+/-/_ only." session.msg({"alert":string}) return # strip excessive spaces in playername playername = re.sub(r"\s+", " ", playername).strip() if PlayerDB.objects.filter(username__iexact=playername): # player already exists (we also ignore capitalization here) session.msg({"alert":_("Sorry, there is already a player with the name '%s'.") % playername}) return # Reserve playernames found in GUEST_LIST if settings.GUEST_LIST and playername.lower() in (guest.lower() for guest in settings.GUEST_LIST): string = "\n\r That name is reserved. Please choose another Playername." session.msg({"alert":string}) return # sanity checks if not (0 < len(nickname) <= 30): # Nickname's length string = "\n\r Nickname can max be 30 characters or fewer." session.msg({"alert":string}) return # strip excessive spaces in playername nickname = re.sub(r"\s+", " ", nickname).strip() if not re.findall('^[\w. @+-]+$', password) or not (3 < len(password)): string = "\n\r Password should be longer than 3 characers. Letters, spaces, digits and @\.\+\-\_ only." \ "\nFor best security, make it longer than 8 characters. You can also use a phrase of" \ "\nmany words if you enclose the password in quotes." session.msg({"alert":string}) return # Check IP and/or name bans bans = ServerConfig.objects.conf("server_bans") if bans and (any(tup[0]==playername.lower() for tup in bans) or any(tup[2].match(session.address) for tup in bans if tup[2])): # this is a banned IP or name! string = "{rYou have been banned and cannot continue from here." \ "\nIf you feel this ban is in error, please email an admin.{x" session.msg({"alert":string}) session.execute_cmd('{"cmd":"quit","args":""}') return # everything's ok. Create the new player account. try: permissions = settings.PERMISSION_PLAYER_DEFAULT typeclass = settings.BASE_CHARACTER_TYPECLASS new_player = _create_player(session, playername, password, permissions) if new_player: if MULTISESSION_MODE < 2: default_home = settings.DEFAULT_HOME try: default_home_key = GAME_SETTINGS.get("default_player_home_key") if default_home_key: rooms = search_obj_data_key(default_home_key) default_home = rooms[0] except: pass start_location = default_home try: start_location_key = GAME_SETTINGS.get("start_location_key") if start_location_key: rooms = search_obj_data_key(start_location_key) start_location = rooms[0] except: pass _create_character(GAME_SETTINGS.get("default_player_character_key"), 1, session, new_player, typeclass, start_location, default_home, permissions, nickname) # tell the caller everything went well. # string = "A new account '%s' was created. Welcome!" # if " " in playername: # string += "\n\nYou can now log in with the command 'connect \"%s\" <your password>'." # else: # string += "\n\nYou can now log with the command 'connect %s <your password>'." # session.msg(string % (playername, playername)) # actually do the login. This will call all other hooks: # session.at_login() # player.at_init() # always called when object is loaded from disk # player.at_pre_login() # player.at_first_login() # only once # player.at_post_login(sessid=sessid) session.msg({"login":{"name": playername, "dbref": new_player.dbref}}) session.sessionhandler.login(session, new_player) except Exception: # We are in the middle between logged in and -not, so we have # to handle tracebacks ourselves at this point. If we don't, # we won't see any errors at all. string = "%s\nThis is a bug. Please e-mail an admin if the problem persists." session.msg({"alert":string % (traceback.format_exc())}) logger.log_tracemsg()
def create_character(new_player, nickname, permissions=None, character_key=None, level=1, typeclass=None, location=None, home=None): """ Helper function, creates a character based on a player's name. """ if not character_key: character_key = GAME_SETTINGS.get("default_player_character_key") if not typeclass: typeclass = settings.BASE_PLAYER_CHARACTER_TYPECLASS if not permissions: permissions = settings.PERMISSION_ACCOUNT_DEFAULT if not home: home = settings.DEFAULT_HOME try: default_home_key = GAME_SETTINGS.get("default_player_home_key") if default_home_key: rooms = utils.search_obj_data_key(default_home_key) home = rooms[0] except: pass if not location: location = home try: start_location_key = GAME_SETTINGS.get("start_location_key") if start_location_key: rooms = utils.search_obj_data_key(start_location_key) location = rooms[0] except: pass new_character = create.create_object(typeclass, key=new_player.key, location=location, home=home, permissions=permissions) # set character info new_character.set_data_key(character_key) new_character.set_level(level) # set playable character list new_player.db._playable_characters.append(new_character) # allow only the character itself and the player to puppet this character (and Immortals). new_character.locks.add("puppet:id(%i) or pid(%i) or perm(Immortals) or pperm(Immortals)" % (new_character.id, new_player.id)) # If no description is set, set a default description if not new_character.db.desc: new_character.db.desc = "This is a Player." # Add nickname if not nickname: nickname = character_key new_character.set_nickname(nickname) # We need to set this to have @ic auto-connect to this character new_player.db._last_puppet = new_character return new_character
def func(self): "Do checks, create account and login." session = self.caller try: playername = self.args["playername"] nickname = self.args["nickname"] password = self.args["password"] except Exception: string = 'Syntax error!' string += '\nUsage:' string += '\n {"cmd":"create_connect",' string += '\n "args":{' string += '\n "playername":<playername>,' string += '\n "nickname":<nickname>,' string += '\n "password":<password>' string += '\n }' logger.log_errmsg(string) self.caller.msg({"alert": string}) return # sanity checks if not re.findall('^[\w. @+-]+$', playername) or not (0 < len(playername) <= 30): # this echoes the restrictions made by django's auth # module (except not allowing spaces, for convenience of # logging in). string = "\n\r Playername can max be 30 characters or fewer. Letters, spaces, digits and @/./+/-/_ only." session.msg({"alert": string}) return # strip excessive spaces in playername playername = re.sub(r"\s+", " ", playername).strip() if PlayerDB.objects.filter(username__iexact=playername): # player already exists (we also ignore capitalization here) session.msg({ "alert": "Sorry, there is already a player with the name '%s'." % playername }) return # Reserve playernames found in GUEST_LIST if settings.GUEST_LIST and playername.lower() in ( guest.lower() for guest in settings.GUEST_LIST): string = "\n\r That name is reserved. Please choose another Playername." session.msg({"alert": string}) return # sanity checks if not (0 < len(nickname) <= 30): # Nickname's length string = "\n\r Nickname can max be 30 characters or fewer." session.msg({"alert": string}) return # strip excessive spaces in playername nickname = re.sub(r"\s+", " ", nickname).strip() if not re.findall('^[\w. @+-]+$', password) or not (3 < len(password)): string = "\n\r Password should be longer than 3 characers. Letters, spaces, digits and @\.\+\-\_ only." \ "\nFor best security, make it longer than 8 characters. You can also use a phrase of" \ "\nmany words if you enclose the password in quotes." session.msg({"alert": string}) return # Check IP and/or name bans bans = ServerConfig.objects.conf("server_bans") if bans and (any(tup[0] == playername.lower() for tup in bans) or any(tup[2].match(session.address) for tup in bans if tup[2])): # this is a banned IP or name! string = "{rYou have been banned and cannot continue from here." \ "\nIf you feel this ban is in error, please email an admin.{x" session.msg({"alert": string}) session.execute_cmd('{"cmd":"quit","args":""}') return # everything's ok. Create the new player account. try: permissions = settings.PERMISSION_PLAYER_DEFAULT typeclass = settings.BASE_CHARACTER_TYPECLASS new_player = _create_player(session, playername, password, permissions) if new_player: if MULTISESSION_MODE < 2: default_home = ObjectDB.objects.get_id( settings.DEFAULT_PLAYER_HOME) start_location = None start_location_key = GAME_SETTINGS.get( "start_location_key") if start_location_key: start_location = search_obj_data_key( start_location_key) if start_location: start_location = start_location[0] else: start_location = default_home _create_character( GAME_SETTINGS.get("default_player_model_key"), 1, session, new_player, typeclass, start_location, default_home, permissions, nickname) # tell the caller everything went well. # string = "A new account '%s' was created. Welcome!" # if " " in playername: # string += "\n\nYou can now log in with the command 'connect \"%s\" <your password>'." # else: # string += "\n\nYou can now log with the command 'connect %s <your password>'." # session.msg(string % (playername, playername)) # actually do the login. This will call all other hooks: # session.at_login() # player.at_init() # always called when object is loaded from disk # player.at_pre_login() # player.at_first_login() # only once # player.at_post_login(sessid=sessid) session.msg( {"login": { "name": playername, "dbref": new_player.dbref }}) session.sessionhandler.login(session, new_player) except Exception: # We are in the middle between logged in and -not, so we have # to handle tracebacks ourselves at this point. If we don't, # we won't see any errors at all. string = "%s\nThis is a bug. Please e-mail an admin if the problem persists." session.msg({"alert": string % (traceback.format_exc())}) logger.log_tracemsg()
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 = apps.get_model(settings.WORLD_DATA_APP, settings.WORLD_ROOMS) 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_data_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_data_key(default_player_home_key) if default_player_home: # Set player's default home. settings.DEFAULT_PLAYER_HOME = default_player_home[0].dbref