def test_collision_on_line_with_stopped_1(self): test_line_idx = 13 train_1 = self.player['trains'][0] train_2 = self.player['trains'][1] self.move_train(test_line_idx, train_1['idx'], 1) self.turn() self.move_train(test_line_idx, train_1['idx'], 0) self.turn() self.move_train(test_line_idx, train_2['idx'], 1) self.turn() map_data = self.get_map(1) self.assertTrue( self.check_collision_event( map_data['trains'][0]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_2['idx']) ) ) self.assertTrue( self.check_collision_event( map_data['trains'][1]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_1['idx']) ) ) self.assertEqual(map_data['trains'][0]['line_idx'], train_1['line_idx']) self.assertEqual(map_data['trains'][0]['position'], train_1['position']) self.assertEqual(map_data['trains'][1]['line_idx'], train_2['line_idx']) self.assertEqual(map_data['trains'][1]['position'], train_2['position'])
def test_collision_in_post_2(self): train_1 = self.player['trains'][0] train_2 = self.player['trains'][1] self.move_train_until_stop(1, train_1['idx'], 1) self.move_train_until_stop(13, train_2['idx'], 1) self.move_train_until_stop(2, train_2['idx'], -1) self.move_train(7, train_2['idx'], -1) self.turn() map_data = self.get_map(1) self.assertTrue( self.check_collision_event( map_data['trains'][0]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_2['idx']) ) ) self.assertTrue( self.check_collision_event( map_data['trains'][1]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_1['idx']) ) ) self.assertEqual(map_data['trains'][0]['line_idx'], train_1['line_idx']) self.assertEqual(map_data['trains'][0]['position'], train_1['position']) self.assertEqual(map_data['trains'][1]['line_idx'], train_2['line_idx']) self.assertEqual(map_data['trains'][1]['position'], train_2['position'])
def test_collision_goods_destroyed(self): test_line_idx = 1 town_idx = self.player['town']['idx'] start_product = self.player['town']['product'] population = self.player['town']['population'] train_1 = self.player['trains'][0] train_2 = self.player['trains'][1] self.move_train_until_stop(test_line_idx, train_1['idx'], 1) self.move_train(test_line_idx, train_1['idx'], -1) self.move_train(test_line_idx, train_2['idx'], 1) self.turn() map_data = self.get_map(1) self.assertTrue( self.check_collision_event( map_data['trains'][0]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_2['idx']) ) ) self.assertTrue( self.check_collision_event( map_data['trains'][1]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_1['idx']) ) ) self.assertEqual(map_data['trains'][0]['line_idx'], train_1['line_idx']) self.assertEqual(map_data['trains'][0]['position'], train_1['position']) self.assertEqual(map_data['trains'][1]['line_idx'], train_2['line_idx']) self.assertEqual(map_data['trains'][1]['position'], train_2['position']) self.assertEqual(map_data['trains'][0]['goods'], 0) self.assertEqual(map_data['trains'][1]['goods'], 0) # No new product arrived: self.assertEqual(self.get_post(town_idx)['product'], start_product - population * self.current_tick)
def test_refugees_arrival(self): turns_num = 6 refugees_number = 1 # From game config for testing. town_idx = self.player['town']['idx'] turns_ids = [i + 1 for i in range(turns_num)] turns_with_refugees = turns_ids[::CONFIG. REFUGEES_COOLDOWN_COEFFICIENT * refugees_number] for _ in range(turns_num): town = self.get_post(town_idx) population_before_turn = town['population'] self.turn() town = self.get_post(town_idx) check_event_result = self.check_refugees_arrival_event( town['event'], Event(EventType.REFUGEES_ARRIVAL, self.current_tick, refugees_number=refugees_number)) if self.current_tick in turns_with_refugees: self.assertTrue(check_event_result) self.assertEqual(town['population'], population_before_turn + refugees_number) else: self.assertFalse(check_event_result) self.assertEqual(town['population'], population_before_turn)
def test_parasites_assault(self): turns_num = 6 parasites_power = 1 # From game config for testing. town_idx = self.player['town']['idx'] turns_ids = [i + 1 for i in range(turns_num)] turns_with_assault = turns_ids[::CONFIG. PARASITES_COOLDOWN_COEFFICIENT * parasites_power] for _ in range(turns_num): town = self.get_post(town_idx) product_before_turn = town['product'] population_before_turn = town['population'] self.turn() town = self.get_post(town_idx) check_event_result = self.check_parasites_assault_event( town['event'], Event(EventType.PARASITES_ASSAULT, self.current_tick, parasites_power=parasites_power)) if self.current_tick in turns_with_assault: self.assertTrue(check_event_result) self.assertEqual( town['product'], max( product_before_turn - parasites_power - population_before_turn, 0)) else: self.assertFalse(check_event_result) self.assertEqual( town['product'], max(product_before_turn - population_before_turn, 0))
def test_no_collision_on_move_towards_1_turn_before_and_after(self): test_line_idx = 18 train_1 = self.player['trains'][0] train_2 = self.player['trains'][1] self.move_train_until_stop(test_line_idx, train_1['idx'], -1) self.move_train(test_line_idx, train_1['idx'], 1) self.move_train(test_line_idx, train_2['idx'], -1) self.turn() map_data = self.get_map(1) self.assertFalse( self.check_collision_event( map_data['trains'][0]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_2['idx']) ) ) self.assertFalse( self.check_collision_event( map_data['trains'][1]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_1['idx']) ) ) self.assertEqual(map_data['trains'][0]['line_idx'], test_line_idx) self.assertEqual(map_data['trains'][0]['position'], 1) self.assertEqual(map_data['trains'][1]['line_idx'], test_line_idx) self.assertEqual(map_data['trains'][1]['position'], 2) self.turn() map_data = self.get_map(1) self.assertTrue( self.check_collision_event( map_data['trains'][0]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_2['idx']) ) ) self.assertTrue( self.check_collision_event( map_data['trains'][1]['events'], Event(EventType.TRAIN_COLLISION, self.current_tick, train=train_1['idx']) ) ) self.assertEqual(map_data['trains'][0]['line_idx'], train_1['line_idx']) self.assertEqual(map_data['trains'][0]['position'], train_1['position']) self.assertEqual(map_data['trains'][1]['line_idx'], train_2['line_idx']) self.assertEqual(map_data['trains'][1]['position'], train_2['position'])
def test_hijackers_assault(self): turns_num = 10 hijackers_power = 1 # From game config for testing. refugees_number = 1 # From game config for testing. town_idx = self.player['town']['idx'] turns_ids = [i + 1 for i in range(turns_num)] turns_with_assault = turns_ids[::CONFIG. HIJACKERS_COOLDOWN_COEFFICIENT * hijackers_power] turns_with_refugees = turns_ids[::CONFIG. REFUGEES_COOLDOWN_COEFFICIENT * refugees_number] for _ in range(turns_num): town = self.get_post(town_idx) armor_before_turn = town['armor'] population_before_turn = town['population'] self.turn() town = self.get_post(town_idx) check_event_result = self.check_hijackers_assault_event( town['event'], Event(EventType.HIJACKERS_ASSAULT, self.current_tick, hijackers_power=hijackers_power)) if self.current_tick in turns_with_assault: self.assertTrue(check_event_result) armor_after_turn = armor_before_turn - hijackers_power if armor_after_turn >= 0: self.assertEqual(town['armor'], armor_after_turn) if self.current_tick in turns_with_refugees: self.assertEqual( town['population'], population_before_turn + refugees_number) else: self.assertEqual(town['population'], population_before_turn) else: self.assertEqual(town['armor'], 0) if self.current_tick in turns_with_refugees: self.assertEqual( town['population'], max( population_before_turn + refugees_number - hijackers_power, 0)) else: self.assertEqual( town['population'], max(population_before_turn - hijackers_power, 0)) else: self.assertFalse(check_event_result) self.assertEqual(town['armor'], armor_before_turn) if self.current_tick in turns_with_refugees: self.assertEqual(town['population'], population_before_turn + refugees_number) else: self.assertEqual(town['population'], population_before_turn)
def test_user_events(self): """ Test users events independence. """ players_in_game = 2 player0 = AttrDict( self.login(self.players[0], num_players=players_in_game)) player1 = AttrDict( self.login(self.players[1], num_players=players_in_game)) train0 = AttrDict(self.get_train(self.players[0], player0.train[0].idx)) train1 = AttrDict(self.get_train(self.players[1], player1.train[0].idx)) # path to Market #1-2-3-4 for line_idx in (1, 2, 3, 4): self.move_train(self.players[0], line_idx, train0.idx, 1) self.players_turn(self.players[:players_in_game], turns_count=4) for line_idx in (9, 8, 7, 6, 5): self.move_train(self.players[1], line_idx, train1.idx, -1) self.players_turn(self.players[:players_in_game], turns_count=4) train0_after = AttrDict( self.get_train(self.players[0], player0.train[0].idx)) self.assertTrue( self.check_collision_event( train0_after.event, Event(EventType.TRAIN_COLLISION, self.current_tick, train=train1.idx))) self.assertEqual(train0_after.line_idx, train0.line_idx) self.assertEqual(train0_after.position, train0.position) train1_after = AttrDict( self.get_train(self.players[1], player1.train[0].idx)) self.assertTrue( self.check_collision_event( train1_after.event, Event(EventType.TRAIN_COLLISION, self.current_tick, train=train0.idx))) self.assertEqual(train1_after.line_idx, train1.line_idx) self.assertEqual(train1_after.position, train1.position)
def test_user_events(self): """ Test users events independence. """ players_in_game = 2 player0 = AttrDict( self.login(self.players[0].name, num_players=players_in_game, connection=self.players[0].conn)) player1 = AttrDict( self.login(self.players[1].name, num_players=players_in_game, connection=self.players[1].conn)) train0 = AttrDict( self.get_train(player0.trains[0].idx, connection=self.players[0].conn)) train1 = AttrDict( self.get_train(player1.trains[0].idx, connection=self.players[1].conn)) # Path to collision for 1-st train: path = [ (1, 1, 4), (2, 1, 4), (3, 1, 4), (4, 1, 4), ] for line_idx, speed, length in path: self.move_train(line_idx, train0.idx, speed, connection=self.players[0].conn) self.players_turn(self.players[:players_in_game], turns_count=length) # Path to collision for 2-nd train: path = [ (9, -1, 4), (8, -1, 4), (7, -1, 4), (6, -1, 4), (5, -1, 4), ] for line_idx, speed, length in path: self.move_train(line_idx, train1.idx, speed, connection=self.players[1].conn) self.players_turn(self.players[:players_in_game], turns_count=length) train0_after = AttrDict( self.get_train(player0.trains[0].idx, connection=self.players[0].conn)) self.assertTrue( self.check_collision_event( train0_after.events, Event(EventType.TRAIN_COLLISION, self.current_tick, train=train1.idx))) self.assertEqual(train0_after.line_idx, train0.line_idx) self.assertEqual(train0_after.position, train0.position) train1_after = AttrDict( self.get_train(player1.trains[0].idx, connection=self.players[1].conn)) self.assertTrue( self.check_collision_event( train1_after.events, Event(EventType.TRAIN_COLLISION, self.current_tick, train=train0.idx))) self.assertEqual(train1_after.line_idx, train1.line_idx) self.assertEqual(train1_after.position, train1.position)