starraid/star-raid/js/net.js
JOJO ab403f4cc7 后端化:账号系统+排行榜+图鉴收集同步+飞行员大厅(零依赖Node+SQLite)
- server/:node:http+node:sqlite 零依赖后端(users/sessions/user_stats/codex_unlocks/runs 五表)
- 安全:scrypt密码哈希/服务端session/全prepared statement/输入白名单/限流/CSP等安全头/静态白名单防穿越
- 前端:o-auth登陆注册 + o-home飞行员大厅(排行榜Top100三排序+图鉴展柜背景:随机3主3副2僚机+刷怪巡游)
- 图鉴改为收集点亮制(未收集=剪影+???),服务端codex_unlocks与本地并集同步
- 修复:回标题/大厅后背景卡在上场残影——render按battleUI门控+离场G=null;空闲=星空
- 结算上报:endRun/rush胜利两处submitRun,Boss击杀计数入排行榜
- playwright全流程验证通过(注册/登陆/游戏/结算/图鉴/安全注入/限流);游戏视觉未验收(用户亲自)
2026-08-06 04:20:25 +08:00

77 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
/* ================= 网络层:服务端 API 客户端 ================= */
const NET = {
user: null, // {username,email}
stats: null, // {bossKills,endlessHigh,runs}
codexTotal: 62,
online: false,
async api(path, method, body) {
let res;
try {
res = await fetch(path, {
method: method || "GET",
credentials: "same-origin",
headers: body ? { "Content-Type": "application/json" } : undefined,
body: body ? JSON.stringify(body) : undefined,
});
} catch (e) {
return { ok: false, status: 0, data: { error: "无法连接服务器" } };
}
let data = {};
try { data = await res.json(); } catch (e) { /* 空响应 */ }
return { ok: res.ok, status: res.status, data };
},
/* 启动尝试恢复会话。true=已登录 */
async boot() {
const r = await this.api("/api/me", "GET");
if (r.ok) { this._apply(r.data); return true; }
return false;
},
async login(id, password) {
const r = await this.api("/api/login", "POST", { id, password });
if (r.ok) { this._apply(r.data); return null; }
return r.data.error || "登陆失败";
},
async register(username, email, password) {
const r = await this.api("/api/register", "POST", { username, email, password });
if (r.ok) { this._apply(r.data); return null; }
return r.data.error || "注册失败";
},
async logout() {
await this.api("/api/logout", "POST");
this.user = null; this.stats = null; this.online = false;
},
/* 应用服务端返回的用户数据:同步图鉴解锁到本地存档(并集) */
_apply(d) {
this.user = d.user;
this.stats = d.stats;
this.codexTotal = d.codexTotal || 62;
this.online = true;
if (Array.isArray(d.codex)) {
SAVE.codex = SAVE.codex || {};
for (const k of d.codex) SAVE.codex[k] = true;
persist();
}
},
/* 结算上报fire-and-forget成功后回写最新统计 */
submitRun(payload) {
if (!this.user) return;
this.api("/api/run", "POST", payload).then((r) => {
if (r.ok) {
this.stats = r.data.stats;
if (Array.isArray(r.data.codex)) {
for (const k of r.data.codex) SAVE.codex[k] = true;
persist();
}
}
}).catch(() => { /* 网络失败静默:本地存档仍在 */ });
},
};