Exemplo n.º 1
0
    def _generate_answer_to_system_message(self, message):
        command, args = parse_system_question(message.text)

        if command == 'list-units':
            sender = self.units_by_IDs[message.sender_ID]
            player = sender.player
            text = "%d " % len(player.units)
            text += " ".join(map(lambda unit: "%d %s" % \
                                   (unit.ID, unit.type.main_name),
                                 player.units))

        elif command == 'unit-info':
            unit_ID = args[0]
            unit = self.units_by_IDs.get(unit_ID, None)
            if unit == None:
                return None

            text_dict = {'ID':unit.ID,
                         'type':unit.type.main_name,
                         'x':unit.position[0],
                         'y':unit.position[1],
                         'more_info': (unit.minerals
                                       if unit.type.storage_size != 0
                                       else unit.type.attack_range)}
            text = "%(ID)d %(type)s %(x)d %(y)d %(more_info)d" % text_dict

        else:
            assert command == 'error'
            return None

        response = Message(sender_ID=0,
                           receiver_ID=message.sender_ID,
                           text=text)
        return response
Exemplo n.º 2
0
 def test_case_insensitive(self):
     answer = parse_system_question('lIsT UNITS')
     expected_answer = ('list-units', ())
     self.assertEqual(answer, expected_answer)
Exemplo n.º 3
0
 def test_whitespaces(self):
     answer = parse_system_question('  list\tunits\r')
     expected_answer = ('list-units', ())
     self.assertEqual(answer, expected_answer)
Exemplo n.º 4
0
 def test_parsing_blank_input(self):
     answer = parse_system_question('')
     expected_answer = ('error', ())
     self.assertEqual(answer, expected_answer)
Exemplo n.º 5
0
 def test_parsing_u_command(self):
     answer = parse_system_question('u 123')
     expected_answer = ('unit-info', (123,))
     self.assertEqual(answer, expected_answer)
Exemplo n.º 6
0
 def test_parsing_lu_command(self):
     answer = parse_system_question('lu')
     expected_answer = ('list-units', ())
     self.assertEqual(answer, expected_answer)