cortico-world-parlor 0.26.7 → 0.26.8

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 CHANGED
@@ -1,3 +1,16 @@
1
+ ## 0.26.8
2
+
3
+ 把「登录十几秒」和「挂了没人管」两件事一起治了。
4
+
5
+ - **换席位不再无限等**:`/v1/parent/play` 这条以前是裸 `fetch`,没有超时。现在和别的接口一样
6
+ 25 秒超时,超时落到「连不上客厅 + 重试」,钥匙留在内存里,「重试」会再用它试一次。
7
+ 加入页的「加入客厅」也加了同样的超时。
8
+ - **少两个来回**:带钥匙下场改成一次请求(`POST /v1/parent/play {key}` 顺手建会话 + 设 cookie),
9
+ 不再先 `/v1/parent/session` 再一次 play;首屏两次连续快照去掉一次,SSE 连上后的那次补拉还在。
10
+ - **牌图不抢带宽**:`cards.webp` 是 346KB,以前在首屏就拉,跨境链路上把登录的几个来回挤到十几秒。
11
+ 现在第一次真要画牌时才拉,图没到先按点数画,到了 CSS 自动切牌面;缺图仍是纯文字。
12
+ 实测首屏请求从 15 个降到 12 个,其中没有牌图;登录交换从 2 次请求变 1 次。
13
+
1
14
  ## 0.26.7
2
15
 
3
16
  - 环境提示词补一句「大厅不能开桌」:默认落大厅之后,新装的 Coo 常先去 `parlor_table_open` 撞一次错;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-parlor",
3
- "version": "0.26.7",
3
+ "version": "0.26.8",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "筆離嘢"
@@ -546,8 +546,20 @@ export class WorldServer {
546
546
  }
547
547
 
548
548
  if (method === 'POST' && url.pathname === '/v1/parent/play') {
549
- const session = this.parentSession(req);
550
549
  const body = await readJson(req);
550
+ // 带钥匙时一次做完「换会话 + 领席位」:跨境链路上每多一个来回就是两三秒。
551
+ const key = typeof body.key === 'string' ? body.key.trim() : '';
552
+ if (key) {
553
+ this.checkParentKeyRate(req);
554
+ const account = this.coo.verifyOwnerKey(key);
555
+ if (!account) throw new HttpError(401, '这个家长链接不对,或已经失效了');
556
+ const fresh = this.coo.createSession(account.id);
557
+ if (!fresh) throw new HttpError(403, '这个家长账户被停用了');
558
+ res.setHeader('set-cookie', this.parentCookie(fresh.token, req));
559
+ json(res, 200, this.parentPlay(account, body));
560
+ return;
561
+ }
562
+ const session = this.parentSession(req);
551
563
  json(res, 200, this.parentPlay(session.account, body));
552
564
  return;
553
565
  }
package/src/web/common.js CHANGED
@@ -122,8 +122,12 @@ export const btn = (label, onClick, options = {}) => {
122
122
  const SPRITE_COLS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
123
123
  const SPRITE_ROWS = { S: 0, H: 1, C: 2, D: 3 };
124
124
 
125
+ /** 牌面图拉没拉过:idle / loading / ready / missing。 */
126
+ let cardArtState = 'idle';
127
+
125
128
  /** 给一张牌画上真牌面;服务器没给花色(老回放、或没装图)就只留文字。 */
126
129
  export const paintCard = (node, rank, suit) => {
130
+ probeCardArt();
127
131
  const row = suit && SPRITE_ROWS[suit] !== undefined ? SPRITE_ROWS[suit] : 4;
128
132
  const col = row === 4 ? (rank === 'BJ' ? 0 : 1) : SPRITE_COLS.indexOf(rank);
129
133
  if (col < 0) return node;
@@ -133,10 +137,23 @@ export const paintCard = (node, rank, suit) => {
133
137
  return node;
134
138
  };
135
139
 
136
- /** 牌面图缺了就在 <html> 上挂 no-card-art,CSS 退回纯文字,桌子照常。 */
140
+ /**
141
+ * 牌面雪碧图 346KB。以前在首屏就拉,跨境链路上它把登录的几个来回挤到十几秒;
142
+ * 改成第一次真要画牌时才拉:图没到先按点数画,到了挂 `card-art-ready` 由 CSS 切牌面,
143
+ * 缺图就挂 `no-card-art` 永远用文字。
144
+ */
137
145
  export const probeCardArt = () => {
146
+ if (cardArtState !== 'idle') return;
147
+ cardArtState = 'loading';
138
148
  const img = new Image();
139
- img.addEventListener('error', () => document.documentElement.classList.add('no-card-art'));
149
+ img.addEventListener('load', () => {
150
+ cardArtState = 'ready';
151
+ document.documentElement.classList.add('card-art-ready');
152
+ });
153
+ img.addEventListener('error', () => {
154
+ cardArtState = 'missing';
155
+ document.documentElement.classList.add('no-card-art');
156
+ });
140
157
  img.src = '/play/assets/assets/cards/cards.webp';
141
158
  };
142
159
 
package/src/web/play.css CHANGED
@@ -1688,7 +1688,9 @@ input:focus, select:focus { outline: none; border-color: var(--accent-border); b
1688
1688
  transform: rotate(var(--rot, 0deg)) translateY(calc(var(--lift, 0px) + var(--arc, 0px)));
1689
1689
  transition: transform .12s ease, box-shadow .12s ease, filter .12s ease;
1690
1690
  }
1691
- .pcard.has-art {
1691
+ /* 牌:默认是点数;牌面雪碧图加载好了(html.card-art-ready)才切成真牌面。 */
1692
+ .pcard.has-art { color: inherit; }
1693
+ html.card-art-ready .pcard.has-art {
1692
1694
  background-image: url(assets/cards/cards.webp);
1693
1695
  background-size: 1300% 500%;
1694
1696
  background-position: calc(var(--c) * 100% / 12) calc(var(--r) * 100% / 4);
package/src/web/play.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * 牌桌 tab 发的是桌内私聊(只有同桌看得见),客厅 tab 才是全房间公开。 */
4
4
  import {
5
5
  el, clear, icon, initialOf, fmtClock, fmtDateTime, fmtLeft, clockSpan, btn, playerClass,
6
- isNearBottom, readable, probeCardArt, GAME_LABELS, STATUS_LABELS, KIND_LABELS,
6
+ isNearBottom, readable, GAME_LABELS, STATUS_LABELS, KIND_LABELS,
7
7
  } from './common.js';
8
8
  import { ENABLED_GAMES, GAMES, gameOf, gameLabel, gameIcon, heroGame } from './games/registry.js';
9
9
 
@@ -101,8 +101,7 @@ function bootFailureText(error) {
101
101
  return error && error.message ? error.message : String(error);
102
102
  }
103
103
 
104
- probeCardArt();
105
- boot();
104
+ boot(); // 牌面图改成第一次画牌时才拉,见 common.js 的 probeCardArt
106
105
 
107
106
  // 在已经打开的页面地址栏里粘一条新的「我下场玩」链接:hash 变了但页面不会重载,
108
107
  // 这里主动换一次钥匙,换好再整页重来,免得旧席位的日志和牌桌还挂着。
@@ -157,9 +156,8 @@ async function boot() {
157
156
  state.tab = hasLiveTable() ? 'table' : 'room';
158
157
  buildShell();
159
158
  render();
159
+ // 回放补历史;它之后不再补一次快照——SSE 从第一份快照的游标接上,中间漏的会补发。
160
160
  await seedLog();
161
- await refreshSnapshot();
162
- render();
163
161
  if (state.view === 'hall') void loadHall(true);
164
162
  connect();
165
163
  void loadDmThreads();
@@ -183,7 +181,7 @@ function takeKeyFromHash() {
183
181
 
184
182
  async function hasParentSession() {
185
183
  try {
186
- return (await fetch('/v1/parent/me')).ok;
184
+ return (await fetch('/v1/parent/me', timeoutSignal())).ok;
187
185
  } catch {
188
186
  return false;
189
187
  }
@@ -197,25 +195,15 @@ async function hasParentSession() {
197
195
  async function tryParentEntry(key = '', options = {}) {
198
196
  const before = state.cfg && state.cfg.name;
199
197
  try {
200
- if (key) {
201
- const session = await fetch('/v1/parent/session', {
202
- method: 'POST',
203
- headers: { 'content-type': 'application/json' },
204
- body: JSON.stringify({ key }),
205
- });
206
- if (!session.ok) {
207
- const body = await session.json().catch(() => ({}));
208
- showToast(body.error || '这个家长链接不对,或已经失效了', 'bad');
209
- return false;
210
- }
211
- }
198
+ // 带钥匙时一次做完「换会话 + 领席位」:跨境链路上每多一个来回就是两三秒。
212
199
  const played = await fetch('/v1/parent/play', {
213
200
  method: 'POST',
214
201
  headers: { 'content-type': 'application/json' },
215
- body: '{}',
202
+ body: JSON.stringify(key ? { key } : {}),
203
+ ...timeoutSignal(),
216
204
  });
217
205
  const body = await played.json().catch(() => ({}));
218
- if (!played.ok) throw new Error(body.error || '现在下不了场');
206
+ if (!played.ok) throw new Error(body.error || (key ? '这个家长链接不对,或已经失效了' : '现在下不了场'));
219
207
  if (options.hadSeat) {
220
208
  state.latest = 0;
221
209
  state.seen = new Set();
@@ -236,7 +224,7 @@ async function tryParentEntry(key = '', options = {}) {
236
224
  }
237
225
  return true;
238
226
  } catch (error) {
239
- showToast(error.message || '现在下不了场', 'bad');
227
+ showToast(bootFailureText(error), 'bad');
240
228
  return false;
241
229
  }
242
230
  }
@@ -2969,6 +2957,7 @@ function renderJoin(message = '') {
2969
2957
  method: 'POST',
2970
2958
  headers: { 'content-type': 'application/json', 'x-parlor-invite': invite },
2971
2959
  body: JSON.stringify({ name, ...(wantedRoom ? { room: wantedRoom } : {}) }),
2960
+ ...timeoutSignal(),
2972
2961
  });
2973
2962
  const body = await response.json().catch(() => ({}));
2974
2963
  if (!response.ok) throw new Error(body.error || '加入失败');