Пример #1
0
    def setUp(self):
        "sets up testing environment"
        settings.DEFAULT_HOME = "#2"
        # print "creating player %i: %s" % (self.CID, self.__class__.__name__)
        self.player = create.create_player(
            "TestPlayer%i" % self.CID, "*****@*****.**", "testpassword", typeclass=TestPlayerClass
        )
        self.player2 = create.create_player(
            "TestPlayer%ib" % self.CID, "*****@*****.**", "testpassword", typeclass=TestPlayerClass
        )
        self.room1 = create.create_object("src.objects.objects.Room", key="Room%i" % self.CID, nohome=True)
        self.room1.db.desc = "room_desc"
        self.room2 = create.create_object("src.objects.objects.Room", key="Room%ib" % self.CID)
        self.obj1 = create.create_object(TestObjectClass, key="Obj%i" % self.CID, location=self.room1, home=self.room1)
        self.obj2 = create.create_object(TestObjectClass, key="Obj%ib" % self.CID, location=self.room1, home=self.room1)
        self.char1 = create.create_object(
            TestCharacterClass, key="Char%i" % self.CID, location=self.room1, home=self.room1
        )
        self.char1.permissions.add("Immortals")
        self.char2 = create.create_object(
            TestCharacterClass, key="Char%ib" % self.CID, location=self.room1, home=self.room1
        )
        self.char1.player = self.player
        self.char2.player = self.player2
        self.script = create.create_script("src.scripts.scripts.Script", key="Script%i" % self.CID)
        self.player.permissions.add("Immortals")

        # set up a fake session

        global SESSIONS
        session = ServerSession()
        session.init_session("telnet", ("localhost", "testmode"), SESSIONS)
        session.sessid = self.CID
        SESSIONS.portal_connect(session.get_sync_data())
        SESSIONS.login(SESSIONS.session_from_sessid(self.CID), self.player, testmode=True)
Пример #2
0
    def setUp(self):
        "sets up testing environment"
        settings.DEFAULT_HOME = "#2"
        #print "creating player %i: %s" % (self.CID, self.__class__.__name__)
        self.player = create.create_player("TestPlayer%i" % self.CID, "*****@*****.**", "testpassword", typeclass=TestPlayerClass)
        self.player2 = create.create_player("TestPlayer%ib" % self.CID, "*****@*****.**", "testpassword", typeclass=TestPlayerClass)
        self.room1 = create.create_object("src.objects.objects.Room", key="Room%i"%self.CID, nohome=True)
        self.room1.db.desc = "room_desc"
        self.room2 = create.create_object("src.objects.objects.Room", key="Room%ib" % self.CID)
        self.obj1 = create.create_object(TestObjectClass, key="Obj%i" % self.CID, location=self.room1, home=self.room1)
        self.obj2 = create.create_object(TestObjectClass, key="Obj%ib" % self.CID, location=self.room1, home=self.room1)
        self.char1 = create.create_object(TestCharacterClass, key="Char%i" % self.CID, location=self.room1, home=self.room1)
        self.char1.permissions.add("Immortals")
        self.char2 = create.create_object(TestCharacterClass, key="Char%ib" % self.CID, location=self.room1, home=self.room1)
        self.char1.player = self.player
        self.char2.player = self.player2
        self.script = create.create_script("src.scripts.scripts.Script", key="Script%i" % self.CID)
        self.player.permissions.add("Immortals")

        # set up a fake session

        global SESSIONS
        session = ServerSession()
        session.init_session("telnet", ("localhost", "testmode"), SESSIONS)
        session.sessid = self.CID
        SESSIONS.portal_connect(session.get_sync_data())
        SESSIONS.login(SESSIONS.session_from_sessid(self.CID), self.player, testmode=True)
Пример #3
0
 def at_repeat(self):
     """
     Calls subscriptions every self.interval seconds
     """
     for (func_key, sessid, interval, args, kwargs) in self.ndb.subscriptions.values():
         session = SESSIONS.session_from_sessid(sessid)
         OOB_HANDLER.execute_cmd(session, func_key, *args, **kwargs)
