用户:BCM墨水瓶查看:13 回复:4 评论:13 创建时间:2020-10-10T20:54:50
有人会排行榜代码吗?
有人会排行榜代码吗?
有人会排行榜代码吗?
有人会排行榜代码吗?
有人会排行榜代码吗?
https://docs.box3.codemao.cn/box3databasepreface.html
自己学去!
点赞0
评论
pokOS示例代码1
竞速游戏历史排行榜
let leader; // 当前排行榜的领跑
// 创建排行榜表
async function createTables () {
// 创建表
await db.sql`CREATE TABLE IF NOT EXISTS leaderboard (
name VARCHAR(50) NOT NULL,
record REAL NOT NULL
)`
}
// 向排行榜添加数据
async function insertLeaderboard(name, time) {
try {
await db.sql`INSERT INTO leaderboard VALUES (${name}, ${time})`;
} catch (e) {
console.log(`insert sql error: ${e}`) ;
}
}
// 清空所有数据
async function removeAll() {
await db.sql`DELETE FROM leaderboard`
world.say(`leaderboard 排行榜数据已被清空!`);
}
// 获取排行榜第一名
async function getBestTime() {
const rows = await db.sql`SELECT * FROM leaderboard ORDER BY record ASC LIMIT 1`;
if (rows && !rows.length) {
console.log('no record');
return {name:'吉吉喵', record: '30.0'};
}
world.say(rows[0].name + ' / ' + rows[0].record);
return rows[0];
}
// 获取排行榜Top10
async function getTop10() {
const rows = await db.sql`SELECT * FROM leaderboard ORDER BY record ASC LIMIT 10`;
// 在控制台打印数据
console.log(`result = ${JSON.stringify(rows)}`);
// 将搜索的结果在聊天中显示
for await (const row of rows) {
world.say(row.name + ' | ' + row.record)
}
}
// 获取玩家最高分数
async function getMyBestTime(player) {
const rows = await db.sql`SELECT * FROM leaderboard WHERE name=${player} ORDER BY record ASC LIMIT 1`;
if (row && !row.length) {
console.log('no record');
return null;
}
world.say(rows[0].name + ' / ' + rows[0].record);
return rows[0];
}
//-----------------------------------------------------------------
// 在游戏开始之前,创建数据库
(async function() {
console.clear();
await createTables();
// 向排行榜添加临时测试的数据
await insertLeaderboard('吉吉喵', '22.8');
await insertLeaderboard('吉吉喵的小伙伴', '23.4');
await insertLeaderboard('吉吉喵的好朋友', '23.6');
await insertLeaderboard('吉吉喵的邻居小红', '24');
await insertLeaderboard('吉吉喵的邻居小绿', '25');
await insertLeaderboard('吉吉喵的邻居小蓝', '28');
// 更新最佳成绩
leader = await getBestTime();
// 如果没有排行榜数据,最高纪录保持者为吉吉喵。
world.say(`[最高纪录保持者] ${leader.name?leader.name:'吉吉喵'} : ${leader.record}s`)
}());
// 通过聊天命令来获取最高纪录
world.onChat(async ({entity:user,message})=>{
if(message==='top1'){
const personalBest = await getMyBestTime(user.player.name); // 个人最佳成绩
const str = personalBest ? `你目前最佳成绩是:${personalBest.record}s` : `你还没有挑战记录。`
await user.player.dialog({
type: Box3DialogType.TEXT,
content: `${leader.name?leader.name:'吉吉喵'}目前以${leader.record?leader.record:'30.0'}s的成绩排名第一!\n
${str}`,
})
}else if(message==='top10') {
await getTop10();
}else if(message==='清空排行榜') {
await removeAll();
}
});点赞0
评论