From 0bd709f49e8f6d3fea39ce4887e1d913dc361ecc Mon Sep 17 00:00:00 2001 From: 283375 Date: Sat, 23 Sep 2023 14:53:04 +0800 Subject: [PATCH] impr: fallback when no chart data in database --- ui/extends/components/ocrQueue.py | 8 ++- ui/extends/shared/delegates/chartDelegate.py | 7 ++- ui/extends/shared/delegates/scoreDelegate.py | 8 ++- ui/extends/shared/models/tables/score.py | 62 +++++++++++++------- ui/extends/tabs/tabOcr/tabOcr_B30.py | 10 +++- ui/extends/tabs/tabOcr/tabOcr_Device.py | 7 +++ 6 files changed, 77 insertions(+), 25 deletions(-) diff --git a/ui/extends/components/ocrQueue.py b/ui/extends/components/ocrQueue.py index ce608df..4cbfd9e 100644 --- a/ui/extends/components/ocrQueue.py +++ b/ui/extends/components/ocrQueue.py @@ -263,7 +263,13 @@ class OcrQueueModel(QAbstractListModel): index = self.index(row, 0) chart = index.data(self.ChartRole) score = index.data(self.ScoreRole) - if isinstance(chart, Chart) and isinstance(score, Score): + if ( + isinstance(chart, Chart) + and isinstance(score, Score) + and chart.notes + and score.pure + and score.far + ): scoreRange = calculate_score_range(chart.notes, score.pure, score.far) scoreValidateOk = scoreRange[0] <= score.score <= scoreRange[1] self.setData(index, scoreValidateOk, self.ScoreValidateOkRole) diff --git a/ui/extends/shared/delegates/chartDelegate.py b/ui/extends/shared/delegates/chartDelegate.py index 02bc60f..5e604fe 100644 --- a/ui/extends/shared/delegates/chartDelegate.py +++ b/ui/extends/shared/delegates/chartDelegate.py @@ -93,13 +93,18 @@ class ChartDelegate(TextSegmentDelegate): [{self.TextRole: "Chart Invalid", self.ColorRole: QColor("#ff0000")}] ] + chartConstantString = ( + f"{chart.constant / 10:.1f}" + if chart.constant is not None and chart.constant > 0 + else "?" + ) return [ [ {self.TextRole: f"{chart.title}"}, ], [ { - self.TextRole: f"{rating_class_to_text(chart.rating_class)} {chart.constant / 10:.1f}", + self.TextRole: f"{rating_class_to_text(chart.rating_class)} {chartConstantString}", self.ColorRole: self.RatingClassColors[chart.rating_class], }, ], diff --git a/ui/extends/shared/delegates/scoreDelegate.py b/ui/extends/shared/delegates/scoreDelegate.py index 3cef3da..2560404 100644 --- a/ui/extends/shared/delegates/scoreDelegate.py +++ b/ui/extends/shared/delegates/scoreDelegate.py @@ -96,7 +96,13 @@ class ScoreDelegate(TextSegmentDelegate): score = self.getScore(index) chart = self.getChart(index) - if isinstance(score, Score) and isinstance(chart, Chart): + if ( + isinstance(score, Score) + and isinstance(chart, Chart) + and chart.notes is not None + and score.pure is not None + and score.far is not None + ): scoreRange = calculate_score_range(chart.notes, score.pure, score.far) return scoreRange[0] <= score.score <= scoreRange[1] diff --git a/ui/extends/shared/models/tables/score.py b/ui/extends/shared/models/tables/score.py index 3f90adb..f196a90 100644 --- a/ui/extends/shared/models/tables/score.py +++ b/ui/extends/shared/models/tables/score.py @@ -29,12 +29,26 @@ class DbScoreTableModel(DbTableModel): def syncDb(self): newScores = self._db.get_scores() newScores = sorted(newScores, key=lambda x: x.id) - newCharts = [ - self._db.get_chart(score.song_id, score.rating_class) for score in newScores - ] + newCharts = [] + for score in newScores: + dbChart = self._db.get_chart(score.song_id, score.rating_class) + newCharts.append( + dbChart + if isinstance(dbChart, Chart) + else Chart( + song_id=score.song_id, + rating_class=score.rating_class, + title=score.song_id, + set="unknown", + ) + ) newPtts = [] for chart, score in zip(newCharts, newScores): - if isinstance(chart, Chart) and isinstance(score, Score): + if ( + isinstance(chart, Chart) + and chart.constant is not None + and isinstance(score, Score) + ): newPtts.append(calculate_play_rating(chart.constant / 10, score.score)) else: newPtts.append(None) @@ -103,10 +117,11 @@ class DbScoreTableModel(DbTableModel): elif index.column() == 2 and role in [self.ChartRole, self.ScoreRole]: return self.__items[index.row()][role] elif index.column() == 3: + potential = self.__items[index.row()][self.PttRole] if role == Qt.ItemDataRole.DisplayRole: - return f"{self.__items[index.row()][self.PttRole]:.3f}" + return f"{potential:.3f}" if potential is not None else "-" elif role == self.PttRole: - return self.__items[index.row()][self.PttRole] + return potential return None def setData(self, index, value, role): @@ -165,30 +180,35 @@ class DbScoreTableSortFilterProxyModel(QSortFilterProxyModel): Sort_C2_ScoreRole = Qt.ItemDataRole.UserRole + 75 Sort_C2_TimeRole = Qt.ItemDataRole.UserRole + 76 - def lessThan(self, source_left, source_right) -> bool: - if source_left.column() != source_right.column(): + def lessThan(self, sourceLeft, sourceRight) -> bool: + if sourceLeft.column() != sourceRight.column(): return - column = source_left.column() + column = sourceLeft.column() if column == 0: - return source_left.data(DbScoreTableModel.IdRole) < source_right.data( + return sourceLeft.data(DbScoreTableModel.IdRole) < sourceRight.data( DbScoreTableModel.IdRole ) elif column == 2: - score_left = source_left.data(DbScoreTableModel.ScoreRole) - score_right = source_right.data(DbScoreTableModel.ScoreRole) - if isinstance(score_left, Score) and isinstance(score_right, Score): + scoreLeft = sourceLeft.data(DbScoreTableModel.ScoreRole) + scoreRight = sourceRight.data(DbScoreTableModel.ScoreRole) + if isinstance(scoreLeft, Score) and isinstance(scoreRight, Score): if self.sortRole() == self.Sort_C2_ScoreRole: - return score_left.score < score_right.score + return scoreLeft.score < scoreRight.score elif self.sortRole() == self.Sort_C2_TimeRole: - if score_left.date and score_right.date: - return score_left.date < score_right.date - elif score_left.date: + if scoreLeft.date and scoreRight.date: + return scoreLeft.date < scoreRight.date + elif scoreLeft.date: return False else: return True elif column == 3: - return source_left.data(DbScoreTableModel.PttRole) < source_right.data( - DbScoreTableModel.PttRole - ) - return super().lessThan(source_left, source_right) + pttLeft = sourceLeft.data(DbScoreTableModel.PttRole) + pttRight = sourceRight.data(DbScoreTableModel.PttRole) + if pttLeft and pttRight: + return pttLeft < pttRight + elif pttLeft: + return False + else: + return True + return super().lessThan(sourceLeft, sourceRight) diff --git a/ui/extends/tabs/tabOcr/tabOcr_B30.py b/ui/extends/tabs/tabOcr/tabOcr_B30.py index 55b5168..04861c7 100644 --- a/ui/extends/tabs/tabOcr/tabOcr_B30.py +++ b/ui/extends/tabs/tabOcr/tabOcr_B30.py @@ -1,7 +1,7 @@ import logging from arcaea_offline.database import Database -from arcaea_offline.models import Score +from arcaea_offline.models import Chart, Score from arcaea_offline_ocr.b30.chieri.v4.ocr import ChieriBotV4Ocr from arcaea_offline_ocr.b30.shared import B30OcrResultItem from PySide6.QtGui import QImage @@ -49,4 +49,12 @@ def b30ResultToScore(_: None, qImage: QImage, result: B30OcrResultItem): comment="B30 OCR", ) + if not chart: + chart = Chart( + song_id=song_id, + rating_class=result.rating_class, + title=song_id, + constant=0.0, + ) + return (chart, score) diff --git a/ui/extends/tabs/tabOcr/tabOcr_Device.py b/ui/extends/tabs/tabOcr/tabOcr_Device.py index a1a4be0..8ea963a 100644 --- a/ui/extends/tabs/tabOcr/tabOcr_Device.py +++ b/ui/extends/tabs/tabOcr/tabOcr_Device.py @@ -85,4 +85,11 @@ class ScoreConverter: comment=f"OCR {QFileInfo(imagePath).fileName()}", ) chart = db.get_chart(score.song_id, score.rating_class) + if not chart: + chart = Chart( + song_id=result.song_id, + rating_class=result.rating_class, + title=result.song_id, + constant=0.0, + ) return (chart, score)