🔧 开发者菜单
在游戏中数字6未点亮前快速点击3次即可唤出
Cloudflare Worker 代码
// ═══════════════════════════════════════════
// 【熠星 · 视界之维】 Worker API v1.0
// 部署到 Cloudflare Workers
// ═══════════════════════════════════════════
export default {
async fetch(request, env) {
const url = new URL(request.url);
const path = url.pathname;
// CORS 头
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
// 预检请求
if (request.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
try {
// ─────────────────────────────────────────
// 【提交成绩】 POST /api/submit-score
// ─────────────────────────────────────────
if (path === '/api/submit-score' && request.method === 'POST') {
const data = await request.json();
const { user, pin, time, ageGroup, note, sequence } = data;
// 验证必填字段
if (!user || !pin || !time || !ageGroup || !sequence) {
return new Response(JSON.stringify({ error: '缺少必填字段' }), {
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
// 合理性校验:< 3秒 拒绝
if (time < 3) {
return new Response(JSON.stringify({ error: '成绩异常' }), {
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
// 入榜门槛:< 25.2131秒
const canRank = time < 25.2131;
// 生成记录ID
const id = crypto.randomUUID();
const timestamp = Date.now();
const dateKey = new Date().toISOString().split('T')[0];
// 构造记录
const record = {
id,
user: `${user}#${pin}`,
time: parseFloat(time.toFixed(3)),
ageGroup,
note: note || '',
sequence,
timestamp,
canRank
};
// 读取当日成绩
const dayKey = `scores:${dateKey}`;
let dayScores = await env.AETHER_KV.get(dayKey, 'json') || { records: [] };
dayScores.records.push(record);
// 写入KV
await env.AETHER_KV.put(dayKey, JSON.stringify(dayScores), {
expirationTtl: 60 * 60 * 24 * 365 // 保存1年
});
// 如果可入榜,更新排行榜
if (canRank) {
let leaderboard = await env.AETHER_KV.get('leaderboard', 'json') || { records: [] };
leaderboard.records.push(record);
leaderboard.records.sort((a, b) => a.time - b.time);
leaderboard.records = leaderboard.records.slice(0, 100); // 只保留Top100
await env.AETHER_KV.put('leaderboard', JSON.stringify(leaderboard));
}
return new Response(JSON.stringify({
success: true,
id,
canRank,
message: canRank ? '成绩已入榜!' : '成绩已记录'
}), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
// ─────────────────────────────────────────
// 【获取排行榜】 GET /api/leaderboard
// ─────────────────────────────────────────
if (path === '/api/leaderboard' && request.method === 'GET') {
const leaderboard = await env.AETHER_KV.get('leaderboard', 'json') || { records: [] };
return new Response(JSON.stringify(leaderboard), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
// ─────────────────────────────────────────
// 【获取用户历史】 GET /api/history?user=xxx
// ─────────────────────────────────────────
if (path === '/api/history' && request.method === 'GET') {
const userQuery = url.searchParams.get('user');
if (!userQuery) {
return new Response(JSON.stringify({ error: '缺少用户参数' }), {
status: 400,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
// 遍历最近30天的记录
const history = [];
for (let i = 0; i < 30; i++) {
const date = new Date();
date.setDate(date.getDate() - i);
const dateKey = date.toISOString().split('T')[0];
const dayScores = await env.AETHER_KV.get(`scores:${dateKey}`, 'json');
if (dayScores) {
const userRecords = dayScores.records.filter(r => r.user === userQuery);
history.push(...userRecords);
}
}
history.sort((a, b) => b.timestamp - a.timestamp);
return new Response(JSON.stringify({ records: history.slice(0, 100) }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
// 默认响应
return new Response(JSON.stringify({
name: '熠星 · 视界之维 API',
version: '1.0',
endpoints: ['/api/submit-score', '/api/leaderboard', '/api/history']
}), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
});
}
}
};
wrangler.toml 配置
name = "aetherfocus-api"
main = "src/index.js"
compatibility_date = "2024-01-01"
[[kv_namespaces]]
binding = "AETHER_KV"
id = "你的KV命名空间ID"