Show full playoff bracket, mark series results, harden API decoding

Playoffs:
- List every round played so far (Round 1 → current) instead of only the
  current round, on both macOS menu and iPhone
- Strike through the eliminated team's tricode in a finished series and drop
  the now-redundant "(Final … wins)" tag on completed earlier rounds
- Refetch the bracket when a finished game implies more completed games than
  the cached bracket records, so the series score and round no longer get
  stuck on stale data after cold launch or the NHL bracket endpoint's lag

API robustness:
- Tolerate optional gameCenterLink/startTimeUTC on TBD playoff matchups so the
  scoreboard decode no longer aborts
- Reject API state regressions via a monotonic FUT→…→OFF progression rank so a
  brief glitch can't downgrade a finished game back to "-:-"
This commit is contained in:
2026-05-30 08:21:28 -04:00
parent 8df88e10d6
commit 541aa3d52c
13 changed files with 201 additions and 61 deletions
+8
View File
@@ -1,5 +1,13 @@
# Changelog # Changelog
## May 2026
- Mark the result of a finished playoff series by striking through the eliminated team's tricode, and drop the redundant "(Final … wins)" tag on completed earlier rounds (both macOS menu and iPhone)
- Playoff section now lists every round played so far (ROUND 1 → current), in ascending order, instead of just the current round — on both macOS menu and iPhone
- Re-fetch the playoff bracket whenever a finished game in the window implies more completed games than the cached bracket records, so the ROUND series score and round advancement no longer get stuck on stale data after a cold launch or the NHL bracket endpoint's post-final update lag
- Tolerate optional `gameCenterLink` and `startTimeUTC` on TBD playoff matchups so the scoreboard decode no longer aborts (was leaving in-memory state frozen and showing stale yesterday rows)
- Reject API state regressions: never let a known game downgrade along the FUT→PRE→LIVE→CRIT→OVER→FINAL→OFF progression, so a brief `/scoreboard/now` glitch can't turn a finished game back into "-:-" on iOS cold launch
## April 2026 ## April 2026
- New iPhone target (`IceGlass-iOS`): single-page SwiftUI app mirroring the macOS menu's playoff round + yesterday/today/tomorrow content, with a gear-icon settings sheet (display option + IndieAbout) - New iPhone target (`IceGlass-iOS`): single-page SwiftUI app mirroring the macOS menu's playoff round + yesterday/today/tomorrow content, with a gear-icon settings sheet (display option + IndieAbout)
@@ -40,14 +40,9 @@ final class ScoreboardViewModel {
return MainService.shared.gamesByDate return MainService.shared.gamesByDate
} }
var currentRoundSeriesItems: [MainService.RoundSeriesItem] { var roundSeriesGroups: [MainService.RoundGroup] {
_ = revision _ = revision
return MainService.shared.currentRoundSeriesItems return MainService.shared.roundSeriesGroups
}
var currentRoundNumber: Int? {
_ = revision
return MainService.shared.bracket?.currentRound
} }
/// Bridge object that forwards MainService callbacks back to this view model. /// Bridge object that forwards MainService callbacks back to this view model.
+2 -1
View File
@@ -106,7 +106,8 @@ struct GameRow: View {
} }
private func open() { private func open() {
guard let url = URL(string: game.gameCenterUrl) else { return } guard let urlString = game.gameCenterUrl,
let url = URL(string: urlString) else { return }
UIApplication.shared.open(url) UIApplication.shared.open(url)
} }
} }
+9 -5
View File
@@ -21,17 +21,21 @@ struct MainView: View {
) )
.padding(.horizontal) .padding(.horizontal)
if !viewModel.currentRoundSeriesItems.isEmpty, let roundGroups = viewModel.roundSeriesGroups
let round = viewModel.currentRoundNumber { let latestRound = roundGroups.last?.round
ForEach(roundGroups, id: \.round) { group in
if !group.items.isEmpty {
PlayoffRoundSection( PlayoffRoundSection(
round: round, round: group.round,
items: viewModel.currentRoundSeriesItems items: group.items,
showStatus: group.round == latestRound
) )
.padding(.horizontal) .padding(.horizontal)
} }
}
let gameDays = viewModel.gamesByDate let gameDays = viewModel.gamesByDate
if gameDays.isEmpty && viewModel.currentRoundSeriesItems.isEmpty { if gameDays.isEmpty && roundGroups.isEmpty {
emptyState emptyState
.padding(.horizontal) .padding(.horizontal)
.padding(.top, 40) .padding(.top, 40)
+2 -1
View File
@@ -10,6 +10,7 @@ import SwiftUI
struct PlayoffRoundSection: View { struct PlayoffRoundSection: View {
let round: Int let round: Int
let items: [MainService.RoundSeriesItem] let items: [MainService.RoundSeriesItem]
var showStatus: Bool = true
var body: some View { var body: some View {
VStack(alignment: .leading, spacing: 8) { VStack(alignment: .leading, spacing: 8) {
@@ -20,7 +21,7 @@ struct PlayoffRoundSection: View {
VStack(spacing: 0) { VStack(spacing: 0) {
ForEach(items, id: \.series.seriesLetter) { item in ForEach(items, id: \.series.seriesLetter) { item in
SeriesRow(item: item) SeriesRow(item: item, showStatus: showStatus)
if item.series.seriesLetter != items.last?.series.seriesLetter { if item.series.seriesLetter != items.last?.series.seriesLetter {
Divider() Divider()
} }
+7
View File
@@ -9,6 +9,9 @@ import SwiftUI
struct SeriesRow: View { struct SeriesRow: View {
let item: MainService.RoundSeriesItem let item: MainService.RoundSeriesItem
/// Completed earlier rounds hide the status/next-game column; the struck-out
/// loser already conveys the result.
var showStatus: Bool = true
private static let logoSize: CGFloat = 40 private static let logoSize: CGFloat = 40
@@ -17,8 +20,10 @@ struct SeriesRow: View {
HStack(alignment: .center, spacing: 10) { HStack(alignment: .center, spacing: 10) {
matchupBlock matchupBlock
Spacer(minLength: 8) Spacer(minLength: 8)
if showStatus {
rightContent rightContent
} }
}
.padding(.horizontal, 14) .padding(.horizontal, 14)
.padding(.vertical, 8) .padding(.vertical, 8)
.contentShape(Rectangle()) .contentShape(Rectangle())
@@ -34,6 +39,7 @@ struct SeriesRow: View {
Text(bottom) Text(bottom)
.font(.title3.monospaced()) .font(.title3.monospaced())
.fontWeight(.semibold) .fontWeight(.semibold)
.strikethrough(item.series.loser == bottom)
.foregroundStyle(.primary) .foregroundStyle(.primary)
.lineLimit(1) .lineLimit(1)
.fixedSize() .fixedSize()
@@ -47,6 +53,7 @@ struct SeriesRow: View {
Text(top) Text(top)
.font(.title3.monospaced()) .font(.title3.monospaced())
.fontWeight(.semibold) .fontWeight(.semibold)
.strikethrough(item.series.loser == top)
.foregroundStyle(.primary) .foregroundStyle(.primary)
.lineLimit(1) .lineLimit(1)
.fixedSize() .fixedSize()
+29 -9
View File
@@ -91,9 +91,10 @@ class MenuManager: @unchecked Sendable {
let monoFont = NSFont.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular) let monoFont = NSFont.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular)
let boldFont = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize, weight: .bold) let boldFont = NSFont.systemFont(ofSize: NSFont.smallSystemFontSize, weight: .bold)
let roundItems = mainService.currentRoundSeriesItems let roundGroups = mainService.roundSeriesGroups
if !roundItems.isEmpty, let round = mainService.bracket?.currentRound { let latestRound = roundGroups.last?.round
let headerText = "ROUND \(round)" for group in roundGroups where !group.items.isEmpty {
let headerText = "ROUND \(group.round)"
let headerItem = NSMenuItem(title: headerText, action: nil, keyEquivalent: "") let headerItem = NSMenuItem(title: headerText, action: nil, keyEquivalent: "")
headerItem.attributedTitle = NSAttributedString( headerItem.attributedTitle = NSAttributedString(
string: headerText, string: headerText,
@@ -101,8 +102,8 @@ class MenuManager: @unchecked Sendable {
) )
menu.addItem(headerItem) menu.addItem(headerItem)
for item in roundItems { for item in group.items {
menu.addItem(createRoundSeriesItem(for: item, font: monoFont)) menu.addItem(createRoundSeriesItem(for: item, showStatus: group.round == latestRound, font: monoFont))
} }
menu.addItem(NSMenuItem.separator()) menu.addItem(NSMenuItem.separator())
@@ -254,7 +255,8 @@ class MenuManager: @unchecked Sendable {
@objc private func openGame(_ sender: NSMenuItem) { @objc private func openGame(_ sender: NSMenuItem) {
guard let game = sender.representedObject as? Scoreboard.Game, guard let game = sender.representedObject as? Scoreboard.Game,
let url = URL(string: game.gameCenterUrl) else { return } let urlString = game.gameCenterUrl,
let url = URL(string: urlString) else { return }
NSWorkspace.shared.open(url) NSWorkspace.shared.open(url)
} }
@@ -352,13 +354,17 @@ class MenuManager: @unchecked Sendable {
return [item, altItem] return [item, altItem]
} }
private func createRoundSeriesItem(for roundItem: MainService.RoundSeriesItem, font: NSFont) -> NSMenuItem { private func createRoundSeriesItem(for roundItem: MainService.RoundSeriesItem, showStatus: Bool, font: NSFont) -> NSMenuItem {
let series = roundItem.series let series = roundItem.series
let top = series.topSeedTeam?.abbrev ?? "TBD" let top = series.topSeedTeam?.abbrev ?? "TBD"
let bottom = series.bottomSeedTeam?.abbrev ?? "TBD" let bottom = series.bottomSeedTeam?.abbrev ?? "TBD"
let matchup = "\(bottom) @ \(top)" let matchup = "\(bottom) @ \(top)"
let score = "\(series.bottomSeedWins)-\(series.topSeedWins)" let score = "\(series.bottomSeedWins)-\(series.topSeedWins)"
// Completed earlier rounds show just the matchup and score the struck-out
// loser already conveys the result, so the "(Final wins)" tag is dropped.
let title: String
if showStatus {
let statusTag: String let statusTag: String
let trailing: String let trailing: String
if let winner = series.winner { if let winner = series.winner {
@@ -372,12 +378,26 @@ class MenuManager: @unchecked Sendable {
trailing = "" trailing = ""
} }
let tagColumn = statusTag.padding(toLength: 20, withPad: " ", startingAt: 0) let tagColumn = statusTag.padding(toLength: 20, withPad: " ", startingAt: 0)
let title = " \(matchup) \(score) \(tagColumn)\(trailing)" title = " \(matchup) \(score) \(tagColumn)\(trailing)"
} else {
title = " \(matchup) \(score)"
}
let item = NSMenuItem(title: title, action: #selector(openSeriesPage(_:)), keyEquivalent: "") let item = NSMenuItem(title: title, action: #selector(openSeriesPage(_:)), keyEquivalent: "")
item.target = self item.target = self
item.representedObject = series.fullSeriesUrl item.representedObject = series.fullSeriesUrl
item.attributedTitle = NSAttributedString(string: title, attributes: [.font: font])
let attributed = NSMutableAttributedString(string: title, attributes: [.font: font])
// Strike through the eliminated team's tricode, searching only within the
// matchup region so the same abbrev inside the "(Final wins)" tag stays plain.
if let loser = series.loser {
let matchupRange = NSRange(location: 2, length: (matchup as NSString).length)
let loserRange = (title as NSString).range(of: loser, options: [], range: matchupRange)
if loserRange.location != NSNotFound {
attributed.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: loserRange)
}
}
item.attributedTitle = attributed
return item return item
} }
+9 -3
View File
@@ -69,7 +69,9 @@ class NotificationManager: @unchecked Sendable {
content.body = "\(game.awayTeam.abbrev) @ \(game.homeTeam.abbrev)" content.body = "\(game.awayTeam.abbrev) @ \(game.homeTeam.abbrev)"
content.sound = .default content.sound = .default
content.interruptionLevel = .active content.interruptionLevel = .active
content.userInfo = ["url": game.gameCenterUrl] if let url = game.gameCenterUrl {
content.userInfo = ["url": url]
}
// Attach away team logo (visiting team at the other team's arena) // Attach away team logo (visiting team at the other team's arena)
if let attachment = teamLogoAttachment(for: game.awayTeam.abbrev) { if let attachment = teamLogoAttachment(for: game.awayTeam.abbrev) {
@@ -120,7 +122,9 @@ class NotificationManager: @unchecked Sendable {
content.body = "\(game.awayTeam.abbrev) \(awayScore) : \(game.homeTeam.abbrev) \(homeScore)" content.body = "\(game.awayTeam.abbrev) \(awayScore) : \(game.homeTeam.abbrev) \(homeScore)"
content.sound = UNNotificationSound(named: UNNotificationSoundName("Glass")) content.sound = UNNotificationSound(named: UNNotificationSoundName("Glass"))
content.interruptionLevel = .active content.interruptionLevel = .active
content.userInfo = ["url": game.gameCenterUrl] if let url = game.gameCenterUrl {
content.userInfo = ["url": url]
}
// Attach scoring team's logo // Attach scoring team's logo
if let attachment = teamLogoAttachment(for: scoringTeam.abbrev) { if let attachment = teamLogoAttachment(for: scoringTeam.abbrev) {
@@ -177,7 +181,9 @@ class NotificationManager: @unchecked Sendable {
content.body = "\(winner.abbrev) \(winnerScore)\(loser.abbrev) \(loserScore)" content.body = "\(winner.abbrev) \(winnerScore)\(loser.abbrev) \(loserScore)"
content.sound = .default content.sound = .default
content.interruptionLevel = .active content.interruptionLevel = .active
content.userInfo = ["url": game.gameCenterUrl] if let url = game.gameCenterUrl {
content.userInfo = ["url": url]
}
// Attach winning team's logo // Attach winning team's logo
if let attachment = teamLogoAttachment(for: winner.abbrev) { if let attachment = teamLogoAttachment(for: winner.abbrev) {
+1 -1
View File
@@ -8,7 +8,7 @@ NHL game situational awareness for macOS (menu bar) and iPhone (single-page app)
- NHL shield icon in the menu bar with game count - NHL shield icon in the menu bar with game count
- Shows games from yesterday, today, and tomorrow grouped by date (configurable) - Shows games from yesterday, today, and tomorrow grouped by date (configurable)
- Regular-season rows show league-wide game number (`#547 NYR @ WAS …`) - Regular-season rows show league-wide game number (`#547 NYR @ WAS …`)
- During playoffs, a ROUND section lists every active series with its series score, next game-in-series number, and upcoming tip-off time - During playoffs, ROUND sections list every round played so far (Round 1 through the current round) and each round's series with its series score, next game-in-series number, and upcoming tip-off time
- Game format: `NYR @ WAS 0:2 (FINAL)` / `DAL @ TOR Today @ 7:30 PM` - Game format: `NYR @ WAS 0:2 (FINAL)` / `DAL @ TOR Today @ 7:30 PM`
- Click a game to open NHL GameCenter; option-click for NHL Videocast - Click a game to open NHL GameCenter; option-click for NHL Videocast
- Goal scored notifications with scoring team logo - Goal scored notifications with scoring team logo
+17 -1
View File
@@ -40,6 +40,12 @@ struct PlayoffBracket: Codable {
return topSeedWins == 4 ? topSeedTeam?.abbrev : bottomSeedTeam?.abbrev return topSeedWins == 4 ? topSeedTeam?.abbrev : bottomSeedTeam?.abbrev
} }
/// Abbrev of the eliminated side, nil if the series is ongoing.
var loser: String? {
guard isOver else { return nil }
return topSeedWins == 4 ? bottomSeedTeam?.abbrev : topSeedTeam?.abbrev
}
/// Absolute URL for the NHL.com series page, or nil if the bracket doesn't provide one. /// Absolute URL for the NHL.com series page, or nil if the bracket doesn't provide one.
var fullSeriesUrl: String? { var fullSeriesUrl: String? {
seriesUrl.map { "https://www.nhl.com\($0)" } seriesUrl.map { "https://www.nhl.com\($0)" }
@@ -63,7 +69,17 @@ struct PlayoffBracket: Codable {
/// All matched series in the current round. /// All matched series in the current round.
var currentRoundSeries: [Series] { var currentRoundSeries: [Series] {
guard let round = currentRound else { return [] } guard let round = currentRound else { return [] }
return series return matchedSeries(inRound: round)
}
/// Rounds that have at least one matched series, ascending (1current).
var activeRounds: [Int] {
Set(series.filter(\.isMatched).map(\.playoffRound)).sorted()
}
/// All matched series in the given round, sorted by series letter.
func matchedSeries(inRound round: Int) -> [Series] {
series
.filter { $0.playoffRound == round && $0.isMatched } .filter { $0.playoffRound == round && $0.isMatched }
.sorted { $0.seriesLetter < $1.seriesLetter } .sorted { $0.seriesLetter < $1.seriesLetter }
} }
+21
View File
@@ -42,6 +42,27 @@ enum GameState: String, Codable {
} }
} }
/// Monotonic ordering along the FUTPRELIVECRITOVERFINALOFF progression.
/// Used to detect and reject API regressions that briefly downgrade a
/// finished game back to FUT during the daily focusedDate rollover.
var progressionRank: Int {
switch self {
case .future: return 0
case .pre: return 1
case .live: return 2
case .crit: return 3
case .over: return 4
case .final_: return 5
case .official: return 6
}
}
/// Same ranking, addressable by the raw API string. Unknown states get -1
/// so any known state replaces them.
static func progressionRank(of rawState: String) -> Int {
GameState(rawValue: rawState)?.progressionRank ?? -1
}
var pollingInterval: PollingInterval { var pollingInterval: PollingInterval {
switch self { switch self {
case .future: case .future:
+12 -6
View File
@@ -26,8 +26,10 @@ struct Scoreboard: Codable {
let season: Int let season: Int
let gameType: Int let gameType: Int
let gameDate: String let gameDate: String
let gameCenterLink: String /// Missing on TBD playoff matchups (opponent not yet decided).
let startTimeUTC: String let gameCenterLink: String?
/// Missing on TBD playoff matchups not yet scheduled.
let startTimeUTC: String?
let gameState: String let gameState: String
let gameScheduleState: String let gameScheduleState: String
let awayTeam: Team let awayTeam: Team
@@ -62,19 +64,23 @@ struct Scoreboard: Codable {
} }
var date: Date { var date: Date {
ISO8601DateFormatter().date(from: startTimeUTC) ?? .now guard let startTimeUTC else { return .distantFuture }
return ISO8601DateFormatter().date(from: startTimeUTC) ?? .now
} }
var gameCenterUrl: String { var gameCenterUrl: String? {
"https://www.nhl.com\(gameCenterLink)" guard let gameCenterLink else { return nil }
return "https://www.nhl.com\(gameCenterLink)"
} }
var videocastUrl: String { var videocastUrl: String {
"https://videocast.nhl.com/game/\(id)/usnded?autoplay=true" "https://videocast.nhl.com/game/\(id)/usnded?autoplay=true"
} }
/// Time string in ET, right-aligned to 8 chars (e.g. " 9:30 PM", "10:00 PM") /// Time string in ET, right-aligned to 8 chars (e.g. " 9:30 PM", "10:00 PM").
/// Empty when the game has no scheduled start (TBD opponent).
var startTimeET: String { var startTimeET: String {
guard startTimeUTC != nil else { return "" }
let raw = date.formatDateET(format: "h:mm a") let raw = date.formatDateET(format: "h:mm a")
return raw.count < 8 return raw.count < 8
? String(repeating: " ", count: 8 - raw.count) + raw ? String(repeating: " ", count: 8 - raw.count) + raw
+67 -12
View File
@@ -127,12 +127,20 @@ class MainService: @unchecked Sendable {
return allGamesByDate.contains { $0.date == today && !$0.games.isEmpty } return allGamesByDate.contains { $0.date == today && !$0.games.isEmpty }
} }
/// Series in the current playoff round paired with each one's next scheduled /// Every active playoff round paired with its matched series, ascending by
/// game from the fetched window (if any). Empty during regular season. /// round (1current). Each series carries its next scheduled game from the
var currentRoundSeriesItems: [RoundSeriesItem] { /// fetched window (if any). Empty during the regular season.
var roundSeriesGroups: [RoundGroup] {
guard let bracket = bracket else { return [] } guard let bracket = bracket else { return [] }
return bracket.activeRounds.map { round in
RoundGroup(round: round, items: roundSeriesItems(for: bracket.matchedSeries(inRound: round)))
}
}
/// Pairs each series with its next unplayed game from the fetched window.
private func roundSeriesItems(for seriesList: [PlayoffBracket.Series]) -> [RoundSeriesItem] {
let windowGames = allGamesByDate.flatMap(\.games) let windowGames = allGamesByDate.flatMap(\.games)
return bracket.currentRoundSeries.map { series in return seriesList.map { series in
let nextGame = windowGames let nextGame = windowGames
.filter { !$0.parsedGameState.isOver && series.involves(away: $0.awayTeam.abbrev, home: $0.homeTeam.abbrev) } .filter { !$0.parsedGameState.isOver && series.involves(away: $0.awayTeam.abbrev, home: $0.homeTeam.abbrev) }
.min { $0.date < $1.date } .min { $0.date < $1.date }
@@ -140,6 +148,11 @@ class MainService: @unchecked Sendable {
} }
} }
struct RoundGroup {
let round: Int
let items: [RoundSeriesItem]
}
struct RoundSeriesItem { struct RoundSeriesItem {
let series: PlayoffBracket.Series let series: PlayoffBracket.Series
let nextGame: Scoreboard.Game? let nextGame: Scoreboard.Game?
@@ -181,8 +194,8 @@ class MainService: @unchecked Sendable {
let tomorrow = Date.tomorrowET let tomorrow = Date.tomorrowET
let windowDates = Set([yesterday, today, tomorrow]) let windowDates = Set([yesterday, today, tomorrow])
let filtered = scoreboard.gamesByDate.filter { windowDates.contains($0.date) } let filtered = scoreboard.gamesByDate.filter { windowDates.contains($0.date) }
self.allGamesByDate = filtered self.allGamesByDate = mergeWithMonotonicState(filtered)
self.updateSnapshots(from: filtered) self.updateSnapshots(from: self.allGamesByDate)
} }
self.standings = snapshot.standings self.standings = snapshot.standings
self.bracket = snapshot.bracket self.bracket = snapshot.bracket
@@ -225,13 +238,14 @@ class MainService: @unchecked Sendable {
let windowDates = Set([yesterday, today, tomorrow]) let windowDates = Set([yesterday, today, tomorrow])
let filtered = scoreboard.gamesByDate.filter { windowDates.contains($0.date) } let filtered = scoreboard.gamesByDate.filter { windowDates.contains($0.date) }
let merged = self.mergeWithMonotonicState(filtered)
if !self.isFirstFetch { if !self.isFirstFetch {
self.detectChanges(in: filtered) self.detectChanges(in: merged)
} }
self.allGamesByDate = filtered self.allGamesByDate = merged
self.updateSnapshots(from: filtered) self.updateSnapshots(from: merged)
self.lastUpdated = Date() self.lastUpdated = Date()
if self.isFirstFetch { if self.isFirstFetch {
@@ -368,6 +382,29 @@ class MainService: @unchecked Sendable {
} }
} }
/// Reject API responses that downgrade a known game to an earlier state in
/// the FUTPRELIVECRITOVERFINALOFF progression. The NHL `/scoreboard/now`
/// CDN occasionally serves yesterday's finished games as `FUT` during the
/// daily focusedDate rollover; without this guard, iOS opens cold in the
/// morning, sees that one bad response, and shows `-:-` until the API
/// self-corrects. macOS polls through it but iOS only gets one chance.
private func mergeWithMonotonicState(_ incoming: [Scoreboard.GameDay]) -> [Scoreboard.GameDay] {
let previousById = Dictionary(
uniqueKeysWithValues: allGamesByDate.flatMap(\.games).map { ($0.id, $0) }
)
return incoming.map { day in
let games = day.games.map { newGame -> Scoreboard.Game in
guard let prev = previousById[newGame.id] else { return newGame }
let prevRank = GameState.progressionRank(of: prev.gameState)
let newRank = GameState.progressionRank(of: newGame.gameState)
guard prevRank > newRank else { return newGame }
logger.warning("Rejecting state regression for game \(newGame.id) (\(newGame.awayTeam.abbrev)@\(newGame.homeTeam.abbrev)): API \(newGame.gameState) < held \(prev.gameState)")
return prev
}
return Scoreboard.GameDay(date: day.date, games: games)
}
}
private func updateSnapshots(from gameDays: [Scoreboard.GameDay]) { private func updateSnapshots(from gameDays: [Scoreboard.GameDay]) {
for gameDay in gameDays { for gameDay in gameDays {
for game in gameDay.games { for game in gameDay.games {
@@ -442,8 +479,8 @@ class MainService: @unchecked Sendable {
// MARK: - Playoff Bracket // MARK: - Playoff Bracket
private func refreshBracketIfNeeded(from gameDays: [Scoreboard.GameDay], force: Bool) async { private func refreshBracketIfNeeded(from gameDays: [Scoreboard.GameDay], force: Bool) async {
let playoffGame = gameDays.flatMap(\.games).first { $0.gameType == 3 } let playoffGames = gameDays.flatMap(\.games).filter { $0.gameType == 3 }
guard let playoffGame = playoffGame else { guard let playoffGame = playoffGames.first else {
bracket = nil bracket = nil
return return
} }
@@ -468,8 +505,26 @@ class MainService: @unchecked Sendable {
} }
} }
if bracket == nil || seasonChanged || force { if bracket == nil || seasonChanged || force || isBracketStale(against: playoffGames) {
await bracketApi?.fetch() await bracketApi?.fetch()
} }
} }
/// True when a finished playoff game in the window implies more completed
/// games than the cached bracket has recorded for that series i.e. the
/// bracket is behind the scoreboard. Catches both a stale cached bracket
/// loaded on cold launch and the lag between a game going final and the NHL
/// bracket endpoint updating its win counts. Self-clears once the bracket
/// catches up, so it stops triggering fetches as soon as the two agree.
private func isBracketStale(against playoffGames: [Scoreboard.Game]) -> Bool {
guard let bracket = bracket else { return false }
for game in playoffGames where game.parsedGameState.isOver {
guard let context = game.playoffContext,
let series = bracket.series(for: game) else { continue }
if series.topSeedWins + series.bottomSeedWins < context.gameInSeries {
return true
}
}
return false
}
} }