Skip to content

Commit 28fdab5

Browse files
CopilotKagamiChan
andcommitted
Add lightweight checkNosakiPresent helper and use currentTime consistently
Co-authored-by: KagamiChan <3816900+KagamiChan@users.noreply.github.com>
1 parent 0bf3737 commit 28fdab5

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/fleet-utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ export const checkRepairActive = (
108108
return { active, repairShip: repairShipFlagship, flagship }
109109
}
110110

111+
/**
112+
* Lightweight helper to check if Nosaki is present in position 1 or 2 of a fleet.
113+
* Does NOT require $ships (const ship data), making it safer when const data isn't loaded.
114+
* Use this for timer management; use getFleetStatus for full eligibility checks.
115+
*/
116+
export const checkNosakiPresent = (
117+
fleet: APIDeckPort,
118+
ships: Record<number, APIShip>,
119+
): boolean => {
120+
// Check positions 0 and 1 (flagship and second position)
121+
for (let position = 0; position <= 1; position++) {
122+
const shipId = _.get(fleet, `api_ship.${position}`, -1)
123+
const ship = ships[shipId]
124+
if (ship && NOSAKI_ID_LIST.includes(ship.api_ship_id)) {
125+
return true
126+
}
127+
}
128+
return false
129+
}
130+
111131
export const getFleetStatus = (
112132
fleet: APIDeckPort,
113133
ships: Record<number, APIShip>,

src/index.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { akashiEstimate, AKASHI_INTERVAL, NOSAKI_INTERVAL } from './functions'
2525
import {
2626
checkRepairActive,
27+
checkNosakiPresent,
2728
REPAIR_SHIP_ID,
2829
NOSAKI_ID_LIST,
2930
getFleetStatus,
@@ -132,15 +133,26 @@ const PluginAnchorageRepair: React.FC = () => {
132133

133134
// Global Nosaki timer handling
134135
// Check if ANY fleet has Nosaki present in position 1 or 2
136+
// Use lightweight checkNosakiPresent first, then full getFleetStatus only if needed
135137
let anyFleetNosakiPresent = false
136138
let anyFleetCanBoostMorale = false
137139
for (const fleet of fleets) {
138-
const status = getFleetStatus(fleet, ships, $ships, repairId, equips)
139-
if (status.nosakiPresent) {
140+
// Quick check first - doesn't require $ships
141+
if (checkNosakiPresent(fleet, ships)) {
140142
anyFleetNosakiPresent = true
141-
if (status.canBoostMorale) {
142-
anyFleetCanBoostMorale = true
143-
break
143+
// Only call getFleetStatus for full eligibility check when $ships is available
144+
if ($ships && Object.keys($ships).length > 0) {
145+
const status = getFleetStatus(
146+
fleet,
147+
ships,
148+
$ships,
149+
repairId,
150+
equips,
151+
)
152+
if (status.canBoostMorale) {
153+
anyFleetCanBoostMorale = true
154+
break
155+
}
144156
}
145157
}
146158
}
@@ -203,17 +215,11 @@ const PluginAnchorageRepair: React.FC = () => {
203215
)
204216

205217
// Helper to check if any other fleet has Nosaki in position 1 or 2
218+
// Uses lightweight checkNosakiPresent that doesn't require $ships
206219
const hasNosakiInOtherFleets = (excludeFleetId: number) =>
207220
fleets.some((fleet) => {
208221
if (fleet.api_id === excludeFleetId) return false
209-
const status = getFleetStatus(
210-
fleet,
211-
ships,
212-
$ships,
213-
repairId,
214-
equips,
215-
)
216-
return status.nosakiPresent
222+
return checkNosakiPresent(fleet, ships)
217223
})
218224

219225
// Handle changes to slot 1/2 (position 0 or 1)
@@ -239,7 +245,7 @@ const PluginAnchorageRepair: React.FC = () => {
239245
// If timer hasn't started yet, start it;
240246
// otherwise, before 15 min, placing Nosaki resets timer.
241247
if (lastNosakiRefresh === 0) {
242-
timerState.setLastNosakiRefresh(Date.now())
248+
timerState.setLastNosakiRefresh(currentTime)
243249
} else if (nosakiTimeElapsed < NOSAKI_INTERVAL / 1000) {
244250
timerState.resetNosakiTimer()
245251
}

0 commit comments

Comments
 (0)