coopani-world-life 0.7.2 → 0.8.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/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/src/bridge.ts +35 -8
- package/src/world.ts +84 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# 变更记录
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
**扩展名旁边加了三盏灯。**
|
|
6
|
+
|
|
7
|
+
| 灯 | 绿 | 灰 | 红 |
|
|
8
|
+
|---|---|---|---|
|
|
9
|
+
| **过日子** | 刚刚还在动 | —— | 已经 N 分钟没推进了 —— 她没在过日子,五条也不会再掉 |
|
|
10
|
+
| **本城** | 从你的服务器读到了 | 用内置的芦花塘(**没配,不是故障**) | 配了地址但读不到 |
|
|
11
|
+
| **旅行** | 知道她在哪 | 没装旅行扩展(**不是故障**) | 旅行那边 10 分钟没更新了 |
|
|
12
|
+
|
|
13
|
+
- 灰和红是两件事:**灰是「没启用」,红是「配好了但用不了」**。全标成红的,用户会去修一个没坏的东西。
|
|
14
|
+
- 「过日子」那盏最要紧:定时器挂掉、Core 被系统挂起的时候,面板上五个数字还是最后的样子,**看起来一切正常,其实她已经不动了**。
|
|
15
|
+
|
|
16
|
+
**顺带修了一个会让这盏灯点不出来的问题:** `readTown()` 原来把「没配地址」和「配了但连不上」都退成内置,面板上永远显示「内置」,看起来像你没配。现在两者分开(`builtin` / `failed`),红绿才有意义。
|
|
17
|
+
|
|
18
|
+
|
|
3
19
|
## 0.7.2
|
|
4
20
|
|
|
5
21
|
> **0.7.0 / 0.7.1 / 0.7.2 内容相同。** 0.7.0 发布后 registry 落库花了超过八分钟,
|
package/package.json
CHANGED
package/src/bridge.ts
CHANGED
|
@@ -89,8 +89,17 @@ export interface TownView {
|
|
|
89
89
|
rules: string[];
|
|
90
90
|
/** 服务器设过没有。false = 现在用的是内置默认。 */
|
|
91
91
|
custom: boolean;
|
|
92
|
-
/**
|
|
93
|
-
|
|
92
|
+
/**
|
|
93
|
+
* 从哪儿来的。
|
|
94
|
+
*
|
|
95
|
+
* **`builtin` 和 `failed` 必须分开** —— 两个都退成内置数据,但对用户意思完全不同:
|
|
96
|
+
* `builtin` 是「你没配,我在用默认」,不是故障(灰灯);
|
|
97
|
+
* `failed` 是「你配了但读不到」,是故障(红灯)。
|
|
98
|
+
* 混成一个,面板上就永远看不出「明明配了地址却一直是默认城名」。
|
|
99
|
+
*/
|
|
100
|
+
source: 'server' | 'builtin' | 'failed';
|
|
101
|
+
/** `failed` 时的一句话原因。给灯上的 hint 用。 */
|
|
102
|
+
problem?: string;
|
|
94
103
|
}
|
|
95
104
|
|
|
96
105
|
/**
|
|
@@ -157,7 +166,17 @@ export async function readTown(
|
|
|
157
166
|
signal: ctrl.signal,
|
|
158
167
|
headers: { accept: 'application/json' },
|
|
159
168
|
});
|
|
160
|
-
if (!res.ok)
|
|
169
|
+
if (!res.ok) {
|
|
170
|
+
/*
|
|
171
|
+
* 配了地址但服务端回非 200 —— **这是故障,不是「没配」**。
|
|
172
|
+
* 常见是 Worker 还是旧版(没有 `/town/info` 这个口),或者域名指错了。
|
|
173
|
+
*/
|
|
174
|
+
return {
|
|
175
|
+
...BUILTIN_TOWN,
|
|
176
|
+
source: 'failed',
|
|
177
|
+
problem: `服务器回了 ${res.status},先按内置的算(可能是那个服务器还是旧版,没有 /town/info)`,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
161
180
|
const j: any = await res.json();
|
|
162
181
|
const streets = Array.isArray(j?.streets) ? j.streets.filter((x: unknown) => typeof x === 'string') : [];
|
|
163
182
|
const rules = Array.isArray(j?.rules) ? j.rules.filter((x: unknown) => typeof x === 'string') : [];
|
|
@@ -172,12 +191,20 @@ export async function readTown(
|
|
|
172
191
|
};
|
|
173
192
|
} catch {
|
|
174
193
|
/*
|
|
175
|
-
* 超时、断网、证书错、返回不是 JSON
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
194
|
+
* 超时、断网、证书错、返回不是 JSON。
|
|
195
|
+
*
|
|
196
|
+
* **对她来说结果一样**(今天不知道城名改了没有),所以照样退内置、不抛。
|
|
197
|
+
* **但对用户不一样** —— 他明明配了地址,城名却一直显示「内置」,
|
|
198
|
+
* 而「内置」两个字看起来像「我没配」。所以要标成 `failed`,让灯上说清。
|
|
199
|
+
*
|
|
200
|
+
* 具体是哪种错(ECONNREFUSED / 证书 / JSON)**不往 hint 里塞** ——
|
|
201
|
+
* 那既不解释问题也不指向动作。
|
|
179
202
|
*/
|
|
180
|
-
return {
|
|
203
|
+
return {
|
|
204
|
+
...BUILTIN_TOWN,
|
|
205
|
+
source: 'failed',
|
|
206
|
+
problem: '连不上那个地址(超时或断网),先按内置的算',
|
|
207
|
+
};
|
|
181
208
|
} finally {
|
|
182
209
|
clearTimeout(timer);
|
|
183
210
|
}
|
package/src/world.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { statSync } from 'node:fs';
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import type {
|
|
5
|
-
Logger, ToolDef, World, WorldHost, WorldWebUiDecl, TriggerMode,
|
|
5
|
+
Logger, ToolDef, World, WorldHost, WorldWebUiDecl, WorldLamp, TriggerMode,
|
|
6
6
|
} from 'cortico/core/types.ts';
|
|
7
7
|
import { LIFE_ID, lifeConfigGroups, type LifeConfigSection } from './config.ts';
|
|
8
8
|
import {
|
|
@@ -34,7 +34,16 @@ export interface LifeWorldOptions {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/** 版本号。envPrompt 里露出来,报障时第一个要确认的数。 */
|
|
37
|
-
const VERSION = '0.
|
|
37
|
+
const VERSION = '0.8.0';
|
|
38
|
+
|
|
39
|
+
/** 人话的时长:「3 分钟」「2 小时」。灯上的 hint 用,不要写 180000 毫秒。 */
|
|
40
|
+
function fmtSpan(ms: number): string {
|
|
41
|
+
const sec = Math.round(ms / 1000);
|
|
42
|
+
if (sec < 90) return `${sec} 秒`;
|
|
43
|
+
const min = Math.round(sec / 60);
|
|
44
|
+
if (min < 90) return `${min} 分钟`;
|
|
45
|
+
return `${(min / 60).toFixed(1)} 小时`;
|
|
46
|
+
}
|
|
38
47
|
|
|
39
48
|
export class LifeWorld implements World {
|
|
40
49
|
readonly id = LIFE_ID;
|
|
@@ -82,6 +91,15 @@ export class LifeWorld implements World {
|
|
|
82
91
|
* **不读内容**:内容归宿主的记忆系统管,生活层碰了就越界了(方案 §6.2)。
|
|
83
92
|
*/
|
|
84
93
|
private sessionMtime = 0;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 上一次 tick 跑起来的时刻(内存,不进存档)。
|
|
97
|
+
*
|
|
98
|
+
* 这一格是「**她还在过日子吗**」的唯一证据。盘上的 `state.updatedAt` 测不出来 ——
|
|
99
|
+
* 定时器挂了之后那个数就一直停在那儿,而面板上五条还是好好的,
|
|
100
|
+
* 看起来一切正常,其实她已经不动了。
|
|
101
|
+
*/
|
|
102
|
+
private lastTickAt = 0;
|
|
85
103
|
/**
|
|
86
104
|
* 基线立过没有。
|
|
87
105
|
*
|
|
@@ -108,6 +126,8 @@ export class LifeWorld implements World {
|
|
|
108
126
|
*/
|
|
109
127
|
this.sessionMtime = this.sessionStamp();
|
|
110
128
|
this.sessionBaselineSet = this.sessionMtime > 0;
|
|
129
|
+
/* 刚起来算活着 —— 否则第一分钟灯就是红的,白白吓人一次。 */
|
|
130
|
+
this.lastTickAt = Date.now();
|
|
111
131
|
this.restartTimer();
|
|
112
132
|
host.log.info(
|
|
113
133
|
`模拟鱼生已就绪:${this.town.name}(${this.town.source === 'server' ? '读服务器' : '内置'})`
|
|
@@ -174,6 +194,8 @@ export class LifeWorld implements World {
|
|
|
174
194
|
/* ---------------- 推进 ---------------- */
|
|
175
195
|
|
|
176
196
|
private async tick(): Promise<void> {
|
|
197
|
+
/* 先打点再干活:这一格记的是「定时器还活着」,不是「这一跳有活干」。 */
|
|
198
|
+
this.lastTickAt = Date.now();
|
|
177
199
|
const now = new Date();
|
|
178
200
|
|
|
179
201
|
/* 先把热改接上:周期变了重排定时器,地址变了重读城名。 */
|
|
@@ -969,9 +991,69 @@ export class LifeWorld implements World {
|
|
|
969
991
|
};
|
|
970
992
|
}
|
|
971
993
|
|
|
994
|
+
/**
|
|
995
|
+
* 扩展名旁边那三盏灯。
|
|
996
|
+
*
|
|
997
|
+
* ## 三态语义(照抄 journey 定下的,别自己发明)
|
|
998
|
+
*
|
|
999
|
+
* - **绿 `online`** = 上一次成功了
|
|
1000
|
+
* - **灰 `offline`** = 没启用。**这不是故障** —— 点成红的,用户会去修一个没坏的东西
|
|
1001
|
+
* - **红 `error`** = 配好了但用不了。**这一种不会自己好,正是最需要有人提醒的**
|
|
1002
|
+
*
|
|
1003
|
+
* ## 为什么第一盏是「过日子」
|
|
1004
|
+
*
|
|
1005
|
+
* 前两盏管外部依赖,第一盏管**她自己**。定时器挂掉、Core 被系统挂起、
|
|
1006
|
+
* `tickMs` 配成了一个荒唐的值 —— 这些情况下需求不再衰减,
|
|
1007
|
+
* 而面板上五个数字还是最后一次的样子,**看起来一切正常,其实她已经不动了**。
|
|
1008
|
+
* 这是整套东西里最安静的失败。
|
|
1009
|
+
*/
|
|
1010
|
+
private worldLamps(): WorldLamp[] {
|
|
1011
|
+
const now = new Date();
|
|
1012
|
+
const tickMs = Math.max(15_000, Number(this.cfg.tickMs) || 60_000);
|
|
1013
|
+
|
|
1014
|
+
/* ---- 过日子 ---- */
|
|
1015
|
+
const idleMs = Math.max(0, Date.now() - this.lastTickAt);
|
|
1016
|
+
const idleLimit = Math.max(3 * tickMs, 180_000);
|
|
1017
|
+
const alive: WorldLamp = idleMs <= idleLimit
|
|
1018
|
+
? { label: '过日子', state: 'online', hint: idleMs < 10_000 ? '刚刚还在动' : `上一次推进在 ${fmtSpan(idleMs)}前` }
|
|
1019
|
+
: {
|
|
1020
|
+
label: '过日子',
|
|
1021
|
+
state: 'error',
|
|
1022
|
+
hint: `已经 ${fmtSpan(idleMs)}没有推进了 —— 她没在过日子,五条也不会再掉。去看 Core 日志`,
|
|
1023
|
+
};
|
|
1024
|
+
|
|
1025
|
+
/* ---- 本城 ---- */
|
|
1026
|
+
const host = (() => {
|
|
1027
|
+
try { return new URL(String(this.cfg.townUrl || '')).host; } catch { return ''; }
|
|
1028
|
+
})();
|
|
1029
|
+
const town: WorldLamp = this.town.source === 'server'
|
|
1030
|
+
? { label: '本城', state: 'online', hint: `从 ${host} 读的:${this.town.name}` }
|
|
1031
|
+
: this.town.source === 'failed'
|
|
1032
|
+
? { label: '本城', state: 'error', hint: this.town.problem || '配了地址但读不到,先按内置的算' }
|
|
1033
|
+
: { label: '本城', state: 'offline', hint: `用内置的${this.town.name} —— 没配服务器,这不算故障` };
|
|
1034
|
+
|
|
1035
|
+
/* ---- 旅行 ---- */
|
|
1036
|
+
const j = readJourney(this.opts.dataDir, now);
|
|
1037
|
+
const staleLimitSec = 600;
|
|
1038
|
+
const travel: WorldLamp = !j.installed
|
|
1039
|
+
? { label: '旅行', state: 'offline', hint: '没装旅行扩展 —— 不知道她在哪座城,日子照过' }
|
|
1040
|
+
: j.staleSec > staleLimitSec
|
|
1041
|
+
? { label: '旅行', state: 'error', hint: `旅行那边 ${Math.round(j.staleSec / 60)} 分钟没更新了,位置可能已经过时` }
|
|
1042
|
+
: {
|
|
1043
|
+
label: '旅行',
|
|
1044
|
+
state: 'online',
|
|
1045
|
+
hint: j.phase === 'traveling'
|
|
1046
|
+
? `在路上,往${j.toName || '下一站'}去`
|
|
1047
|
+
: j.phase === 'staying' ? `在${j.cityName || '外地'}` : `在家${j.cityName ? `(${j.cityName})` : ''}`,
|
|
1048
|
+
};
|
|
1049
|
+
|
|
1050
|
+
return [alive, town, travel];
|
|
1051
|
+
}
|
|
1052
|
+
|
|
972
1053
|
webUi(): WorldWebUiDecl {
|
|
973
1054
|
return {
|
|
974
1055
|
label: '模拟鱼生',
|
|
1056
|
+
lamps: this.worldLamps(),
|
|
975
1057
|
panels: [
|
|
976
1058
|
{
|
|
977
1059
|
id: 'now',
|