From dc44f714f8d0d69510e06b922b095e824314b45f Mon Sep 17 00:00:00 2001 From: DasAmpharos Date: Sat, 20 Apr 2024 08:19:23 -0400 Subject: [PATCH] Prevent calibrating when 'actual' fields have a value of 0 (#103) Implement missing gen5 calibration logic --- eon_timer/timers/gen3/model.py | 10 ++--- eon_timer/timers/gen3/widget.py | 15 +++---- eon_timer/timers/gen4/model.py | 12 +++--- eon_timer/timers/gen4/widget.py | 17 ++++---- eon_timer/timers/gen5/model.py | 20 ++++----- eon_timer/timers/gen5/widget.py | 62 ++++++++++++++++++++++++--- eon_timer/util/properties/property.py | 17 ++++++++ 7 files changed, 110 insertions(+), 43 deletions(-) diff --git a/eon_timer/timers/gen3/model.py b/eon_timer/timers/gen3/model.py index bf765b4a..b239aaaf 100644 --- a/eon_timer/timers/gen3/model.py +++ b/eon_timer/timers/gen3/model.py @@ -3,7 +3,7 @@ from eon_timer.util.enum import EnhancedEnum from eon_timer.util.injector import component -from eon_timer.util.properties.property import Property +from eon_timer.util.properties.property import Property, IntProperty from eon_timer.util.properties.settings import Settings @@ -15,10 +15,10 @@ class Gen3Mode(EnhancedEnum, StrEnum): @component() class Gen3Model(Settings): mode = Property(Gen3Mode.STANDARD, value_type=str) - pre_timer = Property(5000) - target_frame = Property(1000) - calibration = Property(0) - frame_hit = Property(0, transient=True) + pre_timer = IntProperty(5000) + target_frame = IntProperty(1000) + calibration = IntProperty(0) + frame_hit = IntProperty(0, transient=True) @property @override diff --git a/eon_timer/timers/gen3/widget.py b/eon_timer/timers/gen3/widget.py index a1d8aabc..f4698799 100644 --- a/eon_timer/timers/gen3/widget.py +++ b/eon_timer/timers/gen3/widget.py @@ -5,7 +5,6 @@ from PySide6.QtCore import Qt, Signal from PySide6.QtWidgets import QGroupBox, QPushButton, QSizePolicy, QSpinBox -from eon_timer.app_state import AppState from eon_timer.timers import FrameTimer from eon_timer.util import const, pyside from eon_timer.util.injector import component @@ -109,13 +108,13 @@ def create_phases(self) -> list[int]: ) def calibrate(self): - calibration = self.model.calibration.get() - offset = self.frame_timer.calibrate( - self.model.target_frame.get(), - self.model.frame_hit.get() - ) - self.model.calibration.set(calibration + offset) - self.model.frame_hit.set(0) + if self.model.frame_hit.get() > 0: + offset = self.frame_timer.calibrate( + self.model.target_frame.get(), + self.model.frame_hit.get() + ) + self.model.calibration.add(offset) + self.model.frame_hit.set(0) def __on_mode_changed(self, event: PropertyChangeEvent[Gen3Mode]) -> None: self.set_visible(self.Field.SET_TARGET_FRAME, diff --git a/eon_timer/timers/gen4/model.py b/eon_timer/timers/gen4/model.py index 63840e74..e3d1ad0d 100644 --- a/eon_timer/timers/gen4/model.py +++ b/eon_timer/timers/gen4/model.py @@ -1,17 +1,17 @@ from typing import override from eon_timer.util.injector import component -from eon_timer.util.properties.property import Property +from eon_timer.util.properties.property import IntProperty from eon_timer.util.properties.settings import Settings @component() class Gen4Model(Settings): - target_delay = Property(600) - target_second = Property(50) - calibrated_delay = Property(500) - calibrated_second = Property(14) - delay_hit = Property(0, transient=True) + target_delay = IntProperty(600) + target_second = IntProperty(50) + calibrated_delay = IntProperty(500) + calibrated_second = IntProperty(14) + delay_hit = IntProperty(0, transient=True) @property @override diff --git a/eon_timer/timers/gen4/widget.py b/eon_timer/timers/gen4/widget.py index 303db223..414434e6 100644 --- a/eon_timer/timers/gen4/widget.py +++ b/eon_timer/timers/gen4/widget.py @@ -5,7 +5,6 @@ from PySide6.QtCore import Qt, Signal from PySide6.QtWidgets import QGroupBox, QSizePolicy, QSpinBox -from eon_timer.app_state import AppState from eon_timer.timers import Calibrator, DelayTimer from eon_timer.util import const, pyside from eon_timer.util.injector import component @@ -102,15 +101,15 @@ def create_phases(self) -> list[int]: ) def calibrate(self): - calibration = self.calibrator.to_delays( - self.delay_timer.calibrate( - self.model.target_delay.get(), - self.model.delay_hit.get() + if self.model.delay_hit.get() > 0: + calibration = self.calibrator.to_delays( + self.delay_timer.calibrate( + self.model.target_delay.get(), + self.model.delay_hit.get() + ) ) - ) - calibrated_delay = self.model.calibrated_delay.get() + calibration - self.model.calibrated_delay.set(calibrated_delay) - self.model.delay_hit.set(0) + self.model.calibrated_delay.add(calibration) + self.model.delay_hit.set(0) def get_calibration(self) -> int: return self.calibrator.create_calibration( diff --git a/eon_timer/timers/gen5/model.py b/eon_timer/timers/gen5/model.py index 6c78bc81..7d659db9 100644 --- a/eon_timer/timers/gen5/model.py +++ b/eon_timer/timers/gen5/model.py @@ -3,7 +3,7 @@ from eon_timer.util.enum import EnhancedEnum from eon_timer.util.injector import component -from eon_timer.util.properties.property import Property +from eon_timer.util.properties.property import Property, IntProperty from eon_timer.util.properties.settings import Settings @@ -17,15 +17,15 @@ class Gen5Mode(EnhancedEnum, StrEnum): @component() class Gen5Model(Settings): mode = Property(Gen5Mode.STANDARD, value_type=str) - calibration = Property(-95) - frame_calibration = Property(0) - entralink_calibration = Property(256) - target_delay = Property(1200) - target_second = Property(50) - target_advances = Property(100) - delay_hit = Property(0, transient=True) - second_hit = Property(0, transient=True) - advances_hit = Property(0, transient=True) + calibration = IntProperty(-95) + frame_calibration = IntProperty(0) + entralink_calibration = IntProperty(256) + target_delay = IntProperty(1200) + target_second = IntProperty(50) + target_advances = IntProperty(100) + delay_hit = IntProperty(0, transient=True) + second_hit = IntProperty(0, transient=True) + advances_hit = IntProperty(0, transient=True) @property @override diff --git a/eon_timer/timers/gen5/widget.py b/eon_timer/timers/gen5/widget.py index 3e594ea9..fdd5452d 100644 --- a/eon_timer/timers/gen5/widget.py +++ b/eon_timer/timers/gen5/widget.py @@ -5,15 +5,14 @@ from PySide6.QtCore import Qt, Signal from PySide6.QtWidgets import QSizePolicy, QSpinBox, QGroupBox -from eon_timer.app_state import AppState +from eon_timer.timers import Calibrator, DelayTimer, SecondTimer, EntralinkTimer, EnhancedEntralinkTimer from eon_timer.util import const, pyside from eon_timer.util.injector import component from eon_timer.util.properties import bindings from eon_timer.util.properties.property_change import PropertyChangeEvent -from eon_timer.util.pyside import EnumComboBox, ScrollWidget +from eon_timer.util.pyside import EnumComboBox from eon_timer.util.pyside.form import FormWidget, FormLayout from .model import Gen5Model, Gen5Mode -from eon_timer.timers import Calibrator, DelayTimer, SecondTimer, EntralinkTimer, EnhancedEntralinkTimer @component() @@ -36,7 +35,6 @@ class Field(FormWidget.Field): timer_changed: Final[Signal] = Signal() def __init__(self, - state: AppState, model: Gen5Model, calibrator: Calibrator, delay_timer: DelayTimer, @@ -44,7 +42,6 @@ def __init__(self, entralink_timer: EntralinkTimer, enhanced_entralink_timer: EnhancedEntralinkTimer) -> None: super().__init__(None) - self.state: Final[AppState] = state self.model: Final[Gen5Model] = model self.calibrator: Final[Calibrator] = calibrator self.delay_timer: Final[DelayTimer] = delay_timer @@ -174,3 +171,58 @@ def create_phases(self) -> list[int]: self.model.entralink_calibration.get(), self.model.frame_calibration.get() ) + + def calibrate(self): + if self.model.delay_hit.get() > 0: + mode = self.model.mode.get() + match mode: + case Gen5Mode.STANDARD: + self.model.calibration.add(self.calibrator.calibrate_to_delays(self.second_calibration)) + case Gen5Mode.C_GEAR: + self.model.calibration.add(self.calibrator.calibrate_to_delays(self.delay_calibration)) + case (Gen5Mode.ENTRALINK, + Gen5Mode.ENTRALINK_PLUS): + self.model.calibration.add(self.calibrator.calibrate_to_delays(self.second_calibration)) + self.model.entralink_calibration.add( + self.calibrator.calibrate_to_delays(self.entralink_calibration) + ) + if mode == Gen5Mode.ENTRALINK_PLUS: + self.model.frame_calibration.add(self.advances_calibration) + + self.model.delay_hit.set(0) + self.model.second_hit.set(0) + self.model.advances_hit.set(0) + + def __can_calibrate(self) -> bool: + match self.model.mode.get(): + case Gen5Mode.STANDARD: + return self.model.second_hit.get() > 0 + case Gen5Mode.C_GEAR: + return self.model.delay_hit.get() > 0 + case Gen5Mode.ENTRALINK: + return (self.model.delay_hit.get() > 0 and + self.model.second_hit.get() > 0) + case Gen5Mode.ENTRALINK_PLUS: + return (self.model.delay_hit.get() > 0 and + self.model.second_hit.get() > 0 and + self.model.advances_hit.get() > 0) + + @property + def delay_calibration(self) -> int: + return self.delay_timer.calibrate(self.model.target_delay.get(), + self.model.delay_hit.get()) + + @property + def second_calibration(self) -> int: + return self.second_timer.calibrate(self.model.target_second.get(), + self.model.second_hit.get()) + + @property + def entralink_calibration(self) -> int: + return self.entralink_timer.calibrate(self.model.target_delay.get(), + self.model.delay_hit.get() - self.second_calibration) + + @property + def advances_calibration(self) -> int: + return self.enhanced_entralink_timer.calibrate(self.model.target_advances.get(), + self.model.advances_hit.get()) diff --git a/eon_timer/util/properties/property.py b/eon_timer/util/properties/property.py index a0ab13f4..1a42f274 100644 --- a/eon_timer/util/properties/property.py +++ b/eon_timer/util/properties/property.py @@ -37,3 +37,20 @@ def set(self, new_value: T) -> None: @property def transient(self) -> bool: return self.__transient + + +class IntProperty(Property[int]): + def __init__(self, initial_value: int | None = None, transient: bool = False): + super().__init__(initial_value, int, transient) + + def add(self, value: int): + self.set(self.get() + value) + + def sub(self, value: int): + self.set(self.get() - value) + + def mul(self, value: int): + self.set(self.get() * value) + + def div(self, value: int): + self.set(int(self.get() / value))