Code cleanup and sound consolidation.
This commit is contained in:
72
services.py
72
services.py
@ -17,37 +17,37 @@ from .config import gamePath, globalPath, write_config, read_config
|
||||
|
||||
class ConfigService:
|
||||
"""Configuration management service."""
|
||||
|
||||
|
||||
_instance = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
"""Get or create the singleton instance."""
|
||||
if cls._instance is None:
|
||||
cls._instance = ConfigService()
|
||||
return cls._instance
|
||||
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize configuration parsers."""
|
||||
self.localConfig = configparser.ConfigParser()
|
||||
self.globalConfig = configparser.ConfigParser()
|
||||
self.gameTitle = None
|
||||
self.pathService = None
|
||||
|
||||
|
||||
def set_game_info(self, gameTitle, pathService):
|
||||
"""Set game information and initialize configs.
|
||||
|
||||
|
||||
Args:
|
||||
gameTitle (str): Title of the game
|
||||
pathService (PathService): Path service instance
|
||||
"""
|
||||
self.gameTitle = gameTitle
|
||||
self.pathService = pathService
|
||||
|
||||
|
||||
# Load existing configurations
|
||||
self.read_local_config()
|
||||
self.read_global_config()
|
||||
|
||||
|
||||
def read_local_config(self):
|
||||
"""Read local configuration from file."""
|
||||
try:
|
||||
@ -66,7 +66,7 @@ class ConfigService:
|
||||
self.localConfig.read_dict(globals().get('localConfig', {}))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def read_global_config(self):
|
||||
"""Read global configuration from file."""
|
||||
try:
|
||||
@ -85,7 +85,7 @@ class ConfigService:
|
||||
self.globalConfig.read_dict(globals().get('globalConfig', {}))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def write_local_config(self):
|
||||
"""Write local configuration to file."""
|
||||
try:
|
||||
@ -104,7 +104,7 @@ class ConfigService:
|
||||
write_config(False)
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to write local config: {e}")
|
||||
|
||||
|
||||
def write_global_config(self):
|
||||
"""Write global configuration to file."""
|
||||
try:
|
||||
@ -127,37 +127,37 @@ class ConfigService:
|
||||
|
||||
class VolumeService:
|
||||
"""Volume management service."""
|
||||
|
||||
|
||||
_instance = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
"""Get or create the singleton instance."""
|
||||
if cls._instance is None:
|
||||
cls._instance = VolumeService()
|
||||
return cls._instance
|
||||
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize volume settings."""
|
||||
self.bgmVolume = 0.75 # Default background music volume
|
||||
self.sfxVolume = 1.0 # Default sound effects volume
|
||||
self.masterVolume = 1.0 # Default master volume
|
||||
|
||||
|
||||
def adjust_master_volume(self, change, pygameMixer=None):
|
||||
"""Adjust the master volume for all sounds.
|
||||
|
||||
|
||||
Args:
|
||||
change (float): Amount to change volume by (positive or negative)
|
||||
pygameMixer: Optional pygame.mixer module for real-time updates
|
||||
"""
|
||||
self.masterVolume = max(0.0, min(1.0, self.masterVolume + change))
|
||||
|
||||
|
||||
# Update real-time audio if pygame mixer is provided
|
||||
if pygameMixer:
|
||||
# Update music volume
|
||||
if pygameMixer.music.get_busy():
|
||||
pygameMixer.music.set_volume(self.bgmVolume * self.masterVolume)
|
||||
|
||||
|
||||
# Update all sound channels
|
||||
for i in range(pygameMixer.get_num_channels()):
|
||||
channel = pygameMixer.Channel(i)
|
||||
@ -170,29 +170,29 @@ class VolumeService:
|
||||
# Stereo audio
|
||||
left, right = currentVolume
|
||||
channel.set_volume(left * self.masterVolume, right * self.masterVolume)
|
||||
|
||||
|
||||
def adjust_bgm_volume(self, change, pygameMixer=None):
|
||||
"""Adjust only the background music volume.
|
||||
|
||||
|
||||
Args:
|
||||
change (float): Amount to change volume by (positive or negative)
|
||||
pygameMixer: Optional pygame.mixer module for real-time updates
|
||||
"""
|
||||
self.bgmVolume = max(0.0, min(1.0, self.bgmVolume + change))
|
||||
|
||||
|
||||
# Update real-time audio if pygame mixer is provided
|
||||
if pygameMixer and pygameMixer.music.get_busy():
|
||||
pygameMixer.music.set_volume(self.bgmVolume * self.masterVolume)
|
||||
|
||||
|
||||
def adjust_sfx_volume(self, change, pygameMixer=None):
|
||||
"""Adjust volume for sound effects only.
|
||||
|
||||
|
||||
Args:
|
||||
change (float): Amount to change volume by (positive or negative)
|
||||
pygameMixer: Optional pygame.mixer module for real-time updates
|
||||
"""
|
||||
self.sfxVolume = max(0.0, min(1.0, self.sfxVolume + change))
|
||||
|
||||
|
||||
# Update real-time audio if pygame mixer is provided
|
||||
if pygameMixer:
|
||||
# Update all sound channels except reserved ones
|
||||
@ -208,18 +208,18 @@ class VolumeService:
|
||||
left, right = currentVolume
|
||||
channel.set_volume(left * self.sfxVolume * self.masterVolume,
|
||||
right * self.sfxVolume * self.masterVolume)
|
||||
|
||||
|
||||
def get_bgm_volume(self):
|
||||
"""Get the current BGM volume with master adjustment.
|
||||
|
||||
|
||||
Returns:
|
||||
float: Current adjusted BGM volume
|
||||
"""
|
||||
return self.bgmVolume * self.masterVolume
|
||||
|
||||
|
||||
def get_sfx_volume(self):
|
||||
"""Get the current SFX volume with master adjustment.
|
||||
|
||||
|
||||
Returns:
|
||||
float: Current adjusted SFX volume
|
||||
"""
|
||||
@ -228,32 +228,32 @@ class VolumeService:
|
||||
|
||||
class PathService:
|
||||
"""Path management service."""
|
||||
|
||||
|
||||
_instance = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
"""Get or create the singleton instance."""
|
||||
if cls._instance is None:
|
||||
cls._instance = PathService()
|
||||
return cls._instance
|
||||
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize path variables."""
|
||||
self.globalPath = None
|
||||
self.gamePath = None
|
||||
self.gameName = None
|
||||
|
||||
|
||||
# Try to initialize from global variables for backward compatibility
|
||||
global gamePath, globalPath
|
||||
if gamePath:
|
||||
self.gamePath = gamePath
|
||||
if globalPath:
|
||||
self.globalPath = globalPath
|
||||
|
||||
|
||||
def initialize(self, gameTitle):
|
||||
"""Initialize paths for a game.
|
||||
|
||||
|
||||
Args:
|
||||
gameTitle (str): Title of the game
|
||||
"""
|
||||
@ -261,14 +261,14 @@ class PathService:
|
||||
self.globalPath = os.path.join(BaseDirectory.xdg_config_home, "storm-games")
|
||||
self.gamePath = os.path.join(self.globalPath,
|
||||
str.lower(str.replace(gameTitle, " ", "-")))
|
||||
|
||||
|
||||
# Create game directory if it doesn't exist
|
||||
if not os.path.exists(self.gamePath):
|
||||
os.makedirs(self.gamePath)
|
||||
|
||||
|
||||
# Update global variables for backward compatibility
|
||||
global gamePath, globalPath
|
||||
gamePath = self.gamePath
|
||||
globalPath = self.globalPath
|
||||
|
||||
|
||||
return self
|
||||
|
Reference in New Issue
Block a user