Skip to content

Commit

Permalink
Fix bug when last used loadout is not in loadouts list.
Browse files Browse the repository at this point in the history
Also changed creation of the main classes to use dependency injection, for easier oversight
  • Loading branch information
stixes committed Jun 26, 2024
1 parent 760b80b commit 3b4b769
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
19 changes: 15 additions & 4 deletions hell_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@
import constants
import sys
from src.controller import Controller
from src.listener_pynput import PynputKeyListener
from src.loadouts import LoadoutManager
from src.model import Model
from src.settings import SettingsManager

def main():
# Initialize and run the app
model = Model()
# Settings manager handles app configuration (e.g. hotkeys)
settingsManager = SettingsManager()
# Loadout manager handles persistance of loadouts.
loadoutsManager = LoadoutManager()
# Model handles combining loadouts with stratagem macros
model = Model(loadoutsManager, settingsManager)
# Controller handles logic and communication between the model and view
controller = Controller(model)
# Key listener handles hotkey detection
keyListener = PynputKeyListener(controller)

Check failure on line 20 in hell_snake.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Ruff (F841)

hell_snake.py:20:5: F841 Local variable `keyListener` is assigned to but never used

# Initialize our presentation
if model.settingsManager.view_framework == constants.VIEW_PYQT5:
if settingsManager.view_framework == constants.VIEW_PYQT5:
from src.view.pyqt5.pyqt5 import PyQT5View
view = PyQT5View(controller)
else:
# If the view framework is not supported or recognized, print an error and exit.
print(f"Error: Unsupported view framework '{model.settingsManager.view_framework}'.", file=sys.stderr)
print(f"Error: Unsupported view framework '{settingsManager.view_framework}'.", file=sys.stderr)
print("Please configure a supported view framework in the settings.", file=sys.stderr)
sys.exit(1)

# Set the view in the controller so it can communicate with the view.
controller.set_view(view)

if __name__ == "__main__":
Expand Down
8 changes: 2 additions & 6 deletions src/controller.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import constants
import sys
from src.model import Model
from src.listener_pynput import PynputKeyListener

class Controller:
def __init__(self, model: Model):
self.model = model

self.loadouts_updated = False

# Ensure current loadout is valid
if self.model.settingsManager.currentLoadoutId not in self.model.loadoutsManager.loadouts:
self.model.settingsManager.currentLoadoutId = next(iter(self.model.loadoutsManager.loadouts))

self.keyListener = PynputKeyListener(self.model, self)

# Helper for view classes
def get_settings_manager(self):
return self.model.settingsManager
Expand Down Expand Up @@ -70,7 +66,7 @@ def set_armed(self, isArmed):

def cycle_loadout(self, offset):
# Get current active loadout ID and available loadout IDs
current_loadout_id = self.model.currentLoadoutId
current_loadout_id = self.model.settingsManager.currentLoadoutId
loadout_ids = list(self.model.loadoutsManager.loadouts.keys())

# Calculate the index of the next loadout
Expand Down
20 changes: 10 additions & 10 deletions src/listener_pynput.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from pynput import keyboard
import constants
from src.controller import Controller
from src.key_parser_pynput import PynputKeyparser
from src.model import Model

class PynputKeyListener:
def __init__(self, model : Model, controller):
self.model = model
def __init__(self, controller : Controller):
self.controller = controller
self.settings = controller.get_settings_manager()
self.getNextCallbacks = []

# Initialize the actual keyboard event listener
Expand All @@ -15,19 +15,19 @@ def __init__(self, model : Model, controller):
self.listener.start()

# Attach a listener to notify us of changes to settings
self.model.settingsManager.attach_change_listener(self._on_settings_changed)
self.settings.attach_change_listener(self._on_settings_changed)
self._on_settings_changed()

### Handlers ###

# Global arm handler
def handle_global_arm_press(self, key):
if (self.model.settingsManager.globalArmMode == constants.ARM_MODE_TOGGLE):
if (self.settings.globalArmMode == constants.ARM_MODE_TOGGLE):
self.controller.toggle_armed()
elif (self.model.settingsManager.globalArmMode == constants.ARM_MODE_PUSH and not self.model.isArmed):
elif (self.settings.globalArmMode == constants.ARM_MODE_PUSH and not self.model.isArmed):
self.controller.set_armed(True)
def handle_global_arm_release(self, key):
if (self.model.settingsManager.globalArmMode == constants.ARM_MODE_PUSH):
if (self.settings.globalArmMode == constants.ARM_MODE_PUSH):
self.controller.set_armed(False)

# Loadout browser
Expand All @@ -41,9 +41,9 @@ def handle_prev_loadout(self, key):
### Helpers ###
def _on_settings_changed(self):
''' This function is called when settings are updated, since we attach it as a listener in __init__ '''
self.globalArmKey = PynputKeyparser.parse_key(self.model.settingsManager.globalArmKey)
self.nextLoadoutKey = PynputKeyparser.parse_key(self.model.settingsManager.nextLoadoutKey)
self.prevLoadoutKey = PynputKeyparser.parse_key(self.model.settingsManager.prevLoadoutKey)
self.globalArmKey = PynputKeyparser.parse_key(self.settings.globalArmKey)
self.nextLoadoutKey = PynputKeyparser.parse_key(self.settings.nextLoadoutKey)
self.prevLoadoutKey = PynputKeyparser.parse_key(self.settings.prevLoadoutKey)
self.key_press_handlers = {
self.globalArmKey: self.handle_global_arm_press,
self.nextLoadoutKey: self.handle_next_loadout,
Expand Down
13 changes: 5 additions & 8 deletions src/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
from src.stratagem import Stratagem

class Model:
def __init__(self):
def __init__(self, loaddoutsManager: LoadoutManager, settingsManager: SettingsManager):
self.loadoutManger = loaddoutsManager
self.settingsManager = settingsManager

# Handle armed state
self.isArmed = False

# List of stratagems, and the respective macro definition
self.stratagems = self.loadStratagemsFromFile()

# Settings manager handles app configuration (e.g. hotkeys)
self.settingsManager = SettingsManager()

# Loadout manager handles persistance of loadouts.
self.loadoutsManager = LoadoutManager()
# a Loadout is a list of keys, and the respective stratagem to be activated

def loadStratagemsFromFile(self):
with open(constants.RESOURCE_PATH+"stratagems.json") as json_file:
tmp = json.load(json_file)
Expand Down

0 comments on commit 3b4b769

Please sign in to comment.