用户:
Aoken查看:3 回复:3 评论:3 创建时间:2023-08-29T17:03:12
求助,
怎么做旧版的云排行榜

该代码转载至build,幸好我吧收藏的贴都存海龟了)
/*
版权所归@build_一只建筑师吖
如需使用,复制粘贴时请勿删除此注释
违者必究!
功能:玩家数据保存和排行榜
*/
// 创建玩家数据表
(async function () {
await db.sql`
CREATE TABLE IF NOT EXISTS players (
money INTEGER NOT NULL,
lv INTEGER NOT NULL,
times INTEGER NOT NULL,
exp INTEGER NOT NULL,
userKey TEXT PRIMARY KEY UNIQUE NOT NULL
)
`
})();
// 保存玩家数据
async function saveData(entity) {
if (entity.player.userKey) {
await db.sql`
INSERT INTO players (
money,
lv,
times,
exp,
userKey
)
VALUES (
${entity.player.money},
${entity.player.lv},
${entity.player.times},
${entity.player.exp},
${entity.player.userKey}
)
ON CONFLICT(userKey)
DO UPDATE SET
money = excluded.money,
lv = excluded.lv,
times = excluded.times,
exp = excluded.exp,
userKey = excluded.userKey
`
}
}
world.onPlayerLeave(async ({ entity }) => { //玩家离开游戏后保存
try {
await saveData(entity);
} catch (error) {
world.say(`SQL ERROR: ${error}`)
console.error(`SQL ERROR: ${error}`);
}
});
// 玩家数据读取
async function loadData(entity) {
const data = (
await (
db.sql`
SELECT * FROM players WHERE userKey = ${entity.player.userKey} limit 1
`
)
)[0];
if (data) {
try {
entity.player.money = data.money;
entity.player.lv = data.lv;
entity.player.times = data.times;
entity.player.exp = data.exp;
entity.player.directMessage('已读取数据');
world.say(entity.player.name + ' 来到了发射场!')
} catch (error) {
world.say(`SQL ERROR: ${error}`)
console.error(`SQL ERROR: ${error}`);
}
} else {
await insertLeaderboard(entity.player.name, entity.player.lv);
entity.player.directMessage('你目前还没有数据,已帮你创建');
world.say(entity.player.name + " 第一次来到地图!")
}
}
// 创建排行榜表
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, lv) {
try {
await db.sql`INSERT INTO leaderboard VALUES (${name}, ${lv})`;
} catch (e) {
console.log(`SQL ERROR: ${e}`);
}
}
// 清空所有数据
async function removeAll() {
await db.sql`DELETE FROM leaderboard`
world.say(`排行榜数据已被清空!`);
}
// 获取排行榜第一名
async function getBestTime() {
const rows = await db.sql`SELECT * FROM leaderboard ORDER BY record DESC LIMIT 1`;
if (rows && !rows.length) {
console.log('暂无数据');
}
return rows[0];
}
// 获取排行榜前100名
async function getTop100() {
// 向排行榜插入数据
let i = 0;
phb = ['',];
const rows = await db.sql`SELECT * FROM leaderboard ORDER BY record DESC LIMIT 100`;
console.log(`result = ${JSON.stringify(rows)}`);
for await (const row of rows) {
phb[i]=`第${i+1}名:${row.name} ${row.record}级`;
i++;
}
//调试输出
console.log(phb)
}
// 获取玩家最高分数
async function getMyBestTime(player) {
const rows = await db.sql`SELECT * FROM leaderboard WHERE name=${player} ORDER BY record DESC LIMIT 1`;
if (rows && !rows.length) {
console.log('暂无数据');
return null;
}
return rows[0];
}
// 更新玩家排行榜数据
async function updatephb(user) {
await db.sql`DELETE FROM leaderboard WHERE name = ${user.name}`
await insertLeaderboard(user.name, user.lv);
}
// 当程序运行后
(async function () {
console.clear();
await createTables();
getTop100(); //获取排行榜100名
}());
world.onChat(async ({ entity: user, message }) => {
if (message === 'top100') {
await getTop100(); //获取排行榜100名
} else if (message === '清空排行榜') {
await removeAll(); //清空排行榜
}
});
world.onPlayerJoin(async ({ entity }) => {
entity.player.money = 0;
entity.player.lv = 0;
entity.player.times = 0;
entity.player.exp = 0;
// 调试玩家数据
try {
await loadData(entity); // 导入玩家数据
await saveData(entity); // 保存玩家数据
} catch (error) {
world.say(`SQL ERROR: ${error}`) // 聊天区报错
console.error(`SQL ERROR: ${error}`); // 控制台报错
}
try {
await createTables() // 创建排行榜
await getBestTime(); // 获取排行榜第一名
await getMyBestTime(); // 获取玩家最高
}
catch (error) {
world.say(`SQL ERROR: ${error}`) // 聊天区报错
console.error(`SQL ERROR: ${error}`); // 控制台报错
}
// 作者专用
// if(entity.player.name=="build_一只建筑师吖"){
// entity.player.money=Infinity;
// entity.player.lv=100;
// entity.player.exp=Infinity;
// }
updatephb(entity.player) //更新排行榜数据
});
// 右键保存玩家数据
world.onPress(async ({ button, raycast, entity }) => {
//测试代码
entity.player.money+=100;
entity.player.lv+=1;
entity.player.exp+=50;
//保存数据
if (button === 'action1') {
const main = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: entity.player.name,
content: `等级:${entity.player.lv}\n经验:${entity.player.exp}\n金币:${entity.player.money}`,
options: ['保存'],
});
if (!main || main === null) {
return;
}
if (main.index == 0) {
try {
await saveData(entity);
await updatephb(entity.player)
entity.player.directMessage('成功保存所有数据')
} catch (error) {
world.say(`SQL ERROR: ${error}`)
console.error(`SQL ERROR: ${error}`);
}
}
}
})
// 放置一个名为排行榜的模型
const 排行榜 = world.querySelector('#排行榜');
排行榜.enableInteract = true;
排行榜.interactRadius = 3;
排行榜.interactHint = 排行榜.id;
排行榜.interactColor = new Box3RGBColor(1, 1, 1);
// 当与玩家交互时
排行榜.onInteract(async ({ entity }) => {
await getTop100()
const result = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: 排行榜.id,
titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
content: `本图前100强排行榜:`,
options: phb,
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
});
});点赞0
评论
叁式橙子const paiming = world.querySelector('#排行榜')
paiming.enableInteract = true
paiming.interactRadius = 3
paiming.interactHint = paiming.id
paiming.interactColor.set(0,0,1)
xuanfu(paiming)
paiming.onInteract(async({entity})=>{
entity.player.dialog({
type: GameDialogType.TEXT,
title: "等级排行榜",
content: dialogContent(),
})
})
function dialogContent() {
// 首先获取当前世界中的所有玩家
const allPlayerEntities = world.querySelectorAll('player');
// 然后将所有的玩家进行排序,排序的规则是按照玩家持有的金币数量降序
const sortedPlayerEntities = allPlayerEntities.sort((a, b)=> b.lv - a.lv);
// 接下来,我们将在排行榜上以: xxx 有 yyy 个金币 的格式展示,并且每一条信息换一行显示!
// 首先声明一个content字符串,一会用来保存每一个玩家的信息
let content = "";
// 在这里需要做一个循环遍历,将刚才得到的有序的玩家数组遍历一遍
for(const entity of sortedPlayerEntities ) {
// 在遍历的过程中,每一次遍历,我们拿到当前的实体(entity),
// 将实体上的玩家名字(player.name)以及玩家金币数量(player.coin)拼接成排行榜上的一行信息
// 这样就得到: xxx 有 yyy 个金币 的格式啦
// 我们在这个文字的末尾加一个\n, 这样我们就能实现换行的效果
// 紧接着,再把这行信息加上content,这样做是为了把每一个玩家的排行榜信息拼接起来,保存到content中
content = content + `${entity.player.name}有${entity.lv}级\n`
}
// 最后,把得到的完整的带有玩家名称和金币数量,并且会换行的排行榜数据(content)返回出去
return content
}
}catch(e){world.say(e)}
点赞0
评论