Attempt to fix traceback on game exit with some older games.

This commit is contained in:
Storm Dragon
2025-03-14 23:10:14 -04:00
parent 2ad22ff1ae
commit be6dfdf53a
9 changed files with 483 additions and 457 deletions

View File

@ -17,25 +17,25 @@ from .config import localConfig, write_config, read_config
class Scoreboard:
"""Handles high score tracking with player names."""
def __init__(self, score=0, config_service=None, speech=None):
def __init__(self, score=0, configService=None, speech=None):
"""Initialize scoreboard.
Args:
score (int): Initial score (default: 0)
config_service (ConfigService): Config service (default: global instance)
configService (ConfigService): Config service (default: global instance)
speech (Speech): Speech system (default: global instance)
"""
self.config_service = config_service or ConfigService.get_instance()
self.configService = configService or ConfigService.get_instance()
self.speech = speech or Speech.get_instance()
self.current_score = score
self.high_scores = []
self.currentScore = score
self.highScores = []
# For backward compatibility
read_config()
try:
# Try to use config_service
self.config_service.local_config.add_section("scoreboard")
# Try to use configService
self.configService.local_config.add_section("scoreboard")
except:
# Fallback to old method
try:
@ -46,10 +46,10 @@ class Scoreboard:
# Load existing high scores
for i in range(1, 11):
try:
# Try to use config_service
score = self.config_service.local_config.getint("scoreboard", f"score_{i}")
name = self.config_service.local_config.get("scoreboard", f"name_{i}")
self.high_scores.append({
# Try to use configService
score = self.configService.local_config.getint("scoreboard", f"score_{i}")
name = self.configService.local_config.get("scoreboard", f"name_{i}")
self.highScores.append({
'name': name,
'score': score
})
@ -58,45 +58,45 @@ class Scoreboard:
try:
score = localConfig.getint("scoreboard", f"score_{i}")
name = localConfig.get("scoreboard", f"name_{i}")
self.high_scores.append({
self.highScores.append({
'name': name,
'score': score
})
except:
self.high_scores.append({
self.highScores.append({
'name': "Player",
'score': 0
})
# Sort high scores by score value in descending order
self.high_scores.sort(key=lambda x: x['score'], reverse=True)
self.highScores.sort(key=lambda x: x['score'], reverse=True)
def get_score(self):
"""Get current score."""
return self.current_score
return self.currentScore
def get_high_scores(self):
"""Get list of high scores."""
return self.high_scores
return self.highScores
def decrease_score(self, points=1):
"""Decrease the current score."""
self.current_score -= int(points)
self.currentScore -= int(points)
return self
def increase_score(self, points=1):
"""Increase the current score."""
self.current_score += int(points)
self.currentScore += int(points)
return self
def set_score(self, score):
"""Set the current score to a specific value."""
self.current_score = int(score)
self.currentScore = int(score)
return self
def reset_score(self):
"""Reset the current score to zero."""
self.current_score = 0
self.currentScore = 0
return self
def check_high_score(self):
@ -105,8 +105,8 @@ class Scoreboard:
Returns:
int: Position (1-10) if high score, None if not
"""
for i, entry in enumerate(self.high_scores):
if self.current_score > entry['score']:
for i, entry in enumerate(self.highScores):
if self.currentScore > entry['score']:
return i + 1
return None
@ -132,34 +132,34 @@ class Scoreboard:
name = "Player"
# Insert new score at correct position
self.high_scores.insert(position - 1, {
self.highScores.insert(position - 1, {
'name': name,
'score': self.current_score
'score': self.currentScore
})
# Keep only top 10
self.high_scores = self.high_scores[:10]
self.highScores = self.highScores[:10]
# Save to config - try both methods for maximum compatibility
try:
# Try new method first
for i, entry in enumerate(self.high_scores):
self.config_service.local_config.set("scoreboard", f"score_{i+1}", str(entry['score']))
self.config_service.local_config.set("scoreboard", f"name_{i+1}", entry['name'])
for i, entry in enumerate(self.highScores):
self.configService.local_config.set("scoreboard", f"score_{i+1}", str(entry['score']))
self.configService.local_config.set("scoreboard", f"name_{i+1}", entry['name'])
# Try to write with config_service
# Try to write with configService
try:
self.config_service.write_local_config()
self.configService.write_local_config()
except Exception as e:
# Fallback to old method if config_service fails
for i, entry in enumerate(self.high_scores):
# Fallback to old method if configService fails
for i, entry in enumerate(self.highScores):
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
localConfig.set("scoreboard", f"name_{i+1}", entry['name'])
write_config()
except Exception as e:
# If all else fails, try direct old method
for i, entry in enumerate(self.high_scores):
for i, entry in enumerate(self.highScores):
localConfig.set("scoreboard", f"score_{i+1}", str(entry['score']))
localConfig.set("scoreboard", f"name_{i+1}", entry['name'])
write_config()