-
Notifications
You must be signed in to change notification settings - Fork 1
fix: improve handling of vote counts and minId logic in RankReportHis… #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -315,12 +315,12 @@ protected function updateThousandVotesIncremental(array $cachedSummary) | |||||
| ->get(); | ||||||
|
|
||||||
| $newCount = $newVotes->count(); | ||||||
|
|
||||||
| if ($newCount == 0) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| // 如果剛好是1000則直接用newVotes的結果 | ||||||
| // 如果一次抓回來就滿1000筆(或更多),直接覆蓋成最新的這1000筆 | ||||||
| if ($newCount === 1000) { | ||||||
| $winCount = $newVotes->where('winner_id', $this->report->element_id)->count(); | ||||||
| $loseCount = $newVotes->where('loser_id', $this->report->element_id)->count(); | ||||||
|
|
@@ -330,34 +330,66 @@ protected function updateThousandVotesIncremental(array $cachedSummary) | |||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // 計算新票數的勝敗 | ||||||
| $winNew = $newVotes->where('winner_id', $this->report->element_id)->count(); | ||||||
| $loseNew = $newVotes->where('loser_id', $this->report->element_id)->count(); | ||||||
|
|
||||||
| $winOutdated = 0; | ||||||
| $loseOutdated = 0; | ||||||
|
|
||||||
| // 計算目前的總票數與預期總票數 | ||||||
zeng407 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| $currentTotal = $winCount + $loseCount; | ||||||
| $totalAfterAdd = $currentTotal + $newCount; | ||||||
|
|
||||||
| // 只有當 (原票數 + 新票數) > 1000 時,才需要移除舊資料 | ||||||
| // 需要移除的數量 = 總數 - 1000 | ||||||
| $countToRemove = max(0, $totalAfterAdd - 1000); | ||||||
|
|
||||||
| // 初始化 newMinId,預設維持原狀 | ||||||
| $newMinId = $minId; | ||||||
|
|
||||||
| if ($newCount > 0 && $minId > 0) { | ||||||
| // Fetch the oldest portion plus the next one to derive newMinId | ||||||
| // [情況 A] 總數超過 1000,需要移除舊的 ($countToRemove > 0) | ||||||
| // 且必須原本就有 minId (有資料可移) | ||||||
| if ($countToRemove > 0 && $minId > 0) { | ||||||
| // Fetch the oldest portion needed to be removed plus one to find new boundary | ||||||
| $outdatedVotes = $this->getThousandVotesBaseQuery() | ||||||
| ->where('game_1v1_rounds.id', '>=', $minId) | ||||||
| ->orderBy('game_1v1_rounds.id') | ||||||
| ->limit($newCount + 1) | ||||||
| ->limit($countToRemove + 1) // 多抓一筆用來定位新的 min_id | ||||||
| ->get(); | ||||||
|
|
||||||
| if ($outdatedVotes->isNotEmpty()) { | ||||||
| $outdatedSlice = $outdatedVotes->take($newCount); | ||||||
| // 取出真正要扣掉的那幾筆 | ||||||
| $outdatedSlice = $outdatedVotes->take($countToRemove); | ||||||
| $winOutdated = $outdatedSlice->where('winner_id', $this->report->element_id)->count(); | ||||||
| $loseOutdated = $outdatedSlice->where('loser_id', $this->report->element_id)->count(); | ||||||
|
|
||||||
| $newMinId = $outdatedVotes->last()->id; | ||||||
| // 新的 min_id 是被移除區段後的下一筆資料 ID | ||||||
| // 如果 outdatedVotes 數量足夠,last() 就是新的起點 | ||||||
| if ($outdatedVotes->count() > $countToRemove) { | ||||||
| $newMinId = $outdatedVotes->last()->id; | ||||||
| } else { | ||||||
| // 防呆:如果資料庫剛好斷層,取最後一筆 | ||||||
| $newMinId = $outdatedVotes->last()->id; | ||||||
| } | ||||||
zeng407 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
| } | ||||||
| // [情況 B] 總數還沒滿 1000,不需要移除舊資料 | ||||||
| // 但如果是第一次建立 ($minId == 0),需要設定 minId | ||||||
| elseif ($minId == 0 && $newCount > 0) { | ||||||
|
||||||
| elseif ($minId == 0 && $newCount > 0) { | |
| elseif ($minId === 0 && $newCount > 0) { |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states this handles the first time case when $minId == 0, but there's a potential edge case. If the cached summary has min_id set to 0 but has non-zero win/lose counts, this could indicate data corruption or an invalid state. The code should either validate this scenario doesn't happen, or handle it more explicitly.
Consider adding a check: if $minId == 0 but $currentTotal > 0, this indicates an inconsistent state that should either be logged or trigger a rebuild from scratch.
Uh oh!
There was an error while loading. Please reload this page.