Advertisement
Kovitikus

Can't set custom home.

Sep 12th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.33 KB | None | 0 0
  1. """Creating new character home as 'homeroom', but the character's home isn't set to the new room nor is the the character sent to it upon first login, but instead it is still set to Limbo. Yet, the new homeroom room does exist."""
  2.  
  3.  
  4. """
  5. Account
  6.  
  7. The Account represents the game "account" and each login has only one
  8. Account object. An Account is what chats on default channels but has no
  9. other in-game-world existence. Rather the Account puppets Objects (such
  10. as Characters) in order to actually participate in the game world.
  11.  
  12.  
  13. Guest
  14.  
  15. Guest accounts are simple low-level accounts that are created/deleted
  16. on the fly and allows users to test the game without the commitment
  17. of a full registration. Guest accounts are deactivated by default; to
  18. activate them, add the following line to your settings file:
  19.  
  20.    GUEST_ENABLED = True
  21.  
  22. You will also need to modify the connection screen to reflect the
  23. possibility to connect with a guest account. The setting file accepts
  24. several more options for customizing the Guest account system.
  25.  
  26. """
  27. from django.conf import settings
  28.  
  29. from evennia import DefaultAccount, DefaultGuest
  30. from evennia.utils.create import create_object
  31. from evennia.utils import class_from_module, create, logger
  32. from evennia.comms.models import ChannelDB
  33. from evennia.server.throttle import Throttle
  34. from evennia.server.signals import (SIGNAL_ACCOUNT_POST_CREATE, SIGNAL_OBJECT_POST_PUPPET,
  35.                                     SIGNAL_OBJECT_POST_UNPUPPET)
  36.  
  37. CREATION_THROTTLE = Throttle(limit=2, timeout=10 * 60)
  38. LOGIN_THROTTLE = Throttle(limit=5, timeout=5 * 60)
  39. _MULTISESSION_MODE = settings.MULTISESSION_MODE
  40.  
  41.  
  42. class Account(DefaultAccount):
  43.     def at_account_creation(self):
  44.         """
  45.        This is called once, the very first time the account is created
  46.        (i.e. first time they register with the game). It's a good
  47.        place to store attributes all accounts should have, like
  48.        configuration values etc.
  49.        """
  50.         # set an (empty) attribute holding the characters this account has
  51.         lockstring = "attrread:perm(Admins);attredit:perm(Admins);" \
  52.                      "attrcreate:perm(Admins);"
  53.         self.attributes.add("_playable_characters", [], lockstring=lockstring)
  54.         self.attributes.add("_saved_protocol_flags", {}, lockstring=lockstring)
  55.         self.tags.add("new_account")
  56.  
  57.     @classmethod
  58.     def create(cls, *args, **kwargs):
  59.         """
  60.        Creates an Account (or Account/Character pair for MULTISESSION_MODE<2)
  61.        with default (or overridden) permissions and having joined them to the
  62.        appropriate default channels.
  63.        Kwargs:
  64.            username (str): Username of Account owner
  65.            password (str): Password of Account owner
  66.            email (str, optional): Email address of Account owner
  67.            ip (str, optional): IP address of requesting connection
  68.            guest (bool, optional): Whether or not this is to be a Guest account
  69.            permissions (str, optional): Default permissions for the Account
  70.            typeclass (str, optional): Typeclass to use for new Account
  71.            character_typeclass (str, optional): Typeclass to use for new char
  72.                when applicable.
  73.        Returns:
  74.            account (Account): Account if successfully created; None if not
  75.            errors (list): List of error messages in string form
  76.        """
  77.  
  78.         account = None
  79.         errors = []
  80.  
  81.         username = kwargs.get('username')
  82.         password = kwargs.get('password')
  83.         email = kwargs.get('email', '').strip()
  84.         guest = kwargs.get('guest', False)
  85.  
  86.         permissions = kwargs.get('permissions', settings.PERMISSION_ACCOUNT_DEFAULT)
  87.         typeclass = kwargs.get('typeclass', cls)
  88.  
  89.         ip = kwargs.get('ip', '')
  90.         if ip and CREATION_THROTTLE.check(ip):
  91.             errors.append("You are creating too many accounts. Please log into an existing account.")
  92.             return None, errors
  93.  
  94.         # Normalize username
  95.         username = cls.normalize_username(username)
  96.  
  97.         # Validate username
  98.         if not guest:
  99.             valid, errs = cls.validate_username(username)
  100.             if not valid:
  101.                 # this echoes the restrictions made by django's auth
  102.                 # module (except not allowing spaces, for convenience of
  103.                 # logging in).
  104.                 errors.extend(errs)
  105.                 return None, errors
  106.  
  107.         # Validate password
  108.         # Have to create a dummy Account object to check username similarity
  109.         valid, errs = cls.validate_password(password, account=cls(username=username))
  110.         if not valid:
  111.             errors.extend(errs)
  112.             return None, errors
  113.  
  114.         # Check IP and/or name bans
  115.         banned = cls.is_banned(username=username, ip=ip)
  116.         if banned:
  117.             # this is a banned IP or name!
  118.             string = "|rYou have been banned and cannot continue from here." \
  119.                      "\nIf you feel this ban is in error, please email an admin.|x"
  120.             errors.append(string)
  121.             return None, errors
  122.  
  123.         # everything's ok. Create the new account.
  124.         try:
  125.             try:
  126.                 account = create.create_account(username, email, password, permissions=permissions, typeclass=typeclass)
  127.                 logger.log_sec(f'Account Created: {account} (IP: {ip}).')
  128.  
  129.             except Exception as e:
  130.                 errors.append("There was an error creating the Account. If this problem persists, contact an admin.")
  131.                 logger.log_trace()
  132.                 return None, errors
  133.  
  134.             # This needs to be set so the engine knows this account is
  135.             # logging in for the first time. (so it knows to call the right
  136.             # hooks during login later)
  137.             account.db.FIRST_LOGIN = True
  138.  
  139.             # Record IP address of creation, if available
  140.             if ip:
  141.                 account.db.creator_ip = ip
  142.  
  143.             # join the new account to the public channel
  144.             pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"])
  145.             if not pchannel or not pchannel.connect(account):
  146.                 string = f"New account '{account.key}' could not connect to public channel!"
  147.                 errors.append(string)
  148.                 logger.log_err(string)
  149.  
  150.             if account and settings.MULTISESSION_MODE < 2:
  151.                 # Load the appropriate Character class
  152.                 character_typeclass = kwargs.get('character_typeclass', settings.BASE_CHARACTER_TYPECLASS)
  153.                 Character = class_from_module(character_typeclass)
  154.                 name = account.key
  155.                 possessive = '\'' if name[-1] == 's' else '\'s'
  156.                 homeroom = create_object(typeclass='typeclasses.rooms.Room',
  157.                                 key=f"{name}{possessive} Home",
  158.                                 tags='ooc_room')
  159.                 # Create the character
  160.                 character, errs = Character.create(
  161.                     account.key, account, ip=ip, typeclass=character_typeclass,
  162.                     permissions=permissions, home=homeroom
  163.                 )
  164.                 errors.extend(errs)
  165.  
  166.                 if character:
  167.                     # Update playable character list
  168.                     if character not in account.characters:
  169.                         account.db._playable_characters.append(character)
  170.  
  171.                     # We need to set this to have @ic auto-connect to this character
  172.                     account.db._last_puppet = character
  173.  
  174.         except Exception:
  175.             # We are in the middle between logged in and -not, so we have
  176.             # to handle tracebacks ourselves at this point. If we don't,
  177.             # we won't see any errors at all.
  178.             errors.append("An error occurred. Please e-mail an admin if the problem persists.")
  179.             logger.log_trace()
  180.  
  181.         # Update the throttle to indicate a new account was created from this IP
  182.         if ip and not guest:
  183.             CREATION_THROTTLE.update(ip, 'Too many accounts being created.')
  184.         SIGNAL_ACCOUNT_POST_CREATE.send(sender=account, ip=ip)
  185.         return account, errors
  186.  
  187. class Guest(DefaultGuest):
  188.     """
  189.    This class is used for guest logins. Unlike Accounts, Guests and their
  190.    characters are deleted after disconnection.
  191.    """
  192.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement