cortico-world-fun 0.2.1 → 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/CHANGELOG.md +19 -0
- package/PUBLISHING.md +57 -205
- package/README.md +59 -5
- package/describe.json +171 -10
- package/dist/artwork.js +64 -0
- package/dist/stories.js +395 -0
- package/dist/webui.js +383 -74
- package/package.json +5 -5
- package/src/ENV_PROMPT.md +2 -1
- package/src/branches.js +62 -0
- package/src/definition.js +16 -4
- package/src/exploration.js +268 -0
- package/src/journeys.js +65 -0
- package/src/keepsakes.js +28 -6
- package/src/recommendations.js +109 -0
- package/src/state.js +71 -39
- package/src/story-examples.js +111 -0
- package/src/story-library.js +149 -0
- package/src/story-packs.js +428 -0
- package/src/world.js +425 -124
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-fun",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "娱乐时间 —— 给 Coopanion 桌宠二十处真能去逛的地方:游乐场、海洋馆、庙会、漫展、电影院、动物园、公园、电玩城、KTV、博物馆、猫咖、爬山、咖啡店、奶茶店、餐厅、图书馆、夜市、澡堂、旧货市场、江边。进门、逛摊位、买纪念品、拍张照、吃喝点什么,收摊记一笔。能和「鱼的每一天」「生图工作室」联动,但都不依赖。",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,13 +39,13 @@
|
|
|
39
39
|
"PUBLISHING.md"
|
|
40
40
|
],
|
|
41
41
|
"scripts": {
|
|
42
|
-
"test": "node test/venues.test.mjs && node test/state.test.mjs && node test/events.test.mjs && node test/memories.test.mjs && node test/phases.test.mjs && node test/keepsakes.test.mjs && node test/user-venues.test.mjs && node test/aquarium.test.mjs && node test/wishes.test.mjs && node test/choices.test.mjs && node test/world.test.mjs && node test/panels.test.mjs",
|
|
42
|
+
"test": "node test/story-packs.test.mjs && node test/story-world.test.mjs && node test/journal-world.test.mjs && node test/branches.test.mjs && node test/journeys.test.mjs && node test/exploration.test.mjs && node test/venues.test.mjs && node test/state.test.mjs && node test/events.test.mjs && node test/memories.test.mjs && node test/phases.test.mjs && node test/keepsakes.test.mjs && node test/user-venues.test.mjs && node test/aquarium.test.mjs && node test/wishes.test.mjs && node test/choices.test.mjs && node test/world.test.mjs && node test/panels.test.mjs",
|
|
43
43
|
"verify": "node tools/fix-ps1-bom.mjs install.ps1 && node tools/check-prompt.mjs && node tools/check-isolation.mjs && node tools/smoke.mjs && node tools/make-describe.mjs --check",
|
|
44
44
|
"describe": "node tools/make-describe.mjs",
|
|
45
45
|
"check:publish": "node tools/publish-check.mjs --online",
|
|
46
46
|
"check:isolation": "node tools/check-isolation.mjs",
|
|
47
47
|
"verify:published": "node tools/verify-published.mjs",
|
|
48
|
-
"prepublishOnly": "node test/venues.test.mjs && node test/state.test.mjs && node test/events.test.mjs && node test/memories.test.mjs && node test/phases.test.mjs && node test/keepsakes.test.mjs && node test/user-venues.test.mjs && node test/aquarium.test.mjs && node test/wishes.test.mjs && node test/choices.test.mjs && node test/world.test.mjs && node test/panels.test.mjs && node tools/check-prompt.mjs && node tools/check-isolation.mjs && node tools/smoke.mjs && node tools/make-describe.mjs --check && node tools/publish-check.mjs --online"
|
|
48
|
+
"prepublishOnly": "node test/story-packs.test.mjs && node test/story-world.test.mjs && node test/journal-world.test.mjs && node test/branches.test.mjs && node test/journeys.test.mjs && node test/exploration.test.mjs && node test/venues.test.mjs && node test/state.test.mjs && node test/events.test.mjs && node test/memories.test.mjs && node test/phases.test.mjs && node test/keepsakes.test.mjs && node test/user-venues.test.mjs && node test/aquarium.test.mjs && node test/wishes.test.mjs && node test/choices.test.mjs && node test/world.test.mjs && node test/panels.test.mjs && node tools/check-prompt.mjs && node tools/check-isolation.mjs && node tools/smoke.mjs && node tools/make-describe.mjs --check && node tools/publish-check.mjs --online"
|
|
49
49
|
},
|
|
50
50
|
"cortico": {
|
|
51
51
|
"kind": "world",
|
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
"locales": {
|
|
61
61
|
"zh": {
|
|
62
62
|
"displayName": "娱乐时间",
|
|
63
|
-
"description": "给 Coo 二十处能真去逛的地方:游乐场、海洋馆、庙会、漫展、电影院、动物园、公园、电玩城、KTV、博物馆、猫咖、爬山、咖啡店、奶茶店、餐厅、图书馆、夜市、澡堂、旧货市场、江边。她说一声进门,一处一处地逛项目、抽个签、买点纪念品、拍张照、喝杯奶茶,收摊时回执把这一趟收成一句话,接着去记进自己的日程。\n\n能和别的插件联动,但都不依赖:装了「鱼的每一天」时,日程叫醒里会顺手提到这块、收摊回执也会提醒她去 feast_life_play 记一笔;咖啡店、奶茶店、餐厅、夜市吃的东西去「鱼的每一天」点一道,那里才有九维口感、香气和余韵——没装就只知道喝了杯奶茶,不编味道。装了「生图工作室」时,收摊回执会告诉她发图该填哪个 scene
|
|
63
|
+
"description": "给 Coo 二十处能真去逛的地方:游乐场、海洋馆、庙会、漫展、电影院、动物园、公园、电玩城、KTV、博物馆、猫咖、爬山、咖啡店、奶茶店、餐厅、图书馆、夜市、澡堂、旧货市场、江边。她说一声进门,一处一处地逛项目、抽个签、买点纪念品、拍张照、喝杯奶茶,收摊时回执把这一趟收成一句话,接着去记进自己的日程。\n\n能和别的插件联动,但都不依赖:装了「鱼的每一天」时,日程叫醒里会顺手提到这块、收摊回执也会提醒她去 feast_life_play 记一笔;咖啡店、奶茶店、餐厅、夜市吃的东西去「鱼的每一天」点一道,那里才有九维口感、香气和余韵——没装就只知道喝了杯奶茶,不编味道。装了「生图工作室」时,收摊回执会告诉她发图该填哪个 scene、哪身衣服;素材库里没有的那几处会明说不用填,绝不替她编一个不存在的场景名。这些插件一个都不装也照样能逛,库挪走了也不影响。\n\n0.3.0 新增用户自填剧情包、两站出游、未完行程恢复、长期探索档案与可检索的出游手账。四个面板配原创地点插画、沿途时间线和纪念品图标。用户剧情与地点配置独立保存,预设只提供少量可停用示例,自填地点无剧情也可正常游玩。"
|
|
64
64
|
},
|
|
65
65
|
"en": {
|
|
66
66
|
"displayName": "Fun time",
|
|
67
|
-
"description": "Twenty places she can actually go: an amusement park, an aquarium, a temple fair, a fan convention, a cinema, a zoo, a park, an arcade, a karaoke box, a museum, a cat cafe, a hike, a coffee shop, a bubble-tea stand, a restaurant, a library, a night market, a bathhouse, a flea market and a riverside walk. She walks in, wanders one spot at a time, buys a souvenir, takes a photo, has something to eat or drink, and the closing receipt sums the trip up in one line she can log into her own day.\n\nWorks alongside other plugins but depends on none. With Fish Every Day installed, the own-time menu points at this world and the closing receipt reminds her to log the trip with feast_life_play; food and drink at the coffee shop, tea stand and restaurant are meant to be ordered from Fish Every Day, where the nine-axis flavour profile and aroma actually live — without it she only knows she had a bubble tea, and she is told not to invent how it tasted. With the image studio installed, the receipt names the scene and outfit to draw; venues the library does not have are marked as such instead of inventing a name. None of them are required, and it still works if the image library is gone."
|
|
67
|
+
"description": "Twenty places she can actually go: an amusement park, an aquarium, a temple fair, a fan convention, a cinema, a zoo, a park, an arcade, a karaoke box, a museum, a cat cafe, a hike, a coffee shop, a bubble-tea stand, a restaurant, a library, a night market, a bathhouse, a flea market and a riverside walk. She walks in, wanders one spot at a time, buys a souvenir, takes a photo, has something to eat or drink, and the closing receipt sums the trip up in one line she can log into her own day.\n\nWorks alongside other plugins but depends on none. With Fish Every Day installed, the own-time menu points at this world and the closing receipt reminds her to log the trip with feast_life_play; food and drink at the coffee shop, tea stand and restaurant are meant to be ordered from Fish Every Day, where the nine-axis flavour profile and aroma actually live — without it she only knows she had a bubble tea, and she is told not to invent how it tasted. With the image studio installed, the receipt names the scene and outfit to draw; venues the library does not have are marked as such instead of inventing a name. None of them are required, and it still works if the image library is gone.\n\nVersion 0.3.0 adds user-authored story packs, two-stop outings, unfinished-trip recovery, permanent exploration and searchable trip journals. Four panels include original venue art, action timelines and souvenir icons. User stories and place settings are stored separately from plugin code; the small preset story collection is optional, and custom places work without stories."
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
}
|
package/src/ENV_PROMPT.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
出游流程:`fun_places` 查询地点 → `fun_go` 进门 → `fun_wander` 逐处游逛 → `fun_home` 收摊;现在出门可用 `fun_start`。地点、项目、纪念品和拍照点名称以工具回执为准;吃喝只填当前地点真实提供的品类。历史与收藏按需用 `fun_view`、`fun_shelf` 查询。
|
|
4
4
|
|
|
5
|
-
-
|
|
5
|
+
- 连逛两处可用 `fun_route` 规划;每站自己进门、收摊,再决定下一站、换一站或提前结束,不自动移动。
|
|
6
|
+
- 中断后用 `fun_view` 找未完行程,再决定续逛或按已有记录收摊。待选事件按回执的选项处理。
|
|
6
7
|
- 明确表达想去或想做时用 `fun_wish` 记录、`fun_wishes` 查询;不催促、不从旧行程推断完成。
|
|
7
8
|
- 发图用 `imggen_draw`:按 `fun_home` 回执填写 `scene` / `outfits` 正名,不编场景或衣服名字;库里没找到的不要填。
|
|
8
9
|
- 食物只记品类。开关联动允许且实际用「鱼的每一天」的 `feast_menu` → `feast_eat` 后才能描述口感;否则只说吃喝了什么,别形容味道。本插件不会探测别的扩展装没装,不依赖其他 World。
|
package/src/branches.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { areasNow } from './phases.js';
|
|
2
|
+
import { VENUES, hoursState } from './venues.js';
|
|
3
|
+
const RIVER = '栏杆外的水面一直在动';
|
|
4
|
+
const MARKET = '走到一半被人流推着往前走';
|
|
5
|
+
const WHALE = '一条大鲸从头顶慢慢游过去,影子把整条隧道盖了一下';
|
|
6
|
+
const JELLY = '看到有人在水母缸前面站了十分钟一动不动';
|
|
7
|
+
const PENGUINS = '企鹅馆正好赶上喂食,一群企鹅扑通扑通全下了水';
|
|
8
|
+
const options = (rows) => rows.map(([id,label,result]) => ({id,label,result}));
|
|
9
|
+
const already = (session,event) => (session?.events ?? []).includes(event) || (session?.choices ?? []).some((c)=>c.event===event) || session?.pendingChoice?.event===event;
|
|
10
|
+
|
|
11
|
+
/** Offers a location-bound decision once; it does not move the companion. */
|
|
12
|
+
export function branchChoiceFor(venueId, area, session, nowMin) {
|
|
13
|
+
const venue=VENUES.find((v)=>v.id===venueId);
|
|
14
|
+
if(!venue || hoursState(venue,nowMin)==='closed' || !areasNow(venue,nowMin).includes(area)) return null;
|
|
15
|
+
if(venueId==='riverside' && area==='栏杆边' && !already(session,RIVER)) {
|
|
16
|
+
const wait = Number(nowMin)<17.5*60;
|
|
17
|
+
return {id:'branch-riverside',event:RIVER,area,prompt:wait?'想在江边等到傍晚亮灯,还是沿步道走走?':'想沿着灯光走,还是坐下来听一会儿水声?',options:options([
|
|
18
|
+
['lights',wait?'等到傍晚亮灯':'沿灯光走走',wait?'决定等到傍晚,再到步道看看沿岸的灯':'决定到步道沿着灯光走走'],
|
|
19
|
+
['water','去长椅坐坐','决定去长椅上听一会儿水声'],
|
|
20
|
+
])};
|
|
21
|
+
}
|
|
22
|
+
if(venueId==='market' && ['烧烤摊','铁板鱿鱼摊'].includes(area) && !already(session,MARKET)) {
|
|
23
|
+
const live=areasNow(venue,nowMin);
|
|
24
|
+
const targets=['糖水铺','卖小玩意儿的摊子','街尾的台阶'].filter((a)=>live.includes(a)).slice(0,2);
|
|
25
|
+
if(targets.length<2) return null;
|
|
26
|
+
return {id:'branch-market',event:MARKET,area,prompt:'夜市里人流往前涌,接下来想往哪一处慢慢逛?',options:options(targets.map((a)=>['to:'+a,'去'+a,'决定接着去'+a+'看看']))};
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const AQUARIUM = [
|
|
32
|
+
[WHALE,'wait','海底隧道','又停在隧道中央时,刚才鲸影经过的位置留在了记忆里'],
|
|
33
|
+
[WHALE,'walk','水母缸','从鲸影下走到水母缸前,抬头看的视线慢慢落回玻璃里的光点'],
|
|
34
|
+
[JELLY,'stay','水母缸','多停留了一会儿之后,才注意到水母张合之间细小的节奏'],
|
|
35
|
+
[JELLY,'move-on','企鹅馆','离开安静的水母缸,企鹅馆的水声让这段路忽然热闹起来'],
|
|
36
|
+
[PENGUINS,'watch','企鹅馆','仍留在企鹅馆看着,水里的小黑影又绕过了玻璃前'],
|
|
37
|
+
[PENGUINS,'photo','纪念品店','逛到纪念品店时,又想起刚才特意记下的企鹅喂食时间'],
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/** A follow-up is emitted only at the chosen real area, with no implicit movement. */
|
|
41
|
+
export function branchFollowup(venueId,area,session,nowMin) {
|
|
42
|
+
const venue=VENUES.find((v)=>v.id===venueId);
|
|
43
|
+
if(!venue || hoursState(venue,nowMin)==='closed' || !areasNow(venue,nowMin).includes(area)) return null;
|
|
44
|
+
const emit=(event,option,target,text,condition=true)=>{
|
|
45
|
+
const id=venueId+'::'+event+'::'+option;
|
|
46
|
+
if(!condition || area!==target || !(session?.choices ?? []).some((c)=>c.event===event&&c.option===option)) return null;
|
|
47
|
+
if((session?.branchSeen ?? []).includes(id) || (session?.events ?? []).includes(text)) return null;
|
|
48
|
+
return {id,event:text};
|
|
49
|
+
};
|
|
50
|
+
if(venueId==='riverside') {
|
|
51
|
+
return emit(RIVER,'lights','步道','沿岸的灯亮起来了,刚才决定等候的这一段江面终于换了颜色',Number(nowMin)>=17.5*60)
|
|
52
|
+
?? emit(RIVER,'water','长椅上','坐上长椅以后,刚才决定慢下来听的水声逐渐清楚了');
|
|
53
|
+
}
|
|
54
|
+
if(venueId==='market') {
|
|
55
|
+
for(const target of ['糖水铺','卖小玩意儿的摊子','街尾的台阶']) {
|
|
56
|
+
const text=target==='糖水铺'?'从人流里转到糖水铺,终于能在摊边停下来看一看招牌':target==='卖小玩意儿的摊子'?'沿刚才选的方向到了小摊前,摊面上的小东西能一件件慢慢看':'走到选好的街尾台阶,人群的声音留在了身后';
|
|
57
|
+
const found=emit(MARKET,'to:'+target,target,text); if(found)return found;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if(venueId==='aquarium') for(const [event,option,target,text] of AQUARIUM) {const found=emit(event,option,target,text);if(found)return found;}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
package/src/definition.js
CHANGED
|
@@ -16,8 +16,12 @@ const TOOL_SUMMARIES = {
|
|
|
16
16
|
en: 'Start a trip and return the current-time description and available areas.',
|
|
17
17
|
},
|
|
18
18
|
fun_choice: {
|
|
19
|
-
zh: '
|
|
20
|
-
en: 'Resolve a
|
|
19
|
+
zh: '处理待选择事件,可按回执选择或略过。',
|
|
20
|
+
en: 'Resolve or skip a pending event choice.',
|
|
21
|
+
},
|
|
22
|
+
fun_resume: {
|
|
23
|
+
zh: '继续一趟未逛完的行程。',
|
|
24
|
+
en: 'Resume an unfinished outing.',
|
|
21
25
|
},
|
|
22
26
|
fun_wander: {
|
|
23
27
|
zh: '逛一个项目,可记录本次购买、拍照或吃喝;待选择事件按回执处理。',
|
|
@@ -31,9 +35,13 @@ const TOOL_SUMMARIES = {
|
|
|
31
35
|
zh: '查看当前推荐地点;详细筛选用 fun_places。',
|
|
32
36
|
en: 'Show current place recommendations; use fun_places for filters.',
|
|
33
37
|
},
|
|
38
|
+
fun_route: {
|
|
39
|
+
zh: '规划和管理最多两站的出门路线,明确前往下一站或提前结束。',
|
|
40
|
+
en: 'Plan and manage a two-stop outing, explicitly move on or end early.',
|
|
41
|
+
},
|
|
34
42
|
fun_view: {
|
|
35
|
-
zh: '
|
|
36
|
-
en: '
|
|
43
|
+
zh: '查看正在逛、未逛完或已收摊的行程。',
|
|
44
|
+
en: 'View active, unfinished, or finished outings.',
|
|
37
45
|
},
|
|
38
46
|
fun_wishes: {
|
|
39
47
|
zh: '查看明确记下的出游心愿和状态,不催促。',
|
|
@@ -47,6 +55,10 @@ const TOOL_SUMMARIES = {
|
|
|
47
55
|
zh: '珍藏或取消珍藏一趟行程,不影响纪念册近期列表和长期收藏。',
|
|
48
56
|
en: 'Keep or unkeep a trip without changing the recent trip book or permanent collection.',
|
|
49
57
|
},
|
|
58
|
+
fun_story: {
|
|
59
|
+
zh: '管理、校验与预览本地地点剧情包,支持自填地点。',
|
|
60
|
+
en: 'Manage, validate, and preview local story packs, including custom places.',
|
|
61
|
+
},
|
|
50
62
|
fun_shelf: {
|
|
51
63
|
zh: '按地点查询长期纪念品收藏和收集进度。',
|
|
52
64
|
en: 'Query long-term souvenirs and collection progress by place.',
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
const text = (value, max) => typeof value === 'string'
|
|
2
|
+
? value.replace(/\s+/g, ' ').trim().slice(0, max)
|
|
3
|
+
: '';
|
|
4
|
+
const DAY_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
5
|
+
const asArray = (value) => Array.isArray(value) ? value : [];
|
|
6
|
+
const count = (value) => Number.isFinite(Number(value)) ? Math.max(0, Math.floor(Number(value))) : 0;
|
|
7
|
+
|
|
8
|
+
function uniqueText(values, max) {
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
for (const value of asArray(values)) {
|
|
11
|
+
const item = text(value, max);
|
|
12
|
+
if (item) seen.add(item);
|
|
13
|
+
}
|
|
14
|
+
return [...seen];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function tripId(trip) {
|
|
18
|
+
const explicit = text(trip?.id, 120);
|
|
19
|
+
if (explicit) return explicit;
|
|
20
|
+
// Legacy state normalization assigns this same stable shape to trips without IDs.
|
|
21
|
+
const placeId = text(trip?.placeId, 40);
|
|
22
|
+
if (!placeId) return '';
|
|
23
|
+
const enteredAt = Number.isFinite(Number(trip?.enteredAt)) ? Number(trip.enteredAt) : 0;
|
|
24
|
+
const leftAt = Number.isFinite(Number(trip?.leftAt)) ? Number(trip.leftAt) : 0;
|
|
25
|
+
return `${placeId}-${enteredAt}-${leftAt}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function tripSequence(trip) {
|
|
29
|
+
return Number.isSafeInteger(trip?.sequence) && trip.sequence > 0 ? trip.sequence : 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function emptyEntry(placeId, placeName = '') {
|
|
33
|
+
return {
|
|
34
|
+
placeId,
|
|
35
|
+
placeName: text(placeName, 40) || placeId,
|
|
36
|
+
visits: 0,
|
|
37
|
+
lastSequence: 0,
|
|
38
|
+
areas: [],
|
|
39
|
+
areaCounts: Object.create(null),
|
|
40
|
+
photos: [],
|
|
41
|
+
events: [],
|
|
42
|
+
phases: [],
|
|
43
|
+
firstDay: '',
|
|
44
|
+
lastDay: '',
|
|
45
|
+
// Only legacy trips need a full ID set; sequenced trips use lastSequence.
|
|
46
|
+
tripIds: [],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function normalizeEntry(raw) {
|
|
51
|
+
const placeId = text(raw?.placeId, 40);
|
|
52
|
+
if (!placeId) return null;
|
|
53
|
+
const out = emptyEntry(placeId, raw?.placeName);
|
|
54
|
+
out.visits = count(raw?.visits);
|
|
55
|
+
out.lastSequence = Number.isSafeInteger(raw?.lastSequence) && raw.lastSequence > 0 ? raw.lastSequence : 0;
|
|
56
|
+
out.areas = uniqueText(raw?.areas, 40);
|
|
57
|
+
out.photos = uniqueText(raw?.photos, 60);
|
|
58
|
+
out.events = uniqueText(raw?.events, 120);
|
|
59
|
+
out.phases = uniqueText(raw?.phases, 60);
|
|
60
|
+
const counts = raw?.areaCounts && typeof raw.areaCounts === 'object' && !Array.isArray(raw.areaCounts)
|
|
61
|
+
? Object.entries(raw.areaCounts)
|
|
62
|
+
: [];
|
|
63
|
+
for (const [rawArea, rawCount] of counts) {
|
|
64
|
+
const area = text(rawArea, 40);
|
|
65
|
+
const value = count(rawCount);
|
|
66
|
+
if (area && value) out.areaCounts[area] = value;
|
|
67
|
+
}
|
|
68
|
+
// Older/incomplete records may have counts without the corresponding set entry.
|
|
69
|
+
for (const area of Object.keys(out.areaCounts)) if (!out.areas.includes(area)) out.areas.push(area);
|
|
70
|
+
out.firstDay = DAY_RE.test(text(raw?.firstDay, 10)) ? text(raw.firstDay, 10) : '';
|
|
71
|
+
out.lastDay = DAY_RE.test(text(raw?.lastDay, 10)) ? text(raw.lastDay, 10) : '';
|
|
72
|
+
out.tripIds = uniqueText(raw?.tripIds, 120);
|
|
73
|
+
out._hadTripIds = Array.isArray(raw?.tripIds);
|
|
74
|
+
out._legacyAggregate = !out._hadTripIds && out.visits > 0;
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function normalizedRawLedger(raw) {
|
|
79
|
+
const rows = Array.isArray(raw) ? raw : (Array.isArray(raw?.places) ? raw.places : []);
|
|
80
|
+
const map = new Map();
|
|
81
|
+
for (const value of rows) {
|
|
82
|
+
const row = normalizeEntry(value);
|
|
83
|
+
if (!row) continue;
|
|
84
|
+
const prior = map.get(row.placeId);
|
|
85
|
+
if (!prior) {
|
|
86
|
+
map.set(row.placeId, row);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
// Repeated rows are malformed input, not separate lifetime histories.
|
|
90
|
+
prior.visits = Math.max(prior.visits, row.visits);
|
|
91
|
+
prior.lastSequence = Math.max(prior.lastSequence, row.lastSequence);
|
|
92
|
+
for (const area of row.areas) if (!prior.areas.includes(area)) prior.areas.push(area);
|
|
93
|
+
for (const [area, value] of Object.entries(row.areaCounts)) prior.areaCounts[area] = Math.max(prior.areaCounts[area] || 0, value);
|
|
94
|
+
for (const key of ['photos', 'events', 'phases']) {
|
|
95
|
+
for (const item of row[key]) if (!prior[key].includes(item)) prior[key].push(item);
|
|
96
|
+
}
|
|
97
|
+
for (const id of row.tripIds) if (!prior.tripIds.includes(id)) prior.tripIds.push(id);
|
|
98
|
+
if (row.firstDay && (!prior.firstDay || row.firstDay < prior.firstDay)) prior.firstDay = row.firstDay;
|
|
99
|
+
if (row.lastDay && (!prior.lastDay || row.lastDay > prior.lastDay)) prior.lastDay = row.lastDay;
|
|
100
|
+
if (!prior.placeName || prior.placeName === prior.placeId) prior.placeName = row.placeName;
|
|
101
|
+
prior._hadTripIds ||= row._hadTripIds;
|
|
102
|
+
prior._legacyAggregate ||= row._legacyAggregate;
|
|
103
|
+
}
|
|
104
|
+
return map;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function serialize(map) {
|
|
108
|
+
return [...map.values()].map((entry) => {
|
|
109
|
+
const { _hadTripIds, _legacyAggregate, ...plain } = entry;
|
|
110
|
+
return {
|
|
111
|
+
...plain,
|
|
112
|
+
areas: [...entry.areas],
|
|
113
|
+
areaCounts: Object.fromEntries(Object.entries(entry.areaCounts)),
|
|
114
|
+
photos: [...entry.photos],
|
|
115
|
+
events: [...entry.events],
|
|
116
|
+
phases: [...entry.phases],
|
|
117
|
+
tripIds: [...entry.tripIds],
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function dayFor(trip, timezone) {
|
|
123
|
+
const stored = text(trip?.leftDay, 10);
|
|
124
|
+
if (DAY_RE.test(stored)) return stored;
|
|
125
|
+
const leftAt = Number(trip?.leftAt);
|
|
126
|
+
if (!Number.isFinite(leftAt) || !leftAt) return '';
|
|
127
|
+
try {
|
|
128
|
+
const day = new Intl.DateTimeFormat('en-CA', {
|
|
129
|
+
timeZone: timezone,
|
|
130
|
+
year: 'numeric',
|
|
131
|
+
month: '2-digit',
|
|
132
|
+
day: '2-digit',
|
|
133
|
+
}).format(new Date(leftAt));
|
|
134
|
+
return DAY_RE.test(day) ? day : '';
|
|
135
|
+
} catch {
|
|
136
|
+
return '';
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function addUnique(target, values, max) {
|
|
141
|
+
for (const value of uniqueText(values, max)) if (!target.includes(value)) target.push(value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function addTripFacts(entry, trip, timezone) {
|
|
145
|
+
entry.visits += 1;
|
|
146
|
+
const visited = [];
|
|
147
|
+
for (const item of asArray(trip?.visited)) {
|
|
148
|
+
const area = text(item?.area, 40);
|
|
149
|
+
if (!area) continue;
|
|
150
|
+
visited.push(area);
|
|
151
|
+
entry.areaCounts[area] = (entry.areaCounts[area] || 0) + 1;
|
|
152
|
+
}
|
|
153
|
+
addUnique(entry.areas, visited, 40);
|
|
154
|
+
addUnique(entry.photos, trip?.photos, 60);
|
|
155
|
+
addUnique(entry.events, trip?.events, 120);
|
|
156
|
+
addUnique(entry.phases, trip?.phases, 60);
|
|
157
|
+
const day = dayFor(trip, timezone);
|
|
158
|
+
if (day && (!entry.firstDay || day < entry.firstDay)) entry.firstDay = day;
|
|
159
|
+
if (day && (!entry.lastDay || day > entry.lastDay)) entry.lastDay = day;
|
|
160
|
+
entry.placeName = text(trip?.placeName, 40) || entry.placeName || entry.placeId;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function addTrip(entry, trip, id, timezone) {
|
|
164
|
+
const sequence = tripSequence(trip);
|
|
165
|
+
if (sequence) {
|
|
166
|
+
if (sequence <= entry.lastSequence) return false;
|
|
167
|
+
// Older ledgers stored every trip ID. Preserve those IDs, but do not infer
|
|
168
|
+
// sequence values from their arbitrary text; a matching ID is already counted.
|
|
169
|
+
if (entry.tripIds.includes(id)) {
|
|
170
|
+
entry.lastSequence = Math.max(entry.lastSequence, sequence);
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
addTripFacts(entry, trip, timezone);
|
|
174
|
+
entry.lastSequence = sequence;
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
// Truly legacy trips have no usable sequence and need explicit ID provenance.
|
|
178
|
+
if (entry.tripIds.includes(id)) return false;
|
|
179
|
+
entry.tripIds.push(id);
|
|
180
|
+
addTripFacts(entry, trip, timezone);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function retainedTrips(input) {
|
|
185
|
+
const rows = Array.isArray(input)
|
|
186
|
+
? input
|
|
187
|
+
: [...asArray(input?.trips), ...asArray(input?.keepsakeTrips)];
|
|
188
|
+
const seen = new Set();
|
|
189
|
+
const out = [];
|
|
190
|
+
for (const trip of rows) {
|
|
191
|
+
if (!trip || typeof trip !== 'object' || Array.isArray(trip)) continue;
|
|
192
|
+
const placeId = text(trip.placeId, 40);
|
|
193
|
+
const id = tripId(trip);
|
|
194
|
+
if (!placeId || !id || seen.has(id)) continue;
|
|
195
|
+
seen.add(id);
|
|
196
|
+
out.push({ trip, placeId, id });
|
|
197
|
+
}
|
|
198
|
+
return out.sort((a, b) => tripSequence(a.trip) - tripSequence(b.trip));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Normalize the compact lifetime exploration ledger, seeding places absent from
|
|
203
|
+
* it using only the retained trip records supplied by the caller. `trips` may be
|
|
204
|
+
* an array or `{ trips, keepsakeTrips }`; duplicate trip IDs are counted once.
|
|
205
|
+
* Existing aggregate rows without provenance are migrated conservatively: their
|
|
206
|
+
* matching retained records are marked as represented, never added again.
|
|
207
|
+
* Positive `trip.sequence` values use `lastSequence` as a compact per-place
|
|
208
|
+
* high-water mark; `tripIds` remains for legacy/unsequenced IDs.
|
|
209
|
+
*
|
|
210
|
+
* Entry shape: `{ placeId, placeName, visits, lastSequence, areas, areaCounts,
|
|
211
|
+
* photos, events, phases, firstDay, lastDay, tripIds }`.
|
|
212
|
+
*/
|
|
213
|
+
export function normalizeExploration(raw, trips = [], timezone = 'Asia/Shanghai') {
|
|
214
|
+
const map = normalizedRawLedger(raw);
|
|
215
|
+
for (const { trip, placeId, id } of retainedTrips(trips)) {
|
|
216
|
+
let entry = map.get(placeId);
|
|
217
|
+
if (!entry) {
|
|
218
|
+
entry = emptyEntry(placeId, trip.placeName);
|
|
219
|
+
entry._hadTripIds = true;
|
|
220
|
+
map.set(placeId, entry);
|
|
221
|
+
}
|
|
222
|
+
if (entry._legacyAggregate) {
|
|
223
|
+
const sequence = tripSequence(trip);
|
|
224
|
+
if (sequence) entry.lastSequence = Math.max(entry.lastSequence, sequence);
|
|
225
|
+
else if (!entry.tripIds.includes(id)) entry.tripIds.push(id);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
addTrip(entry, trip, id, timezone);
|
|
229
|
+
entry._hadTripIds = true;
|
|
230
|
+
}
|
|
231
|
+
return serialize(map);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Add one completed trip to a ledger without mutating either input. */
|
|
235
|
+
export function recordExploration(ledger, trip, timezone = 'Asia/Shanghai') {
|
|
236
|
+
const map = normalizedRawLedger(ledger);
|
|
237
|
+
if (!trip || typeof trip !== 'object' || Array.isArray(trip)) return serialize(map);
|
|
238
|
+
const placeId = text(trip.placeId, 40);
|
|
239
|
+
const id = tripId(trip);
|
|
240
|
+
if (!placeId || !id) return serialize(map);
|
|
241
|
+
let entry = map.get(placeId);
|
|
242
|
+
if (!entry) {
|
|
243
|
+
entry = emptyEntry(placeId, trip.placeName);
|
|
244
|
+
entry._hadTripIds = true;
|
|
245
|
+
map.set(placeId, entry);
|
|
246
|
+
}
|
|
247
|
+
addTrip(entry, trip, id, timezone);
|
|
248
|
+
entry._hadTripIds = true;
|
|
249
|
+
return serialize(map);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Return a copy of one place's lifetime entry, or null if it has no history. */
|
|
253
|
+
export function explorationFor(ledger, placeId) {
|
|
254
|
+
const key = text(placeId, 40);
|
|
255
|
+
if (!key) return null;
|
|
256
|
+
const row = normalizedRawLedger(ledger).get(key);
|
|
257
|
+
if (!row) return null;
|
|
258
|
+
const { _hadTripIds, ...entry } = row;
|
|
259
|
+
return {
|
|
260
|
+
...entry,
|
|
261
|
+
areas: [...row.areas],
|
|
262
|
+
areaCounts: Object.fromEntries(Object.entries(row.areaCounts)),
|
|
263
|
+
photos: [...row.photos],
|
|
264
|
+
events: [...row.events],
|
|
265
|
+
phases: [...row.phases],
|
|
266
|
+
tripIds: [...row.tripIds],
|
|
267
|
+
};
|
|
268
|
+
}
|
package/src/journeys.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const clean = (v, n = 160) => typeof v === 'string' ? v.replace(/\s+/g, ' ').trim().slice(0, n) : '';
|
|
2
|
+
const stamp = (v) => Number.isFinite(Number(v)) && Number(v) > 0 ? Number(v) : null;
|
|
3
|
+
const KINDS = new Set(['enter', 'area', 'event', 'choice', 'buy', 'photo', 'consume', 'close']);
|
|
4
|
+
|
|
5
|
+
export function normalizeTimeline(raw) {
|
|
6
|
+
const points = (Array.isArray(raw) ? raw : []).filter((x) => KINDS.has(x?.kind) && clean(x.text))
|
|
7
|
+
.map((x) => ({ kind: x.kind, text: clean(x.text, 240), at: stamp(x.at), area: clean(x.area, 60), event: clean(x.event, 160) }));
|
|
8
|
+
if (points.length <= 240) return points;
|
|
9
|
+
const enter = points.find((p) => p.kind === 'enter');
|
|
10
|
+
return enter ? [enter, ...points.filter((p) => p !== enter).slice(-239)] : points.slice(-240);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function addMoment(outing, kind, text, at, area = '', event = '') {
|
|
14
|
+
if (!outing || !KINDS.has(kind) || !clean(text)) return;
|
|
15
|
+
if (!Array.isArray(outing.timeline)) outing.timeline = [];
|
|
16
|
+
outing.timeline.push({ kind, text: clean(text, 240), at: stamp(at), area: clean(area, 60), event: clean(event, 160) });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Legacy entries retain their known times; missing associations stay unknown. */
|
|
20
|
+
export function timelineFor(trip, timezone = 'Asia/Shanghai') {
|
|
21
|
+
let points = normalizeTimeline(trip?.timeline);
|
|
22
|
+
if (!points.length && trip) {
|
|
23
|
+
const item = (kind, text, at, area = '', event = '') => points.push({ kind, text, at: stamp(at), area, event });
|
|
24
|
+
if (trip.enteredAt) item('enter', `进了${trip.placeName}`, trip.enteredAt);
|
|
25
|
+
const list = (value) => Array.isArray(value) ? value : [];
|
|
26
|
+
for (const v of list(trip.visited)) if (clean(v?.area)) item('area', v.area + (v.note ? ` · ${v.note}` : ''), v.at, v.area);
|
|
27
|
+
for (const e of list(trip.events)) if (clean(e)) item('event', e, null, '', e);
|
|
28
|
+
for (const c of list(trip.choices)) if (clean(c?.result || c?.option)) item('choice', c.result || c.option, c.at, '', c.event || '');
|
|
29
|
+
for (const s of list(trip.souvenirs)) if (clean(s)) item('buy', `买了${s}`, null);
|
|
30
|
+
for (const p of list(trip.photos)) if (clean(p)) item('photo', `在${p}拍了照`, null);
|
|
31
|
+
for (const a of list(trip.ate)) if (clean(a?.item)) item('consume', `${a.verb || '吃'}了${a.item}`, a.at);
|
|
32
|
+
if (trip.leftAt) item('close', trip.note ? `收摊 · ${trip.note}` : '收摊', trip.leftAt);
|
|
33
|
+
}
|
|
34
|
+
// Keep action order: clock changes and legacy unknown dates must not invent order.
|
|
35
|
+
const clock = new Intl.DateTimeFormat('en-GB', { timeZone: timezone, hour: '2-digit', minute: '2-digit', hourCycle: 'h23' });
|
|
36
|
+
const day = new Intl.DateTimeFormat('en-CA', { timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit' });
|
|
37
|
+
return points.map((p) => ({ ...p, hm: p.at ? clock.format(p.at) : '', day: p.at ? day.format(p.at) : '' }));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeRoute(raw) {
|
|
41
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
42
|
+
const stops = (Array.isArray(raw.stops) ? raw.stops : []).map((x) => ({ placeId: clean(x?.placeId, 40), placeName: clean(x?.placeName, 40) })).filter((x) => x.placeId && x.placeName).slice(0, 2);
|
|
43
|
+
if (stops.length !== 2 || stops[0].placeId === stops[1].placeId || !clean(raw.id, 100)) return null;
|
|
44
|
+
const states = ['planned', 'active', 'between', 'completed', 'cancelled'];
|
|
45
|
+
return { id: clean(raw.id, 100), stops, company: clean(raw.company, 40), startedAt: stamp(raw.startedAt), nextIndex: Math.max(0, Math.min(2, Math.floor(Number(raw.nextIndex) || 0))), status: states.includes(raw.status) ? raw.status : 'planned', tripIds: [...new Set((Array.isArray(raw.tripIds) ? raw.tripIds : []).map((x) => clean(x, 100)).filter(Boolean))].slice(0, 2) };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function normalizeRoutes(raw) {
|
|
49
|
+
const map = new Map();
|
|
50
|
+
for (const r of Array.isArray(raw) ? raw : []) { const value = normalizeRoute(r); if (value) map.set(value.id, value); }
|
|
51
|
+
return [...map.values()];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function storeRoute(state, route) {
|
|
55
|
+
const normalized = normalizeRoute(route);
|
|
56
|
+
if (!normalized) return;
|
|
57
|
+
state.routes = [...(state.routes ?? []).filter((x) => x.id !== normalized.id), normalized];
|
|
58
|
+
state.route = ['planned', 'active', 'between'].includes(normalized.status) ? normalized : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function routeText(route) {
|
|
62
|
+
if (!route) return '';
|
|
63
|
+
const status = { planned: '待出发', active: '正在逛', between: '待下一站', completed: '已结束', cancelled: '已取消后续' };
|
|
64
|
+
return `这次出门:${route.stops.map((x) => x.placeName).join(' → ')}(${status[route.status]},ID:${route.id})`;
|
|
65
|
+
}
|
package/src/keepsakes.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { tripDay, tripSummary } from './state.js';
|
|
2
|
+
import { explorationFor } from './exploration.js';
|
|
2
3
|
const norm = (v, max) => typeof v === 'string' ? v.replace(/\s+/g, ' ').trim().slice(0, max) : '';
|
|
3
4
|
|
|
4
5
|
/** 收藏可来自旧行程,或持久账本。详细回忆只关联仍保存的近期/珍藏行程。 */
|
|
@@ -39,20 +40,41 @@ export function collectSouvenirs(trips, timezone = 'Asia/Shanghai', collection =
|
|
|
39
40
|
return { items, byPlace: [...places.values()].sort((a, b) => b.items.length - a.items.length || a.placeName.localeCompare(b.placeName)), total: items.reduce((n, x) => n + x.times, 0), uniquePlaces: places.size };
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Calculate checklist progress. When an exploration ledger (or its place entry)
|
|
45
|
+
* is supplied, visits/areas/photos come from lifetime history and souvenirs come
|
|
46
|
+
* from the persistent collection; with null/missing history, retain the legacy
|
|
47
|
+
* recent-trip behavior.
|
|
48
|
+
*/
|
|
49
|
+
export function progressFor(place, trips, collection = null, exploration = null) {
|
|
43
50
|
const placeId = norm(place?.id, 40);
|
|
44
51
|
const mine = (Array.isArray(trips) ? trips : []).filter((t) => t && t.placeId === placeId);
|
|
52
|
+
const hasExploration = Array.isArray(exploration) || Array.isArray(exploration?.places)
|
|
53
|
+
|| (exploration && typeof exploration === 'object');
|
|
54
|
+
const lifetime = hasExploration
|
|
55
|
+
? (Array.isArray(exploration) || Array.isArray(exploration?.places)
|
|
56
|
+
? explorationFor(exploration, placeId)
|
|
57
|
+
: (exploration.placeId === placeId ? exploration : null))
|
|
58
|
+
: null;
|
|
45
59
|
const seenAreas = new Set(); const bought = new Set(); const shot = new Set();
|
|
46
|
-
|
|
47
|
-
for (const
|
|
60
|
+
if (hasExploration) {
|
|
61
|
+
for (const area of Array.isArray(lifetime?.areas) ? lifetime.areas : []) { const a = norm(area, 40); if (a) seenAreas.add(a); }
|
|
62
|
+
for (const area of Object.keys(lifetime?.areaCounts && typeof lifetime.areaCounts === 'object' ? lifetime.areaCounts : {})) { const a = norm(area, 40); if (a) seenAreas.add(a); }
|
|
63
|
+
for (const p of Array.isArray(lifetime?.photos) ? lifetime.photos : []) { const x = norm(p, 60); if (x) shot.add(x); }
|
|
64
|
+
} else {
|
|
65
|
+
for (const t of mine) {
|
|
66
|
+
for (const v of Array.isArray(t.visited) ? t.visited : []) { const a = norm(v?.area, 40); if (a) seenAreas.add(a); }
|
|
67
|
+
for (const p of Array.isArray(t.photos) ? t.photos : []) { const x = norm(p, 60); if (x) shot.add(x); }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (!hasExploration) for (const t of mine) {
|
|
48
71
|
for (const s of Array.isArray(t.souvenirs) ? t.souvenirs : []) { const x = norm(s, 40); if (x) bought.add(x); }
|
|
49
|
-
for (const p of Array.isArray(t.photos) ? t.photos : []) { const x = norm(p, 60); if (x) shot.add(x); }
|
|
50
72
|
}
|
|
51
|
-
for (const x of Array.isArray(collection) ? collection : []) if (x?.placeId === placeId)
|
|
73
|
+
for (const x of Array.isArray(collection) ? collection : []) if (x?.placeId === placeId) { const name = norm(x.name, 40); if (name) bought.add(name); }
|
|
52
74
|
const part = (all, seen) => { all = [...new Set((Array.isArray(all) ? all : []).map((x) => norm(x, 60)).filter(Boolean))]; const got = all.filter((x) => seen.has(x)); const left = all.filter((x) => !seen.has(x)); return { got, left, total: all.length, done: got.length, complete: all.length > 0 && !left.length }; };
|
|
53
75
|
const areas = part(place?.areas, seenAreas); const souvenirs = part(place?.souvenirs, bought); const photos = part(place?.photoSpots, shot);
|
|
54
76
|
const denom = areas.total + souvenirs.total + photos.total; const numer = areas.done + souvenirs.done + photos.done;
|
|
55
|
-
return { areas, souvenirs, photos, visits: mine.length, percent: denom ? Math.round(numer / denom * 100) : 0 };
|
|
77
|
+
return { areas, souvenirs, photos, visits: hasExploration ? Math.max(0, Math.floor(Number(lifetime?.visits) || 0)) : mine.length, percent: denom ? Math.round(numer / denom * 100) : 0 };
|
|
56
78
|
}
|
|
57
79
|
export function progressLine(progress, max = 3) {
|
|
58
80
|
const left = progress?.areas?.left ?? []; if (!left.length) return '';
|