Пример #4
0
    def msg(self, text=None, from_obj=None, sessid=0, **kwargs):
        """
        Emits something to a session attached to the object.

        message (str): The message to send
        from_obj (obj): object that is sending.
        data (object): an optional data object that may or may not
                       be used by the protocol.
        sessid (int): sessid to relay to, if any.
                      If set to 0 (default), use either from_obj.sessid (if set) or self.sessid automatically
                      If None, echo to all connected sessions
        """
        global _SESSIONS
        if not _SESSIONS:
            from src.server.sessionhandler import SESSIONS as _SESSIONS

        text = to_str(text, force_string=True) if text else ""

        if "data" in kwargs:
            # deprecation warning
            logger.log_depmsg("ObjectDB.msg(): 'data'-dict keyword is deprecated. Use **kwargs instead.")
            data = kwargs.pop("data")
            if isinstance(data, dict):
                kwargs.update(data)

        if from_obj:
            # call hook
            try:
                _GA(from_obj, "at_msg_send")(text=text, to_obj=self, **kwargs)
            except Exception:
                pass

        session = _SESSIONS.session_from_sessid(sessid if sessid else _GA(self, "sessid"))
        if session:
            session.msg(text=text, **kwargs)
Пример #5
0
 def _callback(self):
     "See original for more info"
     for key, (_, args, kwargs) in self.subscriptions.items():
         # args = (sessid, callback_function)
         session = SESSIONS.session_from_sessid(args[0])
         try:
             # execute the oob callback
             yield args[1](OOB_HANDLER, session, *args[2:], **kwargs)
         except Exception:
             logger.log_trace()
Пример #6
0
 def _callback(self):
     "See original for more info"
     for key, (_, args, kwargs) in self.subscriptions.items():
         # args = (sessid, callback_function)
         session = SESSIONS.session_from_sessid(args[0])
         try:
             # execute the oob callback
             yield args[1](OOB_HANDLER, session, *args[2:], **kwargs)
         except Exception:
             logger.log_trace()
Пример #7
0
    def msg(self, text=None, from_obj=None, sessid=0, **kwargs):
        """
        Emits something to a session attached to the object.

        message (str): The message to send
        from_obj (obj): object that is sending.
        data (object): an optional data object that may or may not
                       be used by the protocol.
        sessid (int): sessid to relay to, if any.
                      If set to 0 (default), use either from_obj.sessid (if set) or self.sessid automatically
                      If None, echo to all connected sessions

        When this message is called, from_obj.at_msg_send and self.at_msg_receive are called.

        """
        global _SESSIONS
        if not _SESSIONS:
            from src.server.sessionhandler import SESSIONS as _SESSIONS

        text = to_str(text, force_string=True) if text else ""

        if "data" in kwargs:
            # deprecation warning
            logger.log_depmsg(
                "ObjectDB.msg(): 'data'-dict keyword is deprecated. Use **kwargs instead."
            )
            data = kwargs.pop("data")
            if isinstance(data, dict):
                kwargs.update(data)

        if from_obj:
            # call hook
            try:
                _GA(from_obj, "at_msg_send")(text=text,
                                             to_obj=_GA(self, "typeclass"),
                                             **kwargs)
            except Exception:
                logger.log_trace()
        try:
            if not _GA(_GA(self, "typeclass"), "at_msg_receive")(text=text,
                                                                 **kwargs):
                # if at_msg_receive returns false, we abort message to this object
                return
        except Exception:
            logger.log_trace()

        sessions = _SESSIONS.session_from_sessid(
            [sessid] if sessid else make_iter(_GA(self, "sessid").get()))
        for session in sessions:
            session.msg(text=text, **kwargs)
Пример #8
0
 def call(self,
          cmdobj,
          args,
          msg=None,
          cmdset=None,
          noansi=True,
          caller=None):
     """
     Test a command by assigning all the needed
     properties to cmdobj and  running
         cmdobj.at_pre_cmd()
         cmdobj.parse()
         cmdobj.func()
         cmdobj.at_post_cmd()
     The msgreturn value is compared to eventual
     output sent to caller.msg in the game
     """
     cmdobj.caller = caller if caller else self.char1
     #print "call:", cmdobj.key, cmdobj.caller, caller if caller else cmdobj.caller.player
     #print "perms:", cmdobj.caller.permissions.all()
     cmdobj.cmdstring = cmdobj.key
     cmdobj.args = args
     cmdobj.cmdset = cmdset
     cmdobj.sessid = self.CID
     cmdobj.session = SESSIONS.session_from_sessid(self.CID)
     cmdobj.player = self.player
     cmdobj.raw_string = cmdobj.key + " " + args
     cmdobj.obj = caller if caller else self.char1
     # test
     self.char1.player.ndb.stored_msg = []
     cmdobj.at_pre_cmd()
     cmdobj.parse()
     cmdobj.func()
     cmdobj.at_post_cmd()
     # clean out prettytable sugar
     stored_msg = self.char1.player.ndb.stored_msg if self.char1.player else self.char1.ndb.stored_msg
     returned_msg = "|".join(_RE.sub("", mess) for mess in stored_msg)
     #returned_msg = "|".join(self.char1.player.ndb.stored_msg)
     returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip()
     if msg != None:
         if msg == "" and returned_msg or not returned_msg.startswith(
                 msg.strip()):
             sep1 = "\n" + "=" * 30 + "Wanted message" + "=" * 34 + "\n"
             sep2 = "\n" + "=" * 30 + "Returned message" + "=" * 32 + "\n"
             sep3 = "\n" + "=" * 78
             retval = sep1 + msg.strip() + sep2 + returned_msg + sep3
             raise AssertionError(retval)
