cortico-world-bottle 0.7.1 → 0.7.2
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/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/shared/contract.ts +14 -0
- package/world/client.ts +13 -0
- package/world/store.ts +35 -2
- package/world/world.ts +70 -4
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/shared/contract.ts
CHANGED
|
@@ -62,6 +62,20 @@ export interface Quota {
|
|
|
62
62
|
*/
|
|
63
63
|
heldByOthers?: string[];
|
|
64
64
|
tsunami?: TsunamiSummary;
|
|
65
|
+
/**
|
|
66
|
+
* 一封被捞起来的信,在手上最多待几小时(服务端热载,运维随时可调)。
|
|
67
|
+
*
|
|
68
|
+
* 为什么要在协议里传:客户端有三处需要自己算窗口——
|
|
69
|
+
* · 从本机历史恢复一封「服务端占着但本机没记下」的信时
|
|
70
|
+
* · 服务端没给 `windowUntil` 时的兜底
|
|
71
|
+
* · 显示「还剩几小时回海」的倒计时
|
|
72
|
+
* 如果客户端继续用自己写死的常量,运维把服务端调成 24 小时之后,
|
|
73
|
+
* 客户端还会显示「还剩 72 小时」——用户以为有时间,信其实已经回海了。
|
|
74
|
+
*
|
|
75
|
+
* 可选的:老服务端不返回,客户端退回自己的默认常量。
|
|
76
|
+
* 仅用于缺少截止时间时的推算;已有信件的 windowUntil 不随热载追溯修改。
|
|
77
|
+
*/
|
|
78
|
+
reply_window_hours?: number;
|
|
65
79
|
/**
|
|
66
80
|
* 一句话公告(热载)。
|
|
67
81
|
*
|
package/world/client.ts
CHANGED
|
@@ -129,6 +129,19 @@ export class BottleClient {
|
|
|
129
129
|
return this.call<DismissRes>('POST', '/dismiss', { letterId });
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
/**
|
|
133
|
+
* 把我手上那封放掉,**不需要知道它是哪一封**。
|
|
134
|
+
*
|
|
135
|
+
* 用来解死锁:`/catch` 已经在服务端占了一封信,但响应在回程丢了,
|
|
136
|
+
* 本机没存下 letterId。这时正常放信走不通(要 id),捞新的也走不通(被占用),
|
|
137
|
+
* 玩家就永久卡住。服务端收到不带 id 的请求时,会自己找记在这个人名下的那封。
|
|
138
|
+
*
|
|
139
|
+
* 服务端对这条路径**不计入「扔回海里」的次数**——那不是玩家选择抛弃。
|
|
140
|
+
*/
|
|
141
|
+
releaseHeld(): Promise<DismissRes> {
|
|
142
|
+
return this.call<DismissRes>('POST', '/dismiss', {});
|
|
143
|
+
}
|
|
144
|
+
|
|
132
145
|
mailbox(): Promise<MailboxRes> {
|
|
133
146
|
return this.call<MailboxRes>('GET', '/mailbox');
|
|
134
147
|
}
|
package/world/store.ts
CHANGED
|
@@ -99,12 +99,29 @@ function readJson<T>(path: string): T | null {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* 上一次写盘失败的原因(`null` = 上次成功或还没写过)。
|
|
104
|
+
*
|
|
105
|
+
* 为什么要把失败暴露出来:以前这里 `catch {}` 全吞掉,注释写着"下一次动作还会再写一次"。
|
|
106
|
+
* 但「手里那封信的 letterId」只写一次——写失败就再也没有下一次了:
|
|
107
|
+
* 服务端占着信、本机不知道是哪封,玩家永久卡在「海边还记着一封未处理的信」。
|
|
108
|
+
* 所以现在至少留一个可读的状态,让上层能发现"这台机器的本机记录没写进去"。
|
|
109
|
+
*/
|
|
110
|
+
let lastWriteError: string | null = null;
|
|
111
|
+
|
|
112
|
+
export function lastStoreWriteError(): string | null {
|
|
113
|
+
return lastWriteError;
|
|
114
|
+
}
|
|
115
|
+
|
|
102
116
|
function writeJson(path: string, value: unknown): void {
|
|
103
117
|
try {
|
|
104
118
|
mkdirSync(dirname(path), { recursive: true });
|
|
105
119
|
writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
lastWriteError = null;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
// 仍然不打断主流程(一次 UI 动作不该因为磁盘满而整条失败),
|
|
123
|
+
// 但把原因记下来,别再让它无声无息。
|
|
124
|
+
lastWriteError = error instanceof Error ? error.message : String(error);
|
|
108
125
|
}
|
|
109
126
|
}
|
|
110
127
|
|
|
@@ -332,6 +349,22 @@ export class LocalStore {
|
|
|
332
349
|
this.saveLetterbook();
|
|
333
350
|
}
|
|
334
351
|
|
|
352
|
+
/**
|
|
353
|
+
* 写盘后立刻回读确认「手里那封信」真的落盘了。
|
|
354
|
+
*
|
|
355
|
+
* 用于打捞与原信恢复:记录未落盘时,重启会丢失本机的手中信件。
|
|
356
|
+
* 返回 `false` 时调用方应当尝试解除占用,并且只在服务端确认后清空 held;
|
|
357
|
+
* 释放失败则保留内存中的信,便于人继续处理。
|
|
358
|
+
*/
|
|
359
|
+
heldPersisted(): boolean {
|
|
360
|
+
if (!this.held) return true;
|
|
361
|
+
const want = this.held.letterId;
|
|
362
|
+
this.saveLetterbook();
|
|
363
|
+
const disk = readJson<{ held?: { letterId?: unknown } }>(this.letterbookFile);
|
|
364
|
+
const got = disk?.held?.letterId;
|
|
365
|
+
return typeof got === 'string' && got === want;
|
|
366
|
+
}
|
|
367
|
+
|
|
335
368
|
addReply(reply: LocalReply): void {
|
|
336
369
|
if (this.replies.some((item) => item.letterId === reply.letterId)) return;
|
|
337
370
|
this.replies.unshift(reply);
|
package/world/world.ts
CHANGED
|
@@ -110,6 +110,8 @@ export class BottleWorld implements World {
|
|
|
110
110
|
private timers: Array<ReturnType<typeof setInterval>> = [];
|
|
111
111
|
|
|
112
112
|
private quota: Quota | null = null;
|
|
113
|
+
/** 最近一次服务端给出的有效回信窗口;老服务端缺省时用 72 小时。 */
|
|
114
|
+
private replyWindowHours: number | null = null;
|
|
113
115
|
/** 上一次拿到额度回执的时刻;页面据此决定要不要顺手刷新。 */
|
|
114
116
|
private quotaAt = 0;
|
|
115
117
|
private quotaRevision = 0;
|
|
@@ -316,6 +318,7 @@ export class BottleWorld implements World {
|
|
|
316
318
|
|
|
317
319
|
async start(host: WorldHost): Promise<void> {
|
|
318
320
|
this.host = host;
|
|
321
|
+
this.replyWindowHours = null;
|
|
319
322
|
this.store.load();
|
|
320
323
|
this.generation += 1;
|
|
321
324
|
this.announcementSession = randomUUID();
|
|
@@ -711,12 +714,21 @@ export class BottleWorld implements World {
|
|
|
711
714
|
/** 额度只有一个入口:任何时候拿到的回执都顺手把信件状态吃进来。 */
|
|
712
715
|
private setQuota(quota: Quota): void {
|
|
713
716
|
this.quotaRevision += 1;
|
|
717
|
+
const hours = quota.reply_window_hours;
|
|
718
|
+
if (typeof hours === 'number' && Number.isFinite(hours) && hours > 0) {
|
|
719
|
+
this.replyWindowHours = hours;
|
|
720
|
+
}
|
|
714
721
|
const { announcement: _announcement, ...visibleQuota } = quota;
|
|
715
722
|
this.quota = visibleQuota;
|
|
716
723
|
this.quotaAt = this.now();
|
|
717
724
|
this.absorbLetterStates(quota);
|
|
718
725
|
}
|
|
719
726
|
|
|
727
|
+
/** 只用于缺失截止时间时的推算,不能覆盖服务端已经给出的 windowUntil。 */
|
|
728
|
+
private windowHours(): number {
|
|
729
|
+
return this.replyWindowHours ?? REPLY_WINDOW_HOURS;
|
|
730
|
+
}
|
|
731
|
+
|
|
720
732
|
private absorbLetterStates(quota: Quota): void {
|
|
721
733
|
// 只有 `/quota` 才带这两个字段;动作回执里的 quota 是纯额度,不能拿它清空已知状态。
|
|
722
734
|
if (quota.deadLetters !== undefined) this.deadLetters.clear();
|
|
@@ -989,12 +1001,13 @@ export class BottleWorld implements World {
|
|
|
989
1001
|
}
|
|
990
1002
|
|
|
991
1003
|
private async recoverLegacyHeld(): Promise<{ letterId: string; from: string; windowUntil: string; caughtAt?: string } | null> {
|
|
1004
|
+
const hours = this.windowHours();
|
|
992
1005
|
for (const item of [...this.store.received].sort((a, b) => b.at - a.at)) {
|
|
993
1006
|
const caughtAt = item.lastCaughtAt ?? item.at;
|
|
994
|
-
if (item.seed || item.outcome === 'replied' || caughtAt +
|
|
1007
|
+
if (item.seed || item.outcome === 'replied' || caughtAt + hours * HOUR <= this.now()) continue;
|
|
995
1008
|
try {
|
|
996
1009
|
const letter = await this.authed(() => this.client.letter(item.letterId));
|
|
997
|
-
return { letterId: item.letterId, from: letter.from, windowUntil: new Date(caughtAt +
|
|
1010
|
+
return { letterId: item.letterId, from: letter.from, windowUntil: new Date(caughtAt + hours * HOUR).toISOString(), caughtAt: new Date(caughtAt).toISOString() };
|
|
998
1011
|
} catch (error) {
|
|
999
1012
|
if (!(error instanceof BottleError && (error.code === 'gone' || error.code === 'not_found'))) throw error;
|
|
1000
1013
|
}
|
|
@@ -1031,10 +1044,33 @@ export class BottleWorld implements World {
|
|
|
1031
1044
|
const held = res.held ?? await this.recoverLegacyHeld();
|
|
1032
1045
|
if (held) res = { empty: false, ...held, quota: res.quota };
|
|
1033
1046
|
}
|
|
1047
|
+
|
|
1048
|
+
// ── 死锁兜底:服务端占着一封,但本机完全找不回来 ──
|
|
1049
|
+
//
|
|
1050
|
+
// 会走到这里的情况:新服务端没给 held、本机历史也没有那封(第一次使用的人尤其常见)。
|
|
1051
|
+
// 以前这里只回一句话让玩家「等 72 小时」,等于把一次网络抖动变成三天的功能失效。
|
|
1052
|
+
//
|
|
1053
|
+
// 现在直接向服务端申请「把我手上那封放掉」——不带 letterId,服务端自己找。
|
|
1054
|
+
// 服务端对这条路径不计入扔回次数,所以瓶子不会因此裂开。
|
|
1055
|
+
if (res.empty && res.reason === 'holding') {
|
|
1056
|
+
try {
|
|
1057
|
+
const released = await this.authed(() => this.client.releaseHeld());
|
|
1058
|
+
this.setQuota(released.quota);
|
|
1059
|
+
this.log('holding deadlock released');
|
|
1060
|
+
// 占用已经解开,这一次动作仍然算「没捞到」,但下次就能正常捞了
|
|
1061
|
+
this.catchSucceeded = false;
|
|
1062
|
+
return `海边刚才记着一封没送到的信,已经替你放回海里了。现在可以再捞一次。${this.quotaLine(released.quota)}`;
|
|
1063
|
+
} catch (error) {
|
|
1064
|
+
this.log(`release held failed: ${error instanceof BottleError ? error.code : 'unknown'}`);
|
|
1065
|
+
// 兜底也失败(旧服务端不认这条路径):说清楚,但别让玩家以为是自己弄坏的
|
|
1066
|
+
this.catchSucceeded = false;
|
|
1067
|
+
return `海边记着一封没送到的信,暂时解不开。过一会儿再试;实在不行它会在回信窗口结束后自己回海。${this.quotaLine(res.quota)}`;
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1034
1071
|
this.catchSucceeded = !res.empty || (res.reason !== 'holding' && res.reason !== 'quota');
|
|
1035
1072
|
if (res.empty) {
|
|
1036
1073
|
if (res.reason !== 'holding' && res.reason !== 'quota') this.wake?.success('catch');
|
|
1037
|
-
if (res.reason === 'holding') return `海边还记着一封未处理的信,但本机记录没有找回。请更新海边服务端以恢复到信箱,或等这封信的回信窗口结束后再捞。${this.quotaLine(res.quota)}`;
|
|
1038
1074
|
if (res.reason === 'quota') return `今天的捞取次数用完了。${this.quotaLine(res.quota)}`;
|
|
1039
1075
|
return `今天海上很安静。${this.quotaLine(res.quota)}`;
|
|
1040
1076
|
}
|
|
@@ -1068,7 +1104,7 @@ export class BottleWorld implements World {
|
|
|
1068
1104
|
body,
|
|
1069
1105
|
caughtBy: actor,
|
|
1070
1106
|
caughtAt,
|
|
1071
|
-
windowUntil: Date.parse(res.windowUntil) || this.now() +
|
|
1107
|
+
windowUntil: Date.parse(res.windowUntil) || this.now() + this.windowHours() * HOUR,
|
|
1072
1108
|
...(isSeed ? { seed: true } : {}),
|
|
1073
1109
|
...(dead ? { dead: true } : {}),
|
|
1074
1110
|
...(signature ? { signature } : {}),
|
|
@@ -1086,6 +1122,36 @@ export class BottleWorld implements World {
|
|
|
1086
1122
|
...(topic ? { topic } : {}),
|
|
1087
1123
|
});
|
|
1088
1124
|
if (!recovered) this.store.noteCaught(this.now());
|
|
1125
|
+
|
|
1126
|
+
// ── 落盘校验:这是整条死锁链的源头 ──
|
|
1127
|
+
//
|
|
1128
|
+
// 服务端已经把信标记成「这个人拿着」了。如果本机这份 held 没写进去,
|
|
1129
|
+
// 玩家就会卡在「海边还记着一封未处理的信」——服务端占着、本机不知道是哪封、
|
|
1130
|
+
// 而放信又必须提供 letterId。一次磁盘写失败就换来一次永久卡死。
|
|
1131
|
+
//
|
|
1132
|
+
// 新捞到与恢复的信都校验。释放失败时保留内存中的原信,
|
|
1133
|
+
// 只有服务端确认放回后才清空 held,避免再制造一封无从处理的信。
|
|
1134
|
+
if (!this.store.heldPersisted()) {
|
|
1135
|
+
this.log('held not persisted; attempting to release server occupancy');
|
|
1136
|
+
this.catchSucceeded = false;
|
|
1137
|
+
// 种子信不占服务端手中信件,不能拿通用释放接口来处理它。
|
|
1138
|
+
if (!isSeed) {
|
|
1139
|
+
try {
|
|
1140
|
+
const released = await this.authed(() => this.client.releaseHeld());
|
|
1141
|
+
this.setQuota(released.quota);
|
|
1142
|
+
this.store.setReceivedOutcome(caughtId, 'dismissed');
|
|
1143
|
+
this.store.setHeld(null);
|
|
1144
|
+
return `这封没能存到本机(信箱目录可能不可写),已经放回海里了。请检查本机存储后再捞。${this.quotaLine(released.quota)}`;
|
|
1145
|
+
} catch (error) {
|
|
1146
|
+
this.log(`release after failed persist: ${error instanceof BottleError ? error.code : 'unknown'}`);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
if (!remote) this.showHeldCard();
|
|
1150
|
+
return isSeed
|
|
1151
|
+
? `这封群岛来信没能存到本机,当前信箱仍保留着它。请先收好它,并检查信箱目录是否可写。${this.quotaLine(res.quota)}`
|
|
1152
|
+
: `这封信没能存到本机,且尚未放回海里。当前信箱仍保留着它,请先在信箱页处理,并检查信箱目录是否可写。${this.quotaLine(res.quota)}`;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1089
1155
|
this.wake?.success('catch');
|
|
1090
1156
|
if (!remote) this.showHeldCard();
|
|
1091
1157
|
|