game/assets/game-api.js
李鹏宇 a24bdbcff7 world.html: 接入 yt_world 后端地表记忆(增量持久化)
- 登录闸门拉取后端世界存档 applyWorldState,叠加到确定性底图
- 保存按钮改为增量 flushSave(防抖 800ms),不再写 localStorage
- 相机位/缩放经 scheduleSettingsSave 落库(API.saveSettings)
- handleBuildClick/swapTiles/applyHeight/onHeightSlide/flatten/wheel/mouseup 标脏
- game-api.js 对齐 yt_world 接口 world()/saveTiles()/saveSettings()
2026-07-23 21:49:28 +08:00

82 lines
2.9 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.

// 宇森游戏 · 前端与 Odoo 通信层
// 本地调试:前端 8888 -> Odoo 8069跨域带调试 CORS 头)
// 生产部署:前端与 Odoo 同域,由 nginx 代理 /game/*。
// 说明odoo.conf 已 db_name=yt_game 钉死库URL 上的 ?db= 形同虚设,
// 请求一律落到 yt_game下列 game/* 沿用统一 API_CONFIGyt_world/* 单独带 ?db=yt_game。
const API_CONFIG = {
base: (location.hostname === '127.0.0.1' && location.port === '8888')
? 'http://127.0.0.1:8069'
: (location.hostname === 'localhost' && location.port === '8888')
? 'http://127.0.0.1:8069'
: '',
db: (location.hostname === '127.0.0.1' && location.port === '8888') ? 'study' : 'game',
};
const _cred = API_CONFIG.base ? 'include' : 'same-origin';
function _url(path) {
return `${API_CONFIG.base}${path}?db=${API_CONFIG.db}`;
}
const API = {
async me() {
return (await fetch(_url('/game/api/me'), { credentials: _cred })).json();
},
async login(login, password) {
return (await fetch(_url('/game/api/login'), {
method: 'POST', credentials: _cred,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ login, password }),
})).json();
},
async register(payload) {
return (await fetch(_url('/game/api/register'), {
method: 'POST', credentials: _cred,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
})).json();
},
async logout() {
return (await fetch(_url('/game/api/logout'), {
method: 'POST', credentials: _cred,
})).json();
},
async codex() {
return (await fetch(_url('/game/api/codex'), { credentials: _cred })).json();
},
async rules() {
return (await fetch(_url('/game/api/rules'), { credentials: _cred })).json();
},
async currencies() {
return (await fetch(_url('/game/api/currencies'), { credentials: _cred })).json();
},
async pages() {
return (await fetch(_url('/game/api/pages'), { credentials: _cred })).json();
},
async threads() {
return (await fetch(_url('/game/api/threads'), { credentials: _cred })).json();
},
async announcements() {
return (await fetch(_url('/game/api/announcements'), { credentials: _cred })).json();
},
// ---------- 沙盘记忆yt_world 模块) ----------
async world() {
return (await fetch('/yt_world/api/world?db=yt_game', { credentials: 'same-origin' })).json();
},
async saveTiles(tiles) {
return (await fetch('/yt_world/api/tiles/save?db=yt_game', {
method: 'POST', credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ tiles }),
})).json();
},
async saveSettings(settings) {
return (await fetch('/yt_world/api/settings?db=yt_game', {
method: 'POST', credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ settings }),
})).json();
},
};