def __init__(self, quarter_length, interval_ms=10): if Constraints.check_constraint("quarter_length", quarter_length): self.quarter_length = quarter_length self.running = False self.remaining_time = timedelta(minutes=quarter_length) self.previous_update_time = None self.interval_ms = interval_ms self.scheduler = BackgroundScheduler()
def change_quarter_length(self, quarter_length): """Changes the quarter length. Args: quarter_length (int): Length of a quarter in minutes. Returns: None """ if Constraints.check_constraint("quarter_length", quarter_length): self.quarter_length = quarter_length
def testQuarterLength(self): print("Testing quarter length constraints...") for i in range(1, 100): self.assertTrue(Constraints.check_constraint("quarter_length", i)) for i in [-1, 1000]: self.assertRaises(ValueError, Constraints.check_constraint, "quarter_length", i) for i in ["asdf"]: self.assertRaises(TypeError, Constraints.check_constraint, "quarter_length", i)
def testDowns(self): print("Testing down constraints...") for i in [1, 2, 3, 4]: self.assertTrue(Constraints.check_constraint("down", i)) for i in [0, 5]: self.assertRaises(ValueError, Constraints.check_constraint, "down", i) for i in ["asdf"]: self.assertRaises(TypeError, Constraints.check_constraint, "down", i)
def testScore(self): print("Testing score constraints...") for i in range(0, 1000): self.assertTrue(Constraints.check_constraint("score", i)) for i in [-1, 1000]: self.assertRaises(ValueError, Constraints.check_constraint, "score", i) for i in ["asdf"]: self.assertRaises(TypeError, Constraints.check_constraint, "score", i)
def testBallOn(self): print("Testing ball_on constraints...") for i in range(1, 51): self.assertTrue(Constraints.check_constraint("ball_on", i)) for i in [0, 51]: self.assertRaises(ValueError, Constraints.check_constraint, "ball_on", i) for i in ["asdf"]: self.assertRaises(TypeError, Constraints.check_constraint, "ball_on", i)
def testDistance(self): print("Testing distance constraints...") for i in range(-1, 100): self.assertTrue(Constraints.check_constraint("distance", i)) for i in [-2, 100]: self.assertRaises(ValueError, Constraints.check_constraint, "distance", i) for i in ["asdf"]: self.assertRaises(TypeError, Constraints.check_constraint, "distance", i)
def testQuarter(self): print("Testing quarter constraints...") for i in [1, 2, 3, 4]: self.assertTrue(Constraints.check_constraint("quarter", i)) for i in [0, 5]: self.assertRaises(ValueError, Constraints.check_constraint, "quarter", i) for i in ["asdf"]: self.assertRaises(TypeError, Constraints.check_constraint, "quarter", i)
def modify_state_property(self, var_to_set, value, team=None): """Increase or decrease property. Checks if property to set is a team property. Args: var_to_set (str): The property you want to modify. value (int): The value you want to increase or decrease the property by. For decrementation, simply pass a negative value. team (int, optional): If the property is a team property, you can specify the team here. Returns: None """ if self.check_team_property(var_to_set, team): current_property_value = self.state[var_to_set][team] new_property_value = current_property_value + value if Constraints.check_constraint(var_to_set, new_property_value): self.state[var_to_set][team] = new_property_value else: current_property_value = self.state[var_to_set] new_property_value = current_property_value + value if Constraints.check_constraint(var_to_set, new_property_value): self.state[var_to_set] = new_property_value
def check_team_property(self, var_to_set, team): """Checks if the property to modify is team property and if the team number is valid. Args: var_to_set (str): The property to check. team (int): The team number. Returns: is_team_property (bool): True, if team property and valid team number, False if otherwise. Raises: ValueError: If team number is invalid. """ if var_to_set in Team.TEAM_PROPERTIES: if Constraints.check_constraint("team", team): return True else: return False
def set_state_property(self, var_to_set, value, team=None): """Sets the the given property to the value wanted. Args: var_to_set (str): The property to set. value (str): The value the property should be setted to. team (int): If the property to set is a team property, specify team. Defaults to None, if it's no team property. Returns: None Raises: ValueError: If var_to_set is no valid property or if value is out of valid range. TypeError: If value is not of the proper type """ if Constraints.check_constraint(var_to_set, value): if self.check_team_property(var_to_set, team): self.state[var_to_set][team] = value else: self.state[var_to_set] = value