Exemplo n.º 1
0
    def __init__(self, connection, runtime, account_id, universe_id):
        """
        MenuController is used for the main menu where the user can choose which universe to join or
        create a new universe.

        :param connection: Connection of the user
        :param account: Account of user user
        :param universe: Universe where the play happens
        :type connection: main.Connection
        :type account: models.account.Account
        :type universe: models.universe.Universe
        :return:
        """
        BaseController.__init__(self, connection, runtime)
        self.account_id = account_id
        self.universe_id = universe_id
        self.state = State.normal

        #self.thing = get(account for account in self.universe.things if account == self.account)
        #if not self.thing:
        #    self.thing = Thing(account=self.account, universe=self.universe)
        #    # TODO place!

        runtime.add_controller(universe_id, account_id, self)

        self.being_id = None
        self.start()
Exemplo n.º 2
0
 def __init__(self, connection, runtime):
     BaseController.__init__(self, connection, runtime)
     self.account_id = None
     self.account_name = None
     # self.account = None
     self.state = State.login_username
     self.static_salt = None
     self.dynamic_salt = None
     self.password = None  # For creating a new account, store the password temporarily
     self.greeting()
Exemplo n.º 3
0
    def test_basecontroller(self):
        # Create a test offtopic message
        testverse = Universe(name="Testverse Basecontrol")
        class MockConnection(object):
            def send(self, obj):
                pass

        base = BaseController(MockConnection())
        base.send_offtopic("Text message")

        offtopic = Offtopic(text="Test message",
                            universe=testverse)
        base.send_offtopic(offtopic)
Exemplo n.º 4
0
    def __init__(self, connection, runtime, account_id):
        """
        MenuController is used for the main menu where the user can choose which universe to join or
        create a new universe.

        :param connection: Connection of the user
        :param account: Account of user user
        :type connection: main.Connection
        :type account: models.account.Account
        :return:
        """
        BaseController.__init__(self, connection, runtime)
        self.account_id = account_id
        self.state = State.main_menu
        self.main_menu_view()
Exemplo n.º 5
0
    def handle(self, message={}):
        try:
            return BaseController.handle(self, message)
        except KeyError:
            pass

        key = message.get("k")
        value = message.get("v")
        if key != "msg" or len(value) == 0:
            return

        if self.state == State.main_menu:
            if value.lower() == "c":
                pass
            else:
                try:
                    number_choice = int(value) - 1
                    if number_choice < 0:
                        raise ValueError

                except ValueError:
                    return self.syntax_error()

                selected_universe = self.fetch_nth_universe(number_choice)
                if not selected_universe:
                    return self.main_menu_view(error_message="No such number was found..")

                print("Selected universe:", selected_universe)
                self.connection.controller = PlayController(self.connection, self.runtime, self.account_id, selected_universe[0].id)
Exemplo n.º 6
0
    def handle(self, message={}):
        print("Handle message", message)
        try:
            return BaseController.handle(self, message)
        except KeyError:
            pass

        key = message.get("k")
        value = message.get("v", "")



        # Messages processed in standard mode check only for startswith commands, otherwise default to default action
        if key == "msg":
            # Look for a matching startswith handler
            for startswith_command in self._startswith.keys():
                if value.startswith(startswith_command):
                    print("Startswith command found", startswith_command)
                    cleaned_value = re.sub(re.escape(startswith_command), "", value, 1, re.IGNORECASE)
                    return self._startswith[startswith_command](self, startswith=startswith_command, value=cleaned_value)

            return self._commands.get("default")(self, value=value)

        # TODO command mode
        elif key == "cmd":
            tokens = value.split(" ")
            command = tokens[0].lower()
            print("Command", command)
            print("Self.commands", self._commands)
            raise NotImplementedError


        """

        # Look for a matching handler
        if command in self._commands:
            print("Command found", command)
            return self._commands[command](self, command, tokens[1:])



        # Look for a matching dynamic command handler
        for dcommand in self._dynamic_commands:
            try:
                return dcommand(self, command, tokens[1:])
            except NotImplementedError:
                continue
        """
        return self.syntax_error()
Exemplo n.º 7
0
    def handle(self, message={}):
        """
        Handle a received message in login state

        :param message:
        :return:
        """
        try:
            BaseController.handle(self, message)
        except KeyError:
            key = message.get("k")
            value = message.get("v")
            if key != "msg" or len(value) == 0:
                return

            # State - Login username
            if self.state == State.login_username:
                account = Account.get(lambda account: account.name.lower() == value.lower())

                if account:
                    self.account_id = account.id
                    self.request_password()
                    self.state = State.login_password
                else:
                    self.send_offtopic("Create new account with this name y/n?")
                    self.account_name = value
                    self.state = State.create_account

            # State - Login password
            elif self.state == State.login_password:
                account = Account[self.account_id]

                if self.hash_password_with_salt(account.password, self.dynamic_salt) == value:
                    self.send_offtopic("Login OK")
                    # Set controller to menu controller
                    self.connection.controller = MenuController(self.connection, self.runtime, self.account_id)
                    return

                else:
                    logging.info("Expected password: {}".format(
                        self.hash_password_with_salt(account.password, self.dynamic_salt)))
                    logging.info("Received password: {}".format( value))
                    self.send_offtopic("Login fail")
                    self.state = State.login_username
                    self.greeting()

            # State - Create account
            elif self.state == State.create_account:
                if value.lower()[0] == "y":
                    self.request_password(True, False)
                    self.state = State.create_password
                else:
                    self.greeting()
                    self.state = State.login_username

            # State - Create account - password
            elif self.state == State.create_password:
                self.password = value
                logging.info("Received password 1: {}".format(value))

                self.request_password(False)
                self.state = State.create_password_repeat

            # State - Create account - password repeat
            elif self.state == State.create_password_repeat:
                dynamic_password = self.hash_password_with_salt(self.password, self.dynamic_salt)
                logging.info("Received password 2: {}".format(value))

                if dynamic_password == value:
                    self.state = State.login_username
                    self.send_offtopic("New account has been created, you may now login.")
                    self.greeting()
                    account = Account(name=self.account_name,
                                      password=self.password,
                                      salt=self.static_salt)
                    db.commit()

                else:
                    self.send_offtopic("Password mismatch. Account not created.")
                    self.greeting()
                    self.state = State.login_username