Example #1
0
    def testEncode(self):
        # Game Guess message
        expected = b'\x00\x03\x00\x05\x00\x08\x00t\x00e\x00s\x00t'
        message_id = MESSAGE_ID_GUESS
        game_id = 5
        game_guess = "test"
        game_def_message = MessageFactory.build(message_id,
                                                game_id=game_id,
                                                game_guess=game_guess)
        actual = game_def_message.encode()
        self.assertEqual(expected, actual)

        # New Game message
        expected = b'\x00\x01\x00\x0e\x00A\x000\x000\x000\x000\x000\x001\x00\x06\x00b\x00a\x00r\x00\x06\x00f\x00o\x00o\x00\x04\x00f\x00b'
        message_id = MESSAGE_ID_NEW_GAME
        a_number = "A000001"
        last_name = "bar"
        first_name = "foo"
        alias = "fb"
        new_game_message = MessageFactory.build(message_id,
                                                user_last_name=last_name,
                                                user_first_name=first_name,
                                                user_alias=alias,
                                                user_a_number=a_number)
        actual = new_game_message.encode()
        self.assertEqual(expected, actual)
Example #2
0
 def _ackHeartbeat(self):
     logging.info("Receiver responding to heartbeat.")
     if self.client.game['id'] is None: return
     message = MessageFactory.build(MESSAGE_ID_ACK,
                                    game_id=self.client.game['id'])
     logging.info("Receiver sending bytes: {}".format(message.encode()))
     self.socket.send(message.encode())
Example #3
0
    def testBuildValidMessage(self):
        """Test MessageFactory.build() method with valid parameters"""
        # Test Ack Message
        message_id = MESSAGE_ID_ACK
        game_id = 5
        expected = AckMessage(message_id, game_id=game_id)
        actual = MessageFactory.build(message_id, game_id=game_id)
        self.assertEqual(expected.id, actual.id)
        self.assertEqual(expected.game_id, actual.game_id)

        # Test New Game Message
        message_id = MESSAGE_ID_NEW_GAME
        user_a_number = "A0000001"
        user_last_name = "Smith"
        user_first_name = "John"
        user_alias = "js"
        expected = NewGameMessage(message_id,
                                  user_a_number=user_a_number,
                                  user_last_name=user_last_name,
                                  user_first_name=user_first_name,
                                  user_alias=user_alias)
        actual = MessageFactory.build(message_id,
                                      user_a_number=user_a_number,
                                      user_last_name=user_last_name,
                                      user_first_name=user_first_name,
                                      user_alias=user_alias)
        self.assertEqual(expected.id, actual.id)
        self.assertEqual(expected.a_number, actual.a_number)
        self.assertEqual(expected.last_name, actual.last_name)
        self.assertEqual(expected.first_name, actual.first_name)
        self.assertEqual(expected.alias, actual.alias)

        # Test Error Message
        message_id = MESSAGE_ID_ERROR
        game_id = 5
        error_text = "Test Error Message"
        expected = ErrorMessage(message_id,
                                game_id=game_id,
                                error_text=error_text)
        actual = MessageFactory.build(message_id,
                                      game_id=game_id,
                                      error_text=error_text)
        self.assertEqual(expected.id, actual.id)
        self.assertEqual(expected.game_id, actual.game_id)
        self.assertEqual(expected.error_text, actual.error_text)
Example #4
0
 def _decodeBuffer(self, buf):
     try:
         message = MessageFactory.buildFromBytes(buf)
         if message.id == MESSAGE_ID_HEARTBEAT:
             self._ackHeartbeat()
             message = None
     except KeyError:
         message = None
     return message
Example #5
0
    def testBuildFromBytesValidMessage(self):
        """Test MessageFactory.buildFromBytes() with valid parameters"""
        # Test Game Def with valid bytestring
        bytestring = b'\x00\x02\x00\x05\x00\x0c\x00_\x00_\x00_\x00_\x00_\x00_\x00"\x00A\x00 \x00T\x00e\x00s\x00t\x00 \x00D\x00e\x00f\x00i\x00n\x00i\x00t\x00i\x00o\x00n'

        message_id = MESSAGE_ID_GAME_DEF
        game_id = 5
        game_hint = "______"
        game_definition = "A Test Definition"
        expected = GameDefMessage(message_id,
                                  game_id=game_id,
                                  game_hint=game_hint,
                                  game_definition=game_definition)
        actual = MessageFactory.buildFromBytes(bytestring)
        self.assertEqual(expected.id, actual.id)
        self.assertEqual(expected.game_id, actual.game_id)
        self.assertEqual(expected.game_hint, actual.game_hint)
        self.assertEqual(expected.game_definition, actual.game_definition)
Example #6
0
 def exit(self):
     logging.info("Client exiting")
     if self.game['id'] is None: self.alive = False
     message = MessageFactory.build(MESSAGE_ID_EXIT, game_id=self.game['id'])
     self.sendMessage(message)
     print("Exiting Game. Press enter to end program.")
Example #7
0
 def getHint(self):
     if self.game['id'] is None: return
     message = MessageFactory.build(MESSAGE_ID_GET_HINT, game_id=self.game['id'])
     self.sendMessage(message)
Example #8
0
 def guess(self, game_guess):
     if self.game['id'] is None: return
     message = MessageFactory.build(MESSAGE_ID_GUESS, game_id=self.game['id'], game_guess=game_guess)
     self.sendMessage(message)
Example #9
0
 def newGame(self):
     message = MessageFactory.build(MESSAGE_ID_NEW_GAME, user_a_number=self.user['a_number'],
                                    user_last_name=self.user['last_name'], user_first_name=self.user['first_name'],
                                    user_alias=self.user['alias'])
     self.sendMessage(message)