
Lv.1
念往昔岁月
签名:dao3 羽岚.龍族[原]:Aoken 是我 2022-05-29 22:55:02 注册此账号 如想知道自己账号注册时间 记得先登录好账号可打开:https://api.codemao.cn/web/users/details 查看create_time的数字 并在 https://tool.lu/timestamp/ 进行时间轴转换
在 【最终版】史上最简单易用的SQL模板,点进来就是赚到! 中回复
报错了怎么办?

代码:
console.clear();
const PlayerData = {
money: [1, true],
lv: [0, true],
exp:[0,true],
itemList: [['铁剑'], true],
test: ['yee', false]
};
const CorrespondingName = {// 在此添加排行榜对应的单位和名称(无名称 则表示不显示名称)
'money': ['枚', '金币'],
'lv': ['级', '无名称']
};
(async () => {await poll(createTable)})();
async function poll(fn, msg) {
while (1) {
try {
return await fn()
} catch (e) {
e.message.includes('timeout') ? world.say(e.message) : world.say(msg || e.stack)
}
await sleep(2000)
}
};
async function createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS "YJZTX" (
"datas" TEXT NOT NULL,
"userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL
)
`
}
/**
* 初始化玩家数据
*
* @param {Box3Entity} entity
*/
function initPlayer(entity) {
for (let i in PlayerData) {
entity[i] = PlayerData[i][0]
}
};
/**
* 获取玩家数据
*
* @param {Box3Entity} entity
*/
function getPlayerData(entity) {
var data = { "name": entity.player.name };
for (let i in PlayerData) {
PlayerData[i][1] ? data[i] = entity[i] : null
};
return JSON.stringify(data);
};
/**
* 存档
*
* @param {Box3Entity} entity
*/
async function savePlayer(entity) {
if (!entity.player.userKey) return;
await db.sql`
INSERT INTO "YJZTX" (
"datas",
"userKey"
)
VALUES (
${getPlayerData(entity)},
${entity.player.userKey}
)
ON CONFLICT("userKey")
DO UPDATE SET
"datas" = excluded."datas"
`
};
/**
* 读档
*
* @param {Box3Entity} entity
*/
async function loadPlayer(entity) {
const [data] = await db.sql`SELECT * FROM "YJZTX" WHERE "userKey" = ${entity.player.userKey} LIMIT 1`;
if (data) {
var Data = JSON.parse(data.datas);
for (let i in PlayerData) {
Data[i] ? entity[i] = Data[i] : entity[i] = PlayerData[i][0]
}
} else {
initPlayer(entity)
};
};
/**
* 读取榜单前100
*/
async function getTop100(type) {
var list = [];
for await (let row of db.sql`SELECT * FROM "YJZTX"`) {
list.push([JSON.parse(row.datas).name, JSON.parse(row.datas)[type]])
};
return list.sort((a, b) => a[1] - b[1]).map((value, num) => {
return `第${num + 1}名 | ${value[0]} | ${value[1]}${CorrespondingName[type][0]}${CorrespondingName[type][1] != '无名称' ? CorrespondingName[type][1] : ''}`
})
}
/**
* 删档
*
* @param {Box3Entity} entity
*/
async function deletePlayer(entity) {
await db.sql`DELETE FROM YJZTX WHERE "userKey" = ${entity.player.userKey}`;
};
world.onPlayerJoin(async ({ entity }) => {
await loadPlayer(entity);
});
world.onPlayerLeave(async ({ entity }) => {
await savePlayer(entity)
})
world.onPress(async({ entity, button }) => {
if (button == 'action1') {
const ask = await entity.player.dialog({
type: 'select',
title: '测试',
content: `你目前[${entity.lv}]级\n[${entity.exp}]点经验\n[${entity.money}]枚金币\n背包:[${entity.itemList}]`,
options: ['点我加等级', '点我加经验', '点我加金币','排行榜']
});
if (!ask) return;
if (ask) {
if (ask.index <= 2) {
entity[['lv', 'exp', 'money'][ask.index]]++;
}if(ask.index == 3){
await entity.player.dialog({
type: 'select',
title: '排行',
content: await getTop100('money'),
options: ['确认']
});
}
};
};
});
2023-08-29T20:11:34 点赞:1