Moved the high score stuff to the scoreboard because it makes more sense for it to be there even though it is used in the menu.

This commit is contained in:
Storm Dragon
2025-03-15 18:18:22 -04:00
parent 3f8385599b
commit 1dc0ac2a7f
3 changed files with 114 additions and 256 deletions

View File

@ -8,8 +8,10 @@ Provides functionality for:
"""
import time
from .services import ConfigService
import os
from .services import ConfigService, PathService
from .speech import Speech
from .display import display_text
# For backward compatibility
from .config import localConfig, write_config, read_config
@ -71,35 +73,35 @@ class Scoreboard:
# Sort high scores by score value in descending order
self.highScores.sort(key=lambda x: x['score'], reverse=True)
def get_score(self):
def getScore(self):
"""Get current score."""
return self.currentScore
def get_high_scores(self):
def getHighScores(self):
"""Get list of high scores."""
return self.highScores
def decrease_score(self, points=1):
def decreaseScore(self, points=1):
"""Decrease the current score."""
self.currentScore -= int(points)
return self
def increase_score(self, points=1):
def increaseScore(self, points=1):
"""Increase the current score."""
self.currentScore += int(points)
return self
def set_score(self, score):
def setScore(self, score):
"""Set the current score to a specific value."""
self.currentScore = int(score)
return self
def reset_score(self):
def resetScore(self):
"""Reset the current score to zero."""
self.currentScore = 0
return self
def check_high_score(self):
def checkHighScore(self):
"""Check if current score qualifies as a high score.
Returns:
@ -110,7 +112,7 @@ class Scoreboard:
return i + 1
return None
def add_high_score(self, name=None):
def addHighScore(self, name=None):
"""Add current score to high scores if it qualifies.
Args:
@ -119,7 +121,7 @@ class Scoreboard:
Returns:
bool: True if score was added, False if not
"""
position = self.check_high_score()
position = self.checkHighScore()
if position is None:
return False
@ -174,3 +176,95 @@ class Scoreboard:
time.sleep(1)
return True
@staticmethod
def hasHighScores():
"""Check if the current game has any high scores.
Returns:
bool: True if at least one high score exists, False otherwise
"""
try:
# Get PathService to access game name
pathService = PathService.get_instance()
gameName = pathService.gameName
# Ensure path service is properly initialized
if not pathService.gamePath:
pathService.initialize(gameName)
# Get the config file path
configPath = os.path.join(pathService.gamePath, "config.ini")
# If config file doesn't exist, there are no scores
if not os.path.exists(configPath):
return False
# Ensure config service is properly connected to path service
configService = ConfigService.get_instance()
configService.set_game_info(gameName, pathService)
# Create scoreboard using the properly initialized services
board = Scoreboard(0, configService)
# Force a read of local config to ensure fresh data
configService.read_local_config()
# Get high scores
scores = board.getHighScores()
# Check if any score is greater than zero
return any(score['score'] > 0 for score in scores)
except Exception as e:
print(f"Error checking high scores: {e}")
return False
@staticmethod
def displayHighScores():
"""Display high scores for the current game.
Reads the high scores from Scoreboard class.
Shows the game name at the top followed by the available scores.
"""
try:
# Get PathService to access game name
pathService = PathService.get_instance()
gameName = pathService.gameName
# Ensure path service is properly initialized
if not pathService.gamePath:
pathService.initialize(gameName)
# Ensure config service is properly connected to path service
configService = ConfigService.get_instance()
configService.set_game_info(gameName, pathService)
# Create scoreboard using the properly initialized services
board = Scoreboard(0, configService)
# Force a read of local config to ensure fresh data
configService.read_local_config()
# Get high scores
scores = board.getHighScores()
# Filter out scores with zero points
validScores = [score for score in scores if score['score'] > 0]
# Prepare the lines to display
lines = [f"High Scores for {gameName}:"]
# Add scores to the display list
if validScores:
for i, entry in enumerate(validScores, 1):
scoreStr = f"{i}. {entry['name']}: {entry['score']}"
lines.append(scoreStr)
else:
lines.append("No high scores yet.")
# Display the high scores
display_text(lines)
except Exception as e:
print(f"Error displaying high scores: {e}")
info = ["Could not display high scores."]
display_text(info)