dsh-plugin-cyrene-live2d 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/assets/default-prefs.json +8 -8
- package/assets/icon.svg +89 -89
- package/assets/locale/notes/en.json +6 -6
- package/assets/locale/notes/zh-CN.json +6 -6
- package/assets/locale/reminder/en.json +6 -6
- package/assets/locale/reminder/zh-CN.json +6 -6
- package/assets/model/Cyrene.model3.json +441 -441
- package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
- package/assets/model/bongo-standard/cat.cdi3.json +296 -296
- package/assets/model/bongo-standard/cat.model3.json +33 -33
- package/assets/model/bongo-standard/cat.physics3.json +232 -232
- package/assets/model/expressions/0.exp3.json +24 -24
- package/assets/model/expressions/1.exp3.json +11 -11
- package/assets/model/expressions/10.exp3.json +9 -9
- package/assets/model/expressions/11.exp3.json +9 -9
- package/assets/model/expressions/2.exp3.json +9 -9
- package/assets/model/expressions/3.exp3.json +9 -9
- package/assets/model/expressions/4.exp3.json +11 -11
- package/assets/model/expressions/5.exp3.json +11 -11
- package/assets/model/expressions/6.exp3.json +11 -11
- package/assets/model/expressions/7.exp3.json +11 -11
- package/assets/model/expressions/8.exp3.json +11 -11
- package/assets/model/expressions/9.exp3.json +9 -9
- package/assets/model/model.physics3.json +641 -641
- package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
- package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
- package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
- package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
- package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
- package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
- package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
- package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
- package/assets/model/we-q/model0.model3.json +245 -245
- package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
- package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
- package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
- package/assets/runtime/live2dcubismcore.min.js +8 -8
- package/assets/stickers.json +84 -84
- package/assets/vendor/cubism4.min.js +1 -1
- package/assets/vendor/pixi.min.js +9 -9
- package/assets/vendor/theme-cyrene-factory.js +4314 -4314
- package/assets/vendor/theme-cyrene.js +4317 -4317
- package/assets/voice/angry-00.opus +0 -0
- package/assets/voice/angry-02.opus +0 -0
- package/assets/voice/approval-00.opus +0 -0
- package/assets/voice/approval-01.opus +0 -0
- package/assets/voice/approval-02.opus +0 -0
- package/assets/voice/askAck-00.opus +0 -0
- package/assets/voice/away-01.opus +0 -0
- package/assets/voice/away-02.opus +0 -0
- package/assets/voice/away-03.opus +0 -0
- package/assets/voice/away-05.opus +0 -0
- package/assets/voice/back-00.opus +0 -0
- package/assets/voice/back-01.opus +0 -0
- package/assets/voice/backLong-05.opus +0 -0
- package/assets/voice/backShort-03.opus +0 -0
- package/assets/voice/backShort-04.opus +0 -0
- package/assets/voice/backShort-07.opus +0 -0
- package/assets/voice/backVeryLong-01.opus +0 -0
- package/assets/voice/backVeryLong-02.opus +0 -0
- package/assets/voice/backVeryLong-03.opus +0 -0
- package/assets/voice/backVeryLong-04.opus +0 -0
- package/assets/voice/backVeryLong-05.opus +0 -0
- package/assets/voice/backVeryLong-07.opus +0 -0
- package/assets/voice/backVeryLong-08.opus +0 -0
- package/assets/voice/balLow-00.opus +0 -0
- package/assets/voice/balLow-01.opus +0 -0
- package/assets/voice/birthday-02.opus +0 -0
- package/assets/voice/click-00.opus +0 -0
- package/assets/voice/click-01.opus +0 -0
- package/assets/voice/click-07.opus +0 -0
- package/assets/voice/click-10.opus +0 -0
- package/assets/voice/click-11.opus +0 -0
- package/assets/voice/click-12.opus +0 -0
- package/assets/voice/click-16.opus +0 -0
- package/assets/voice/click-17.opus +0 -0
- package/assets/voice/click-18.opus +0 -0
- package/assets/voice/comfort-03.opus +0 -0
- package/assets/voice/comfort-04.opus +0 -0
- package/assets/voice/cost10-00.opus +0 -0
- package/assets/voice/cost2-00.opus +0 -0
- package/assets/voice/cost5-00.opus +0 -0
- package/assets/voice/costTurn-01.opus +0 -0
- package/assets/voice/costTurn-02.opus +0 -0
- package/assets/voice/ctxFull-01.opus +0 -0
- package/assets/voice/ctxHigh-00.opus +0 -0
- package/assets/voice/dawn-01.opus +0 -0
- package/assets/voice/dawn-04.opus +0 -0
- package/assets/voice/dawn-06.opus +0 -0
- package/assets/voice/dawn-07.opus +0 -0
- package/assets/voice/deepWake-03.opus +0 -0
- package/assets/voice/deepWake-04.opus +0 -0
- package/assets/voice/deepWake-06.opus +0 -0
- package/assets/voice/deepWake-07.opus +0 -0
- package/assets/voice/done-05.opus +0 -0
- package/assets/voice/done-08.opus +0 -0
- package/assets/voice/done-09.opus +0 -0
- package/assets/voice/done-11.opus +0 -0
- package/assets/voice/empty-00.opus +0 -0
- package/assets/voice/encourage-02.opus +0 -0
- package/assets/voice/failed-01.opus +0 -0
- package/assets/voice/failed-05.opus +0 -0
- package/assets/voice/failkind-auth-01.opus +0 -0
- package/assets/voice/failkind-auth-02.opus +0 -0
- package/assets/voice/failkind-ctx-00.opus +0 -0
- package/assets/voice/failkind-missing-01.opus +0 -0
- package/assets/voice/focusEnd-02.opus +0 -0
- package/assets/voice/focusEnd-04.opus +0 -0
- package/assets/voice/forgive-00.opus +0 -0
- package/assets/voice/hello-00.opus +0 -0
- package/assets/voice/hello-03.opus +0 -0
- package/assets/voice/hold-00.opus +0 -0
- package/assets/voice/idleTap-00.opus +0 -0
- package/assets/voice/idleTapLong-02.opus +0 -0
- package/assets/voice/lateNight-00.opus +0 -0
- package/assets/voice/lateNight-02.opus +0 -0
- package/assets/voice/lateNight-03.opus +0 -0
- package/assets/voice/lateNight-06.opus +0 -0
- package/assets/voice/lateNight-07.opus +0 -0
- package/assets/voice/murmur-04.opus +0 -0
- package/assets/voice/night-01.opus +0 -0
- package/assets/voice/night-03.opus +0 -0
- package/assets/voice/night-05.opus +0 -0
- package/assets/voice/noon-04.opus +0 -0
- package/assets/voice/petArm-02.opus +0 -0
- package/assets/voice/petCheek-00.opus +0 -0
- package/assets/voice/petCheek-01.opus +0 -0
- package/assets/voice/petCheek-02.opus +0 -0
- package/assets/voice/petHead-01.opus +0 -0
- package/assets/voice/petHead-03.opus +0 -0
- package/assets/voice/petHead-04.opus +0 -0
- package/assets/voice/praised-01.opus +0 -0
- package/assets/voice/praised-03.opus +0 -0
- package/assets/voice/question-00.opus +0 -0
- package/assets/voice/question-03.opus +0 -0
- package/assets/voice/question-04.opus +0 -0
- package/assets/voice/question-06.opus +0 -0
- package/assets/voice/shy-00.opus +0 -0
- package/assets/voice/spot-0-0.opus +0 -0
- package/assets/voice/spot-0-1.opus +0 -0
- package/assets/voice/spot-0-2.opus +0 -0
- package/assets/voice/spot-3-0.opus +0 -0
- package/assets/voice/spot-4-0.opus +0 -0
- package/assets/voice/spot-4-2.opus +0 -0
- package/assets/voice/spot-5-2.opus +0 -0
- package/assets/voice/spot-6-0.opus +0 -0
- package/assets/voice/spot-6-1.opus +0 -0
- package/assets/voice/spot-7-0.opus +0 -0
- package/assets/voice/spot-7-2.opus +0 -0
- package/assets/voice/spot-8-1.opus +0 -0
- package/assets/voice/tagent-00.opus +0 -0
- package/assets/voice/thinkFast-01.opus +0 -0
- package/assets/voice/thinkFast-02.opus +0 -0
- package/assets/voice/thinkFast-03.opus +0 -0
- package/assets/voice/thinkSlow-01.opus +0 -0
- package/assets/voice/thinkSlow-03.opus +0 -0
- package/assets/voice/thinking-01.opus +0 -0
- package/assets/voice/thinking-02.opus +0 -0
- package/assets/voice/thinking-03.opus +0 -0
- package/assets/voice/thinking-09.opus +0 -0
- package/assets/voice/tread-00.opus +0 -0
- package/assets/voice/tread-01.opus +0 -0
- package/assets/voice/trun-02.opus +0 -0
- package/assets/voice/tsun-00.opus +0 -0
- package/assets/voice/tsun-01.opus +0 -0
- package/assets/voice/tsun-04.opus +0 -0
- package/assets/voice/twrite-00.opus +0 -0
- package/assets/voice/unknown-01.opus +0 -0
- package/assets/voice/waitApproval-00.opus +0 -0
- package/assets/voice/waitApproval-01.opus +0 -0
- package/assets/voice/waitApproval-02.opus +0 -0
- package/assets/voice/waitApproval-03.opus +0 -0
- package/assets/voice/waitApprovalLate-02.opus +0 -0
- package/assets/voice/waitApprovalLate-03.opus +0 -0
- package/assets/voice/waitAsk-00.opus +0 -0
- package/assets/voice/waitAsk-02.opus +0 -0
- package/assets/voice/waitAskLate-03.opus +0 -0
- package/assets/voice/wake-03.opus +0 -0
- package/assets/voice/wake-04.opus +0 -0
- package/assets/voice/wake-05.opus +0 -0
- package/assets/voice/wake-06.opus +0 -0
- package/lib/client.js +7 -7
- package/lib/features/notes.js +172 -172
- package/lib/features/reminder.js +216 -216
- package/lib/l2d-plan.js +295 -295
- package/lib/l2d-tools.js +293 -293
- package/lib/ledger.js +163 -163
- package/lib/mys/config.js +183 -183
- package/lib/mys/ds.js +209 -209
- package/lib/mys/http.js +85 -85
- package/lib/mys/mys.js +419 -419
- package/lib/mys/qr.js +434 -434
- package/lib/mys/scheduler.js +206 -206
- package/lib/mys/store.js +248 -248
- package/lib/mys/tz.js +174 -174
- package/lib/wavtrim.js +245 -245
- package/lib/worldbook/NOTICE.md +18 -18
- package/lib/worldbook/data/Cyrene.md +111 -111
- package/lib/worldbook/data/_glossary.md +181 -181
- package/lib/worldbook/data/characters.md +289 -289
- package/lib/worldbook/data/story.md +130 -130
- package/lib/worldbook/data/world.md +182 -182
- package/lib/worldbook.js +189 -189
- package/package.json +72 -69
package/lib/mys/scheduler.js
CHANGED
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Daily scheduling: one run per day, at a random instant inside a window.
|
|
3
|
-
*
|
|
4
|
-
* The planner is a pure function so the timing rules can be tested without a
|
|
5
|
-
* live timer; the scheduler is a thin loop that re-arms itself and tolerates a
|
|
6
|
-
* suspended machine (a late timer still runs once, never twice).
|
|
7
|
-
*/
|
|
8
|
-
import { dayKey, gameDayKey, minuteOfDay, parseClock, pickInstantInWindow, shiftDayKey, zonedParts, zonedToInstant } from './tz.js'
|
|
9
|
-
|
|
10
|
-
const MAX_ARM_MS = 6 * 60 * 60 * 1000
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Decide when the next run should happen.
|
|
14
|
-
*
|
|
15
|
-
* @param {object} input
|
|
16
|
-
* @param {object} input.config - normalized configuration.
|
|
17
|
-
* @param {number} input.now - current epoch milliseconds.
|
|
18
|
-
* @param {string|null} input.lastRunDate - `YYYY-MM-DD` of the last completed run.
|
|
19
|
-
* @param {() => number} [input.random]
|
|
20
|
-
* @returns {{date: string, targetAt: Date, window: {start: string, end: string}|null, reason: string}|null}
|
|
21
|
-
*/
|
|
22
|
-
export function planNext({ config, now, lastRunDate = null, random = Math.random }) {
|
|
23
|
-
if (config?.enabled !== true) return null
|
|
24
|
-
const timeZone = config.timezone
|
|
25
|
-
const current = new Date(now)
|
|
26
|
-
const today = dayKey(current, timeZone)
|
|
27
|
-
const minute = minuteOfDay(current, timeZone)
|
|
28
|
-
// 按窗口起始时刻排序再挑:配置里的顺序不代表先后。
|
|
29
|
-
// 以前 tomorrowPlan 固定取 config.windows[0],而今天的循环按配置顺序返回第一个「还没过」的,
|
|
30
|
-
// 两处都可能拿到「配置里排第一但时间上不是最早」的那个窗口。
|
|
31
|
-
const windows = [...(Array.isArray(config.windows) ? config.windows : [])].sort((a, b) => {
|
|
32
|
-
const sa = parseClock(a && a.start)
|
|
33
|
-
const sb = parseClock(b && b.start)
|
|
34
|
-
return (sa === null ? 1e9 : sa) - (sb === null ? 1e9 : sb)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
// 空窗口防御:normalizeConfig 理论上会保证非空,但这里再兜一次 ——
|
|
38
|
-
// 一旦 windows 是空的,windows[0].start 会在插件加载期抛 TypeError。
|
|
39
|
-
if (windows.length === 0) return null
|
|
40
|
-
|
|
41
|
-
const tomorrowPlan = (reason) => {
|
|
42
|
-
const date = shiftDayKey(today, 1)
|
|
43
|
-
const window = windows[0]
|
|
44
|
-
return {
|
|
45
|
-
date,
|
|
46
|
-
targetAt: pickInstantInWindow(date, { start: parseClock(window.start), end: parseClock(window.end) }, timeZone, random),
|
|
47
|
-
window,
|
|
48
|
-
reason,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// 「今天跑过没有」必须和 lastRunDate 用同一个「今天」—— 它是游戏日(04:00 换日)。
|
|
53
|
-
// 以前这里比的是自然日,窗口落在 00:00–04:00 时会判定失效、多跑一次拿到 already。
|
|
54
|
-
// 两个都认:新记录是游戏日,老记录(升级前)是自然日。
|
|
55
|
-
if (lastRunDate === gameDayKey(current, timeZone) || lastRunDate === today) return tomorrowPlan('done-today')
|
|
56
|
-
|
|
57
|
-
for (const window of windows) {
|
|
58
|
-
const start = parseClock(window.start)
|
|
59
|
-
const end = parseClock(window.end)
|
|
60
|
-
if (start === null || end === null) continue
|
|
61
|
-
if (end <= minute) continue
|
|
62
|
-
|
|
63
|
-
if (start > minute) {
|
|
64
|
-
return {
|
|
65
|
-
date: today,
|
|
66
|
-
targetAt: pickInstantInWindow(today, { start, end }, timeZone, random),
|
|
67
|
-
window,
|
|
68
|
-
reason: 'window',
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Already inside the window: spread the remaining minutes uniformly.
|
|
73
|
-
const remaining = Math.max(1, end - minute)
|
|
74
|
-
const offset = Math.floor(random() * remaining)
|
|
75
|
-
const parts = zonedParts(current, timeZone)
|
|
76
|
-
const targetMinute = Math.min(1439, minute + offset)
|
|
77
|
-
return {
|
|
78
|
-
date: today,
|
|
79
|
-
targetAt: zonedToInstant(
|
|
80
|
-
{ year: parts.year, month: parts.month, day: parts.day, hour: Math.floor(targetMinute / 60), minute: targetMinute % 60 },
|
|
81
|
-
timeZone,
|
|
82
|
-
),
|
|
83
|
-
window,
|
|
84
|
-
reason: 'inside',
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (config.runWhenMissed === true) {
|
|
89
|
-
// Every window has passed and today has no run: catch up in a few minutes,
|
|
90
|
-
// still jittered so a restarting app does not fire on a round minute.
|
|
91
|
-
const delayMs = 180_000 + Math.floor(random() * 420_000)
|
|
92
|
-
return { date: today, targetAt: new Date(now + delayMs), window: null, reason: 'missed' }
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return tomorrowPlan('passed')
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Timer loop around {@link planNext}.
|
|
100
|
-
*
|
|
101
|
-
* `setTimer(callback, delayMs)` must return a disposer; the host passes
|
|
102
|
-
* cordis' `ctx.timeout` so a plugin reload clears the pending timer.
|
|
103
|
-
*/
|
|
104
|
-
export class Scheduler {
|
|
105
|
-
constructor({ getConfig, getLastRunDate, onRun, onPlan, onError, now = () => Date.now(), random = Math.random, setTimer }) {
|
|
106
|
-
this.getConfig = getConfig
|
|
107
|
-
this.getLastRunDate = getLastRunDate
|
|
108
|
-
this.onRun = onRun
|
|
109
|
-
this.onPlan = onPlan
|
|
110
|
-
this.onError = onError ?? (() => {})
|
|
111
|
-
this.now = now
|
|
112
|
-
this.random = random
|
|
113
|
-
this.setTimer = setTimer
|
|
114
|
-
this.timer = null
|
|
115
|
-
this.plan = null
|
|
116
|
-
this.busy = false
|
|
117
|
-
this.stopped = false
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** Whether a run is currently in flight. */
|
|
121
|
-
get running() {
|
|
122
|
-
return this.busy
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** Current plan without re-computing it. */
|
|
126
|
-
getPlan() {
|
|
127
|
-
return this.plan
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Compute a fresh plan and arm the timer for it. */
|
|
131
|
-
refresh() {
|
|
132
|
-
if (this.stopped) return null
|
|
133
|
-
this.clearTimer()
|
|
134
|
-
const plan = planNext({
|
|
135
|
-
config: this.getConfig(),
|
|
136
|
-
now: this.now(),
|
|
137
|
-
lastRunDate: this.getLastRunDate(),
|
|
138
|
-
random: this.random,
|
|
139
|
-
})
|
|
140
|
-
this.plan = plan
|
|
141
|
-
this.onPlan?.(plan)
|
|
142
|
-
if (plan !== null) this.arm(plan)
|
|
143
|
-
return plan
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
arm(plan) {
|
|
147
|
-
const delay = Math.max(1000, plan.targetAt.getTime() - this.now())
|
|
148
|
-
if (delay > MAX_ARM_MS) {
|
|
149
|
-
// Re-check in six hours: a clock change or a long sleep must not skip the day.
|
|
150
|
-
this.timer = this.setTimer(() => {
|
|
151
|
-
this.timer = null
|
|
152
|
-
if (this.stopped) return
|
|
153
|
-
this.refresh()
|
|
154
|
-
}, MAX_ARM_MS)
|
|
155
|
-
return
|
|
156
|
-
}
|
|
157
|
-
this.timer = this.setTimer(() => {
|
|
158
|
-
this.timer = null
|
|
159
|
-
void this.fire()
|
|
160
|
-
}, delay)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
clearTimer() {
|
|
164
|
-
if (this.timer !== null) {
|
|
165
|
-
this.timer()
|
|
166
|
-
this.timer = null
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
async fire() {
|
|
171
|
-
if (this.stopped) return
|
|
172
|
-
if (this.busy) return
|
|
173
|
-
const plan = this.plan
|
|
174
|
-
if (plan === null) {
|
|
175
|
-
this.refresh()
|
|
176
|
-
return
|
|
177
|
-
}
|
|
178
|
-
const early = plan.targetAt.getTime() - this.now()
|
|
179
|
-
if (early > 5000) {
|
|
180
|
-
this.arm(plan)
|
|
181
|
-
return
|
|
182
|
-
}
|
|
183
|
-
this.busy = true
|
|
184
|
-
try {
|
|
185
|
-
await this.onRun('schedule')
|
|
186
|
-
} catch (error) {
|
|
187
|
-
this.onError(error)
|
|
188
|
-
} finally {
|
|
189
|
-
this.busy = false
|
|
190
|
-
this.refresh()
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/** Start the loop. */
|
|
195
|
-
start() {
|
|
196
|
-
this.stopped = false
|
|
197
|
-
return this.refresh()
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** Stop the loop and drop the pending timer. */
|
|
201
|
-
stop() {
|
|
202
|
-
this.stopped = true
|
|
203
|
-
this.clearTimer()
|
|
204
|
-
this.plan = null
|
|
205
|
-
}
|
|
206
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Daily scheduling: one run per day, at a random instant inside a window.
|
|
3
|
+
*
|
|
4
|
+
* The planner is a pure function so the timing rules can be tested without a
|
|
5
|
+
* live timer; the scheduler is a thin loop that re-arms itself and tolerates a
|
|
6
|
+
* suspended machine (a late timer still runs once, never twice).
|
|
7
|
+
*/
|
|
8
|
+
import { dayKey, gameDayKey, minuteOfDay, parseClock, pickInstantInWindow, shiftDayKey, zonedParts, zonedToInstant } from './tz.js'
|
|
9
|
+
|
|
10
|
+
const MAX_ARM_MS = 6 * 60 * 60 * 1000
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Decide when the next run should happen.
|
|
14
|
+
*
|
|
15
|
+
* @param {object} input
|
|
16
|
+
* @param {object} input.config - normalized configuration.
|
|
17
|
+
* @param {number} input.now - current epoch milliseconds.
|
|
18
|
+
* @param {string|null} input.lastRunDate - `YYYY-MM-DD` of the last completed run.
|
|
19
|
+
* @param {() => number} [input.random]
|
|
20
|
+
* @returns {{date: string, targetAt: Date, window: {start: string, end: string}|null, reason: string}|null}
|
|
21
|
+
*/
|
|
22
|
+
export function planNext({ config, now, lastRunDate = null, random = Math.random }) {
|
|
23
|
+
if (config?.enabled !== true) return null
|
|
24
|
+
const timeZone = config.timezone
|
|
25
|
+
const current = new Date(now)
|
|
26
|
+
const today = dayKey(current, timeZone)
|
|
27
|
+
const minute = minuteOfDay(current, timeZone)
|
|
28
|
+
// 按窗口起始时刻排序再挑:配置里的顺序不代表先后。
|
|
29
|
+
// 以前 tomorrowPlan 固定取 config.windows[0],而今天的循环按配置顺序返回第一个「还没过」的,
|
|
30
|
+
// 两处都可能拿到「配置里排第一但时间上不是最早」的那个窗口。
|
|
31
|
+
const windows = [...(Array.isArray(config.windows) ? config.windows : [])].sort((a, b) => {
|
|
32
|
+
const sa = parseClock(a && a.start)
|
|
33
|
+
const sb = parseClock(b && b.start)
|
|
34
|
+
return (sa === null ? 1e9 : sa) - (sb === null ? 1e9 : sb)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
// 空窗口防御:normalizeConfig 理论上会保证非空,但这里再兜一次 ——
|
|
38
|
+
// 一旦 windows 是空的,windows[0].start 会在插件加载期抛 TypeError。
|
|
39
|
+
if (windows.length === 0) return null
|
|
40
|
+
|
|
41
|
+
const tomorrowPlan = (reason) => {
|
|
42
|
+
const date = shiftDayKey(today, 1)
|
|
43
|
+
const window = windows[0]
|
|
44
|
+
return {
|
|
45
|
+
date,
|
|
46
|
+
targetAt: pickInstantInWindow(date, { start: parseClock(window.start), end: parseClock(window.end) }, timeZone, random),
|
|
47
|
+
window,
|
|
48
|
+
reason,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 「今天跑过没有」必须和 lastRunDate 用同一个「今天」—— 它是游戏日(04:00 换日)。
|
|
53
|
+
// 以前这里比的是自然日,窗口落在 00:00–04:00 时会判定失效、多跑一次拿到 already。
|
|
54
|
+
// 两个都认:新记录是游戏日,老记录(升级前)是自然日。
|
|
55
|
+
if (lastRunDate === gameDayKey(current, timeZone) || lastRunDate === today) return tomorrowPlan('done-today')
|
|
56
|
+
|
|
57
|
+
for (const window of windows) {
|
|
58
|
+
const start = parseClock(window.start)
|
|
59
|
+
const end = parseClock(window.end)
|
|
60
|
+
if (start === null || end === null) continue
|
|
61
|
+
if (end <= minute) continue
|
|
62
|
+
|
|
63
|
+
if (start > minute) {
|
|
64
|
+
return {
|
|
65
|
+
date: today,
|
|
66
|
+
targetAt: pickInstantInWindow(today, { start, end }, timeZone, random),
|
|
67
|
+
window,
|
|
68
|
+
reason: 'window',
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Already inside the window: spread the remaining minutes uniformly.
|
|
73
|
+
const remaining = Math.max(1, end - minute)
|
|
74
|
+
const offset = Math.floor(random() * remaining)
|
|
75
|
+
const parts = zonedParts(current, timeZone)
|
|
76
|
+
const targetMinute = Math.min(1439, minute + offset)
|
|
77
|
+
return {
|
|
78
|
+
date: today,
|
|
79
|
+
targetAt: zonedToInstant(
|
|
80
|
+
{ year: parts.year, month: parts.month, day: parts.day, hour: Math.floor(targetMinute / 60), minute: targetMinute % 60 },
|
|
81
|
+
timeZone,
|
|
82
|
+
),
|
|
83
|
+
window,
|
|
84
|
+
reason: 'inside',
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (config.runWhenMissed === true) {
|
|
89
|
+
// Every window has passed and today has no run: catch up in a few minutes,
|
|
90
|
+
// still jittered so a restarting app does not fire on a round minute.
|
|
91
|
+
const delayMs = 180_000 + Math.floor(random() * 420_000)
|
|
92
|
+
return { date: today, targetAt: new Date(now + delayMs), window: null, reason: 'missed' }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return tomorrowPlan('passed')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Timer loop around {@link planNext}.
|
|
100
|
+
*
|
|
101
|
+
* `setTimer(callback, delayMs)` must return a disposer; the host passes
|
|
102
|
+
* cordis' `ctx.timeout` so a plugin reload clears the pending timer.
|
|
103
|
+
*/
|
|
104
|
+
export class Scheduler {
|
|
105
|
+
constructor({ getConfig, getLastRunDate, onRun, onPlan, onError, now = () => Date.now(), random = Math.random, setTimer }) {
|
|
106
|
+
this.getConfig = getConfig
|
|
107
|
+
this.getLastRunDate = getLastRunDate
|
|
108
|
+
this.onRun = onRun
|
|
109
|
+
this.onPlan = onPlan
|
|
110
|
+
this.onError = onError ?? (() => {})
|
|
111
|
+
this.now = now
|
|
112
|
+
this.random = random
|
|
113
|
+
this.setTimer = setTimer
|
|
114
|
+
this.timer = null
|
|
115
|
+
this.plan = null
|
|
116
|
+
this.busy = false
|
|
117
|
+
this.stopped = false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Whether a run is currently in flight. */
|
|
121
|
+
get running() {
|
|
122
|
+
return this.busy
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Current plan without re-computing it. */
|
|
126
|
+
getPlan() {
|
|
127
|
+
return this.plan
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Compute a fresh plan and arm the timer for it. */
|
|
131
|
+
refresh() {
|
|
132
|
+
if (this.stopped) return null
|
|
133
|
+
this.clearTimer()
|
|
134
|
+
const plan = planNext({
|
|
135
|
+
config: this.getConfig(),
|
|
136
|
+
now: this.now(),
|
|
137
|
+
lastRunDate: this.getLastRunDate(),
|
|
138
|
+
random: this.random,
|
|
139
|
+
})
|
|
140
|
+
this.plan = plan
|
|
141
|
+
this.onPlan?.(plan)
|
|
142
|
+
if (plan !== null) this.arm(plan)
|
|
143
|
+
return plan
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
arm(plan) {
|
|
147
|
+
const delay = Math.max(1000, plan.targetAt.getTime() - this.now())
|
|
148
|
+
if (delay > MAX_ARM_MS) {
|
|
149
|
+
// Re-check in six hours: a clock change or a long sleep must not skip the day.
|
|
150
|
+
this.timer = this.setTimer(() => {
|
|
151
|
+
this.timer = null
|
|
152
|
+
if (this.stopped) return
|
|
153
|
+
this.refresh()
|
|
154
|
+
}, MAX_ARM_MS)
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
this.timer = this.setTimer(() => {
|
|
158
|
+
this.timer = null
|
|
159
|
+
void this.fire()
|
|
160
|
+
}, delay)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
clearTimer() {
|
|
164
|
+
if (this.timer !== null) {
|
|
165
|
+
this.timer()
|
|
166
|
+
this.timer = null
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async fire() {
|
|
171
|
+
if (this.stopped) return
|
|
172
|
+
if (this.busy) return
|
|
173
|
+
const plan = this.plan
|
|
174
|
+
if (plan === null) {
|
|
175
|
+
this.refresh()
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
const early = plan.targetAt.getTime() - this.now()
|
|
179
|
+
if (early > 5000) {
|
|
180
|
+
this.arm(plan)
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
this.busy = true
|
|
184
|
+
try {
|
|
185
|
+
await this.onRun('schedule')
|
|
186
|
+
} catch (error) {
|
|
187
|
+
this.onError(error)
|
|
188
|
+
} finally {
|
|
189
|
+
this.busy = false
|
|
190
|
+
this.refresh()
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Start the loop. */
|
|
195
|
+
start() {
|
|
196
|
+
this.stopped = false
|
|
197
|
+
return this.refresh()
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Stop the loop and drop the pending timer. */
|
|
201
|
+
stop() {
|
|
202
|
+
this.stopped = true
|
|
203
|
+
this.clearTimer()
|
|
204
|
+
this.plan = null
|
|
205
|
+
}
|
|
206
|
+
}
|