Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
fix local incrementer issue
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Dec 8, 2022
1 parent 952fdc4 commit d6c5d0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions PaimonMenuBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions PaimonMenuBar/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool>("is_status_icon_template", default: true)

// whether or not to render the text next to the resin icon
static let isShowResinText = Key<Bool>("is_show_resin_text", default: true)

Expand All @@ -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<Bool>("has_notified_parametric_ready", default: false)

// resin restores every 8 minutes
static let recordUpdateInterval = Key<Double>("update_interval", default: 60 * 8)
// update every 2 hours to prevent captchas
static let recordUpdateInterval = Key<Double>("update_interval", default: 2)

// if the API encounters a failure (fetch failed, mostly because of the new captcha) ...
static let fetchFailed = Key<Bool>("fetch_failed", default: false)
Expand Down
26 changes: 12 additions & 14 deletions PaimonMenuBar/GameRecordUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit d6c5d0f

Please sign in to comment.