Skip to content

Commit

Permalink
Merge branch 'bugfix/abstract_methods_not_implemented_in_HackingEffec…
Browse files Browse the repository at this point in the history
…ts' into development
  • Loading branch information
iav-DaBi committed Dec 2, 2024
2 parents 44b4591 + 409422a commit ed9812b
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/CyberSecurityManager/CyberSecurityManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
logger = logging.getLogger(__name__)


def _set_scenarios() -> list[dict[str, Any]]:
def _set_scenarios() -> list[dict[str, str|Any]]:
scenario0 = {"id": "0",
"name": "normal",
"description": "no hacking",
Expand All @@ -46,8 +46,8 @@ def _set_scenarios() -> list[dict[str, Any]]:
"effect": HackedNoSafetyModule}

scenario5 = {"id": "5",
"name": "U-turn",
"description": "the car perfoms a U-turn",
"name": "O-turn",
"description": "the car perfoms a O-turn",
"effect": HackedSporadicOTurn}

scenario6 = {"id": "6",
Expand Down
5 changes: 3 additions & 2 deletions src/DataModel/Effects/HackingEffects/CleanHackedEffect.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ def on_start(self, vehicle: Vehicle) -> bool:
vehicle.hacking_scenario = self._scenario
return True

def on_end(self, vehicle: 'Vehicle') -> None:
return
def on_end(self, vehicle: 'Vehicle') -> bool:
vehicle.hacking_scenario = "0"
return True
17 changes: 15 additions & 2 deletions src/DataModel/Effects/HackingEffects/HackedEffect.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ def __init__(self, scenario: str):
super().__init__()
self._scenario = scenario

def identify(self) -> VehicleEffectIdentification:
return VehicleEffectIdentification.NO_EFFECT

def can_be_applied(self, vehicle: 'Vehicle') -> bool:
return False

def on_start(self, vehicle: Vehicle) -> bool:
vehicle.hacking_scenario = self._scenario
return True

def on_end(self, vehicle: 'Vehicle') -> bool:
vehicle.hacking_scenario = "0"
return True

def effect_should_end(self, vehicle: Vehicle) -> bool:
return False

def conflicts_with(self) -> list[VehicleEffectIdentification]:
return [VehicleEffectIdentification.HACKING_PROTECTION]

def remove_other_hacking_effects(self, vehicle: Vehicle):
def remove_other_hacking_effects(self, vehicle: Vehicle) -> None:
"""
Removes all hacking effects so a new one can be applied without problems
"""
Expand All @@ -30,5 +40,8 @@ def remove_other_hacking_effects(self, vehicle: Vehicle):
effect_type = effect.identify()
if effect_type == VehicleEffectIdentification.HACKED_NO_SAFETY_MODULE or \
effect_type == VehicleEffectIdentification.HACKED_NO_DRIVING or \
effect_type == VehicleEffectIdentification.HACKED_REDUCED_SPEED:
effect_type == VehicleEffectIdentification.HACKED_REDUCED_SPEED or \
effect_type == VehicleEffectIdentification.HACKED_SPORADIC_O_TURNS or \
effect_type == VehicleEffectIdentification.HACKED_INVERTED_USER_INPUT:
vehicle.remove_effect(effect)
return
16 changes: 14 additions & 2 deletions src/DataModel/Effects/HackingEffects/HackedInvertedUserInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@ class HackedInvertedUserInput(HackedEffect):
def identify(self) -> VehicleEffectIdentification:
return VehicleEffectIdentification.HACKED_INVERTED_USER_INPUT

def on_start(self, vehicle: 'Vehicle') -> None:
def can_be_applied(self, vehicle: Vehicle) -> bool:
_ = vehicle
return True

def conflicts_with(self) -> list[VehicleEffectIdentification]:
conflict_list: list[VehicleEffectIdentification] = super().conflicts_with()
conflict_list.append(VehicleEffectIdentification.HACKED_NO_DRIVING)
return conflict_list

def on_start(self, vehicle: 'Vehicle') -> bool:
super().on_start(vehicle)
self.remove_other_hacking_effects(vehicle)
vehicle.speed_factor = -1
vehicle.speed_offset = 119
vehicle.lange_change_inverted = True
vehicle.turn_blocked = True
return True

def on_end(self, vehicle: 'Vehicle') -> None:
def on_end(self, vehicle: 'Vehicle') -> bool:
super().on_end(vehicle)
vehicle.speed_factor = 1
vehicle.speed_offset = 0
vehicle.lange_change_inverted = False
vehicle.turn_blocked = False
return True
8 changes: 7 additions & 1 deletion src/DataModel/Effects/HackingEffects/HackedNoDriving.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ def can_be_applied(self, vehicle: Vehicle) -> bool:
_ = vehicle
return True

def conflicts_with(self) -> list[VehicleEffectIdentification]:
conflict_list: list[VehicleEffectIdentification] = super().conflicts_with()
return conflict_list

def on_start(self, vehicle: 'Vehicle') -> bool:
super().on_start(vehicle)
self.remove_other_hacking_effects(vehicle)
vehicle.speed_factor = 0.0
return True

def on_end(self, vehicle: 'Vehicle') -> None:
def on_end(self, vehicle: 'Vehicle') -> bool:
super().on_end(vehicle)
vehicle.speed_factor = 1
return True
8 changes: 7 additions & 1 deletion src/DataModel/Effects/HackingEffects/HackedNoSafetyModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ def can_be_applied(self, vehicle: Vehicle) -> bool:
_ = vehicle
return True

def conflicts_with(self) -> list[VehicleEffectIdentification]:
conflict_list: list[VehicleEffectIdentification] = super().conflicts_with()
return conflict_list

def on_start(self, vehicle: Vehicle) -> bool:
super().on_start(vehicle)
self.remove_other_hacking_effects(vehicle)
vehicle.speed_factor = 1.5
vehicle.set_safemode(False)
return True

def on_end(self, vehicle: Vehicle) -> None:
def on_end(self, vehicle: Vehicle) -> bool:
super().on_end(vehicle)
vehicle.speed_factor = 1
vehicle.set_safemode(True)
return True
10 changes: 8 additions & 2 deletions src/DataModel/Effects/HackingEffects/HackedReducedSpeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ def identify(self) -> VehicleEffectIdentification:
def can_be_applied(self, vehicle: Vehicle) -> bool:
_ = vehicle
return True

def conflicts_with(self) -> list[VehicleEffectIdentification]:
conflict_list: list[VehicleEffectIdentification] = super().conflicts_with()
conflict_list.append(VehicleEffectIdentification.HACKED_NO_DRIVING)
return conflict_list

def on_start(self, vehicle: Vehicle) -> bool:
super().on_start(vehicle)
self.remove_other_hacking_effects(vehicle)
vehicle.speed_factor = 0.3
return True

def on_end(self, vehicle: 'Vehicle') -> None:
def on_end(self, vehicle: 'Vehicle') -> bool:
super().on_end(vehicle)
vehicle.speed_factor = 1
return
return True
20 changes: 16 additions & 4 deletions src/DataModel/Effects/HackingEffects/HackedSporadicOTurn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@ class HackedSporadicOTurn(HackedEffect):
def identify(self) -> VehicleEffectIdentification:
return VehicleEffectIdentification.HACKED_SPORADIC_O_TURNS

def on_start(self, vehicle: 'Vehicle') -> None:
def can_be_applied(self, vehicle: Vehicle) -> bool:
_ = vehicle
return True

def conflicts_with(self) -> list[VehicleEffectIdentification]:
conflict_list: list[VehicleEffectIdentification] = super().conflicts_with()
conflict_list.append(VehicleEffectIdentification.HACKED_NO_DRIVING)
return conflict_list

def on_start(self, vehicle: 'Vehicle') -> bool:
super().on_start(vehicle)
self.remove_other_hacking_effects(vehicle)
self.task = asyncio.create_task(self.perform_uturns(vehicle)) # create an async task
return True

def on_end(self, vehicle: 'Vehicle') -> bool:
super().on_end(vehicle)
self.task.cancel() # cancel the task
return True

async def perform_uturns(self, vehicle: 'Vehicle') -> None:
while True:
vehicle.request_uturn() # first u-turn of the pair
await asyncio.sleep(2) # delay 2 seconds
vehicle.request_uturn() # second u-turn of the pair
await asyncio.sleep(random.uniform(4, 12)) # random delay before the next pair

def on_end(self, vehicle: 'Vehicle') -> None:
self.task.cancel() # cancel the task
2 changes: 1 addition & 1 deletion src/DataModel/Effects/VehicleEffect.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def on_start(self, vehicle: 'Vehicle') -> bool:
raise NotImplementedError

@abstractmethod
def on_end(self, vehicle: 'Vehicle') -> None:
def on_end(self, vehicle: 'Vehicle') -> bool:
"""
Runs when removed from a vehicle. Should be used for cleaning up
"""
Expand Down
1 change: 1 addition & 0 deletions src/DataModel/Effects/VehicleEffectList.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class VehicleEffectIdentification(Enum):
# todo this list should be resorted
NO_EFFECT = 0,
HACKING_PROTECTION = 1,
HACKED_REDUCED_SPEED = 2,
HACKED_NO_DRIVING = 3,
Expand Down
21 changes: 12 additions & 9 deletions src/DataModel/Vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def hacking_scenario(self, value: str) -> None:
self._active_hacking_scenario = value
self._on_driving_data_change()

def remove_hacking_scenario(self) -> None:
self._active_hacking_scenario = "0"

@property
def speed_factor(self) -> float:
return self._speed_factor
Expand Down Expand Up @@ -332,19 +335,19 @@ def apply_effect(self, new_effect: VehicleEffect) -> None:
if effect.identify() == new_effect.identify() or effect.identify() in new_effect.conflicts_with():
return

if not new_effect.can_be_applied(self):
return

logger.info("Car %s now has the effect %s", self.vehicle_id, str(new_effect.identify()))
self._effects.append(new_effect)
if new_effect.on_start(self) is True:
self._on_item_data_change('hacking_protection')
if new_effect.can_be_applied(self):
self.hacking_scenario = str(new_effect.identify())
self._effects.append(new_effect)
logger.info("Car %s now has the effect %s", self.vehicle_id, str(new_effect.identify()))
if new_effect.on_start(self) is True:
self._on_item_data_change('hacking_protection')

return

def remove_effect(self, effect: VehicleEffect):
effect.on_end(self)
self._effects.remove(effect)
if effect.on_end(self) is True:
self.hacking_scenario = "0"
self._effects.remove(effect)

async def _check_effect_removal(self):
while True:
Expand Down

0 comments on commit ed9812b

Please sign in to comment.