def setUp(self): # Setup self.test_disaster_controller = DisasterController() self.test_effort_controller = EffortController() self.player = Player() self.player.action = Action() self.player.city = City()
def test_upgrade(self): # Setup player = Player() player.action = Action() player.city = City() player.city.population = 1000 fire_sensor = player.city.sensors[SensorType.fire] self.assertEqual(fire_sensor.level, SensorLevel.level_zero) player.action.add_effort(fire_sensor, 50) self.test_effort_controller.handle_actions(player) self.assertEqual(fire_sensor.level, SensorLevel.level_one) player.action = Action() player.action.add_effort(fire_sensor, 100) self.test_effort_controller.handle_actions(player) self.assertEqual(fire_sensor.level, SensorLevel.level_two) player.action = Action() player.action.add_effort(fire_sensor, 500) self.test_effort_controller.handle_actions(player) self.assertEqual(fire_sensor.level, SensorLevel.level_three)
def setUp(self): self.myPlayer = Player(12, "Sean") self.myPlayer.truck.money = 10000 self.actionCont = ActionController()
def boot(self): # Insert path of where clients are expected to be inside where python will look current_dir = os.getcwd() sys.path.insert(0, current_dir) sys.path.insert(0, f'{current_dir}/{CLIENT_DIRECTORY}') # Find and load clients in for filename in os.listdir(CLIENT_DIRECTORY): filename = filename.replace('.py', '') # Filter out files that do not contain CLIENT_KEYWORD in their filename (located in config) if CLIENT_KEYWORD.upper() not in filename.upper(): continue # Filter out folders if os.path.isdir(os.path.join(CLIENT_DIRECTORY, filename)): continue # Otherwise, instantiate the player player = Player() self.clients.append(player) # Verify client isn't using invalid imports or opening anything imports, opening = verify_code(filename + '.py') if len(imports) != 0: player.functional = False player.error = ImportError( f'Player has attempted illegal imports: {imports}') if opening: player.functional = False player.error = PermissionError( f'Player is using "open" which is forbidden.') # Import client's code im = importlib.import_module(f'{filename}', CLIENT_DIRECTORY) # Attempt creation of the client object obj = None try: obj = im.Client() except Exception: player.functional = False player.error = traceback.format_exc() player.code = obj # Retrieve team name thr = CommunicationThread(player.code.team_name, list(), str) thr.start() thr.join(0.01) # Shouldn't take long to get a string if thr.is_alive(): player.functional = False player.error = TimeoutError( 'Client failed to provide a team name in time.') if thr.error is not None: player.functional = False player.error = thr.error player.team_name = thr.retrieve_value() # Verify correct number of clients have connected to start func_clients = [client for client in self.clients if client.functional] client_num_correct = verify_num_clients(func_clients, SET_NUMBER_OF_CLIENTS_START, MIN_CLIENTS_START, MAX_CLIENTS_START) if client_num_correct is not None: self.shutdown(source='Invalid number of clients found') # Finally, request master controller to establish clients with basic objects if SET_NUMBER_OF_CLIENTS_START == 1: self.master_controller.give_clients_objects(self.clients[0]) else: self.master_controller.give_clients_objects(self.clients)
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_to_and_back(self): bruh = copy.deepcopy(self.myPlayer.truck) tojsn = self.myPlayer.truck.to_json() trk = Player(2131, 'John') trk.truck.from_json(tojsn) self.assertEqual(bruh.to_json(), trk.truck.to_json())
def deserialize(self, data): objs = [] for serialized_obj in data: obj_type = serialized_obj['object_type'] if obj_type == ObjectType.player: obj = Player() obj.from_json(serialized_obj) objs.append(obj) elif obj_type == ObjectType.disaster: dis_type = serialized_obj['type'] obj = None if dis_type == DisasterType.earthquake: obj = Earthquake() elif dis_type == DisasterType.fire: obj = Fire() elif dis_type == DisasterType.blizzard: obj = Blizzard() elif dis_type == DisasterType.monster: obj = Monster() elif dis_type == DisasterType.tornado: obj = Tornado() elif dis_type == DisasterType.ufo: obj = Ufo() obj.from_json(serialized_obj) objs.append(obj) elif obj_type == ObjectType.action: obj = Action() obj.from_json(serialized_obj) objs.append(obj) else: print(obj_type) return objs