From d6c5d0fb29bfc9306602968c48aa74dd41d456e8 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Thu, 8 Dec 2022 13:09:57 +0800 Subject: [PATCH] fix local incrementer issue --- PaimonMenuBar.xcodeproj/project.pbxproj | 4 ++-- PaimonMenuBar/Defaults.swift | 6 +++--- PaimonMenuBar/GameRecordUpdater.swift | 26 ++++++++++++------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/PaimonMenuBar.xcodeproj/project.pbxproj b/PaimonMenuBar.xcodeproj/project.pbxproj index 480a610..70fc0f8 100644 --- a/PaimonMenuBar.xcodeproj/project.pbxproj +++ b/PaimonMenuBar.xcodeproj/project.pbxproj @@ -368,7 +368,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 127; + CURRENT_PROJECT_VERSION = 128; DEAD_CODE_STRIPPING = YES; DEVELOPMENT_ASSET_PATHS = "\"PaimonMenuBar/Preview Content\""; DEVELOPMENT_TEAM = W2HGAU9MPP; @@ -401,7 +401,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 127; + CURRENT_PROJECT_VERSION = 128; DEAD_CODE_STRIPPING = YES; DEVELOPMENT_ASSET_PATHS = "\"PaimonMenuBar/Preview Content\""; DEVELOPMENT_TEAM = W2HGAU9MPP; diff --git a/PaimonMenuBar/Defaults.swift b/PaimonMenuBar/Defaults.swift index cec7d25..405e176 100644 --- a/PaimonMenuBar/Defaults.swift +++ b/PaimonMenuBar/Defaults.swift @@ -17,7 +17,7 @@ extension Defaults.Keys { // render the icon in the status menu view as template (white icon) or original (colored icon) static let isStatusIconTemplate = Key("is_status_icon_template", default: true) - + // whether or not to render the text next to the resin icon static let isShowResinText = Key("is_show_resin_text", default: true) @@ -27,8 +27,8 @@ extension Defaults.Keys { // store a state of whether the notification has been sent, to avoid duplicated notifications static let hasNotifiedParametricReady = Key("has_notified_parametric_ready", default: false) - // resin restores every 8 minutes - static let recordUpdateInterval = Key("update_interval", default: 60 * 8) + // update every 2 hours to prevent captchas + static let recordUpdateInterval = Key("update_interval", default: 2) // if the API encounters a failure (fetch failed, mostly because of the new captcha) ... static let fetchFailed = Key("fetch_failed", default: false) diff --git a/PaimonMenuBar/GameRecordUpdater.swift b/PaimonMenuBar/GameRecordUpdater.swift index a0d5883..48b4b6a 100644 --- a/PaimonMenuBar/GameRecordUpdater.swift +++ b/PaimonMenuBar/GameRecordUpdater.swift @@ -122,7 +122,7 @@ class GameRecordUpdater { if localUpdateTimer != nil { localUpdateTimer?.invalidate() } - localUpdateTimer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true, block: { _ in + localUpdateTimer = Timer.scheduledTimer(withTimeInterval: 8 * 60, repeats: true, block: { _ in print("Local updater triggered.") guard self.updateTask == nil else { @@ -137,25 +137,23 @@ class GameRecordUpdater { return } - // Elapsed time since last update in seconds - let elapsedTime = Int(Date().timeIntervalSince(gameRecord.fetchAt!)) - print("Elapsed time since last fetch:", elapsedTime) - // Update resin and recovery time - var currentResin = gameRecord.data.current_resin + Int(elapsedTime / 8 / 60) - currentResin = currentResin < gameRecord.data.max_resin ? currentResin : gameRecord.data.max_resin - - var currentRecoveryTime = (Int(gameRecord.data.resin_recovery_time) ?? 0) - elapsedTime - currentRecoveryTime = currentRecoveryTime > 0 ? currentRecoveryTime : 0 - - gameRecord.data.current_resin = currentResin - gameRecord.data.resin_recovery_time = String(currentRecoveryTime) + if gameRecord.data.current_resin < gameRecord.data.max_resin { + gameRecord.data.current_resin += 1 + } + let resinRecoveryTime = Int(gameRecord.data.resin_recovery_time) ?? 0 + if resinRecoveryTime > 0 { + let updatedTime = resinRecoveryTime - 8 * 60 + gameRecord.data + .resin_recovery_time = + String(updatedTime > 0 ? updatedTime : 0) + } // Update expedition and their status for (index, expedition) in gameRecord.data.expeditions.enumerated() { let expeditionRemainedTime = Int(expedition.remained_time) ?? 0 if expeditionRemainedTime > 0 { - let updatedRemainedTime = expeditionRemainedTime - elapsedTime + let updatedRemainedTime = expeditionRemainedTime - 8 * 60 gameRecord.data.expeditions[index] .remained_time = String(updatedRemainedTime > 0 ? updatedRemainedTime : 0) if updatedRemainedTime <= 0 {