def __init__(self): super().__init__() self.game_over = False self.turn = None self.current_world_data = None self.action_controller = ActionController()
class TestActionController(unittest.TestCase): # This method is used to set up anything you wish to test prior to every test method below. def setUp(self): self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController() def test_event_controller_tires(self): neg = stats.GameStats.costs_and_effectiveness[ ObjectType.tires]['effectiveness'][self.myPlayer.truck.tires] actneg = self.actionCont.event_controller.negation( self.myPlayer.truck, EventType.icy_road) self.assertAlmostEqual(neg, actneg["DamageMod"]) self.assertAlmostEqual(neg, actneg["HealthMod"]) def test_heal(self): startHealth = 1 self.myPlayer.truck.money = 100000 startMoney = self.myPlayer.truck.money self.myPlayer.truck.health = startHealth self.myPlayer.truck.current_node = Node("Bruh") self.actionCont.heal(self.myPlayer) self.assertLess(startHealth, self.myPlayer.truck.health) self.assertLess(self.myPlayer.truck.money, startMoney) def test_event_controller_tires_upgrade(self): self.myPlayer.truck.tires = TireType.tire_sticky neg = stats.GameStats.costs_and_effectiveness[ ObjectType.tires]['effectiveness'][self.myPlayer.truck.tires] actneg = self.actionCont.event_controller.negation( self.myPlayer.truck, EventType.icy_road) self.assertAlmostEqual(neg, actneg['DamageMod']) self.assertAlmostEqual(neg, actneg['HealthMod']) def test_event_controller_sentry_gun(self): self.myPlayer.truck.body = SentryGun() neg = stats.GameStats.costs_and_effectiveness[ObjectType.sentryGun][ 'effectiveness'][self.myPlayer.truck.body.level] actneg = self.actionCont.event_controller.negation( self.myPlayer.truck, EventType.bandits) self.assertAlmostEqual(neg, actneg["DamageMod"]) self.assertAlmostEqual(neg, actneg["HealthMod"])
def setUp(self): self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController()
class TestUpgradeRabbitFoot(unittest.TestCase): # This method is used to set up anything you wish to test prior to every test method below. def setUp(self): self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController() # Test methods should always start with the word 'test' def test_upgrade_one_level(self): self.myPlayer.truck.addons.level = 0 self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.costs_and_effectiveness[ObjectType.rabbitFoot]['cost'][1] self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.assertEqual(self.myPlayer.truck.addons.level,RabbitFootLevel.level_one) self.assertEqual(expectedCash, self.myPlayer.truck.money) self.assertTrue(isinstance(self.myPlayer.truck.addons, RabbitFoot)) def test_upgrade_two_level(self): self.myPlayer.truck.addons.level = 0 self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.costs_and_effectiveness[ObjectType.rabbitFoot]['cost'][1] - stats.GameStats.costs_and_effectiveness[ObjectType.rabbitFoot]['cost'][2] self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.assertEqual(self.myPlayer.truck.addons.level, RabbitFootLevel.level_two) self.assertEqual(expectedCash, self.myPlayer.truck.money) self.assertTrue(isinstance(self.myPlayer.truck.addons, RabbitFoot)) def test_upgrade_beyond_allowable(self): self.myPlayer.truck.addons.level = 0 self.myPlayer.truck.money = 100000 expectedCash = self.myPlayer.truck.money - helpers.addTogetherDictValues(stats.GameStats.costs_and_effectiveness[ObjectType.rabbitFoot]['cost']) self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.assertEqual(self.myPlayer.truck.addons.level, RabbitFootLevel.level_three) self.assertEqual(self.myPlayer.truck.money, expectedCash) self.assertTrue(isinstance(self.myPlayer.truck.addons, RabbitFoot)) def test_no_money(self): self.myPlayer.truck.addons.level = 0 self.myPlayer.truck.money = 1 self.actionCont.upgrade_level(self.myPlayer, ObjectType.rabbitFoot) self.assertEqual(self.myPlayer.truck.addons.level, RabbitFootLevel.level_zero) self.assertEqual(self.myPlayer.truck.money, 1)
def setUp( self ): # This method is used to set up anything you wish to test prior to every test method below. self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController()
class TestUpgradeTires( unittest.TestCase ): # Your test class is a subclass of unittest.Testcase, this is important def setUp( self ): # This method is used to set up anything you wish to test prior to every test method below. self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController() def test_upgrade_sticky(self): self.myPlayer.truck.tires = TireType.tire_normal self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.tire_switch_cost self.actionCont.upgrade_level(self.myPlayer, ObjectType.tires, TireType.tire_sticky) self.assertEqual(self.myPlayer.truck.tires, TireType.tire_sticky) self.assertEqual(expectedCash, self.myPlayer.truck.money) def test_upgrade_econ(self): self.myPlayer.truck.tires = TireType.tire_normal self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.tire_switch_cost self.actionCont.upgrade_level(self.myPlayer, ObjectType.tires, TireType.tire_econ) self.assertEqual(self.myPlayer.truck.tires, TireType.tire_econ) def test_upgrade_normal(self): self.myPlayer.truck.tires = TireType.tire_sticky self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.tire_switch_cost self.actionCont.upgrade_level(self.myPlayer, ObjectType.tires, TireType.tire_normal) self.assertEqual(self.myPlayer.truck.tires, TireType.tire_normal) def test_same_type(self): self.myPlayer.truck.money = 10000 expectedCash = 10000 self.myPlayer.truck.tires = TireType.tire_normal self.actionCont.upgrade_level(self.myPlayer, ObjectType.tires, TireType.tire_normal) self.assertEqual(self.myPlayer.truck.tires, TireType.tire_normal) self.assertEqual(self.myPlayer.truck.money, expectedCash) def test_upgrade_beyond_allowable(self): self.myPlayer.truck.tires = TireType.tire_normal self.myPlayer.truck.money = 10000 self.actionCont.upgrade_level(self.myPlayer, ObjectType.tires, 10) self.assertEqual(self.myPlayer.truck.tires, TireType.tire_normal) self.assertEqual(self.myPlayer.truck.money, 10000) def test_no_money(self): self.myPlayer.truck.tires = TireType.tire_normal self.myPlayer.truck.money = 10 self.actionCont.upgrade_level(self.myPlayer, ObjectType.tires, TireType.tire_sticky) self.assertEqual(self.myPlayer.truck.tires, TireType.tire_normal) self.assertEqual(self.myPlayer.truck.money, 10)
class TestUpgradeTank(unittest.TestCase): # This method is used to set up anything you wish to test prior to every test method below. def setUp(self): self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController() # Test methods should always start with the word 'test' def test_upgrade_one_level(self): self.myPlayer.truck.body.level = 0 self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.costs_and_effectiveness[ ObjectType.tank]['cost'][1] self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.assertEqual(self.myPlayer.truck.body.level, TankLevel.level_one) self.assertEqual(expectedCash, self.myPlayer.truck.money) self.assertTrue(isinstance(self.myPlayer.truck.body, Tank)) def test_upgrade_two_level(self): self.myPlayer.truck.body.level = 0 expectedCash = 10000 - stats.GameStats.costs_and_effectiveness[ ObjectType. tank]['cost'][1] - stats.GameStats.costs_and_effectiveness[ ObjectType.tank]['cost'][2] self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.assertEqual(self.myPlayer.truck.body.level, TankLevel.level_two) self.assertEqual(expectedCash, self.myPlayer.truck.money) self.assertTrue(isinstance(self.myPlayer.truck.body, Tank)) def test_upgrade_beyond_allowable(self): self.myPlayer.truck.body = PoliceScanner() self.myPlayer.truck.body.level = 0 self.myPlayer.truck.money = 100000 expectedCash = self.myPlayer.truck.money - \ helpers.addTogetherDictValues( stats.GameStats.costs_and_effectiveness[ObjectType.tank]['cost']) self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.assertEqual(self.myPlayer.truck.body.level, TankLevel.level_three) self.assertEqual(self.myPlayer.truck.money, expectedCash) self.assertTrue(isinstance(self.myPlayer.truck.body, Tank)) def test_no_money(self): self.myPlayer.truck.body.level = 0 self.myPlayer.truck.money = 1 self.actionCont.upgrade_level(self.myPlayer, ObjectType.tank) self.assertEqual(self.myPlayer.truck.body.level, TankLevel.level_zero) self.assertEqual(self.myPlayer.truck.money, 1) def test_not_tank(self): self.myPlayer.truck.body = HeadLights() self.myPlayer.truck.money = 1 self.assertEqual( self.myPlayer.truck.body.current_gas, stats.GameStats.costs_and_effectiveness[ObjectType.tank] ['effectiveness'][0] * stats.GameStats.truck_starting_gas) self.assertEqual( self.myPlayer.truck.body.max_gas, stats.GameStats.costs_and_effectiveness[ObjectType.tank] ['effectiveness'][0] * stats.GameStats.truck_starting_gas) self.assertEqual(self.myPlayer.truck.money, 1) def test_not_tank_upgrade(self): self.myPlayer.truck.body = HeadLights() self.myPlayer.truck.money = 10000 expectedCash = 10000 - stats.GameStats.costs_and_effectiveness[ ObjectType.headlights]['cost'][1] self.actionCont.upgrade_level(self.myPlayer, ObjectType.headlights) self.assertEqual(self.myPlayer.truck.body.level, HeadlightLevel.level_one) self.assertEqual( self.myPlayer.truck.body.current_gas, stats.GameStats.costs_and_effectiveness[ObjectType.tank] ['effectiveness'][0] * stats.GameStats.truck_starting_gas) self.assertEqual( self.myPlayer.truck.body.max_gas, stats.GameStats.costs_and_effectiveness[ObjectType.tank] ['effectiveness'][0] * stats.GameStats.truck_starting_gas) self.assertEqual(self.myPlayer.truck.money, expectedCash)
class MasterController(Controller): def __init__(self): super().__init__() self.game_over = False self.turn = None self.current_world_data = None self.action_controller = ActionController() # Receives all clients for the purpose of giving them the objects they will control def give_clients_objects(self, client): client.truck = Truck() pass # Generator function. Given a key:value pair where the key is the identifier for the current world and the value is # the state of the world, returns the key that will give the appropriate world information def game_loop_logic(self, start=1): self.turn = start # Basic loop from 1 to max turns while True: # Wait until the next call to give the number yield self.turn # Increment the turn counter by 1 self.turn += 1 if self.turn > config.MAX_TICKS: break # Receives world data from the generated game log and is responsible for interpreting it def interpret_current_turn_data(self, client, world, turn): self.current_world_data = world random.seed(world["seed"]) # Receive a specific client and send them what they get per turn. Also obfuscates necessary objects. def client_turn_arguments(self, client, turn): # Add contracts available in city and current active contract to truck for access by client actions = Action() self.contract_status = check_contract_completion(client) contract_list = generate_contracts(client) self.action_controller.contract_list = contract_list client.truck.contract_list = copy.deepcopy(contract_list) client.action = actions # Create deep copies of all objects sent to the player #Truck obfuscation truckCopy = copy.deepcopy(client.truck) truckCopy.obfuscate() for contract in truckCopy.contract_list: contract.obfuscate() #Time copy to be given to player timeCopy = copy.deepcopy(client.time) # Obfuscate data in objects that that player should not be able to see args = (self.turn, actions, self.current_world_data, truckCopy, timeCopy) return args selected_action = ActionType.none selected_route = RoadType.none event = EventType.none # Perform the main logic that happens per turn def turn_logic(self, client, turn): new_action = self.action_controller.handle_actions(client) if not isinstance(new_action, int): self.selected_action = new_action[0] self.selected_route = new_action[1] self.event = new_action[2] self.caught_by_police = new_action[3] else: self.selected_action = new_action self.selected_route = RoadType.none self.event = EventType.none self.caught_by_police = False #client.time -= 10 if client.time <= 0: print("Game is ending because time has run out. Final score is " + str(client.truck.renown) + " ending on turn " + str(self.turn)) self.game_over = True if client.truck.health <= 0: print( "Game is ending because health has run out. Final score is " + str(client.truck.renown) + " ending on turn " + str(self.turn)) self.game_over = True if client.truck.body.current_gas <= 0: print("Game is ending because gas has run out. Final score is " + str(client.truck.renown) + " ending on turn " + str(self.turn)) self.game_over = True # Return serialized version of game def create_turn_log(self, clients, turn): data = dict() # Add things that should be thrown into the turn logs here data['Team Name'] = clients.team_name data['time'] = clients.time data['truck'] = clients.truck.to_json() data['selected_action'] = self.selected_action data['selected_route'] = self.selected_route data['event'] = self.event data['caught_by_police'] = self.caught_by_police data['contract_status'] = self.contract_status return data # Gather necessary data together in results file def return_final_results(self, client, turn): data = dict() # Determine results data['player'] = client.to_json() return data