/* scense 游戏列表入口页渲染(bricks 页面内 Html 脚本区调用) */
window.scenseGameList = (function () {
function api(url, params) {
return fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(params || {})
}).then(function (r) { return r.json(); });
}
function base() {
return window.location.pathname.replace(/\/game\/.*$/, '');
}
function statusText(s) {
var map = {created: '已创建', playing: '进行中', paused: '已暂停', ended: '已结束'};
return map[s] || s;
}
function renderSessions(sessions) {
var box = document.getElementById('scense_session_list');
if (!box) return;
if (!sessions || !sessions.length) { box.innerHTML = '
暂无历史会话
'; return; }
var html = '';
sessions.forEach(function (s) {
html += '' +
'
' + s.game_name + ' ' +
(s.score || 0) + ' 分 / Lv.' + (s.level || 1) + '
' +
'
' +
statusText(s.status) + '' +
'
';
});
box.innerHTML = html;
}
function renderResults(results) {
var box = document.getElementById('scense_result_list');
if (!box) return;
if (!results || !results.length) { box.innerHTML = '暂无结算记录
'; return; }
var html = '';
results.forEach(function (r) {
html += '' +
'' + r.game_id + ' ' + r.created_at + '' +
'' + r.score + ' 分 · Lv.' + r.level + ' · ' +
Math.round(r.duration_ms / 1000) + 's
';
});
box.innerHTML = html;
}
function load() {
api(base() + '/game/api/game_list.dspy', {}).then(function (res) {
if (res && res.success && res.data) {
renderSessions(res.data.sessions);
renderResults(res.data.results);
}
});
}
function startGame(gameId, sessionId) {
var url = base() + '/game/game_play.html?game_id=' + encodeURIComponent(gameId || '');
if (sessionId) url += '&session_id=' + encodeURIComponent(sessionId);
window.open(url, '_blank');
}
return {api: api, load: load, startGame: startGame, renderSessions: renderSessions, renderResults: renderResults};
})();
(function () {
if (window.bricks && window.bricks.app) {
setTimeout(function () { window.scenseGameList && window.scenseGameList.load(); }, 300);
}
})();