Lv.1
在 【神岛暑期活动第一弹】全民创作节比赛结束~ 中回复
https://box3.codemao.cn/e/b48156fe68876a25b8e6
谁合作
2023-07-25T14:38:03 点赞:1
在 SQL的跑酷啊啊啊 中回复
拿去 这是记录xyz的
console.clear()
async function initTable() {
return await db.sql`
CREATE TABLE IF NOT EXISTS "player" (
"x" INT NOT NULL,
"y" INT NOT NULL,
"z" INT NOT NULL,
"userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL
)
`;
}
console.clear()
async function saveUser(user) {
await db.sql`
INSERT INTO "player" (
"x",
"y",
"z",
"userKey"
) VALUES (
${user.x},
${user.y},
${user.z},
${user.player.userKey}
)
ON CONFLICT("userKey")
DO UPDATE SET
"x"=excluded."x",
"y"=excluded."y",
"z"=excluded."z"
`
}
console.clear()
async function loadUser(user) {
const [data] = await db.sql`SELECT * FROM "player" WHERE "userKey"=${user.player.userKey} LIMIT 1`
if (data) {
user.x = data.x
user.y = data.y
user.z = data.z
user.position = [user.x,user.y,user.z]
}
else {
saveUser(user)
}
}
console.clear()
async function poll(fn, msg) {
while (true) {
try {
return await fn()
} catch (e) {
const m = e.message
if (m.includes('timeout')) {
world.say('数据库出错 检查结果 :启动超时,请联系官方进行修复 &&&&& timeout')
}
}
await sleep(2000)
}
}
console.clear()
poll(initTable, '等待主数据库启动...')
world.onPlayerJoin(({ entity }) => {
loadUser(entity)
})
console.clear()
for (const e of world.querySelectorAll('*')) {
if (e.id.startsWith('存档点')) {
e.collides = true
e.fixed = true
e.onEntityContact(({ other }) => {
if (other.isPlayer) {
if (e.position !== other.player.spawnPoint) {
other.player.directMessage('到达新的重生点')
other.player.spawnPoint = e.position
other.x = Math.ceil(other.position.x)
other.y = Math.ceil(other.position.y)
other.z = Math.ceil(other.position.z)
saveUser(other)
}
}
})
}
}
2024-01-29T16:16:50 点赞:1