def test_1(self): bloc = MapListBlock(['1', '3', 'test','mode', '2']) self.assertEqual(1, len(bloc)) self.assertEqual('test', bloc[0]['name']) self.assertEqual('mode', bloc[0]['gamemode']) self.assertEqual(2, bloc[0]['num_of_rounds']) self.assertEqual("MapListBlock[test:mode:2]", repr(bloc)) self.assertEqual(0, len(bloc.getByName('MP_003'))) self.assertEqual(1, len(bloc.getByName('test')))
def test_1(self): bloc = MapListBlock(['1', '3', 'test', 'mode', '2']) self.assertEqual(1, len(bloc)) self.assertEqual('test', bloc[0]['name']) self.assertEqual('mode', bloc[0]['gamemode']) self.assertEqual(2, bloc[0]['num_of_rounds']) self.assertEqual("MapListBlock[test:mode:2]", repr(bloc)) self.assertEqual(0, len(bloc.getByName('MP_003'))) self.assertEqual(1, len(bloc.getByName('test')))
def test_2(self): bloc = MapListBlock( ['2', '3', 'map1', 'mode1', '1', 'map2', 'mode2', '2']) self.assertEqual(2, len(bloc)) self.assertEqual('map1', bloc[0]['name']) self.assertEqual('mode1', bloc[0]['gamemode']) self.assertEqual(1, bloc[0]['num_of_rounds']) self.assertEqual('map2', bloc[1]['name']) self.assertEqual('mode2', bloc[1]['gamemode']) self.assertEqual(2, bloc[1]['num_of_rounds']) self.assertEqual("MapListBlock[map1:mode1:1, map2:mode2:2]", repr(bloc)) self.assertEqual(0, len(bloc.getByName('MP_003'))) self.assertEqual(1, len(bloc.getByName('map1'))) self.assertEqual(1, len(bloc.getByName('map2'))) self.assertIn(0, bloc.getByName('map1')) self.assertIn(1, bloc.getByName('map2')) self.assertTrue(bloc.getByName('map1')[0]['gamemode'] == 'mode1') self.assertTrue(bloc.getByName('map2')[1]['gamemode'] == 'mode2') self.assertEqual(0, len(bloc.getByNameAndGamemode('map1', 'mode?'))) self.assertEqual(1, len(bloc.getByNameAndGamemode('map1', 'mode1'))) self.assertEqual(0, len(bloc.getByNameAndGamemode('map2', 'mode?'))) self.assertEqual(1, len(bloc.getByNameAndGamemode('map2', 'mode2'))) self.assertIn(0, bloc.getByNameAndGamemode('map1', 'mode1')) self.assertIn(1, bloc.getByNameAndGamemode('map2', 'mode2'))
def test_append_list_with_same_num_words(self): data1 = [1, 3, 'a1','a2',1] data2 = [1, 3, 'b1','b2',2] # check both data lists make valid MapListBlock individually mlb1 = MapListBlock(data1) self.assertEqual(1, len(mlb1)) mlb2 = MapListBlock(data2) self.assertEqual(1, len(mlb2)) # check both 2nd list can be appended to the 1st one. mlb3 = MapListBlock(data1) mlb3.append(data2) # check new list length self.assertEqual(len(mlb1) + len(mlb2), len(mlb3))
def test_append_list_with_different_num_words(self): data1 = [1, 3, 'a1','a2',1] data2 = [1, 4, 'b1','b2',1,'b4'] # check both data lists make valid MapListBlock individually self.assertEqual(1, len(MapListBlock(data1))) self.assertEqual(1, len(MapListBlock(data2))) # check both 2nd list cannot be appended to the 1st one. mlb1 = MapListBlock(data1) self.assertEqual(3, mlb1._num_words) try: mlb1.append(data2) except MapListBlockError, err: self.assertIn('cannot append data', str(err), "expecting error message to contain 'cannot append data' but got %r instead" % err)
def cmd_setnextmap(self, data, client=None, cmd=None): """\ <mapname> - Set the nextmap (partial map name works) """ if not data: client.message('Invalid or missing data, try !help setnextmap') else: match = self.console.getMapsSoundingLike(data) if len(match) > 1: client.message('do you mean : %s ?' % ', '.join(match)) return if len(match) == 1: map_id = match[0] maplist = MapListBlock(self.console.write(('mapList.list',))) if not len(maplist): # maplist is empty, fix this situation by loading save mapList from disk try: self.console.write(('mapList.load',)) except Exception, err: self.warning(err) maplist = MapListBlock(self.console.write(('mapList.list',))) current_max_rounds = self.console.write(('mapList.getRounds',))[1] if not len(maplist): # maplist is still empty, nextmap will be inserted at index 0 self.console.write(('mapList.add', map_id, self.console.game.gameType, current_max_rounds, 0)) self.console.write(('mapList.setNextMapIndex', 0)) else: current_map_index = int(self.console.write(('mapList.getMapIndices', ))[0]) matching_maps = maplist.getByName(map_id) if not len(matching_maps): # then insert wanted map in rotation list next_map_index = current_map_index + 1 self.console.write(('mapList.add', map_id, self.console.game.gameType, current_max_rounds, next_map_index)) self.console.write(('mapList.setNextMapIndex', next_map_index)) elif len(matching_maps) == 1: # easy case, just set the nextLevelIndex to the index found self.console.write(('mapList.setNextMapIndex', matching_maps.keys()[0])) else: # multiple matches :s matching_indices = matching_maps.keys() # try to find the next indice after the index of the current map indices_after_current = [x for x in matching_indices if x > current_map_index] if len(indices_after_current): next_map_index = indices_after_current[0] else: next_map_index = matching_indices[0] self.console.write(('mapList.setNextMapIndex', next_map_index)) if client: cmd.sayLoudOrPM(client, 'next map set to %s' % self.console.getEasyName(map_id))
def test_minimal(self): self.assertEqual(0, len(MapListBlock([0, 3]))) self.assertEqual('MapListBlock[]', repr(MapListBlock([0, 3]))) self.assertEqual(0, len(MapListBlock(['0', '3']))) tmp = MapListBlock(['1', '3', 'test', 'mode', '2']) self.assertEqual(1, len(tmp), repr(tmp)) self.assertEqual('MapListBlock[]', repr(MapListBlock(['0', '3']))) self.assertEqual(0, len(MapListBlock(['0', '3']).getByName('MP_003')))
def test_cmd_rotateMap_generates_EVT_GAME_ROUND_END(self): # prepare fake BF3 server responses def fake_write(data): if data == ('mapList.getMapIndices', ): return [0, 1] else: return [] self.parser.write = Mock(side_effect=fake_write) self.parser.getFullMapRotationList = Mock(return_value=MapListBlock(['4', '3', 'MP_007', 'RushLarge0', '4', 'MP_011', 'RushLarge0', '4', 'MP_012', 'SquadRush0', '4', 'MP_013', 'SquadRush0', '4'])) self.parser.rotateMap() self.assertEqual(1, self.parser.queueEvent.call_count) self.assertEqual(self.parser.getEventID("EVT_GAME_ROUND_END"), self.parser.queueEvent.call_args[0][0].type) self.assertIsNone(self.parser.queueEvent.call_args[0][0].data)
def test_append_list_with_same_num_words(self): data1 = [1, 3, 'a1', 'a2', 1] data2 = [1, 3, 'b1', 'b2', 2] # check both data lists make valid MapListBlock individually mlb1 = MapListBlock(data1) self.assertEqual(1, len(mlb1)) mlb2 = MapListBlock(data2) self.assertEqual(1, len(mlb2)) # check both 2nd list can be appended to the 1st one. mlb3 = MapListBlock(data1) mlb3.append(data2) # check new list length self.assertEqual(len(mlb1) + len(mlb2), len(mlb3))
def test_2(self): bloc = MapListBlock(['2','3','map1','mode1', '1', 'map2', 'mode2', '2']) self.assertEqual(2, len(bloc)) self.assertEqual('map1', bloc[0]['name']) self.assertEqual('mode1', bloc[0]['gamemode']) self.assertEqual(1, bloc[0]['num_of_rounds']) self.assertEqual('map2', bloc[1]['name']) self.assertEqual('mode2', bloc[1]['gamemode']) self.assertEqual(2, bloc[1]['num_of_rounds']) self.assertEqual("MapListBlock[map1:mode1:1, map2:mode2:2]", repr(bloc)) self.assertEqual(0, len(bloc.getByName('MP_003'))) self.assertEqual(1, len(bloc.getByName('map1'))) self.assertEqual(1, len(bloc.getByName('map2'))) self.assertIn(0, bloc.getByName('map1')) self.assertIn(1, bloc.getByName('map2')) self.assertTrue(bloc.getByName('map1')[0]['gamemode'] == 'mode1') self.assertTrue(bloc.getByName('map2')[1]['gamemode'] == 'mode2') self.assertEqual(0, len(bloc.getByNameAndGamemode('map1', 'mode?'))) self.assertEqual(1, len(bloc.getByNameAndGamemode('map1', 'mode1'))) self.assertEqual(0, len(bloc.getByNameAndGamemode('map2', 'mode?'))) self.assertEqual(1, len(bloc.getByNameAndGamemode('map2', 'mode2'))) self.assertIn(0, bloc.getByNameAndGamemode('map1', 'mode1')) self.assertIn(1, bloc.getByNameAndGamemode('map2', 'mode2'))
def cmd_setnextmap(self, data, client=None, cmd=None): """\ <mapname> - Set the nextmap (partial map name works) """ if not data: client.message('Invalid or missing data, try !help setnextmap') else: match = self.console.getMapsSoundingLike(data) if len(match) > 1: client.message('do you mean : %s ?' % ', '.join(match)) return if len(match) == 1: map_id = match[0] maplist = MapListBlock(self.console.write(('mapList.list', ))) if not len(maplist): # maplist is empty, fix this situation by loading save mapList from disk try: self.console.write(('mapList.load', )) except Exception, err: self.warning(err) maplist = MapListBlock( self.console.write(('mapList.list', ))) current_max_rounds = self.console.write( ('mapList.getRounds', ))[1] if not len(maplist): # maplist is still empty, nextmap will be inserted at index 0 self.console.write( ('mapList.add', map_id, self.console.game.gameType, current_max_rounds, 0)) self.console.write(('mapList.setNextMapIndex', 0)) else: current_map_index = int( self.console.write(('mapList.getMapIndices', ))[0]) matching_maps = maplist.getByName(map_id) if not len(matching_maps): # then insert wanted map in rotation list next_map_index = current_map_index + 1 self.console.write( ('mapList.add', map_id, self.console.game.gameType, current_max_rounds, next_map_index)) self.console.write( ('mapList.setNextMapIndex', next_map_index)) elif len(matching_maps) == 1: # easy case, just set the nextLevelIndex to the index found self.console.write(('mapList.setNextMapIndex', matching_maps.keys()[0])) else: # multiple matches :s matching_indices = matching_maps.keys() # try to find the next indice after the index of the current map indices_after_current = [ x for x in matching_indices if x > current_map_index ] if len(indices_after_current): next_map_index = indices_after_current[0] else: next_map_index = matching_indices[0] self.console.write( ('mapList.setNextMapIndex', next_map_index)) if client: cmd.sayLoudOrPM( client, 'next map set to %s' % self.console.getEasyName(map_id))
def test_no_param(self): self.assertEqual(0, len(MapListBlock()))