Пример #9
0
    def msg(self, text=None, from_obj=None, sessid=0, **kwargs):
        """
        Emits something to a session attached to the object.

        message (str): The message to send
        from_obj (obj): object that is sending.
        data (object): an optional data object that may or may not
                       be used by the protocol.
        sessid (int): sessid to relay to, if any.
                      If set to 0 (default), use either from_obj.sessid (if set) or self.sessid automatically
                      If None, echo to all connected sessions

        When this message is called, from_obj.at_msg_send and self.at_msg_receive are called.

        """
        global _SESSIONS
        if not _SESSIONS:
            from src.server.sessionhandler import SESSIONS as _SESSIONS

        text = to_str(text, force_string=True) if text else ""

        if "data" in kwargs:
            # deprecation warning
            logger.log_depmsg("ObjectDB.msg(): 'data'-dict keyword is deprecated. Use **kwargs instead.")
            data = kwargs.pop("data")
            if isinstance(data, dict):
                kwargs.update(data)

        if from_obj:
            # call hook
            try:
                _GA(from_obj, "at_msg_send")(text=text, to_obj=_GA(self, "typeclass"), **kwargs)
            except Exception:
                logger.log_trace()
        try:
            if not _GA(_GA(self, "typeclass"), "at_msg_receive")(text=text, **kwargs):
                # if at_msg_receive returns false, we abort message to this object
                return
        except Exception:
            logger.log_trace()

        sessions = _SESSIONS.session_from_sessid([sessid] if sessid else make_iter(_GA(self, "sessid").get()))
        for session in sessions:
            session.msg(text=text, **kwargs)
Пример #10
0
 def call(self, cmdobj, args, msg=None, cmdset=None, noansi=True, caller=None):
     """
     Test a command by assigning all the needed
     properties to cmdobj and  running
         cmdobj.at_pre_cmd()
         cmdobj.parse()
         cmdobj.func()
         cmdobj.at_post_cmd()
     The msgreturn value is compared to eventual
     output sent to caller.msg in the game
     """
     cmdobj.caller = caller if caller else self.char1
     # print "call:", cmdobj.key, cmdobj.caller, caller if caller else cmdobj.caller.player
     # print "perms:", cmdobj.caller.permissions.all()
     cmdobj.cmdstring = cmdobj.key
     cmdobj.args = args
     cmdobj.cmdset = cmdset
     cmdobj.sessid = self.CID
     cmdobj.session = SESSIONS.session_from_sessid(self.CID)
     cmdobj.player = self.player
     cmdobj.raw_string = cmdobj.key + " " + args
     cmdobj.obj = caller if caller else self.char1
     # test
     self.char1.player.ndb.stored_msg = []
     cmdobj.at_pre_cmd()
     cmdobj.parse()
     cmdobj.func()
     cmdobj.at_post_cmd()
     # clean out prettytable sugar
     stored_msg = self.char1.player.ndb.stored_msg if self.char1.player else self.char1.ndb.stored_msg
     returned_msg = "|".join(_RE.sub("", mess) for mess in stored_msg)
     # returned_msg = "|".join(self.char1.player.ndb.stored_msg)
     returned_msg = ansi.parse_ansi(returned_msg, strip_ansi=noansi).strip()
     if msg != None:
         if msg == "" and returned_msg or not returned_msg.startswith(msg.strip()):
             sep1 = "\n" + "=" * 30 + "Wanted message" + "=" * 34 + "\n"
             sep2 = "\n" + "=" * 30 + "Returned message" + "=" * 32 + "\n"
             sep3 = "\n" + "=" * 78
             retval = sep1 + msg.strip() + sep2 + returned_msg + sep3
             raise AssertionError(retval)