fix scores not being overwritten correctly

This commit is contained in:
Anthony Samms
2025-11-07 15:53:06 -05:00
parent e15fc951ec
commit fc8dd75384
3 changed files with 34 additions and 39 deletions

0
scenes/dan/dan_result.py Normal file
View File

View File

@@ -130,16 +130,8 @@ class GameScreen(Screen):
self.player_1 = Player(self.tja, global_data.player_num, global_data.session_data[global_data.player_num-1].selected_difficulty, False, global_data.modifiers[0])
self.start_ms = (get_current_ms() - self.tja.metadata.offset*1000)
def write_score(self):
"""Write the score to the database"""
if self.tja is None:
return
if global_data.modifiers[global_data.player_num-1].auto:
return
with sqlite3.connect('scores.db') as con:
session_data = global_data.session_data[global_data.player_num-1]
cursor = con.cursor()
notes, branch_m, branch_e, branch_n = TJAParser.notes_to_position(TJAParser(self.tja.file_path), self.player_1.difficulty)
def get_song_hash(self, song: Path):
notes, branch_m, branch_e, branch_n = TJAParser.notes_to_position(TJAParser(song), self.player_1.difficulty)
if branch_m:
for branch in branch_m:
notes.play_notes.extend(branch.play_notes)
@@ -156,24 +148,29 @@ class GameScreen(Screen):
notes.draw_notes.extend(branch.draw_notes)
notes.bars.extend(branch.bars)
hash = self.tja.hash_note_data(notes)
return hash
def write_score(self):
"""Write the score to the database"""
if global_data.modifiers[global_data.player_num-1].auto:
return
with sqlite3.connect('scores.db') as con:
session_data = global_data.session_data[global_data.player_num-1]
cursor = con.cursor()
hash = self.get_song_hash(session_data.selected_song)
check_query = "SELECT score, clear FROM Scores WHERE hash = ? LIMIT 1"
cursor.execute(check_query, (hash,))
result = cursor.fetchone()
existing_score = result[0] if result is not None else None
existing_clear = result[1] if result is not None and len(result) > 1 and result[1] is not None else 0
# Determine clear value for this run: 2 for full combo (no bads), 1 for clear gauge, 0 otherwise
run_clear = 2 if session_data.result_bad == 0 else (1 if self.player_1.gauge.is_clear else 0)
best_clear = max(existing_clear, run_clear)
existing_crown = result[1] if result is not None and len(result) > 1 and result[1] is not None else 0
crown = 0
if session_data.result_bad and session_data.result_ok == 0:
crown = 3
elif session_data.result_bad == 0:
crown = 2
elif self.player_1.gauge.is_clear:
crown = 1
if result is None or (existing_score is not None and session_data.result_score > existing_score):
if result is None:
session_data.prev_score = 0
else:
if not isinstance(existing_score, int):
session_data.prev_score = 0
else:
session_data.prev_score = existing_score
insert_query = '''
INSERT OR REPLACE INTO Scores (hash, en_name, jp_name, diff, score, good, ok, bad, drumroll, combo, clear)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
@@ -182,13 +179,11 @@ class GameScreen(Screen):
self.tja.metadata.title.get('ja', ''), self.player_1.difficulty,
session_data.result_score, session_data.result_good,
session_data.result_ok, session_data.result_bad,
session_data.result_total_drumroll, session_data.result_max_combo, best_clear)
session_data.result_total_drumroll, session_data.result_max_combo, crown)
cursor.execute(insert_query, data)
con.commit()
else:
# Score didn't improve; if clear improved, update only the clear column to preserve best crown
if run_clear > existing_clear:
cursor.execute("UPDATE Scores SET clear = ? WHERE hash = ?", (run_clear, hash))
if result is None or (existing_crown is not None and crown > existing_crown):
cursor.execute("UPDATE Scores SET crown = ? WHERE hash = ?", (crown, hash))
con.commit()
def start_song(self, current_time):